Scripts/resources/[standalone]/ox_lib/resource/interface/client/alert.lua

65 lines
1.4 KiB
Lua
Raw Normal View History

2024-12-30 10:15:34 +00:00
---@type promise?
2024-12-29 20:02:43 +00:00
local alert = nil
2024-12-30 10:15:34 +00:00
local alertId = 0
2024-12-29 20:02:43 +00:00
---@class AlertDialogProps
---@field header string;
---@field content string;
---@field centered? boolean?;
---@field size? 'xs' | 'sm' | 'md' | 'lg' | 'xl';
---@field overflow? boolean?;
---@field cancel? boolean?;
---@field labels? {cancel?: string, confirm?: string}
---@param data AlertDialogProps
2024-12-30 10:15:34 +00:00
---@param timeout? number Force the window to timeout after `x` milliseconds.
2024-12-29 20:02:43 +00:00
---@return 'cancel' | 'confirm' | nil
2024-12-30 10:15:34 +00:00
function lib.alertDialog(data, timeout)
2024-12-29 20:02:43 +00:00
if alert then return end
2024-12-30 10:15:34 +00:00
local id = alertId + 1
alertId = id
2024-12-29 20:02:43 +00:00
alert = promise.new()
lib.setNuiFocus(false)
SendNUIMessage({
action = 'sendAlert',
data = data
})
2024-12-30 10:15:34 +00:00
if timeout then
SetTimeout(timeout, function()
if id == alertId then lib.closeAlertDialog('timeout') end
end)
end
2024-12-29 20:02:43 +00:00
return Citizen.Await(alert)
end
2024-12-30 10:15:34 +00:00
---@param reason? string An optional reason for the window to be closed.
function lib.closeAlertDialog(reason)
2024-12-29 20:02:43 +00:00
if not alert then return end
lib.resetNuiFocus()
SendNUIMessage({
action = 'closeAlertDialog'
})
2024-12-30 10:15:34 +00:00
local p = alert
2024-12-29 20:02:43 +00:00
alert = nil
2024-12-30 10:15:34 +00:00
if reason then p:reject(reason) else p:resolve() end
end
2024-12-29 20:02:43 +00:00
RegisterNUICallback('closeAlert', function(data, cb)
cb(1)
lib.resetNuiFocus()
2024-12-30 10:15:34 +00:00
local promise = alert --[[@as promise]]
2024-12-29 20:02:43 +00:00
alert = nil
promise:resolve(data)
end)
RegisterNetEvent('ox_lib:alertDialog', lib.alertDialog)