Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 12 additions & 17 deletions components/AskAiContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -143,26 +143,11 @@ export function AskAiProvider({ children }: { children: ReactNode }) {

try {
const savedSessions = localStorage.getItem(STORAGE_KEYS.chatSessions);
const savedCurrentChatId = localStorage.getItem(
STORAGE_KEYS.currentChatId,
);

if (savedSessions) {
const sessions = JSON.parse(savedSessions) as ChatSession[];
const sanitizedSessions = sanitizeChatSessions(sessions);
setChatSessions(sanitizedSessions);

if (savedCurrentChatId) {
const session = sanitizedSessions.find(
(s) => s.id === savedCurrentChatId,
);
if (session) {
setCurrentChatId(savedCurrentChatId);
setMessages(session.messages);
} else {
localStorage.removeItem(STORAGE_KEYS.currentChatId);
}
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dead code: localStorage write with no reader remaining

Low Severity

The diff removed the code that reads STORAGE_KEYS.currentChatId from localStorage on mount, but left behind the effect that writes it (lines 168–176) and the currentChatId key in STORAGE_KEYS. This effect now runs on every session change, writing to localStorage with no consumer ever reading the value back, making it dead code.

Additional Locations (1)

Fix in Cursor Fix in Web

}
} catch {
// Invalid JSON in localStorage, ignore
Expand Down Expand Up @@ -233,9 +218,19 @@ export function AskAiProvider({ children }: { children: ReactNode }) {
return sessionId;
}, []);

const openSidebar = useCallback(() => setIsOpen(true), []);
const openSidebar = useCallback(() => {
createNewSession();
setIsOpen(true);
}, [createNewSession]);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

openSidebar creates new session even when already open

Medium Severity

openSidebar unconditionally calls createNewSession() every time it's invoked, even if the sidebar is already open. This differs from toggleSidebar, which correctly guards with if (!prev) to only create a session when transitioning from closed to open. If openSidebar is called while the user is mid-conversation, it silently discards their current chat and replaces it with an empty session. The previous implementation was idempotent (setIsOpen(true) was a no-op when already open), and this change breaks that contract.

Fix in Cursor Fix in Web

const closeSidebar = useCallback(() => setIsOpen(false), []);
const toggleSidebar = useCallback(() => setIsOpen((prev) => !prev), []);
const toggleSidebar = useCallback(() => {
setIsOpen((prev) => {
if (!prev) {
createNewSession();
}
return !prev;
});
}, [createNewSession]);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Side effects inside React state updater function

Medium Severity

createNewSession() is called inside the setIsOpen state updater callback. React requires state updater functions to be pure. createNewSession() triggers multiple side effects (setChatSessions, setCurrentChatId, setMessages, etc.) and calls beforeSessionChangeRef.current?.(). In React Strict Mode (development), updater functions are invoked twice, which would create duplicate empty chat sessions each time the sidebar is toggled open.

Fix in Cursor Fix in Web

const openSidebarWithPrompt = useCallback(
(prompt: string) => {
// Create new session before setting prompt to ensure each query starts fresh
Expand Down
Loading