mirror of
https://github.com/mountain-loop/yaak.git
synced 2026-01-15 21:53:36 +01:00
47 lines
1.2 KiB
TypeScript
47 lines
1.2 KiB
TypeScript
import { open } from '@tauri-apps/plugin-shell';
|
|
import { Button } from '../components/core/Button';
|
|
import { useToast } from '../components/ToastContext';
|
|
import { invokeCmd } from '../lib/tauri';
|
|
import { useListenToTauriEvent } from './useListenToTauriEvent';
|
|
|
|
export function useNotificationToast() {
|
|
const toast = useToast();
|
|
|
|
const markRead = (id: string) => {
|
|
invokeCmd('cmd_dismiss_notification', { notificationId: id }).catch(console.error);
|
|
};
|
|
|
|
useListenToTauriEvent<{
|
|
id: string;
|
|
timestamp: string;
|
|
message: string;
|
|
action?: null | {
|
|
url: string;
|
|
label: string;
|
|
};
|
|
}>('notification', ({ payload }) => {
|
|
const actionUrl = payload.action?.url;
|
|
const actionLabel = payload.action?.label;
|
|
toast.show({
|
|
id: payload.id,
|
|
timeout: null,
|
|
message: payload.message,
|
|
onClose: () => markRead(payload.id),
|
|
action:
|
|
actionLabel && actionUrl ? (
|
|
<Button
|
|
size="xs"
|
|
color="secondary"
|
|
className="mr-auto min-w-[5rem]"
|
|
onClick={() => {
|
|
toast.hide(payload.id);
|
|
return open(actionUrl);
|
|
}}
|
|
>
|
|
{actionLabel}
|
|
</Button>
|
|
) : null,
|
|
});
|
|
});
|
|
}
|