From 43905bdd62dcb8e51a04c821dafbf2979de04824 Mon Sep 17 00:00:00 2001 From: Linden <65407488+thelindat@users.noreply.github.com> Date: Wed, 8 May 2024 05:23:27 +1000 Subject: [PATCH] feat(interface/alert): support dialog timeout closeAlertDialog will now error if a reason is passed. Added timeout param to alertDialog. Resolves #522. --- package/client/resource/interface/alert.ts | 5 +++-- resource/interface/client/alert.lua | 23 +++++++++++++++++----- 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/package/client/resource/interface/alert.ts b/package/client/resource/interface/alert.ts index f599b73..4da56c4 100644 --- a/package/client/resource/interface/alert.ts +++ b/package/client/resource/interface/alert.ts @@ -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); diff --git a/resource/interface/client/alert.lua b/resource/interface/client/alert.lua index c2923cc..b56749c 100644 --- a/resource/interface/client/alert.lua +++ b/resource/interface/client/alert.lua @@ -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)