Skip to content

Commit 558eb60

Browse files
committed
Override Alert.alert to use our custom Dialog component
1 parent 8932e8d commit 558eb60

7 files changed

Lines changed: 93 additions & 31 deletions

File tree

packages/components/src/components/AppProviders.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { AppearanceProvider } from '../libs/appearence'
66
import { HelmetProvider } from '../libs/helmet'
77
import { SafeAreaProvider } from '../libs/safe-area-view'
88
import { configureStore } from '../redux/store'
9+
import { OverrideSystemDialog } from './common/OverrideSystemDialog'
910
import { ColumnFiltersProvider } from './context/ColumnFiltersContext'
1011
import { ColumnFocusProvider } from './context/ColumnFocusContext'
1112
import { ColumnWidthProvider } from './context/ColumnWidthContext'
@@ -35,7 +36,10 @@ export function AppProviders(props: AppProvidersProps) {
3536
<AppearanceProvider>
3637
<ThemeProvider>
3738
<SafeAreaProvider>
38-
<DialogProvider>{props.children}</DialogProvider>
39+
<DialogProvider>
40+
{props.children}
41+
<OverrideSystemDialog />
42+
</DialogProvider>
3943
</SafeAreaProvider>
4044
</ThemeProvider>
4145
</AppearanceProvider>
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import React, { useEffect } from 'react'
2+
import { Alert as AlertOriginal } from 'react-native'
3+
4+
import { useDialog } from '../context/DialogContext'
5+
6+
const _alert = window.alert
7+
const _Alert = { alert: AlertOriginal.alert, prompt: AlertOriginal.prompt }
8+
9+
export const OverrideSystemDialog = React.memo(() => {
10+
const Dialog = useDialog()
11+
12+
useEffect(() => {
13+
function alert(message?: string): void {
14+
Dialog.show(undefined, message)
15+
}
16+
window.alert = alert
17+
18+
const Alert = {
19+
alert: Dialog.show,
20+
// prompt: Dialog.show,
21+
}
22+
AlertOriginal.alert = Alert.alert
23+
// AlertOriginal.prompt = Alert.prompt
24+
25+
return () => {
26+
if (window.alert === alert) window.alert = _alert
27+
28+
if (AlertOriginal.alert === Alert.alert)
29+
AlertOriginal.alert = _Alert.alert
30+
31+
// if (AlertOriginal.prompt === Alert.prompt)
32+
// AlertOriginal.prompt = _Alert.prompt
33+
34+
Dialog.hide()
35+
}
36+
}, [])
37+
38+
return null
39+
})
40+
41+
OverrideSystemDialog.displayName = 'OverrideSystemDialog'

packages/components/src/libs/confirm/index.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,16 @@ export const confirm: ConfirmFn = (
1818
title,
1919
message,
2020
[
21-
{
22-
text: cancelLabel || 'Cancel',
23-
onPress: cancelCallback || (() => undefined),
24-
style: 'cancel',
25-
},
2621
{
2722
text: confirmLabel || 'Ok',
2823
onPress: confirmCallback || (() => undefined),
2924
style: destructive ? 'destructive' : undefined,
3025
},
26+
{
27+
text: cancelLabel || 'Cancel',
28+
onPress: cancelCallback || (() => undefined),
29+
style: 'cancel',
30+
},
3131
],
3232
{ cancelable },
3333
)
Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,34 @@
1+
import { Alert } from 'react-native'
2+
13
import { ConfirmFn } from './index.shared'
24

35
export const confirm: ConfirmFn = (
46
title,
57
message,
6-
{ cancelCallback, confirmCallback },
8+
{
9+
cancelCallback,
10+
cancelLabel,
11+
cancelable = true,
12+
confirmCallback,
13+
confirmLabel,
14+
destructive = false,
15+
},
716
) => {
8-
const _message = title && message ? `${title}\n${message}` : message || title
9-
10-
const result = window.confirm(_message)
11-
12-
if (result) {
13-
if (confirmCallback) confirmCallback()
14-
} else {
15-
if (cancelCallback) cancelCallback()
16-
}
17+
Alert.alert(
18+
title,
19+
message,
20+
[
21+
{
22+
text: confirmLabel || 'Ok',
23+
onPress: confirmCallback || (() => undefined),
24+
style: destructive ? 'destructive' : undefined,
25+
},
26+
{
27+
text: cancelLabel || 'Cancel',
28+
onPress: cancelCallback || (() => undefined),
29+
style: 'cancel',
30+
},
31+
],
32+
{ cancelable },
33+
)
1734
}

packages/components/src/libs/prompt/index.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { Alert } from 'react-native'
2+
13
import { PromptButton, PromptOptions } from './index.shared'
24

35
export function isSupported() {
@@ -11,5 +13,5 @@ export function prompt(
1113
_options?: PromptOptions,
1214
) {
1315
console.error('[prompt] Not supported.')
14-
if (__DEV__) alert('Prompt not supported.')
16+
if (__DEV__) Alert.alert('Prompt not supported.')
1517
}

packages/components/src/libs/prompt/index.web.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { Alert } from 'react-native'
2+
13
import { Platform } from '../platform'
24
import { PromptButton, PromptOptions } from './index.shared'
35

@@ -13,7 +15,7 @@ export function prompt(
1315
) {
1416
if (!isSupported()) {
1517
console.error('[prompt] Not supported.')
16-
if (__DEV__) alert('Prompt not supported.')
18+
if (__DEV__) Alert.alert('Prompt not supported.')
1719
return
1820
}
1921

packages/components/src/redux/sagas/subscriptions.ts

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ import {
2929
} from '@devhub/core'
3030
import { Octokit } from '@octokit/rest'
3131
import _ from 'lodash'
32-
import { AppState, InteractionManager } from 'react-native'
32+
import { Alert, AppState, InteractionManager } from 'react-native'
3333
import {
3434
actionChannel,
3535
all,
@@ -719,7 +719,7 @@ function* _markAllGitHubNotificationsAsReadOrUnread({
719719
try {
720720
yield octokit.activity.markAsRead({})
721721
} catch (error) {
722-
alert(
722+
Alert.alert(
723723
`Failed to mark all notifications as ${
724724
unread ? 'unread' : 'read'
725725
}. ${error}`,
@@ -794,18 +794,14 @@ function* onMarkEverythingAsReadWithConfirmation(
794794
>,
795795
) {
796796
const confirmed = yield new Promise(resolve => {
797-
confirm(
798-
'Mark all columns as read?',
799-
'Mark all items from all columns as read? This cannot be undone.',
800-
{
801-
confirmCallback() {
802-
resolve(true)
803-
},
804-
cancelCallback() {
805-
resolve(false)
806-
},
797+
confirm('Mark all columns as read?', 'This cannot be undone.', {
798+
confirmCallback() {
799+
resolve(true)
807800
},
808-
)
801+
cancelCallback() {
802+
resolve(false)
803+
},
804+
})
809805
})
810806

811807
if (!confirmed) return

0 commit comments

Comments
 (0)