import { AutocompletePlugin } from '@algolia/autocomplete-js'; import { getI18n } from 'react-i18next'; import { TEMP_TAB_ID_PREFIX } from '../../../constants/tab'; import { useTabStore } from '../../../store/tabStore'; import { TabState, TabType } from '../../../types/tab'; import { getTabTypeIcon, highlightHits } from '../styles'; type TabSource = { id: string; title: string; type: TabType; favicon?: string; }; export const createOpenTabsPlugin = (): AutocompletePlugin => { const { t } = getI18n(); const plugin = { getSources({ query }) { return [ { sourceId: 'openTabsSource', getItems() { const { tabs } = useTabStore.getState(); // Filter out error tabs and those without titles const openTabs = tabs.filter( (tab) => tab.state !== TabState.ERROR && tab.title, ); if (!query) { return openTabs.map((tab) => ({ id: tab.id, title: tab.title, type: tab.type, favicon: (tab as { favicon?: string }).favicon, })); } // Filter tabs by the search query const lowerCaseQuery = query.toLowerCase(); return openTabs .filter((tab) => tab.title.toLowerCase().includes(lowerCaseQuery)) .map((tab) => ({ id: tab.id, title: tab.title, type: tab.type, favicon: (tab as { favicon?: string }).favicon, })); }, templates: { header() { return (
{t('Search.OpenTabs', { ns: 'agent' })}
); }, item({ item, state }) { return (
{item.favicon ? (
{item.favicon}
) : (
{getTabTypeIcon(item.type)}
)}
); }, noResults() { return (
{t('Search.NoTabsFound', { ns: 'agent' })}
); }, }, onSelect: async ({ item }) => { try { const tabStore = useTabStore.getState(); const { activeTabId, tabs } = tabStore; // Handle current active tab if (activeTabId) { const activeTab = tabs.find(tab => tab.id === activeTabId); // Always close temp tabs or NEW_TAB type tabs when selecting from search if (activeTab && (activeTab.id.startsWith(TEMP_TAB_ID_PREFIX) || activeTab.type === TabType.NEW_TAB)) { // Use tabStore method instead of direct service call tabStore.closeTab(activeTabId); } } // Use the tabStore's setActiveTab method which will handle the backend service call // and update the store state at the same time tabStore.setActiveTab(item.id); } catch (error) { console.error('Failed to select tab in search:', error); } }, }, ]; }, } satisfies AutocompletePlugin; return plugin; };