UseConfirmModal returns a boolean indicating if the modal is open and a function to trigger it
()
| 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)) { |
| 287 | isOpen := UseLocal(false) |
| 288 | |
| 289 | trigger := func(config ModalConfig) { |
| 290 | if isOpen.Get() { |
| 291 | log.Printf("warning: UseConfirmModal trigger called while modal is already open") |
| 292 | if config.OnResult != nil { |
| 293 | go func() { |
| 294 | defer func() { |
| 295 | util.PanicHandler("UseConfirmModal callback goroutine", recover()) |
| 296 | }() |
| 297 | time.Sleep(10 * time.Millisecond) |
| 298 | config.OnResult(false) |
| 299 | }() |
| 300 | } |
| 301 | return |
| 302 | } |
| 303 | isOpen.Set(true) |
| 304 | |
| 305 | // Create modal config for backend |
| 306 | modalId := uuid.New().String() |
| 307 | backendConfig := rpctypes.ModalConfig{ |
| 308 | ModalId: modalId, |
| 309 | ModalType: "confirm", |
| 310 | Icon: config.Icon, |
| 311 | Title: config.Title, |
| 312 | Text: config.Text, |
| 313 | OkText: config.OkText, |
| 314 | CancelText: config.CancelText, |
| 315 | } |
| 316 | |
| 317 | // Show modal and wait for result in a goroutine |
| 318 | go func() { |
| 319 | defer func() { |
| 320 | util.PanicHandler("UseConfirmModal goroutine", recover()) |
| 321 | }() |
| 322 | resultChan := engine.GetDefaultClient().ShowModal(backendConfig) |
| 323 | result := <-resultChan |
| 324 | isOpen.Set(false) |
| 325 | if config.OnResult != nil { |
| 326 | config.OnResult(result) |
| 327 | } |
| 328 | }() |
| 329 | } |
| 330 | |
| 331 | return isOpen.Get(), trigger |
| 332 | } |
| 333 |
no test coverage detected