Files
headquarter/apps/web/src/state/sessions.tsx
T
Fusion b4a7627718 fix(sessions): stabilize setAllSessions to prevent polling loop
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.
2026-05-20 13:41:24 +02:00

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;
};