Skip to content

Offline, privacy-first grammar checker. Fast, open-source, Rust-powered

4.9 KB
Raw Blame
<script lang="ts">
import {
	Button,
	CheckIcon,
	ChevronLeftIcon,
	ChevronRightIcon,
	EditIcon,
	Input,
	PlusIcon,
	TrashIcon,
} from 'components';
import { studioPrimaryButton, studioSecondaryButton } from '$lib/components/weirStudioTheme';

/** Whether to render the file explorer as a closed drawer or an open one. */
export let drawerOpen = true;
export let files: Map<string, string> = new Map();
/** The filename of the currently selected file. */
export let activeFile: string | null = null;

let renamingFile: string | null = null;
let renameValue = '';

export let onCreateFile: () => void;
export let onSelectFile: (id: string) => void;
export let onDeleteFile: (file: string) => void;
export let onRenameFile: (from: string, to: string) => void;
export let onToggleDrawer: () => void = () => {
	drawerOpen = !drawerOpen;
};

function startRename(file: string) {
	renamingFile = file;
	renameValue = file;
}

function commitRename(from: string) {
	if (renamingFile !== from) {
		return;
	}

	const trimmed = renameValue.trim();
	renamingFile = null;

	if (trimmed.length === 0 || trimmed === from) {
		return;
	}

	onRenameFile(from, trimmed);
}
</script>

<aside
	class={`relative z-10 flex h-full flex-col border-r border-black/10 bg-white/80 backdrop-blur dark:border-white/10 dark:bg-[#1c1a16]/80 transition-all duration-300 ${drawerOpen ? 'w-72' : 'w-14'}`}
>
	<div class="flex items-center justify-between px-3 py-3">
		{#if drawerOpen}
			<div class="text-sm font-semibold uppercase tracking-wider text-black/70 dark:text-white/70">Weirpack</div>
			<Button
				size="xs"
				color="white"
				className={`h-8 w-8 !p-0 ${studioSecondaryButton}`}
				on:click={onToggleDrawer}
				title="Collapse drawer"
				aria-label="Collapse drawer"
			>
				<ChevronLeftIcon className="h-4 w-4" />
			</Button>
		{:else}
			<Button
				size="xs"
				color="white"
				className={`mx-auto h-8 w-8 !p-0 ${studioSecondaryButton}`}
				on:click={onToggleDrawer}
				title="Expand drawer"
				aria-label="Expand drawer"
			>
				<ChevronRightIcon className="h-4 w-4" />
			</Button>
		{/if}
	</div>

	{#if drawerOpen}
		<div class="px-3 pb-2">
			<Button
				color="dark"
				size="sm"
				className={`w-full uppercase tracking-wide ${studioPrimaryButton}`}
				on:click={onCreateFile}
			>
				<PlusIcon className="h-4 w-4" />
				New file
			</Button>
		</div>

		<div class="flex-1 overflow-auto px-2 pb-4">
			{#each files as [file]}
				<div
					class={`group flex items-center justify-between rounded-lg px-2 py-2 text-sm ${file == activeFile ? 'bg-white shadow-sm dark:bg-white/10' : 'hover:bg-white/60 dark:hover:bg-white/5'}`}
				>
					<div class="flex flex-1 items-center gap-2 text-left">
						<span class={`h-2 w-2 rounded-full ${file == activeFile ? 'bg-primary': ''} bg-cream`}></span>
						{#if renamingFile === file}
							<div class="flex-1">
								<Input
									size="sm"
									className="w-full text-xs"
									autofocus
									bind:value={renameValue}
									on:keydown={(event: CustomEvent<KeyboardEvent>) => {
										const keyboardEvent = event.detail;
										if (keyboardEvent.key === 'Enter') {
											commitRename(file);
										}
										if (keyboardEvent.key === 'Escape') {
											renamingFile = null;
										}
									}}
									on:blur={() => commitRename(file)}
								/>
							</div>
						{:else}
							<button class="flex-1 truncate text-left dark:text-white/85" on:click={() => onSelectFile(file)}>
								{file}
							</button>
						{/if}
					</div>

					<div
						class={`flex items-center gap-1 text-black/50 transition dark:text-white/50 ${renamingFile === file ? 'opacity-100' : 'opacity-0 group-hover:opacity-100'}`}
					>
						{#if renamingFile === file}
							<Button
								size="xs"
								color="white"
								className={`h-6 w-6 !p-0 ${studioSecondaryButton}`}
								on:click={() => commitRename(file)}
								title="Confirm rename"
								aria-label="Confirm rename"
							>
								<CheckIcon className="h-3.5 w-3.5" />
							</Button>
						{:else}
							<Button
								size="xs"
								color="white"
								className={`h-6 w-6 !p-0 ${studioSecondaryButton}`}
								on:click={() => startRename(file)}
								title="Rename file"
								aria-label="Rename file"
							>
								<EditIcon className="h-3.5 w-3.5" />
							</Button>
							{#if files.size > 1}
								<Button
									size="xs"
									color="white"
									className={`h-6 w-6 !p-0 ${studioSecondaryButton}`}
									on:click={() => onDeleteFile(file)}
									title="Delete file"
									aria-label="Delete file"
								>
									<TrashIcon className="h-3.5 w-3.5" />
								</Button>
							{/if}
						{/if}
					</div>
				</div>
			{/each}
		</div>
	{:else}
		<div class="flex flex-1 flex-col items-center gap-4 pt-6 text-xs text-black/50 dark:text-white/50">
			<div class="rotate-135 text-xs font-semibold tracking-widest uppercase">Files</div>
		</div>
	{/if}
</aside>