UseAlertModal returns a boolean indicating if the modal is open and a function to trigger it
()
| 235 | |
| 236 | // UseAlertModal returns a boolean indicating if the modal is open and a function to trigger it |
| 237 | func UseAlertModal() (modalOpen bool, triggerAlert func(config ModalConfig)) { |
| 238 | isOpen := UseLocal(false) |
| 239 | |
| 240 | trigger := func(config ModalConfig) { |
| 241 | if isOpen.Get() { |
| 242 | log.Printf("warning: UseAlertModal trigger called while modal is already open") |
| 243 | if config.OnClose != nil { |
| 244 | go func() { |
| 245 | defer func() { |
| 246 | util.PanicHandler("UseAlertModal callback goroutine", recover()) |
| 247 | }() |
| 248 | time.Sleep(10 * time.Millisecond) |
| 249 | config.OnClose() |
| 250 | }() |
| 251 | } |
| 252 | return |
| 253 | } |
| 254 | isOpen.Set(true) |
| 255 | |
| 256 | // Create modal config for backend |
| 257 | modalId := uuid.New().String() |
| 258 | backendConfig := rpctypes.ModalConfig{ |
| 259 | ModalId: modalId, |
| 260 | ModalType: "alert", |
| 261 | Icon: config.Icon, |
| 262 | Title: config.Title, |
| 263 | Text: config.Text, |
| 264 | OkText: config.OkText, |
| 265 | CancelText: config.CancelText, |
| 266 | } |
| 267 | |
| 268 | // Show modal and wait for result in a goroutine |
| 269 | go func() { |
| 270 | defer func() { |
| 271 | util.PanicHandler("UseAlertModal goroutine", recover()) |
| 272 | }() |
| 273 | resultChan := engine.GetDefaultClient().ShowModal(backendConfig) |
| 274 | <-resultChan // Wait for result (always dismissed for alerts) |
| 275 | isOpen.Set(false) |
| 276 | if config.OnClose != nil { |
| 277 | config.OnClose() |
| 278 | } |
| 279 | }() |
| 280 | } |
| 281 | |
| 282 | return isOpen.Get(), trigger |
| 283 | } |
| 284 | |
| 285 | // UseConfirmModal returns a boolean indicating if the modal is open and a function to trigger it |
| 286 | func UseConfirmModal() (modalOpen bool, triggerConfirm func(config ModalConfig)) { |
no test coverage detected