From f3bd5e4d583d370dfc8fff5b2f94223649f75941 Mon Sep 17 00:00:00 2001 From: Enrico Ros Date: Thu, 17 Oct 2024 18:32:01 -0700 Subject: [PATCH] Selective Search --- .../components/layout-drawer/ChatDrawer.tsx | 25 ++++++++++++++++--- .../useChatDrawerRenderItems.tsx | 11 +++++--- src/common/stores/chat/chat.message.ts | 4 ++- 3 files changed, 31 insertions(+), 9 deletions(-) diff --git a/src/apps/chat/components/layout-drawer/ChatDrawer.tsx b/src/apps/chat/components/layout-drawer/ChatDrawer.tsx index d0af4ee78..425ae95ae 100644 --- a/src/apps/chat/components/layout-drawer/ChatDrawer.tsx +++ b/src/apps/chat/components/layout-drawer/ChatDrawer.tsx @@ -30,7 +30,7 @@ import { useUIPreferencesStore } from '~/common/state/store-ui'; import { ChatDrawerItemMemo, FolderChangeRequest } from './ChatDrawerItem'; import { ChatFolderList } from './folders/ChatFolderList'; -import { ChatNavGrouping, ChatSearchSorting, isDrawerSearching, useChatDrawerRenderItems } from './useChatDrawerRenderItems'; +import { ChatNavGrouping, ChatSearchDepth, ChatSearchSorting, isDrawerSearching, useChatDrawerRenderItems } from './useChatDrawerRenderItems'; import { ClearFolderText } from '../layout-bar/useFolderDropdown'; import { useChatDrawerFilters } from '../../store-app-chat'; @@ -78,6 +78,7 @@ function ChatDrawer(props: { // local state const [navGrouping, setNavGrouping] = React.useState('date'); const [searchSorting, setSearchSorting] = React.useState('date'); + const [searchDepth, setSearchDepth] = React.useState('attachments'); // default: full search const [debouncedSearchQuery, setDebouncedSearchQuery] = React.useState(''); const [folderChangeRequest, setFolderChangeRequest] = React.useState(null); @@ -92,7 +93,7 @@ function ChatDrawer(props: { } = useChatDrawerFilters(); const { activeFolder, allFolders, enableFolders, toggleEnableFolders } = useFolders(props.activeFolderId); const { filteredChatsCount, filteredChatIDs, filteredChatsAreEmpty, filteredChatsBarBasis, filteredChatsIncludeActive, renderNavItems } = useChatDrawerRenderItems( - props.activeConversationId, props.chatPanesConversationIds, debouncedSearchQuery, activeFolder, allFolders, filterHasStars, filterHasImageAssets, filterHasDocFragments, navGrouping, searchSorting, showRelativeSize, + props.activeConversationId, props.chatPanesConversationIds, debouncedSearchQuery, activeFolder, allFolders, filterHasStars, filterHasImageAssets, filterHasDocFragments, navGrouping, searchSorting, showRelativeSize, searchDepth, ); const [uiComplexityMode, contentScaling] = useUIPreferencesStore(useShallow((state) => [state.complexityMode, state.contentScaling])); const zenMode = uiComplexityMode === 'minimal'; @@ -210,7 +211,7 @@ function ChatDrawer(props: { ) : ( - // While searching, show the sorting options + // While searching, show the sorting and depth options Sort By @@ -223,11 +224,27 @@ function ChatDrawer(props: { {searchSorting === 'date' && } Date + + + Search In + + setSearchDepth('titles')}> + {searchDepth === 'titles' && } + Titles + + setSearchDepth('content')}> + {searchDepth === 'content' && } + Titles + Content + + setSearchDepth('attachments')}> + {searchDepth === 'attachments' && } + Full + )} ), [ - filterHasDocFragments, filterHasImageAssets, filterHasStars, isSearching, navGrouping, searchSorting, showPersonaIcons, showRelativeSize, + filterHasDocFragments, filterHasImageAssets, filterHasStars, isSearching, navGrouping, searchSorting, searchDepth, showPersonaIcons, showRelativeSize, toggleFilterHasDocFragments, toggleFilterHasImageAssets, toggleFilterHasStars, toggleShowPersonaIcons, toggleShowRelativeSize, ]); diff --git a/src/apps/chat/components/layout-drawer/useChatDrawerRenderItems.tsx b/src/apps/chat/components/layout-drawer/useChatDrawerRenderItems.tsx index 401e48720..3c694935a 100644 --- a/src/apps/chat/components/layout-drawer/useChatDrawerRenderItems.tsx +++ b/src/apps/chat/components/layout-drawer/useChatDrawerRenderItems.tsx @@ -38,6 +38,7 @@ export type ChatNavGrouping = false | 'date' | 'persona' | 'dimension'; export type ChatSearchSorting = 'frequency' | 'date'; +export type ChatSearchDepth = 'titles' | 'content' | 'attachments'; function messageHasDocAttachmentFragments(message: DMessage): boolean { @@ -87,6 +88,7 @@ export function useChatDrawerRenderItems( grouping: ChatNavGrouping, searchSorting: ChatSearchSorting, showRelativeSize: boolean, + searchDepth: ChatSearchDepth, ): ChatDrawerRenderItems { const stabilizeRenderItems = React.useRef(); @@ -111,8 +113,8 @@ export function useChatDrawerRenderItems( let hasStars = false, hasImages = false, hasDocs = false; for (const _m of _c.messages) { _m.userFlags?.forEach(flag => messageFlags.add(flag)); - if (isSearching) { - const messageText = messageFragmentsReduceText(_m.fragments, '\n'); + if (isSearching && searchDepth !== 'titles') { + const messageText = messageFragmentsReduceText(_m.fragments, '\n', searchDepth !== 'attachments'); if (messageText) lcMessageSearchText += messageText.toLowerCase() + '\n'; } if (!hasStars && messageHasStarredFragments(_m)) hasStars = true; @@ -134,6 +136,7 @@ export function useChatDrawerRenderItems( const titleFrequency = title.toLowerCase().split(lcTextQuery).length - 1; const messageFrequency = lcMessageSearchText.split(lcTextQuery).length - 1; searchFrequency = titleFrequency + messageFrequency; + if (searchFrequency === 0) return null; } // union of message flags -> emoji string @@ -164,7 +167,7 @@ export function useChatDrawerRenderItems( searchFrequency, }; }) - .filter(item => !!item && (!isSearching || item.searchFrequency > 0)) as ChatNavigationItemData[]; + .filter(item => !!item) as ChatNavigationItemData[]; // check if the active conversation has an item in the list const filteredChatsIncludeActive = chatNavItems.some(_c => _c.conversationId === activeConversationId); @@ -181,7 +184,7 @@ export function useChatDrawerRenderItems( if (isSearching) { // start growing the render array from the nav array - renderNavItems = [...chatNavItems] + renderNavItems = [...chatNavItems]; // only prepend a 'Results' group if there are results if (chatNavItems.length) diff --git a/src/common/stores/chat/chat.message.ts b/src/common/stores/chat/chat.message.ts index bb867c8bf..46b6897ee 100644 --- a/src/common/stores/chat/chat.message.ts +++ b/src/common/stores/chat/chat.message.ts @@ -233,7 +233,7 @@ export function messageSetUserFlag(message: Pick, flag: D // helpers during the transition from V3 -export function messageFragmentsReduceText(fragments: DMessageFragment[], fragmentSeparator: string = '\n\n'): string { +export function messageFragmentsReduceText(fragments: DMessageFragment[], fragmentSeparator: string = '\n\n', excludeAttachmentFragments?: boolean): string { return fragments .map(fragment => { @@ -253,6 +253,8 @@ export function messageFragmentsReduceText(fragments: DMessageFragment[], fragme } break; case isAttachmentFragment(fragment): + if (excludeAttachmentFragments) + return ''; switch (fragment.part.pt) { case 'doc': return fragment.part.data.text;