diff --git a/src/lib/apis/chats/index.ts b/src/lib/apis/chats/index.ts index 05a707cd4..a72b51939 100644 --- a/src/lib/apis/chats/index.ts +++ b/src/lib/apis/chats/index.ts @@ -1,4 +1,5 @@ import { WEBUI_API_BASE_URL } from '$lib/constants'; +import { getTimeRange } from '$lib/utils'; export const createNewChat = async (token: string, chat: object) => { let error = null; @@ -59,7 +60,10 @@ export const getChatList = async (token: string = '') => { throw error; } - return res; + return res.map((chat) => ({ + ...chat, + time_range: getTimeRange(chat.updated_at) + })); }; export const getChatListByUserId = async (token: string = '', userId: string) => { @@ -90,7 +94,10 @@ export const getChatListByUserId = async (token: string = '', userId: string) => throw error; } - return res; + return res.map((chat) => ({ + ...chat, + time_range: getTimeRange(chat.updated_at) + })); }; export const getArchivedChatList = async (token: string = '') => { @@ -248,7 +255,10 @@ export const getChatListByTagName = async (token: string = '', tagName: string) throw error; } - return res; + return res.map((chat) => ({ + ...chat, + time_range: getTimeRange(chat.updated_at) + })); }; export const getChatById = async (token: string, id: string) => { diff --git a/src/lib/components/layout/Sidebar.svelte b/src/lib/components/layout/Sidebar.svelte index 8c15cf516..dcafd527f 100644 --- a/src/lib/components/layout/Sidebar.svelte +++ b/src/lib/components/layout/Sidebar.svelte @@ -44,6 +44,28 @@ let showDropdown = false; let isEditing = false; + let filteredChatList = []; + + $: filteredChatList = $chats.filter((chat) => { + if (search === '') { + return true; + } else { + let title = chat.title.toLowerCase(); + const query = search.toLowerCase(); + + let contentMatches = false; + // Access the messages within chat.chat.messages + if (chat.chat && chat.chat.messages && Array.isArray(chat.chat.messages)) { + contentMatches = chat.chat.messages.some((message) => { + // Check if message.content exists and includes the search query + return message.content && message.content.toLowerCase().includes(query); + }); + } + + return title.includes(query) || contentMatches; + } + }); + onMount(async () => { showSidebar.set(window.innerWidth > BREAKPOINT); await chats.set(await getChatList(localStorage.token)); @@ -418,25 +440,17 @@ {/if}
- {#each $chats.filter((chat) => { - if (search === '') { - return true; - } else { - let title = chat.title.toLowerCase(); - const query = search.toLowerCase(); + {#each filteredChatList as chat, idx} + {#if idx === 0 || (idx > 1 && chat.time_range !== filteredChatList[idx - 1].time_range)} +
+ {chat.time_range} +
+ {/if} - let contentMatches = false; - // Access the messages within chat.chat.messages - if (chat.chat && chat.chat.messages && Array.isArray(chat.chat.messages)) { - contentMatches = chat.chat.messages.some((message) => { - // Check if message.content exists and includes the search query - return message.content && message.content.toLowerCase().includes(query); - }); - } - - return title.includes(query) || contentMatches; - } - }) as chat, i}
{#if chatTitleEditId === chat.id}
{ return results.reverse().join(' '); }; + +export const getTimeRange = (timestamp) => { + const now = new Date(); + const date = new Date(timestamp * 1000); // Convert Unix timestamp to milliseconds + + // Calculate the difference in milliseconds + const diffTime = now.getTime() - date.getTime(); + const diffDays = diffTime / (1000 * 3600 * 24); + + if (diffDays < 1) { + return 'Today'; + } else if (diffDays < 2) { + return 'Yesterday'; + } else if (diffDays <= 7) { + return 'Previous 7 days'; + } else if (diffDays <= 30) { + return 'Previous 30 days'; + } else if (date.getFullYear() === now.getFullYear()) { + return date.toLocaleString('default', { month: 'long' }); + } else { + return date.getFullYear().toString(); + } +};