mirror of
https://github.com/CommunityOx/ox_lib.git
synced 2026-08-17 06:56:03 +01:00
Some people accidentally, mistakenly, and without any malicious intent forget to attribute code from this resource to its source. This change will hopefully assist people, so that they do not make that mistake again.
73 lines
1.7 KiB
Lua
73 lines
1.7 KiB
Lua
--[[
|
|
https://github.com/overextended/ox_lib
|
|
|
|
This file is licensed under LGPL-3.0 or higher <https://www.gnu.org/licenses/lgpl-3.0.en.html>
|
|
|
|
Copyright (c) 2025 Linden <https://github.com/thelindat/fivem>
|
|
]]
|
|
|
|
---@type promise?
|
|
local alert = nil
|
|
local alertId = 0
|
|
|
|
---@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
|
|
---@param timeout? number Force the window to timeout after `x` milliseconds.
|
|
---@return 'cancel' | 'confirm' | nil
|
|
function lib.alertDialog(data, timeout)
|
|
if alert then return end
|
|
|
|
local id = alertId + 1
|
|
alertId = id
|
|
alert = promise.new()
|
|
|
|
lib.setNuiFocus(false)
|
|
SendNUIMessage({
|
|
action = 'sendAlert',
|
|
data = data
|
|
})
|
|
|
|
if timeout then
|
|
SetTimeout(timeout, function()
|
|
if id == alertId then lib.closeAlertDialog('timeout') end
|
|
end)
|
|
end
|
|
|
|
return Citizen.Await(alert)
|
|
end
|
|
|
|
---@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'
|
|
})
|
|
|
|
local p = alert
|
|
alert = nil
|
|
|
|
if reason then p:reject(reason) else p:resolve() end
|
|
end
|
|
|
|
RegisterNUICallback('closeAlert', function(data, cb)
|
|
cb(1)
|
|
lib.resetNuiFocus()
|
|
|
|
local promise = alert --[[@as promise]]
|
|
alert = nil
|
|
|
|
promise:resolve(data)
|
|
end)
|
|
|
|
RegisterNetEvent('ox_lib:alertDialog', lib.alertDialog)
|