mirror of
https://github.com/CommunityOx/ox_lib.git
synced 2026-08-17 06:56:03 +01:00
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:
@@ -13,6 +13,7 @@ interface AlertDialogProps {
|
||||
|
||||
type alertDialog = (data: AlertDialogProps) => Promise<'cancel' | 'confirm'>;
|
||||
|
||||
export const alertDialog: alertDialog = async (data) => await exports.ox_lib.alertDialog(data);
|
||||
export const alertDialog: alertDialog = async (data, timeout?: number) =>
|
||||
await exports.ox_lib.alertDialog(data, timeout);
|
||||
|
||||
export const closeAlertDialog = () => exports.ox_lib.closeAlertDialog();
|
||||
export const closeAlertDialog = (reason?: string) => exports.ox_lib.closeAlertDialog(reason);
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user