mirror of
https://github.com/open-webui/computer.git
synced 2026-12-02 17:57:59 -05:00
refac
This commit is contained in:
1 parent
82e2c59340
commit
bc5efdcafc
3 files changed
+169
-29
No files matched your search
@@ -1,13 +1,16 @@
|
||||
<script lang="ts">
|
||||
import type { ChatInfo } from '$lib/apis/chat';
|
||||
import DropdownMenu from '../DropdownMenu.svelte';
|
||||
import Pagination from '../common/Pagination.svelte';
|
||||
|
||||
interface Props {
|
||||
chats: ChatInfo[];
|
||||
onopen: (id: string) => void;
|
||||
ondelete: (id: string) => void;
|
||||
hasMore?: boolean;
|
||||
onloadmore?: () => void;
|
||||
page?: number;
|
||||
totalPages?: number;
|
||||
perPage?: number;
|
||||
onpagechange?: (page: number) => void;
|
||||
sortBy?: 'title' | 'updated_at';
|
||||
sortDir?: 'asc' | 'desc';
|
||||
onsort?: (field: 'title' | 'updated_at') => void;
|
||||
@@ -16,8 +19,10 @@
|
||||
chats,
|
||||
onopen,
|
||||
ondelete,
|
||||
hasMore = false,
|
||||
onloadmore,
|
||||
page = 1,
|
||||
totalPages = 1,
|
||||
perPage = 10,
|
||||
onpagechange,
|
||||
sortBy = 'updated_at',
|
||||
sortDir = 'desc',
|
||||
onsort,
|
||||
@@ -40,8 +45,11 @@
|
||||
function formatTime(ts: number): string {
|
||||
const d = new Date(ts);
|
||||
const now = new Date();
|
||||
const diffH = Math.floor((now.getTime() - d.getTime()) / 3600000);
|
||||
if (diffH < 1) return 'Just now';
|
||||
const diffMs = now.getTime() - d.getTime();
|
||||
const diffM = Math.floor(diffMs / 60000);
|
||||
if (diffM < 1) return 'Just now';
|
||||
if (diffM < 60) return `${diffM}m ago`;
|
||||
const diffH = Math.floor(diffM / 60);
|
||||
if (diffH < 24) return `${diffH}h ago`;
|
||||
const diffD = Math.floor(diffH / 24);
|
||||
if (diffD < 7) return `${diffD}d ago`;
|
||||
@@ -123,13 +131,8 @@
|
||||
</button>
|
||||
</div>
|
||||
{/each}
|
||||
{#if hasMore && onloadmore}
|
||||
<button
|
||||
class="w-full h-7 mt-1 text-[11px] text-gray-400 dark:text-gray-600 hover:text-gray-600 dark:hover:text-gray-400 transition-colors duration-75"
|
||||
onclick={onloadmore}
|
||||
>
|
||||
Show more
|
||||
</button>
|
||||
{#if onpagechange}
|
||||
<Pagination {page} {totalPages} {onpagechange} />
|
||||
{/if}
|
||||
</div>
|
||||
{/if}
|
||||
|
||||
@@ -212,30 +212,28 @@
|
||||
}
|
||||
}
|
||||
|
||||
const CHATS_PAGE_SIZE = 50;
|
||||
let hasMoreChats = $state(false);
|
||||
const CHATS_PAGE_SIZE = 10;
|
||||
let chatPage = $state(1);
|
||||
let totalChats = $state(0);
|
||||
let chatSortBy = $state<'title' | 'updated_at'>('updated_at');
|
||||
let chatSortDir = $state<'asc' | 'desc'>('desc');
|
||||
const totalPages = $derived(Math.max(1, Math.ceil(totalChats / CHATS_PAGE_SIZE)));
|
||||
|
||||
async function loadPreviousChats() {
|
||||
async function loadPreviousChats(page = 1) {
|
||||
try {
|
||||
const data = await getChats(workspace, CHATS_PAGE_SIZE, 0, chatSortBy, chatSortDir);
|
||||
const offset = (page - 1) * CHATS_PAGE_SIZE;
|
||||
const data = await getChats(workspace, CHATS_PAGE_SIZE, offset, chatSortBy, chatSortDir);
|
||||
previousChats = data.chats || [];
|
||||
hasMoreChats = data.has_more;
|
||||
totalChats = data.total;
|
||||
chatPage = page;
|
||||
} catch {
|
||||
previousChats = [];
|
||||
hasMoreChats = false;
|
||||
totalChats = 0;
|
||||
}
|
||||
}
|
||||
|
||||
async function loadMoreChats() {
|
||||
try {
|
||||
const data = await getChats(workspace, CHATS_PAGE_SIZE, previousChats.length, chatSortBy, chatSortDir);
|
||||
previousChats = [...previousChats, ...(data.chats || [])];
|
||||
hasMoreChats = data.has_more;
|
||||
} catch {
|
||||
hasMoreChats = false;
|
||||
}
|
||||
function handlePageChange(page: number) {
|
||||
loadPreviousChats(page);
|
||||
}
|
||||
|
||||
function handleSort(field: 'title' | 'updated_at') {
|
||||
@@ -245,7 +243,7 @@
|
||||
chatSortBy = field;
|
||||
chatSortDir = field === 'title' ? 'asc' : 'desc';
|
||||
}
|
||||
loadPreviousChats();
|
||||
loadPreviousChats(1);
|
||||
}
|
||||
|
||||
async function openChat(id: string) {
|
||||
@@ -761,7 +759,7 @@
|
||||
onqueueedit={handleQueueEdit}
|
||||
onqueuedelete={handleQueueDelete}
|
||||
/>
|
||||
<ChatHistory chats={previousChats} onopen={openChat} ondelete={deleteChat} hasMore={hasMoreChats} onloadmore={loadMoreChats} sortBy={chatSortBy} sortDir={chatSortDir} onsort={handleSort} />
|
||||
<ChatHistory chats={previousChats} onopen={openChat} ondelete={deleteChat} page={chatPage} {totalPages} perPage={CHATS_PAGE_SIZE} onpagechange={handlePageChange} sortBy={chatSortBy} sortDir={chatSortDir} onsort={handleSort} />
|
||||
</div>
|
||||
</div>
|
||||
{:else}
|
||||
|
||||
Reference in new issue
Block a user