Files
computer/cptr/frontend/src/lib/components/Sidebar.svelte
T
2026-06-06 11:24:55 +01:00

386 lines
10 KiB
Svelte

<script lang="ts">
import { goto } from '$app/navigation';
import { page } from '$app/stores';
import {
workspaceList,
currentWorkspace,
removeWorkspace,
reorderWorkspaces,
sidebarOpen,
sidebarWidth,
appVersion,
showChangelog
} from '$lib/stores';
import Sortable from 'sortablejs';
import Icon from './Icon.svelte';
import DirectoryPicker from './DirectoryPicker.svelte';
import DropdownMenu from './DropdownMenu.svelte';
import SettingsModal from './SettingsModal.svelte';
import AdminPanel from './AdminPanel.svelte';
import { tooltip } from '$lib/tooltip';
import { session, clearSession } from '$lib/session';
import { getWelcome } from '$lib/apis/state';
import { t } from '$lib/i18n';
import { onMount, onDestroy } from 'svelte';
let showPicker = $state(false);
let showMenu = $state(false);
let showSettings = $state(false);
let settingsTab = $state<'general' | 'account' | 'about'>('general');
let showAdmin = $state(false);
let wsMenuPath = $state<string | null>(null);
let wsMenuAnchor = $state<HTMLElement | null>(null);
let menuButtonEl: HTMLButtonElement | undefined = $state();
let wsListEl: HTMLDivElement | undefined = $state();
let sortable: Sortable | null = null;
const MIN_WIDTH = 160;
const MAX_WIDTH = 400;
let isResizing = $state(false);
function startResize(e: PointerEvent) {
// Only allow resize on md+ screens
if (typeof window !== 'undefined' && window.innerWidth < 768) return;
e.preventDefault();
isResizing = true;
const startX = e.clientX;
const startWidth = $sidebarWidth;
function onMove(ev: PointerEvent) {
const delta = ev.clientX - startX;
const newWidth = Math.round(Math.min(MAX_WIDTH, Math.max(MIN_WIDTH, startWidth + delta)));
sidebarWidth.set(newWidth);
}
function onUp() {
isResizing = false;
window.removeEventListener('pointermove', onMove);
window.removeEventListener('pointerup', onUp);
document.body.style.cursor = '';
document.body.style.userSelect = '';
}
window.addEventListener('pointermove', onMove);
window.addEventListener('pointerup', onUp);
document.body.style.cursor = 'col-resize';
document.body.style.userSelect = 'none';
}
// Current workspace path from URL
const currentPath = $derived($page.url.searchParams.get('workspace'));
function isTouchDevice(): boolean {
return (
typeof window !== 'undefined' && ('ontouchstart' in window || navigator.maxTouchPoints > 0)
);
}
function handleWorkspaceClick(e: MouseEvent, path: string) {
// Let Cmd/Ctrl+click open in new tab naturally (it's an <a>)
if (e.metaKey || e.ctrlKey) return;
e.preventDefault();
goto(`/?workspace=${encodeURIComponent(path)}`);
if (typeof window !== 'undefined' && window.innerWidth < 768) {
sidebarOpen.set(false);
}
}
function openWsMenu(e: MouseEvent, path: string) {
e.stopPropagation();
e.preventDefault();
wsMenuAnchor = e.currentTarget as HTMLElement;
wsMenuPath = path;
}
function closeWsMenu() {
wsMenuPath = null;
wsMenuAnchor = null;
}
function closeSidebar() {
sidebarOpen.set(false);
}
function openSettings() {
showMenu = false;
showSettings = true;
}
function logout() {
clearSession();
}
async function handleRemoveWorkspace(path: string) {
closeWsMenu();
await removeWorkspace(path);
// If we removed the workspace we're currently viewing, go home
if (currentPath === path) {
goto('/');
}
}
onMount(() => {
// Enable drag-reorder on non-touch devices
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);
}
}
});
}
});
onDestroy(() => {
sortable?.destroy();
});
</script>
{#if $sidebarOpen}
<button
class="fixed inset-0 bg-black/50 z-40 cursor-default md:hidden"
onclick={closeSidebar}
aria-label={$t('sidebar.closeSidebar')}
></button>
<aside class="sidebar" style="--sw: {$sidebarWidth}px;">
<!-- Resize handle (md+ only) -->
<div
class="resize-handle"
class:active={isResizing}
role="separator"
aria-orientation="vertical"
onpointerdown={startResize}
ondblclick={() => sidebarWidth.set(220)}
></div>
<!-- Logo header with collapse button -->
<div
class="flex items-center justify-between h-9 pl-3.5 pr-1.5 shrink-0 border-b border-gray-200 dark:border-white/6"
>
<a
href="/"
class="flex items-center gap-1.5 text-xs font-semibold tracking-tight text-gray-900 dark:text-white"
onclick={(e) => {
e.preventDefault();
goto('/');
if (typeof window !== 'undefined' && window.innerWidth < 768) sidebarOpen.set(false);
}}><img src="/favicon.png" alt="cptr logo" class="w-4 h-4" />cptr</a
>
<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={() => sidebarOpen.set(false)}
aria-label={$t('sidebar.collapse')}
use:tooltip={$t('sidebar.collapse')}
>
<Icon name="sidebar-expand" size={14} />
</button>
</div>
<!-- Workspace section header -->
<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={() => (showPicker = true)}
aria-label={$t('sidebar.addWorkspace')}
use:tooltip={$t('sidebar.addWorkspace')}
>
<Icon name="plus" size={14} />
</button>
</div>
<!-- Workspace list -->
<div bind:this={wsListEl} class="flex-1 overflow-y-auto px-1.5">
{#each $workspaceList as ws (ws.path)}
<a
href="/?workspace={encodeURIComponent(ws.path)}"
class="group flex items-center gap-1.5 w-full h-7 px-2 rounded-lg text-xs font-medium transition-colors duration-100 no-underline
{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'}"
onclick={(e) => handleWorkspaceClick(e, ws.path)}
>
<Icon name="folder" size={14} />
<span class="flex-1 text-left overflow-hidden text-ellipsis whitespace-nowrap"
>{ws.name}</span
>
<span
class="flex items-center justify-center w-5 h-5 rounded shrink-0 text-gray-400 opacity-0 group-hover:opacity-100 hover:bg-gray-200 dark:hover:bg-white/10 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>
</a>
{/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>
<!-- Settings and profile footer pinned to the bottom -->
<div class="relative px-1 pb-0.5 shrink-0">
<button
bind:this={menuButtonEl}
class="flex items-center gap-2 w-full h-8 px-2 rounded-lg text-xs font-medium text-gray-500 hover:text-gray-700 dark:hover:text-gray-300 transition-colors duration-100"
onclick={() => (showMenu = !showMenu)}
>
<img
src={$session?.profile_image_url || '/user.png'}
alt="Avatar"
class="w-5 h-5 rounded-full object-cover shrink-0"
/>
<span class="truncate"
>{$session?.display_name || $session?.username || $t('sidebar.settings')}</span
>
{#if $appVersion}
<button
onclick={(e) => {
e.stopPropagation();
showChangelog.set(true);
}}
class="ml-auto text-[10px] text-gray-400 dark:text-gray-600 hover:text-gray-500 dark:hover:text-gray-400 font-mono hover:underline cursor-pointer"
>
v{$appVersion}
</button>
{/if}
</button>
</div>
</aside>
{/if}
{#if showMenu && menuButtonEl}
<DropdownMenu
anchor={menuButtonEl}
matchWidth
items={[
...($session
? [
{
label: $session.display_name || $session.username,
image: $session.profile_image_url || '/user.png',
onclick: () => {
settingsTab = 'account';
showSettings = true;
}
}
]
: []),
...($session ? [{ divider: true, label: '', onclick: () => {} }] : []),
{ label: $t('sidebar.settings'), icon: 'settings', onclick: openSettings },
...($session?.role === 'admin'
? [
{
label: $t('sidebar.admin'),
icon: 'shield',
onclick: () => {
showMenu = false;
showAdmin = true;
}
}
]
: []),
{ divider: true, label: '', onclick: () => {} },
{ label: $t('sidebar.logOut'), icon: 'log-out', onclick: logout }
]}
onclose={() => (showMenu = false)}
/>
{/if}
{#if showPicker}
<DirectoryPicker onclose={() => (showPicker = false)} />
{/if}
{#if wsMenuPath && wsMenuAnchor}
<DropdownMenu
anchor={wsMenuAnchor}
items={[
{
label: $t('sidebar.remove'),
icon: 'xmark',
onclick: () => handleRemoveWorkspace(wsMenuPath!)
}
]}
onclose={closeWsMenu}
/>
{/if}
{#if showSettings}
<SettingsModal
initialTab={settingsTab}
onclose={() => {
showSettings = false;
settingsTab = 'general';
}}
/>
{/if}
{#if showAdmin}
<AdminPanel onclose={() => (showAdmin = false)} />
{/if}
<style>
@reference "../../app.css";
.sidebar {
position: fixed;
left: 0;
top: 0;
bottom: 0;
width: 220px;
z-index: 50;
display: flex;
flex-direction: column;
background: white;
border-right: 1px solid var(--color-gray-200);
}
:global(.dark) .sidebar {
background: #000;
border-right-color: rgba(255, 255, 255, 0.06);
}
@media (min-width: 768px) {
.sidebar {
position: relative;
z-index: auto;
width: var(--sw, 220px);
}
}
.resize-handle {
display: none;
}
@media (min-width: 768px) {
.resize-handle {
display: block;
position: absolute;
right: -3px;
top: 0;
bottom: 0;
width: 6px;
cursor: col-resize;
z-index: 10;
transition: background 0.15s;
}
.resize-handle:hover,
.resize-handle.active {
background: rgba(150, 150, 150, 0.12);
}
}
</style>