This commit is contained in:
Timothy Jaeryang Baek
2026-06-06 10:20:56 +01:00
Unverified
parent 82e2c59340
commit bc5efdcafc
3 changed files with 169 additions and 29 deletions
@@ -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}
@@ -0,0 +1,139 @@
<script lang="ts">
interface Props {
page: number;
totalPages: number;
onpagechange: (page: number) => void;
}
let { page, totalPages, onpagechange }: Props = $props();
// Build page numbers with ellipsis for large page counts
const pages = $derived.by((): (number | 'ellipsis')[] => {
if (totalPages <= 7) {
return Array.from({ length: totalPages }, (_, i) => i + 1);
}
const items: (number | 'ellipsis')[] = [1];
if (page > 3) items.push('ellipsis');
const start = Math.max(2, page - 1);
const end = Math.min(totalPages - 1, page + 1);
for (let i = start; i <= end; i++) items.push(i);
if (page < totalPages - 2) items.push('ellipsis');
items.push(totalPages);
return items;
});
</script>
{#if totalPages > 1}
<div class="pagination">
<button
class="pagination-btn"
disabled={page <= 1}
onclick={() => onpagechange(page - 1)}
aria-label="Previous page"
>
<svg width="12" height="12" viewBox="0 0 12 12" fill="none" stroke="currentColor" stroke-width="1.75" stroke-linecap="round" stroke-linejoin="round">
<path d="M7.5 2.5L4 6L7.5 9.5" />
</svg>
</button>
{#each pages as item, i (item === 'ellipsis' ? `e${i}` : item)}
{#if item === 'ellipsis'}
<span class="pagination-ellipsis">…</span>
{:else}
<button
class="pagination-btn"
class:active={item === page}
onclick={() => onpagechange(item)}
aria-label="Page {item}"
aria-current={item === page ? 'page' : undefined}
>
{item}
</button>
{/if}
{/each}
<button
class="pagination-btn"
disabled={page >= totalPages}
onclick={() => onpagechange(page + 1)}
aria-label="Next page"
>
<svg width="12" height="12" viewBox="0 0 12 12" fill="none" stroke="currentColor" stroke-width="1.75" stroke-linecap="round" stroke-linejoin="round">
<path d="M4.5 2.5L8 6L4.5 9.5" />
</svg>
</button>
</div>
{/if}
<style>
.pagination {
display: flex;
align-items: center;
justify-content: center;
gap: 1px;
padding-top: 8px;
}
.pagination-btn {
display: inline-flex;
align-items: center;
justify-content: center;
min-width: 22px;
height: 22px;
padding: 0 3px;
border: none;
border-radius: 5px;
background: transparent;
color: #c4c4c4;
font-size: 10.5px;
font-weight: 500;
cursor: pointer;
transition: background 0.15s ease, color 0.15s ease;
user-select: none;
}
.pagination-btn:hover:not(:disabled):not(.active) {
background: rgba(0, 0, 0, 0.03);
color: #9ca3af;
}
.pagination-btn.active {
background: rgba(0, 0, 0, 0.04);
color: #8b8b8b;
}
.pagination-btn:disabled {
opacity: 0.25;
cursor: not-allowed;
}
.pagination-ellipsis {
display: inline-flex;
align-items: center;
justify-content: center;
min-width: 22px;
height: 22px;
font-size: 10.5px;
color: #d1d5db;
user-select: none;
letter-spacing: 1px;
}
/* Dark mode */
:global(.dark) .pagination-btn {
color: #4b5563;
}
:global(.dark) .pagination-btn:hover:not(:disabled):not(.active) {
background: rgba(255, 255, 255, 0.04);
color: #6b7280;
}
:global(.dark) .pagination-btn.active {
background: rgba(255, 255, 255, 0.06);
color: #9ca3af;
}
:global(.dark) .pagination-ellipsis {
color: #4b5563;
}
</style>