|
1 | 1 | <script lang="ts"> |
2 | 2 | import dayjs from 'dayjs'; |
3 | | - import { onMount, tick, getContext } from 'svelte'; |
| 3 | + import { onDestroy, onMount, tick, getContext } from 'svelte'; |
4 | 4 |
|
5 | 5 | import { decodeString } from '$lib/utils'; |
6 | | - import { getChatList } from '$lib/apis/chats'; |
| 6 | + import { getChatList, getChatListBySearchText } from '$lib/apis/chats'; |
7 | 7 |
|
8 | 8 | import Tooltip from '$lib/components/common/Tooltip.svelte'; |
9 | 9 | import Spinner from '$lib/components/common/Spinner.svelte'; |
10 | 10 | import Loader from '$lib/components/common/Loader.svelte'; |
11 | 11 | import { chatId } from '$lib/stores'; |
| 12 | + import SearchInput from './SearchInput.svelte'; |
12 | 13 |
|
13 | 14 | const i18n = getContext('i18n'); |
14 | 15 |
|
|
18 | 19 |
|
19 | 20 | let items = []; |
20 | 21 | let selectedIdx = 0; |
| 22 | + let query = ''; |
21 | 23 |
|
22 | 24 | let page = 1; |
23 | 25 | let itemsLoading = false; |
24 | 26 | let allItemsLoaded = false; |
| 27 | + let initialized = false; |
| 28 | + let searchDebounceTimer: ReturnType<typeof setTimeout>; |
| 29 | + let requestId = 0; |
| 30 | +
|
| 31 | + $: if (initialized) { |
| 32 | + query; |
| 33 | + scheduleSearch(); |
| 34 | + } |
| 35 | +
|
| 36 | + const scheduleSearch = () => { |
| 37 | + clearTimeout(searchDebounceTimer); |
| 38 | + searchDebounceTimer = setTimeout(init, 200); |
| 39 | + }; |
| 40 | +
|
| 41 | + const init = async () => { |
| 42 | + requestId += 1; |
| 43 | + page = 1; |
| 44 | + items = []; |
| 45 | + selectedIdx = 0; |
| 46 | + allItemsLoaded = false; |
| 47 | + itemsLoading = false; |
| 48 | + await tick(); |
| 49 | + await getItemsPage(requestId); |
| 50 | + }; |
25 | 51 |
|
26 | 52 | const loadMoreItems = async () => { |
27 | 53 | if (allItemsLoaded) return; |
28 | 54 | page += 1; |
29 | | - await getItemsPage(); |
| 55 | + await getItemsPage(requestId); |
30 | 56 | }; |
31 | 57 |
|
32 | | - const getItemsPage = async () => { |
| 58 | + const getItemsPage = async (activeRequestId = requestId) => { |
33 | 59 | itemsLoading = true; |
34 | | - let res = await getChatList(localStorage.token, page, true, true).catch(() => { |
35 | | - return []; |
36 | | - }); |
| 60 | + const res = query.trim() |
| 61 | + ? await getChatListBySearchText(localStorage.token, query.trim(), page).catch(() => { |
| 62 | + return []; |
| 63 | + }) |
| 64 | + : await getChatList(localStorage.token, page, true, true).catch(() => { |
| 65 | + return []; |
| 66 | + }); |
| 67 | + if (activeRequestId !== requestId) return res; |
37 | 68 |
|
38 | 69 | if ((res ?? []).length === 0) { |
39 | 70 | allItemsLoaded = true; |
|
50 | 81 | ...item, |
51 | 82 | type: 'chat', |
52 | 83 | name: item.title, |
53 | | - description: dayjs(item.updated_at * 1000).fromNow() |
| 84 | + description: item.snippet || dayjs(item.updated_at * 1000).fromNow() |
54 | 85 | }; |
55 | 86 | }) |
56 | 87 | ]; |
|
64 | 95 | await tick(); |
65 | 96 |
|
66 | 97 | loaded = true; |
| 98 | + initialized = true; |
| 99 | + }); |
| 100 | +
|
| 101 | + onDestroy(() => { |
| 102 | + clearTimeout(searchDebounceTimer); |
67 | 103 | }); |
68 | 104 | </script> |
69 | 105 |
|
70 | 106 | {#if loaded} |
71 | | - {#if items.length === 0} |
72 | | - <div class="text-center text-xs text-gray-500 py-3">{$i18n.t('No chats found')}</div> |
73 | | - {:else} |
74 | | - <div class="flex flex-col gap-0.5"> |
75 | | - {#each items as item, idx} |
76 | | - <button |
77 | | - class=" h-[1.6875rem] px-2 rounded-xl w-full text-left flex justify-between items-center text-[13px] font-normal {idx === |
78 | | - selectedIdx |
79 | | - ? ' bg-gray-50/40 dark:bg-gray-800/40 dark:text-gray-100 selected-command-option-button' |
80 | | - : ''}" |
81 | | - type="button" |
82 | | - on:click={() => { |
83 | | - onSelect(item); |
84 | | - }} |
85 | | - on:mousemove={() => { |
86 | | - selectedIdx = idx; |
87 | | - }} |
88 | | - on:mouseleave={() => { |
89 | | - if (idx === 0) { |
90 | | - selectedIdx = -1; |
91 | | - } |
92 | | - }} |
93 | | - data-selected={idx === selectedIdx} |
94 | | - > |
95 | | - <div class="text-black dark:text-gray-100 flex items-center gap-1.5"> |
96 | | - <Tooltip content={item.description || decodeString(item?.name)} placement="top-start"> |
97 | | - <div class="line-clamp-1 flex-1"> |
98 | | - {decodeString(item?.name)} |
| 107 | + <div class="flex min-h-0 flex-1 flex-col gap-0.5 overflow-hidden"> |
| 108 | + <SearchInput bind:value={query} placeholder={$i18n.t('Search Chats')} /> |
| 109 | + |
| 110 | + <div class="min-h-0 flex-1 overflow-y-auto overflow-x-hidden scrollbar-thin"> |
| 111 | + {#if items.length === 0 && itemsLoading} |
| 112 | + <div class="py-4.5"> |
| 113 | + <Spinner /> |
| 114 | + </div> |
| 115 | + {:else if items.length === 0} |
| 116 | + <div class="text-center text-xs text-gray-500 py-3">{$i18n.t('No chats found')}</div> |
| 117 | + {:else} |
| 118 | + <div class="flex flex-col gap-0.5"> |
| 119 | + {#each items as item, idx} |
| 120 | + <button |
| 121 | + class=" h-[1.6875rem] px-2 rounded-xl w-full text-left flex justify-between items-center text-[13px] font-normal {idx === |
| 122 | + selectedIdx |
| 123 | + ? ' bg-gray-50/40 dark:bg-gray-800/40 dark:text-gray-100 selected-command-option-button' |
| 124 | + : ''}" |
| 125 | + type="button" |
| 126 | + on:click={() => { |
| 127 | + onSelect(item); |
| 128 | + }} |
| 129 | + on:mousemove={() => { |
| 130 | + selectedIdx = idx; |
| 131 | + }} |
| 132 | + on:mouseleave={() => { |
| 133 | + if (idx === 0) { |
| 134 | + selectedIdx = -1; |
| 135 | + } |
| 136 | + }} |
| 137 | + data-selected={idx === selectedIdx} |
| 138 | + > |
| 139 | + <div class="text-black dark:text-gray-100 flex items-center gap-1.5"> |
| 140 | + <Tooltip |
| 141 | + content={item.description || decodeString(item?.name)} |
| 142 | + placement="top-start" |
| 143 | + > |
| 144 | + <div class="line-clamp-1 flex-1"> |
| 145 | + {decodeString(item?.name)} |
| 146 | + </div> |
| 147 | + </Tooltip> |
99 | 148 | </div> |
100 | | - </Tooltip> |
101 | | - </div> |
102 | | - </button> |
103 | | - {/each} |
104 | | - |
105 | | - {#if !allItemsLoaded} |
106 | | - <Loader |
107 | | - on:visible={(e) => { |
108 | | - if (!itemsLoading) { |
109 | | - loadMoreItems(); |
110 | | - } |
111 | | - }} |
112 | | - > |
113 | | - <div class="w-full flex justify-center py-4 text-xs animate-pulse items-center gap-2"> |
114 | | - <Spinner className=" size-4" /> |
115 | | - <div class=" ">{$i18n.t('Loading...')}</div> |
116 | | - </div> |
117 | | - </Loader> |
| 149 | + </button> |
| 150 | + {/each} |
| 151 | + |
| 152 | + {#if !allItemsLoaded} |
| 153 | + <Loader |
| 154 | + on:visible={(e) => { |
| 155 | + if (!itemsLoading) { |
| 156 | + loadMoreItems(); |
| 157 | + } |
| 158 | + }} |
| 159 | + > |
| 160 | + <div class="w-full flex justify-center py-4 text-xs animate-pulse items-center gap-2"> |
| 161 | + <Spinner className=" size-4" /> |
| 162 | + <div class=" ">{$i18n.t('Loading...')}</div> |
| 163 | + </div> |
| 164 | + </Loader> |
| 165 | + {/if} |
| 166 | + </div> |
118 | 167 | {/if} |
119 | 168 | </div> |
120 | | - {/if} |
| 169 | + </div> |
121 | 170 | {:else} |
122 | 171 | <div class="py-4.5"> |
123 | 172 | <Spinner /> |
|
0 commit comments