Files
computer/cptr/frontend/src/lib/components/SidebarWorkspaceList.svelte
T
2026-07-09 16:31:38 -05:00

427 lines
11 KiB
Svelte

<script lang="ts">
import { goto } from '$app/navigation';
import { page } from '$app/stores';
import { workspaceList, removeWorkspace, reorderWorkspaces, sidebarOpen } from '$lib/stores';
import { chatEnabled } from '$lib/stores/chat';
import { socketStore } from '$lib/stores/socket.svelte';
import { deleteChat as apiDeleteChat, getChats, type ChatInfo } from '$lib/apis/chat';
import { t } from '$lib/i18n';
import { tooltip } from '$lib/tooltip';
import Sortable from 'sortablejs';
import { onDestroy, onMount } from 'svelte';
import ChatItem from './common/ChatItem.svelte';
import DropdownMenu from './DropdownMenu.svelte';
import Icon from './Icon.svelte';
interface Props {
onaddworkspace: () => void;
}
let { onaddworkspace }: Props = $props();
let wsMenuPath = $state<string | null>(null);
let wsMenuAnchor = $state<HTMLElement | null>(null);
let chatMenu = $state<{ chatId: string; wsPath: string; anchor: HTMLElement } | null>(null);
let wsListEl: HTMLDivElement | undefined = $state();
let sortable: Sortable | null = null;
let unbindSocketListener: (() => void) | null = null;
let expandedWorkspaces = $state<Set<string>>(new Set());
let wsChatsCache = $state<Map<string, ChatInfo[]>>(new Map());
let wsChatsLoading = $state<Set<string>>(new Set());
let currentPath = $derived($page.url.searchParams.get('workspace'));
function toggleWorkspaceExpand(path: string) {
const next = new Set(expandedWorkspaces);
if (next.has(path)) {
next.delete(path);
} else {
next.add(path);
if (!wsChatsCache.has(path)) fetchWorkspaceChats(path);
}
expandedWorkspaces = next;
}
async function fetchWorkspaceChats(path: string) {
if (wsChatsLoading.has(path)) return;
wsChatsLoading = new Set([...wsChatsLoading, path]);
try {
const data = await getChats(path, 5, 0, 'updated_at', 'desc');
wsChatsCache = new Map([...wsChatsCache, [path, data.chats || []]]);
} catch {
wsChatsCache = new Map([...wsChatsCache, [path, []]]);
} finally {
const next = new Set(wsChatsLoading);
next.delete(path);
wsChatsLoading = next;
}
}
function closeMobileSidebar() {
if (typeof window !== 'undefined' && window.innerWidth < 768) sidebarOpen.set(false);
}
function openWorkspace(e: MouseEvent, path: string) {
if (e.metaKey || e.ctrlKey) return;
e.preventDefault();
goto(`/?workspace=${encodeURIComponent(path)}`);
closeMobileSidebar();
}
function openChat(chatId: string, wsPath: string) {
goto(`/?workspace=${encodeURIComponent(wsPath)}&chatId=${encodeURIComponent(chatId)}`);
closeMobileSidebar();
}
function showMoreChats(wsPath: string) {
goto(`/?workspace=${encodeURIComponent(wsPath)}&chatId`);
closeMobileSidebar();
}
function newChat(wsPath: string) {
goto(`/?workspace=${encodeURIComponent(wsPath)}&chatId`);
closeMobileSidebar();
}
function openWsMenu(e: MouseEvent, path: string) {
e.stopPropagation();
e.preventDefault();
closeChatMenu();
wsMenuAnchor = e.currentTarget as HTMLElement;
wsMenuPath = path;
}
function closeWsMenu() {
wsMenuPath = null;
wsMenuAnchor = null;
}
function openChatMenu(e: MouseEvent, chatId: string, wsPath: string) {
e.stopPropagation();
closeWsMenu();
chatMenu = { chatId, wsPath, anchor: e.currentTarget as HTMLElement };
}
function closeChatMenu() {
chatMenu = null;
}
async function handleRemoveWorkspace(path: string) {
closeWsMenu();
await removeWorkspace(path);
if (currentPath === path) goto('/');
}
async function handleDeleteChat() {
if (!chatMenu) return;
const { chatId, wsPath } = chatMenu;
closeChatMenu();
await apiDeleteChat(chatId);
const chats = wsChatsCache.get(wsPath) ?? [];
wsChatsCache = new Map([...wsChatsCache, [wsPath, chats.filter((chat) => chat.id !== chatId)]]);
if (currentPath === wsPath && $page.url.searchParams.get('chatId') === chatId) {
goto(`/?workspace=${encodeURIComponent(wsPath)}`);
}
}
const seenChatIds = new Set<string>();
function handleChatEvent(data: {
chat_id: string;
done?: boolean;
title?: string;
delta?: string;
workspace?: string;
}) {
const isNew = !seenChatIds.has(data.chat_id);
seenChatIds.add(data.chat_id);
if (!data.done && !data.title && !isNew) return;
wsChatsCache = new Map();
for (const path of expandedWorkspaces) fetchWorkspaceChats(path);
}
function isTouchDevice(): boolean {
return (
typeof window !== 'undefined' && ('ontouchstart' in window || navigator.maxTouchPoints > 0)
);
}
onMount(() => {
if (wsListEl && !isTouchDevice()) {
sortable = Sortable.create(wsListEl, {
animation: 150,
ghostClass: 'opacity-30',
dragClass: 'cursor-grabbing',
direction: 'vertical',
onEnd: (evt) => {
if (evt.oldIndex != null && evt.newIndex != null && evt.oldIndex !== evt.newIndex) {
reorderWorkspaces(evt.oldIndex, evt.newIndex);
}
}
});
}
unbindSocketListener = socketStore.on('events:chat', handleChatEvent);
});
onDestroy(() => {
sortable?.destroy();
unbindSocketListener?.();
unbindSocketListener = null;
});
</script>
<div class="flex items-center justify-between h-8 pl-3.5 pr-1.5 shrink-0">
<span class="text-xs text-gray-400 dark:text-gray-500">{$t('sidebar.workspaces')}</span>
<button
class="flex items-center justify-center w-7 h-7 rounded-lg text-gray-300 hover:text-gray-500 dark:text-gray-600 dark:hover:text-gray-400 transition-colors duration-100"
onclick={onaddworkspace}
aria-label={$t('sidebar.addWorkspace')}
use:tooltip={$t('sidebar.addWorkspace')}
>
<Icon name="plus" size={14} />
</button>
</div>
<div bind:this={wsListEl} class="flex-1 overflow-y-auto px-1.5">
{#each $workspaceList as ws (ws.path)}
{@const isExpanded = expandedWorkspaces.has(ws.path)}
{@const chats = wsChatsCache.get(ws.path)}
{@const isLoading = wsChatsLoading.has(ws.path)}
<div class="ws-item">
<div
class="group flex items-center gap-1 w-full h-7 px-2 rounded-lg text-xs font-medium transition-colors duration-100
{ws.path === currentPath
? 'bg-gray-200/50 text-gray-900 dark:bg-white/8 dark:text-white'
: 'text-gray-500 hover:text-gray-700 dark:hover:text-gray-300'}"
>
<a
href="/?workspace={encodeURIComponent(ws.path)}"
class="flex items-center gap-1.5 flex-1 min-w-0 no-underline text-inherit"
onclick={(e) => openWorkspace(e, ws.path)}
>
{#if $chatEnabled}
<span
class="ws-icon-toggle shrink-0"
role="button"
tabindex="-1"
onclick={(e) => {
e.stopPropagation();
e.preventDefault();
toggleWorkspaceExpand(ws.path);
}}
aria-label={isExpanded ? $t('sidebar.collapse') : $t('sidebar.addWorkspace')}
>
<span class="ws-icon-folder"><Icon name="folder" size={14} /></span>
<span
class="ws-icon-chevron"
style="transform: rotate({isExpanded ? '90deg' : '0deg'})"
>
<Icon name="chevron-right" size={11} />
</span>
</span>
{:else}
<Icon name="folder" size={14} />
{/if}
<span class="flex-1 text-left overflow-hidden text-ellipsis whitespace-nowrap"
>{ws.name}</span
>
</a>
<span
class="flex items-center justify-center w-4 h-4 shrink-0 text-gray-400 opacity-0 group-hover:opacity-100 hover:text-gray-600 dark:hover:text-gray-300 transition-all duration-75"
role="button"
tabindex="-1"
onclick={(e) => openWsMenu(e, ws.path)}
aria-label={$t('sidebar.workspaceOptions')}
>
<Icon name="three-dots" size={11} />
</span>
{#if $chatEnabled}
<span
class="flex items-center justify-center w-4 h-4 shrink-0 text-gray-400 opacity-0 group-hover:opacity-100 hover:text-gray-600 dark:hover:text-gray-300 transition-all duration-75"
role="button"
tabindex="-1"
onclick={() => newChat(ws.path)}
aria-label={$t('bar.newChat')}
use:tooltip={$t('bar.newChat')}
>
<Icon name="pencil" size={11} />
</span>
{/if}
</div>
{#if $chatEnabled && isExpanded}
<div class="ws-chats">
{#if isLoading && !chats}
<div class="ws-chat-loading">
<span class="ws-chat-loading-dot"></span>
<span class="ws-chat-loading-dot"></span>
<span class="ws-chat-loading-dot"></span>
</div>
{:else if chats && chats.length > 0}
{#each chats as chat (chat.id)}
<ChatItem
{chat}
onclick={() => openChat(chat.id, ws.path)}
onmenu={(e) => openChatMenu(e, chat.id, ws.path)}
/>
{/each}
<button class="ws-chat-show-more" onclick={() => showMoreChats(ws.path)}>
{$t('sidebar.showMore')}
</button>
{/if}
</div>
{/if}
</div>
{/each}
{#if $workspaceList.length === 0}
<div class="flex flex-col items-center justify-center py-12">
<p class="text-xs text-gray-400 dark:text-gray-600">{$t('sidebar.noWorkspaces')}</p>
</div>
{/if}
</div>
{#if wsMenuPath && wsMenuAnchor}
<DropdownMenu
anchor={wsMenuAnchor}
items={[
{
label: $t('sidebar.remove'),
icon: 'xmark',
onclick: () => handleRemoveWorkspace(wsMenuPath!)
}
]}
onclose={closeWsMenu}
/>
{/if}
{#if chatMenu}
<DropdownMenu
anchor={chatMenu.anchor}
align="end"
items={[
{
label: $t('chat.history.delete'),
icon: 'trash',
onclick: handleDeleteChat
}
]}
onclose={closeChatMenu}
/>
{/if}
<style>
@reference "../../app.css";
.ws-item {
margin-bottom: 0.125rem;
}
.ws-icon-toggle {
position: relative;
display: flex;
align-items: center;
justify-content: center;
width: 0.875rem;
height: 0.875rem;
cursor: pointer;
}
.ws-icon-folder {
display: flex;
transition: opacity 0.1s;
}
.ws-icon-chevron {
position: absolute;
inset: 0;
display: flex;
align-items: center;
justify-content: center;
opacity: 0;
transition:
opacity 0.1s,
transform 0.1s;
color: #9ca3af;
}
:global(.dark) .ws-icon-chevron {
color: #6b7280;
}
.ws-icon-toggle:hover .ws-icon-folder {
opacity: 0;
}
.ws-icon-toggle:hover .ws-icon-chevron {
opacity: 1;
}
.ws-chats {
margin-top: 0.125rem;
padding-bottom: 0.25rem;
}
.ws-chat-show-more {
display: block;
width: 100%;
padding: 0.125rem 0.5rem;
border: none;
background: none;
cursor: pointer;
font-size: 0.6875rem;
color: #b0b5be;
text-align: left;
transition: color 0.1s;
}
.ws-chat-show-more:hover {
color: #6b7280;
}
:global(.dark) .ws-chat-show-more {
color: #6b7280;
}
:global(.dark) .ws-chat-show-more:hover {
color: #9ca3af;
}
.ws-chat-loading {
display: flex;
gap: 0.25rem;
padding: 0.375rem 0.5rem;
}
.ws-chat-loading-dot {
width: 0.25rem;
height: 0.25rem;
border-radius: 50%;
background: #9ca3af;
animation: dotPulse 1s ease-in-out infinite;
}
:global(.dark) .ws-chat-loading-dot {
background: #6b7280;
}
.ws-chat-loading-dot:nth-child(2) {
animation-delay: 0.15s;
}
.ws-chat-loading-dot:nth-child(3) {
animation-delay: 0.3s;
}
@keyframes dotPulse {
0%,
100% {
opacity: 0.3;
}
50% {
opacity: 1;
}
}
</style>