Replace the native confirm and alert with an in-app dialog. [skip build]
This commit is contained in:
@@ -0,0 +1,49 @@
|
||||
import FocusTrap from 'focus-trap-react';
|
||||
import { useAtom } from 'jotai';
|
||||
import { Box, Button, Dialog, Overlay, OverlayBackdrop, OverlayCenter, config, Text } from 'folds';
|
||||
import { dialogAtom } from '../state/dialog';
|
||||
import { stopPropagation } from '../utils/keyboard';
|
||||
|
||||
export function DialogHost() {
|
||||
const [dialog, setDialog] = useAtom(dialogAtom);
|
||||
|
||||
if (!dialog) return null;
|
||||
|
||||
const close = (answer: boolean) => {
|
||||
setDialog(null);
|
||||
dialog.resolve(answer);
|
||||
};
|
||||
|
||||
return (
|
||||
<Overlay open backdrop={<OverlayBackdrop />}>
|
||||
<OverlayCenter>
|
||||
<FocusTrap
|
||||
focusTrapOptions={{
|
||||
initialFocus: false,
|
||||
onDeactivate: () => close(false),
|
||||
clickOutsideDeactivates: true,
|
||||
escapeDeactivates: stopPropagation,
|
||||
}}
|
||||
>
|
||||
<Dialog variant="Surface">
|
||||
<Box style={{ padding: config.space.S400 }} direction="Column" gap="400">
|
||||
<Text priority="400" style={{ whiteSpace: 'pre-line' }}>
|
||||
{dialog.message}
|
||||
</Text>
|
||||
<Box direction="Row" gap="200">
|
||||
{dialog.confirm && (
|
||||
<Button variant="Secondary" onClick={() => close(false)} radii="300">
|
||||
<Text size="B300">Cancel</Text>
|
||||
</Button>
|
||||
)}
|
||||
<Button variant="Primary" onClick={() => close(dialog.confirm)} radii="300">
|
||||
<Text size="B300">{dialog.confirm ? 'Confirm' : 'OK'}</Text>
|
||||
</Button>
|
||||
</Box>
|
||||
</Box>
|
||||
</Dialog>
|
||||
</FocusTrap>
|
||||
</OverlayCenter>
|
||||
</Overlay>
|
||||
);
|
||||
}
|
||||
@@ -4,6 +4,7 @@ import { OverlayContainerProvider, PopOutContainerProvider, TooltipContainerProv
|
||||
import { RouterProvider } from 'react-router-dom';
|
||||
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
|
||||
|
||||
import { dialogStore } from '../state/dialog';
|
||||
import { ClientConfigLoader } from '../components/ClientConfigLoader';
|
||||
import { ClientConfigProvider } from '../hooks/useClientConfig';
|
||||
import { ConfigConfigError, ConfigConfigLoading } from './ConfigConfig';
|
||||
@@ -35,7 +36,7 @@ function App() {
|
||||
{(clientConfig) => (
|
||||
<ClientConfigProvider value={clientConfig}>
|
||||
<QueryClientProvider client={queryClient}>
|
||||
<JotaiProvider>
|
||||
<JotaiProvider store={dialogStore}>
|
||||
<RouterProvider router={createRouter(clientConfig, screenSize)} />
|
||||
</JotaiProvider>
|
||||
</QueryClientProvider>
|
||||
|
||||
@@ -61,6 +61,7 @@ import {
|
||||
materializeSharedFile,
|
||||
} from '../../utils/androidShare';
|
||||
import { UpdatesDialogHost } from '../../components/updates-dialog';
|
||||
import { DialogHost } from '../../components/DialogHost';
|
||||
|
||||
/**
|
||||
* Applies the selected emoji style font to the document.
|
||||
@@ -701,6 +702,7 @@ export function ClientNonUIFeatures({ children }: ClientNonUIFeaturesProps) {
|
||||
<AndroidShareIntentHandler />
|
||||
{children}
|
||||
<UpdatesDialogHost />
|
||||
<DialogHost />
|
||||
</>
|
||||
);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
import { atom, createStore } from 'jotai';
|
||||
|
||||
export type Dialog = {
|
||||
message: string;
|
||||
// Confirm needs an affirmative answer; information-only dialogs just close.
|
||||
confirm: boolean;
|
||||
resolve: (answer: boolean) => void;
|
||||
};
|
||||
|
||||
// Non-React callers (client init, background sync) must write the store the app renders
|
||||
// with, so App.tsx passes this one to JotaiProvider.
|
||||
export const dialogStore = createStore();
|
||||
|
||||
export const dialogAtom = atom<Dialog | null>(null);
|
||||
|
||||
export const promptDialog = (message: string, confirm = false): Promise<boolean> =>
|
||||
new Promise((resolve) => {
|
||||
dialogStore.set(dialogAtom, { message, confirm, resolve });
|
||||
});
|
||||
@@ -1,5 +1,6 @@
|
||||
import type { PluginListenerHandle } from '@capacitor/core';
|
||||
import type { IPusherRequest, MatrixClient } from 'matrix-js-sdk';
|
||||
import { promptDialog } from '../state/dialog';
|
||||
|
||||
const CAPACITOR_CORE_MODULE_ID = '@capacitor/core';
|
||||
|
||||
@@ -366,14 +367,11 @@ class AndroidUnifiedPushManager {
|
||||
|
||||
if (typeof window === 'undefined') return;
|
||||
|
||||
try {
|
||||
const openDocs = window.confirm(`${message}\n\nOpen distributor list now?`);
|
||||
promptDialog(`${message}\n\nOpen distributor list now?`, true).then((openDocs) => {
|
||||
if (openDocs) {
|
||||
window.open(docsUrl, '_blank', 'noopener,noreferrer');
|
||||
}
|
||||
} catch {
|
||||
window.alert(message);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/** Install plugin listeners once for the active Matrix client. */
|
||||
|
||||
Reference in New Issue
Block a user