about summary refs log tree commit diff
path: root/src/view/com/util/Alert.web.tsx
blob: 448879173809b5b7ba2759cc195d403b0762493e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import {type AlertButton, type AlertStatic} from 'react-native'

class WebAlert implements Pick<AlertStatic, 'alert'> {
  public alert(title: string, message?: string, buttons?: AlertButton[]): void {
    if (buttons === undefined || buttons.length === 0) {
      // eslint-disable-next-line no-alert
      window.alert([title, message].filter(Boolean).join('\n'))
      return
    }

    // eslint-disable-next-line no-alert
    const result = window.confirm([title, message].filter(Boolean).join('\n'))

    if (result === true) {
      const confirm = buttons.find(({style}) => style !== 'cancel')
      confirm?.onPress?.()
      return
    }

    const cancel = buttons.find(({style}) => style === 'cancel')
    cancel?.onPress?.()
  }
}

export const Alert = new WebAlert()