style: run prettier on entire codebase
This commit is contained in:
@@ -1,53 +1,70 @@
|
||||
import { createContext, useContext, useState, useCallback, useMemo, useRef, type ReactNode } from 'react'
|
||||
import {
|
||||
createContext,
|
||||
useContext,
|
||||
useState,
|
||||
useCallback,
|
||||
useMemo,
|
||||
useRef,
|
||||
type ReactNode,
|
||||
} from "react";
|
||||
|
||||
interface Alert {
|
||||
id: string
|
||||
message: string
|
||||
type: 'success' | 'error' | 'warning' | 'info'
|
||||
id: string;
|
||||
message: string;
|
||||
type: "success" | "error" | "warning" | "info";
|
||||
}
|
||||
|
||||
interface AlertMethods {
|
||||
addAlert: (message: string, type?: string, duration?: number) => string
|
||||
removeAlert: (id: string) => void
|
||||
success: (message: string, duration?: number) => string
|
||||
error: (message: string, duration?: number) => string
|
||||
warning: (message: string, duration?: number) => string
|
||||
info: (message: string, duration?: number) => string
|
||||
addAlert: (message: string, type?: string, duration?: number) => string;
|
||||
removeAlert: (id: string) => void;
|
||||
success: (message: string, duration?: number) => string;
|
||||
error: (message: string, duration?: number) => string;
|
||||
warning: (message: string, duration?: number) => string;
|
||||
info: (message: string, duration?: number) => string;
|
||||
}
|
||||
|
||||
interface AlertStateValue {
|
||||
alerts: Alert[]
|
||||
removeAlert: (id: string) => void
|
||||
alerts: Alert[];
|
||||
removeAlert: (id: string) => void;
|
||||
}
|
||||
|
||||
const AlertContext = createContext<AlertMethods | null>(null)
|
||||
const AlertStateContext = createContext<AlertStateValue | null>(null)
|
||||
const AlertContext = createContext<AlertMethods | null>(null);
|
||||
const AlertStateContext = createContext<AlertStateValue | null>(null);
|
||||
|
||||
export function AlertProvider({ children }: { children: ReactNode }) {
|
||||
const [alerts, setAlerts] = useState<Alert[]>([])
|
||||
const [alerts, setAlerts] = useState<Alert[]>([]);
|
||||
|
||||
const removeAlert = useCallback((id: string) => {
|
||||
setAlerts(prev => prev.filter(alert => alert.id !== id))
|
||||
}, [])
|
||||
setAlerts((prev) => prev.filter((alert) => alert.id !== id));
|
||||
}, []);
|
||||
|
||||
const counterRef = useRef(0)
|
||||
const addAlert = useCallback((message: string, type = 'success', duration = 4000) => {
|
||||
const id = `${Date.now()}-${counterRef.current++}`
|
||||
setAlerts(prev => [...prev, { id, message, type: type as Alert['type'] }])
|
||||
if (duration > 0) {
|
||||
setTimeout(() => removeAlert(id), duration)
|
||||
}
|
||||
return id
|
||||
}, [removeAlert])
|
||||
const counterRef = useRef(0);
|
||||
const addAlert = useCallback(
|
||||
(message: string, type = "success", duration = 4000) => {
|
||||
const id = `${Date.now()}-${counterRef.current++}`;
|
||||
setAlerts((prev) => [
|
||||
...prev,
|
||||
{ id, message, type: type as Alert["type"] },
|
||||
]);
|
||||
if (duration > 0) {
|
||||
setTimeout(() => removeAlert(id), duration);
|
||||
}
|
||||
return id;
|
||||
},
|
||||
[removeAlert],
|
||||
);
|
||||
|
||||
const methods = useMemo<AlertMethods>(() => ({
|
||||
addAlert,
|
||||
removeAlert,
|
||||
success: (message, duration) => addAlert(message, 'success', duration),
|
||||
error: (message, duration) => addAlert(message, 'error', duration),
|
||||
warning: (message, duration) => addAlert(message, 'warning', duration),
|
||||
info: (message, duration) => addAlert(message, 'info', duration),
|
||||
}), [addAlert, removeAlert])
|
||||
const methods = useMemo<AlertMethods>(
|
||||
() => ({
|
||||
addAlert,
|
||||
removeAlert,
|
||||
success: (message, duration) => addAlert(message, "success", duration),
|
||||
error: (message, duration) => addAlert(message, "error", duration),
|
||||
warning: (message, duration) => addAlert(message, "warning", duration),
|
||||
info: (message, duration) => addAlert(message, "info", duration),
|
||||
}),
|
||||
[addAlert, removeAlert],
|
||||
);
|
||||
|
||||
return (
|
||||
<AlertContext.Provider value={methods}>
|
||||
@@ -55,17 +72,19 @@ export function AlertProvider({ children }: { children: ReactNode }) {
|
||||
{children}
|
||||
</AlertStateContext.Provider>
|
||||
</AlertContext.Provider>
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
export function useAlert(): AlertMethods {
|
||||
const context = useContext(AlertContext)
|
||||
if (!context) throw new Error('useAlert must be used within an AlertProvider')
|
||||
return context
|
||||
const context = useContext(AlertContext);
|
||||
if (!context)
|
||||
throw new Error("useAlert must be used within an AlertProvider");
|
||||
return context;
|
||||
}
|
||||
|
||||
export function useAlertState(): AlertStateValue {
|
||||
const context = useContext(AlertStateContext)
|
||||
if (!context) throw new Error('useAlertState must be used within an AlertProvider')
|
||||
return context
|
||||
const context = useContext(AlertStateContext);
|
||||
if (!context)
|
||||
throw new Error("useAlertState must be used within an AlertProvider");
|
||||
return context;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user