open-webui/src/lib/components/layout/SearchModal.svelte

439 lines
11 KiB
Svelte
Raw Normal View History

2025-05-18 21:39:33 +00:00
<script lang="ts">
import { toast } from 'svelte-sonner';
2025-07-19 14:06:59 +00:00
import { getContext, onDestroy, onMount, tick } from 'svelte';
2025-05-18 21:39:33 +00:00
const i18n = getContext('i18n');
import Modal from '$lib/components/common/Modal.svelte';
import SearchInput from './Sidebar/SearchInput.svelte';
2025-07-19 14:06:59 +00:00
import { getChatById, getChatList, getChatListBySearchText } from '$lib/apis/chats';
2025-05-18 21:39:33 +00:00
import Spinner from '../common/Spinner.svelte';
2025-05-18 22:32:06 +00:00
import dayjs from '$lib/dayjs';
2025-05-18 21:39:33 +00:00
import calendar from 'dayjs/plugin/calendar';
import Loader from '../common/Loader.svelte';
2025-07-19 14:06:59 +00:00
import { createMessagesList } from '$lib/utils';
import { user } from '$lib/stores';
import Messages from '../chat/Messages.svelte';
2025-08-14 13:01:31 +00:00
import { goto } from '$app/navigation';
2025-09-24 21:29:02 +00:00
import PencilSquare from '../icons/PencilSquare.svelte';
import Note from '../icons/Note.svelte';
2025-05-18 21:39:33 +00:00
dayjs.extend(calendar);
export let show = false;
2025-05-18 21:54:26 +00:00
export let onClose = () => {};
2025-05-18 21:39:33 +00:00
2025-09-24 21:29:02 +00:00
let actions = [
{
label: 'Start a new conversation',
onClick: async () => {
await goto(`/${query ? `?q=${query}` : ''}`);
show = false;
onClose();
},
icon: PencilSquare
},
{
label: 'Create a new note',
onClick: async () => {
await goto('/notes');
show = false;
onClose();
},
icon: Note
}
];
2025-05-18 21:39:33 +00:00
let query = '';
let page = 1;
let chatList = null;
let chatListLoading = false;
let allChatsLoaded = false;
let searchDebounceTimeout;
2025-08-09 22:10:18 +00:00
let selectedIdx = null;
2025-07-19 14:06:59 +00:00
let selectedChat = null;
let selectedModels = [''];
let history = null;
let messages = null;
2025-07-19 17:20:28 +00:00
$: if (!chatListLoading && chatList) {
loadChatPreview(selectedIdx);
}
2025-07-19 14:06:59 +00:00
const loadChatPreview = async (selectedIdx) => {
2025-08-09 22:10:18 +00:00
if (
!chatList ||
chatList.length === 0 ||
selectedIdx === null ||
chatList[selectedIdx] === undefined
) {
2025-07-19 14:06:59 +00:00
selectedChat = null;
messages = null;
history = null;
selectedModels = [''];
return;
}
2025-09-24 21:29:02 +00:00
const selectedChatIdx = selectedIdx - actions.length;
if (selectedChatIdx < 0) {
selectedChat = null;
return;
}
const chatId = chatList[selectedChatIdx].id;
2025-07-19 14:06:59 +00:00
const chat = await getChatById(localStorage.token, chatId).catch(async (error) => {
return null;
});
if (chat) {
if (chat?.chat?.history) {
selectedModels =
(chat?.chat?.models ?? undefined) !== undefined
? chat?.chat?.models
: [chat?.chat?.models ?? ''];
history = chat?.chat?.history;
messages = createMessagesList(chat?.chat?.history, chat?.chat?.history?.currentId);
// scroll to the bottom of the messages container
await tick();
const messagesContainerElement = document.getElementById('chat-preview');
if (messagesContainerElement) {
messagesContainerElement.scrollTop = messagesContainerElement.scrollHeight;
}
} else {
messages = [];
}
} else {
toast.error($i18n.t('Failed to load chat preview'));
selectedChat = null;
messages = null;
history = null;
selectedModels = [''];
return;
}
};
2025-05-18 21:39:33 +00:00
const searchHandler = async () => {
2025-09-18 22:47:32 +00:00
if (!show) {
return;
}
2025-05-18 21:39:33 +00:00
if (searchDebounceTimeout) {
clearTimeout(searchDebounceTimeout);
}
2025-05-18 22:01:07 +00:00
page = 1;
chatList = null;
2025-05-18 21:39:33 +00:00
if (query === '') {
chatList = await getChatList(localStorage.token, page);
} else {
searchDebounceTimeout = setTimeout(async () => {
chatList = await getChatListBySearchText(localStorage.token, query, page);
2025-08-06 19:36:43 +00:00
if ((chatList ?? []).length === 0) {
allChatsLoaded = true;
} else {
allChatsLoaded = false;
}
2025-05-18 22:07:41 +00:00
}, 500);
2025-05-18 21:39:33 +00:00
}
2025-07-19 17:20:28 +00:00
selectedChat = null;
messages = null;
history = null;
selectedModels = [''];
2025-05-18 21:39:33 +00:00
if ((chatList ?? []).length === 0) {
allChatsLoaded = true;
} else {
allChatsLoaded = false;
}
};
const loadMoreChats = async () => {
chatListLoading = true;
page += 1;
let newChatList = [];
if (query) {
newChatList = await getChatListBySearchText(localStorage.token, query, page);
} else {
newChatList = await getChatList(localStorage.token, page);
}
// once the bottom of the list has been reached (no results) there is no need to continue querying
allChatsLoaded = newChatList.length === 0;
if (newChatList.length > 0) {
chatList = [...chatList, ...newChatList];
}
chatListLoading = false;
};
$: if (show) {
2025-05-18 21:39:33 +00:00
searchHandler();
}
2025-05-18 21:39:33 +00:00
2025-07-19 14:06:59 +00:00
const onKeyDown = (e) => {
const searchOptions = document.getElementById('search-options-container');
2025-08-08 16:52:53 +00:00
if (searchOptions || !show) {
return;
}
2025-07-19 14:06:59 +00:00
if (e.code === 'Escape') {
show = false;
onClose();
2025-09-24 21:29:02 +00:00
} else if (e.code === 'Enter') {
2025-07-19 14:06:59 +00:00
const item = document.querySelector(`[data-arrow-selected="true"]`);
if (item) {
item?.click();
2025-09-24 21:29:02 +00:00
show = false;
2025-07-19 14:06:59 +00:00
}
return;
} else if (e.code === 'ArrowDown') {
const searchInput = document.getElementById('search-input');
if (searchInput) {
// check if focused on the search input
if (document.activeElement === searchInput) {
searchInput.blur();
selectedIdx = 0;
return;
}
}
2025-09-24 21:29:02 +00:00
selectedIdx = Math.min(selectedIdx + 1, (chatList ?? []).length - 1 + actions.length);
2025-07-19 14:06:59 +00:00
} else if (e.code === 'ArrowUp') {
if (selectedIdx === 0) {
const searchInput = document.getElementById('search-input');
if (searchInput) {
// check if focused on the search input
if (document.activeElement !== searchInput) {
searchInput.focus();
selectedIdx = 0;
return;
}
}
}
selectedIdx = Math.max(selectedIdx - 1, 0);
}
const item = document.querySelector(`[data-arrow-selected="true"]`);
item?.scrollIntoView({ block: 'center', inline: 'nearest', behavior: 'instant' });
};
2025-05-18 21:39:33 +00:00
onMount(() => {
2025-07-19 14:06:59 +00:00
document.addEventListener('keydown', onKeyDown);
});
onDestroy(() => {
if (searchDebounceTimeout) {
clearTimeout(searchDebounceTimeout);
}
document.removeEventListener('keydown', onKeyDown);
2025-05-18 21:39:33 +00:00
});
</script>
2025-07-19 14:06:59 +00:00
<Modal size="xl" bind:show>
2025-09-20 06:19:23 +00:00
<div class="py-3 dark:text-gray-300 text-gray-700">
<div class="px-4 pb-1.5">
2025-05-18 21:39:33 +00:00
<SearchInput
bind:value={query}
on:input={searchHandler}
placeholder={$i18n.t('Search')}
showClearButton={true}
2025-08-09 22:10:18 +00:00
onFocus={() => {
selectedIdx = null;
messages = null;
}}
2025-05-20 19:47:41 +00:00
onKeydown={(e) => {
console.log('e', e);
if (e.code === 'Enter' && (chatList ?? []).length > 0) {
const item = document.querySelector(`[data-arrow-selected="true"]`);
if (item) {
item?.click();
}
show = false;
return;
} else if (e.code === 'ArrowDown') {
2025-09-24 21:29:02 +00:00
selectedIdx = Math.min(selectedIdx + 1, (chatList ?? []).length - 1 + actions.length);
2025-05-20 19:47:41 +00:00
} else if (e.code === 'ArrowUp') {
selectedIdx = Math.max(selectedIdx - 1, 0);
} else {
selectedIdx = 0;
}
const item = document.querySelector(`[data-arrow-selected="true"]`);
item?.scrollIntoView({ block: 'center', inline: 'nearest', behavior: 'instant' });
}}
2025-05-18 21:39:33 +00:00
/>
</div>
2025-09-15 19:28:16 +00:00
<!-- <hr class="border-gray-50 dark:border-gray-850 my-1" /> -->
2025-05-18 21:39:33 +00:00
2025-09-20 06:19:23 +00:00
<div class="flex px-4 pb-1">
2025-07-19 14:06:59 +00:00
<div
2025-09-24 21:29:02 +00:00
class="flex flex-col overflow-y-auto h-96 md:h-[40rem] max-h-full scrollbar-hidden w-full flex-1 pr-2"
2025-07-19 14:06:59 +00:00
>
2025-09-24 21:29:02 +00:00
<div class="w-full text-xs text-gray-500 dark:text-gray-500 font-medium pb-2 px-2">
{$i18n.t('Actions')}
</div>
{#each actions as action, idx (action.label)}
<button
class=" w-full flex items-center rounded-xl text-sm py-2 px-3 hover:bg-gray-50 dark:hover:bg-gray-850 {selectedIdx ===
idx
? 'bg-gray-50 dark:bg-gray-850'
: ''}"
data-arrow-selected={selectedIdx === idx ? 'true' : undefined}
dragabble="false"
on:mouseenter={() => {
selectedIdx = idx;
}}
on:click={async () => {
await action.onClick();
}}
>
<div class="pr-2">
<svelte:component this={action.icon} />
</div>
<div class=" flex-1 text-left">
<div class="text-ellipsis line-clamp-1 w-full">
{$i18n.t(action.label)}
</div>
</div>
</button>
{/each}
2025-07-19 14:06:59 +00:00
{#if chatList}
2025-09-24 21:29:02 +00:00
<hr class="border-gray-50 dark:border-gray-850 my-3" />
2025-07-19 14:06:59 +00:00
{#if chatList.length === 0}
2025-09-24 21:29:02 +00:00
<div class="text-xs text-gray-500 dark:text-gray-400 text-center px-5 py-4">
2025-07-19 14:06:59 +00:00
{$i18n.t('No results found')}
</div>
{/if}
2025-05-18 21:39:33 +00:00
2025-07-19 14:06:59 +00:00
{#each chatList as chat, idx (chat.id)}
{#if idx === 0 || (idx > 0 && chat.time_range !== chatList[idx - 1].time_range)}
<div
class="w-full text-xs text-gray-500 dark:text-gray-500 font-medium {idx === 0
? ''
: 'pt-5'} pb-2 px-2"
>
{$i18n.t(chat.time_range)}
<!-- localisation keys for time_range to be recognized from the i18next parser (so they don't get automatically removed):
2025-05-18 21:39:33 +00:00
{$i18n.t('Today')}
{$i18n.t('Yesterday')}
{$i18n.t('Previous 7 days')}
{$i18n.t('Previous 30 days')}
{$i18n.t('January')}
{$i18n.t('February')}
{$i18n.t('March')}
{$i18n.t('April')}
{$i18n.t('May')}
{$i18n.t('June')}
{$i18n.t('July')}
{$i18n.t('August')}
{$i18n.t('September')}
{$i18n.t('October')}
{$i18n.t('November')}
{$i18n.t('December')}
-->
2025-07-19 14:06:59 +00:00
</div>
{/if}
2025-05-18 21:39:33 +00:00
2025-07-19 14:06:59 +00:00
<a
2025-09-24 21:29:02 +00:00
class=" w-full flex justify-between items-center rounded-xl text-sm py-2 px-3 hover:bg-gray-50 dark:hover:bg-gray-850 {selectedIdx ===
idx + actions.length
2025-07-19 14:06:59 +00:00
? 'bg-gray-50 dark:bg-gray-850'
: ''}"
href="/c/{chat.id}"
draggable="false"
2025-09-24 21:29:02 +00:00
data-arrow-selected={selectedIdx === idx + actions.length ? 'true' : undefined}
2025-07-19 14:06:59 +00:00
on:mouseenter={() => {
2025-09-24 21:29:02 +00:00
selectedIdx = idx + actions.length;
2025-07-19 14:06:59 +00:00
}}
2025-08-14 13:01:31 +00:00
on:click={async () => {
await goto(`/c/${chat.id}`);
2025-07-19 14:06:59 +00:00
show = false;
onClose();
}}
>
<div class=" flex-1">
<div class="text-ellipsis line-clamp-1 w-full">
{chat?.title}
</div>
2025-05-18 21:39:33 +00:00
</div>
2025-07-19 14:06:59 +00:00
<div class=" pl-3 shrink-0 text-gray-500 dark:text-gray-400 text-xs">
{dayjs(chat?.updated_at * 1000).calendar()}
</div>
</a>
{/each}
{#if !allChatsLoaded}
<Loader
on:visible={(e) => {
if (!chatListLoading) {
loadMoreChats();
}
}}
>
2025-08-13 19:10:50 +00:00
<div class="w-full flex justify-center py-4 text-xs animate-pulse items-center gap-2">
2025-07-19 14:06:59 +00:00
<Spinner className=" size-4" />
2025-08-19 18:39:17 +00:00
<div class=" ">{$i18n.t('Loading...')}</div>
2025-07-19 14:06:59 +00:00
</div>
</Loader>
{/if}
{:else}
<div class="w-full h-full flex justify-center items-center">
<Spinner className="size-5" />
</div>
{/if}
</div>
<div
id="chat-preview"
class="hidden md:flex md:flex-1 w-full overflow-y-auto h-96 md:h-[40rem] scrollbar-hidden"
>
{#if messages === null}
<div
class="w-full h-full flex justify-center items-center text-gray-500 dark:text-gray-400 text-sm"
2025-05-18 21:39:33 +00:00
>
2025-07-19 14:06:59 +00:00
{$i18n.t('Select a conversation to preview')}
</div>
{:else}
<div class="w-full h-full flex flex-col">
<Messages
className="h-full flex pt-4 pb-8 w-full"
2025-08-09 21:03:02 +00:00
chatId={`chat-preview-${selectedChat?.id ?? ''}`}
2025-07-19 14:06:59 +00:00
user={$user}
readOnly={true}
{selectedModels}
bind:history
bind:messages
autoScroll={true}
2025-08-08 22:00:21 +00:00
sendMessage={() => {}}
2025-07-19 14:06:59 +00:00
continueResponse={() => {}}
regenerateResponse={() => {}}
/>
</div>
2025-05-18 21:39:33 +00:00
{/if}
2025-07-19 14:06:59 +00:00
</div>
2025-05-18 21:39:33 +00:00
</div>
</div>
</Modal>