mirror of
https://github.com/mountain-loop/yaak.git
synced 2026-03-26 03:11:12 +01:00
Remove useNavigate everywhere, and make request a query param. And convert dialog to Jotai
This commit is contained in:
21
src-web/lib/alert.ts
Normal file
21
src-web/lib/alert.ts
Normal file
@@ -0,0 +1,21 @@
|
||||
import type { DialogProps } from '../components/core/Dialog';
|
||||
import type { AlertProps } from '../hooks/Alert';
|
||||
import { Alert } from '../hooks/Alert';
|
||||
import { showDialog } from './dialog';
|
||||
|
||||
interface AlertArgs {
|
||||
id: string;
|
||||
title: DialogProps['title'];
|
||||
body: AlertProps['body'];
|
||||
size?: DialogProps['size'];
|
||||
}
|
||||
|
||||
export function showAlert({ id, title, body, size = 'sm' }: AlertArgs) {
|
||||
showDialog({
|
||||
id,
|
||||
title,
|
||||
hideX: true,
|
||||
size,
|
||||
render: ({ hide }) => Alert({ onHide: hide, body }),
|
||||
});
|
||||
}
|
||||
21
src-web/lib/dialog.ts
Normal file
21
src-web/lib/dialog.ts
Normal file
@@ -0,0 +1,21 @@
|
||||
import { atom } from 'jotai/index';
|
||||
import type { DialogInstance } from '../components/Dialogs';
|
||||
import { trackEvent } from './analytics';
|
||||
import { jotaiStore } from './jotai';
|
||||
|
||||
export const dialogsAtom = atom<DialogInstance[]>([]);
|
||||
|
||||
export function showDialog({ id, ...props }: DialogInstance) {
|
||||
trackEvent('dialog', 'show', { id });
|
||||
jotaiStore.set(dialogsAtom, (a) => [...a.filter((d) => d.id !== id), { id, ...props }]);
|
||||
}
|
||||
|
||||
export function toggleDialog({ id, ...props }: DialogInstance) {
|
||||
const dialogs = jotaiStore.get(dialogsAtom);
|
||||
if (dialogs.some((d) => d.id === id)) hideDialog(id);
|
||||
else showDialog({ id, ...props });
|
||||
}
|
||||
|
||||
export function hideDialog(id: string) {
|
||||
jotaiStore.set(dialogsAtom, (a) => a.filter((d) => d.id !== id));
|
||||
}
|
||||
@@ -1,11 +1,18 @@
|
||||
import { atom } from 'jotai/index';
|
||||
import type { PrivateToastEntry, ToastEntry } from '../components/Toasts';
|
||||
import type { ToastInstance } from '../components/Toasts';
|
||||
import { generateId } from './generateId';
|
||||
import { jotaiStore } from './jotai';
|
||||
|
||||
export const toastsAtom = atom<PrivateToastEntry[]>([]);
|
||||
export const toastsAtom = atom<ToastInstance[]>([]);
|
||||
|
||||
export function showToast({ id, timeout = 5000, ...props }: ToastEntry) {
|
||||
export function showToast({
|
||||
id,
|
||||
timeout = 5000,
|
||||
...props
|
||||
}: Omit<ToastInstance, 'id' | 'timeout'> & {
|
||||
id?: ToastInstance['id'];
|
||||
timeout?: ToastInstance['timeout'];
|
||||
}) {
|
||||
id = id ?? generateId();
|
||||
if (timeout != null) {
|
||||
setTimeout(() => hideToast(id), timeout);
|
||||
|
||||
Reference in New Issue
Block a user