mirror of
https://github.com/arcan1s/ahriman.git
synced 2026-04-07 11:03:37 +00:00
upload ai slop
This commit is contained in:
55
frontend/src/contexts/NotificationProvider.tsx
Normal file
55
frontend/src/contexts/NotificationProvider.tsx
Normal file
@@ -0,0 +1,55 @@
|
||||
import React, { useState, useCallback, useMemo, useRef, type ReactNode } from "react";
|
||||
import { Box, type AlertColor } from "@mui/material";
|
||||
import { NotificationContext } from "contexts/NotificationContext";
|
||||
import NotificationItem from "contexts/NotificationItem";
|
||||
import type { Notification } from "contexts/Notification";
|
||||
|
||||
export function NotificationProvider({ children }: { children: ReactNode }): React.JSX.Element {
|
||||
const nextId = useRef(0);
|
||||
const [notifications, setNotifications] = useState<Notification[]>([]);
|
||||
|
||||
const addNotification = useCallback((title: string, message: string, severity: AlertColor) => {
|
||||
const id = nextId.current++;
|
||||
setNotifications((prev) => [...prev, { id, title, message, severity }]);
|
||||
}, []);
|
||||
|
||||
const removeNotification = useCallback((id: number) => {
|
||||
setNotifications((prev) => prev.filter((n) => n.id !== id));
|
||||
}, []);
|
||||
|
||||
const showSuccess = useCallback(
|
||||
(title: string, message: string) => addNotification(title, message, "success"),
|
||||
[addNotification],
|
||||
);
|
||||
const showError = useCallback(
|
||||
(title: string, message: string) => addNotification(title, message, "error"),
|
||||
[addNotification],
|
||||
);
|
||||
|
||||
const value = useMemo(() => ({ showSuccess, showError }), [showSuccess, showError]);
|
||||
|
||||
return (
|
||||
<NotificationContext.Provider value={value}>
|
||||
{children}
|
||||
<Box
|
||||
sx={{
|
||||
position: "fixed",
|
||||
top: 16,
|
||||
left: "50%",
|
||||
transform: "translateX(-50%)",
|
||||
zIndex: (theme) => theme.zIndex.snackbar,
|
||||
display: "flex",
|
||||
flexDirection: "column",
|
||||
gap: 1,
|
||||
maxWidth: 500,
|
||||
width: "100%",
|
||||
pointerEvents: "none",
|
||||
}}
|
||||
>
|
||||
{notifications.map((n) => (
|
||||
<NotificationItem key={n.id} notification={n} onClose={removeNotification} />
|
||||
))}
|
||||
</Box>
|
||||
</NotificationContext.Provider>
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user