b4a7627718
Wrap setAllSessions in useCallback so it has a stable reference. This breaks the infinite re-render loop that was causing 4-10 requests per second to /users/me/sessions.
45 lines
1.1 KiB
TypeScript
45 lines
1.1 KiB
TypeScript
import { createContext, useCallback, useContext, useState, type ReactNode } from "react";
|
|
|
|
export interface Session {
|
|
id: string;
|
|
display_name: string;
|
|
tool_type_name: string;
|
|
tool_icon: string;
|
|
tool_type_interfaces: string[];
|
|
repository_name: string;
|
|
repository_id: string;
|
|
project_name: string;
|
|
project_id: string;
|
|
status: string;
|
|
url: string | null;
|
|
}
|
|
|
|
interface SessionsContextType {
|
|
sessions: Session[];
|
|
setAllSessions: (sessions: Session[]) => void;
|
|
}
|
|
|
|
const SessionsContext = createContext<SessionsContextType | undefined>(undefined);
|
|
|
|
export const SessionsProvider = ({ children }: { children: ReactNode }) => {
|
|
const [sessions, setSessions] = useState<Session[]>([]);
|
|
|
|
const setAllSessions = useCallback((newSessions: Session[]) => {
|
|
setSessions(newSessions);
|
|
}, []);
|
|
|
|
return (
|
|
<SessionsContext.Provider value={{ sessions, setAllSessions }}>
|
|
{children}
|
|
</SessionsContext.Provider>
|
|
);
|
|
};
|
|
|
|
export const useSessions = () => {
|
|
const context = useContext(SessionsContext);
|
|
if (context === undefined) {
|
|
throw new Error("useSessions must be used within a SessionsProvider");
|
|
}
|
|
return context;
|
|
};
|