Skip to content

Commit

Permalink
feat(interface/alert): support dialog timeout
Browse files Browse the repository at this point in the history
closeAlertDialog will now error if a reason is passed.
Added timeout param to alertDialog.
Resolves #522.
  • Loading branch information
thelindat committed May 7, 2024
1 parent b1cb214 commit 43905bd
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 7 deletions.
5 changes: 3 additions & 2 deletions package/client/resource/interface/alert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
23 changes: 18 additions & 5 deletions resource/interface/client/alert.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
---@type promise?
local alert = nil
local alertId = 0

---@class AlertDialogProps
---@field header string;
Expand All @@ -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)
Expand All @@ -22,27 +27,35 @@ 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()
SendNUIMessage({
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)
Expand Down

0 comments on commit 43905bd

Please sign in to comment.