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 { RouterProvider } from 'react-router-dom';
|
||||||
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
|
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
|
||||||
|
|
||||||
|
import { dialogStore } from '../state/dialog';
|
||||||
import { ClientConfigLoader } from '../components/ClientConfigLoader';
|
import { ClientConfigLoader } from '../components/ClientConfigLoader';
|
||||||
import { ClientConfigProvider } from '../hooks/useClientConfig';
|
import { ClientConfigProvider } from '../hooks/useClientConfig';
|
||||||
import { ConfigConfigError, ConfigConfigLoading } from './ConfigConfig';
|
import { ConfigConfigError, ConfigConfigLoading } from './ConfigConfig';
|
||||||
@@ -35,7 +36,7 @@ function App() {
|
|||||||
{(clientConfig) => (
|
{(clientConfig) => (
|
||||||
<ClientConfigProvider value={clientConfig}>
|
<ClientConfigProvider value={clientConfig}>
|
||||||
<QueryClientProvider client={queryClient}>
|
<QueryClientProvider client={queryClient}>
|
||||||
<JotaiProvider>
|
<JotaiProvider store={dialogStore}>
|
||||||
<RouterProvider router={createRouter(clientConfig, screenSize)} />
|
<RouterProvider router={createRouter(clientConfig, screenSize)} />
|
||||||
</JotaiProvider>
|
</JotaiProvider>
|
||||||
</QueryClientProvider>
|
</QueryClientProvider>
|
||||||
|
|||||||
@@ -61,6 +61,7 @@ import {
|
|||||||
materializeSharedFile,
|
materializeSharedFile,
|
||||||
} from '../../utils/androidShare';
|
} from '../../utils/androidShare';
|
||||||
import { UpdatesDialogHost } from '../../components/updates-dialog';
|
import { UpdatesDialogHost } from '../../components/updates-dialog';
|
||||||
|
import { DialogHost } from '../../components/DialogHost';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Applies the selected emoji style font to the document.
|
* Applies the selected emoji style font to the document.
|
||||||
@@ -701,6 +702,7 @@ export function ClientNonUIFeatures({ children }: ClientNonUIFeaturesProps) {
|
|||||||
<AndroidShareIntentHandler />
|
<AndroidShareIntentHandler />
|
||||||
{children}
|
{children}
|
||||||
<UpdatesDialogHost />
|
<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 { PluginListenerHandle } from '@capacitor/core';
|
||||||
import type { IPusherRequest, MatrixClient } from 'matrix-js-sdk';
|
import type { IPusherRequest, MatrixClient } from 'matrix-js-sdk';
|
||||||
|
import { promptDialog } from '../state/dialog';
|
||||||
|
|
||||||
const CAPACITOR_CORE_MODULE_ID = '@capacitor/core';
|
const CAPACITOR_CORE_MODULE_ID = '@capacitor/core';
|
||||||
|
|
||||||
@@ -366,14 +367,11 @@ class AndroidUnifiedPushManager {
|
|||||||
|
|
||||||
if (typeof window === 'undefined') return;
|
if (typeof window === 'undefined') return;
|
||||||
|
|
||||||
try {
|
promptDialog(`${message}\n\nOpen distributor list now?`, true).then((openDocs) => {
|
||||||
const openDocs = window.confirm(`${message}\n\nOpen distributor list now?`);
|
|
||||||
if (openDocs) {
|
if (openDocs) {
|
||||||
window.open(docsUrl, '_blank', 'noopener,noreferrer');
|
window.open(docsUrl, '_blank', 'noopener,noreferrer');
|
||||||
}
|
}
|
||||||
} catch {
|
});
|
||||||
window.alert(message);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Install plugin listeners once for the active Matrix client. */
|
/** Install plugin listeners once for the active Matrix client. */
|
||||||
|
|||||||
+13
-11
@@ -5,6 +5,7 @@ import { clearNavToActivePathStore } from '../app/state/navToActivePath';
|
|||||||
import { Session, getSessionStoreName } from '../app/state/sessions';
|
import { Session, getSessionStoreName } from '../app/state/sessions';
|
||||||
import { getSettings } from '../app/state/settings';
|
import { getSettings } from '../app/state/settings';
|
||||||
import { recordPacket } from '../app/state/debugPackets';
|
import { recordPacket } from '../app/state/debugPackets';
|
||||||
|
import { promptDialog } from '../app/state/dialog';
|
||||||
|
|
||||||
const MAX_BODY = 8192;
|
const MAX_BODY = 8192;
|
||||||
|
|
||||||
@@ -242,17 +243,18 @@ const setupKeyConflictHandler = (mx: MatrixClient, session: Session): void => {
|
|||||||
|
|
||||||
// Notify user and reload
|
// Notify user and reload
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
if (
|
promptDialog(
|
||||||
confirm(
|
'Detected encryption key conflict. The app needs to reset your encryption session. You may need to re-verify your devices after this. Continue?',
|
||||||
'Detected encryption key conflict. The app needs to reset your encryption session. You may need to re-verify your devices after this. Continue?',
|
true,
|
||||||
)
|
).then((continueFix) => {
|
||||||
) {
|
if (continueFix) {
|
||||||
deleteRustCryptoDatabase(session).then(() => {
|
deleteRustCryptoDatabase(session).then(() => {
|
||||||
window.location.reload();
|
window.location.reload();
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
handlingKeyConflict = false;
|
handlingKeyConflict = false;
|
||||||
}
|
}
|
||||||
|
});
|
||||||
}, 100);
|
}, 100);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user