2025-05-03 15:20:52 +05:30
|
|
|
import React, { createContext, useContext, useState, useCallback, useMemo } from "react";
|
2025-05-02 17:14:36 +05:30
|
|
|
|
2025-05-03 15:20:52 +05:30
|
|
|
export type LogType = "log" | "info" | "warning" | "error" | "success";
|
2025-05-02 17:14:36 +05:30
|
|
|
|
|
|
|
|
export interface LogEntry {
|
|
|
|
|
id: string;
|
|
|
|
|
type: LogType;
|
|
|
|
|
message: string;
|
|
|
|
|
timestamp: Date;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
interface LoggerContextValue {
|
|
|
|
|
logs: LogEntry[];
|
|
|
|
|
setLogs: React.Dispatch<React.SetStateAction<LogEntry[]>>;
|
|
|
|
|
isLogListVisible: boolean;
|
|
|
|
|
setIsLogListVisible: React.Dispatch<React.SetStateAction<boolean>>;
|
|
|
|
|
log: (message: string) => void;
|
|
|
|
|
info: (message: string) => void;
|
|
|
|
|
warn: (message: string) => void;
|
|
|
|
|
error: (message: string) => void;
|
2025-05-03 15:20:52 +05:30
|
|
|
success: (message: string) => void;
|
2025-05-02 17:14:36 +05:30
|
|
|
clear: () => void;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const LoggerContext = createContext<LoggerContextValue | undefined>(undefined);
|
|
|
|
|
|
|
|
|
|
export const LoggerProvider: React.FC<{ children: React.ReactNode }> = ({
|
|
|
|
|
children,
|
|
|
|
|
}) => {
|
|
|
|
|
const [logs, setLogs] = useState<LogEntry[]>([]);
|
|
|
|
|
const [isLogListVisible, setIsLogListVisible] = useState<boolean>(false);
|
|
|
|
|
|
|
|
|
|
const generateId = useCallback(
|
|
|
|
|
() => Math.random().toString(36).substring(2, 9),
|
|
|
|
|
[]
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const addLog = useCallback(
|
|
|
|
|
(type: LogType, message: string) => {
|
|
|
|
|
const newLog: LogEntry = {
|
|
|
|
|
id: generateId(),
|
|
|
|
|
type,
|
|
|
|
|
message,
|
|
|
|
|
timestamp: new Date(),
|
|
|
|
|
};
|
|
|
|
|
setLogs((prevLogs) => [...prevLogs, newLog]);
|
|
|
|
|
},
|
|
|
|
|
[generateId]
|
|
|
|
|
);
|
|
|
|
|
|
2025-05-03 15:20:52 +05:30
|
|
|
const loggerMethods: LoggerContextValue = useMemo(() => ({
|
2025-05-02 17:14:36 +05:30
|
|
|
logs,
|
|
|
|
|
setLogs,
|
|
|
|
|
isLogListVisible,
|
|
|
|
|
setIsLogListVisible,
|
|
|
|
|
log: (message: string) => addLog("log", message),
|
|
|
|
|
info: (message: string) => addLog("info", message),
|
|
|
|
|
warn: (message: string) => addLog("warning", message),
|
|
|
|
|
error: (message: string) => addLog("error", message),
|
2025-05-03 15:20:52 +05:30
|
|
|
success: (message: string) => addLog("success", message),
|
|
|
|
|
clear: () => setLogs([]),
|
|
|
|
|
}), [logs, setLogs, isLogListVisible, setIsLogListVisible, addLog]);
|
2025-05-02 17:14:36 +05:30
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<LoggerContext.Provider value={loggerMethods}>
|
|
|
|
|
{children}
|
|
|
|
|
</LoggerContext.Provider>
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const useLogger = () => {
|
|
|
|
|
const context = useContext(LoggerContext);
|
|
|
|
|
if (!context) {
|
|
|
|
|
throw new Error("useLogger must be used within a LoggerProvider");
|
|
|
|
|
}
|
|
|
|
|
return context;
|
|
|
|
|
};
|