feat(interface/alert): support dialog timeout

closeAlertDialog will now error if a reason is passed.
Added timeout param to alertDialog.
Resolves #522.
This commit is contained in:
Linden
2024-05-08 05:23:27 +10:00
parent b1cb2142f0
commit 43905bdd62
2 changed files with 21 additions and 7 deletions

View File

@@ -1,4 +1,6 @@
---@type promise?
local alert = nil
local alertId = 0
---@class AlertDialogProps
---@field header string;
@@ -10,10 +12,13 @@ local alert = nil
---@field labels? {cancel?: string, confirm?: string}
---@param data AlertDialogProps
---@param timeout number Force the window to timeout after `x` milliseconds.
---@return 'cancel' | 'confirm' | nil
function lib.alertDialog(data)
function lib.alertDialog(data, timeout)
if alert then return end
local id = alertId + 1
alertId = id
alert = promise.new()
lib.setNuiFocus(false)
@@ -22,10 +27,17 @@ function lib.alertDialog(data)
data = data
})
if timeout then
SetTimeout(timeout, function()
if id == alertId then lib.closeAlertDialog('timeout') end
end)
end
return Citizen.Await(alert)
end
function lib.closeAlertDialog()
---@param reason? string An optional reason for the window to be closed.
function lib.closeAlertDialog(reason)
if not alert then return end
lib.resetNuiFocus()
@@ -33,16 +45,17 @@ function lib.closeAlertDialog()
action = 'closeAlertDialog'
})
alert:resolve(nil)
local p = alert
alert = nil
end
if reason then p:reject(reason) else p:resolve() end
end
RegisterNUICallback('closeAlert', function(data, cb)
cb(1)
lib.resetNuiFocus()
local promise = alert
local promise = alert --[[@as promise]]
alert = nil
promise:resolve(data)