Skip to content
Merged
Show file tree
Hide file tree
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
118 changes: 78 additions & 40 deletions ui/desktop/src/components/bottom_menu/BottomMenuExtensionSelection.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { AppEvents } from '../../constants/events';
import { useCallback, useEffect, useMemo, useState, useRef } from 'react';
import { Puzzle } from 'lucide-react';
import { DropdownMenu, DropdownMenuContent, DropdownMenuTrigger } from '../ui/dropdown-menu';
Expand All @@ -16,6 +15,7 @@ import {
getExtensionOverrides,
} from '../../store/extensionOverrides';
import { defineMessages, useIntl } from '../../i18n';
import { AppEvents } from '../../constants/events';

const i18n = defineMessages({
manageExtensions: {
Expand Down Expand Up @@ -68,6 +68,8 @@ interface BottomMenuExtensionSelectionProps {
sessionId: string | null;
}

type GetSessionExtensionsSignal = Parameters<typeof getSessionExtensions>[0]['signal'];

export const BottomMenuExtensionSelection = ({ sessionId }: BottomMenuExtensionSelectionProps) => {
const intl = useIntl();
const [searchQuery, setSearchQuery] = useState('');
Expand All @@ -77,28 +79,25 @@ export const BottomMenuExtensionSelection = ({ sessionId }: BottomMenuExtensionS
const [isTransitioning, setIsTransitioning] = useState(false);
const [pendingSort, setPendingSort] = useState(false);
const [togglingExtension, setTogglingExtension] = useState<string | null>(null);
const [refreshTrigger, setRefreshTrigger] = useState(0);
const [isSessionExtensionsLoaded, setIsSessionExtensionsLoaded] = useState(false);
const sortTimeoutRef = useRef<ReturnType<typeof setTimeout> | null>(null);
const latestSessionIdRef = useRef(sessionId);
const { extensionsList: allExtensions } = useConfig();
const isHubView = !sessionId;

useEffect(() => {
latestSessionIdRef.current = sessionId;
setIsSessionExtensionsLoaded(false);
setSessionExtensions([]);
}, [sessionId]);
setPendingSort(false);
setIsTransitioning(false);
setTogglingExtension(null);

useEffect(() => {
const handleExtensionsLoaded = () => {
setRefreshTrigger((prev) => prev + 1);
};

window.addEventListener(AppEvents.SESSION_EXTENSIONS_LOADED, handleExtensionsLoaded);

return () => {
window.removeEventListener(AppEvents.SESSION_EXTENSIONS_LOADED, handleExtensionsLoaded);
};
}, []);
if (sortTimeoutRef.current) {
clearTimeout(sortTimeoutRef.current);
sortTimeoutRef.current = null;
}
}, [sessionId]);

useEffect(() => {
return () => {
Expand All @@ -108,33 +107,71 @@ export const BottomMenuExtensionSelection = ({ sessionId }: BottomMenuExtensionS
};
}, []);

const loadSessionExtensions = useCallback(
async (targetSessionId: string, signal?: GetSessionExtensionsSignal) => {
const response = await getSessionExtensions({
path: { session_id: targetSessionId },
signal,
throwOnError: true,
});

if (signal?.aborted || latestSessionIdRef.current !== targetSessionId) {
return;
}

setSessionExtensions(response.data?.extensions ?? []);
Comment thread
angiejones marked this conversation as resolved.
setIsSessionExtensionsLoaded(true);
},
[]
);

useEffect(() => {
if (refreshTrigger === 0 && !isOpen) {
if (!sessionId) {
setIsSessionExtensionsLoaded(true);
return;
}

const fetchExtensions = async () => {
if (!sessionId) {
let controller: AbortController | null = null;

const loadExtensionsForCurrentSession = (event: Event) => {
const targetSessionId = (event as CustomEvent<{ sessionId?: string }>).detail?.sessionId;

if (targetSessionId !== sessionId) {
return;
}

try {
const response = await getSessionExtensions({
path: { session_id: sessionId },
});
controller?.abort();
const currentController = new AbortController();
controller = currentController;

if (response.data?.extensions) {
setSessionExtensions(response.data.extensions);
setIsSessionExtensionsLoaded(true);
loadSessionExtensions(targetSessionId, currentController.signal).catch((error) => {
if (currentController.signal.aborted || latestSessionIdRef.current !== targetSessionId) {
return;
}
} catch (error) {

console.error('Failed to fetch session extensions:', error);
setIsSessionExtensionsLoaded(true);
}
});
};

fetchExtensions();
}, [sessionId, isOpen, refreshTrigger]);
window.addEventListener(AppEvents.SESSION_EXTENSIONS_LOADED, loadExtensionsForCurrentSession);

return () => {
controller?.abort();
window.removeEventListener(
AppEvents.SESSION_EXTENSIONS_LOADED,
loadExtensionsForCurrentSession
);
};
}, [sessionId, loadSessionExtensions]);

const finishSessionTransition = useCallback((targetSessionId: string) => {
if (latestSessionIdRef.current === targetSessionId) {
setPendingSort(false);
setIsTransitioning(false);
setTogglingExtension(null);
Comment thread
angiejones marked this conversation as resolved.
}
}, []);

const handleToggle = useCallback(
async (extensionConfig: FixedExtensionEntry) => {
Expand All @@ -160,6 +197,7 @@ export const BottomMenuExtensionSelection = ({ sessionId }: BottomMenuExtensionS
setPendingSort(false);
setIsTransitioning(false);
setTogglingExtension(null);
sortTimeoutRef.current = null;
}, 800);

toastService.success({
Expand Down Expand Up @@ -196,25 +234,25 @@ export const BottomMenuExtensionSelection = ({ sessionId }: BottomMenuExtensionS
clearTimeout(sortTimeoutRef.current);
}

sortTimeoutRef.current = setTimeout(async () => {
const response = await getSessionExtensions({
path: { session_id: sessionId },
});

if (response.data?.extensions) {
setSessionExtensions(response.data.extensions);
}
setPendingSort(false);
setIsTransitioning(false);
setTogglingExtension(null);
sortTimeoutRef.current = setTimeout(() => {
loadSessionExtensions(sessionId)
.catch((error) => {
if (latestSessionIdRef.current === sessionId) {
console.error('Failed to fetch session extensions:', error);
}
})
.finally(() => {
finishSessionTransition(sessionId);
sortTimeoutRef.current = null;
});
}, 800);
} catch {
setIsTransitioning(false);
setPendingSort(false);
setTogglingExtension(null);
}
},
[sessionId, isHubView, togglingExtension, intl]
[sessionId, isHubView, togglingExtension, intl, loadSessionExtensions, finishSessionTransition]
);

// Merge all available extensions with session-specific or hub override state
Expand Down
8 changes: 6 additions & 2 deletions ui/desktop/src/hooks/useChatStream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -748,7 +748,9 @@ export function useChatStream({
},
},
});
window.dispatchEvent(new CustomEvent(AppEvents.SESSION_EXTENSIONS_LOADED));
window.dispatchEvent(
new CustomEvent(AppEvents.SESSION_EXTENSIONS_LOADED, { detail: { sessionId } })
);
onSessionLoaded?.();
return;
}
Expand Down Expand Up @@ -776,7 +778,9 @@ export function useChatStream({
const extensionResults = resumeData?.extension_results;

showExtensionLoadResults(extensionResults);
window.dispatchEvent(new CustomEvent(AppEvents.SESSION_EXTENSIONS_LOADED));
window.dispatchEvent(
new CustomEvent(AppEvents.SESSION_EXTENSIONS_LOADED, { detail: { sessionId } })
);

const pendingRequestId = pendingReattachRequestIdRef.current;
const reattachedToActiveRequest = activeRequestIdRef.current !== null;
Expand Down
Loading