feat(web): expand folders in place in the file browser

Opening a folder replaced the root listing, so every step inward lost
the way back and the tree above it. A folder now opens under its
parent, nested folders open under theirs, and the root stays visible.

Each folder is read again when it is expanded, so files a turn added or
removed show up the next time the folder opens. The size column hides
below 260px of list width, where the indent leaves no room for both a
name and a size.
This commit is contained in:
vhaudiquet committed 2026-12-01 01:09:12 +01:00
1 parent b5babb682e
commit 7d98b04d63
2 files changed
+139 -101

No files matched your search

+96 -64
View File
@@ -1,6 +1,6 @@
<script setup lang="ts">
import { computed, onBeforeUnmount, onMounted, ref } from 'vue'
import { ChevronLeft, ChevronRight, File, Folder, Link as LinkIcon } from 'lucide-vue-next'
import { computed, onBeforeUnmount, onMounted, reactive, ref } from 'vue'
import { ChevronRight, File, Folder, Link as LinkIcon } from 'lucide-vue-next'
import type { FileContentResponse, FileEntry, FileListResponse, GitSummaryResponse } from '~/types'
import { apiErrorMessage } from '~/utils/errors'
import { formatSize } from '~/utils/format'
@@ -18,10 +18,16 @@ const FILES_MAX_WIDTH_PERCENT = 0.55
const FILES_MAX_HEIGHT_PERCENT = 0.6
const FILES_STACKED_WIDTH = 430
const path = ref('.')
const entries = ref<FileEntry[]>([])
const loading = ref(false)
const error = ref('')
type DirState = { loading: boolean; error: string; entries: FileEntry[] }
type TreeRow =
| { kind: 'entry'; key: string; entry: FileEntry; depth: number }
| { kind: 'note'; key: string; text: string; depth: number; error: boolean }
const dirs = reactive(new Map<string, DirState>())
const expanded = reactive(new Set<string>())
const dirTokens = new Map<string, number>()
let dirSeq = 0
const filesEl = ref<HTMLElement | null>(null)
const browserEl = ref<HTMLElement | null>(null)
@@ -51,7 +57,6 @@ const git = ref<GitSummaryResponse | null>(null)
const gitLoading = ref(false)
const gitError = ref('')
let dirRequest = 0
let fileRequest = 0
const narrow = computed(() => filesWidth.value > 0 && filesWidth.value <= FILES_STACKED_WIDTH)
@@ -177,8 +182,38 @@ function onFilesResizeKey(event: KeyboardEvent) {
saveSize(isNarrow ? FILES_HEIGHT_KEY : FILES_WIDTH_KEY, clamped)
}
const segments = computed(() => (path.value === '.' ? [] : path.value.split('/').filter(Boolean)))
const hasParent = computed(() => segments.value.length > 0)
// The visible rows: the root's entries, then the entries of every
// expanded folder under it, each one a level deeper.
const rows = computed<TreeRow[]>(() => {
const out: TreeRow[] = []
const walk = (dir: string, depth: number) => {
const state = dirs.get(dir)
if (!state) return
if (state.loading) {
out.push({ kind: 'note', key: `${dir}:loading`, text: 'Loading files.', depth, error: false })
return
}
if (state.error) {
out.push({ kind: 'note', key: `${dir}:error`, text: state.error, depth, error: true })
return
}
if (state.entries.length === 0) {
const text = dir === '' ? 'The workspace is empty.' : 'This folder is empty.'
out.push({ kind: 'note', key: `${dir}:empty`, text, depth, error: false })
return
}
for (const entry of state.entries) {
out.push({ kind: 'entry', key: entry.path, entry, depth })
if (entry.kind === 'dir' && expanded.has(entry.path)) walk(entry.path, depth + 1)
}
}
walk('', 0)
return out
})
function indent(depth: number) {
return { '--tree-depth': String(depth) }
}
const lineNumbers = computed(() => {
const content = file.value?.content ?? ''
@@ -186,25 +221,24 @@ const lineNumbers = computed(() => {
return Array.from({ length: count }, (_, index) => index + 1).join('\n')
})
function crumbPath(index: number) {
return segments.value.slice(0, index + 1).join('/')
}
// Expanding a folder reads it again, so a file a turn added or removed
// shows up the next time the folder is opened.
async function loadDir(target: string) {
const request = ++dirRequest
loading.value = true
error.value = ''
const request = ++dirSeq
dirTokens.set(target, request)
const previous = dirs.get(target)
dirs.set(target, { loading: true, error: '', entries: previous?.entries ?? [] })
try {
const response = await api.get<FileListResponse>(`/rooms/${props.roomId}/files`, { path: target })
if (request !== dirRequest) return
path.value = response.path
entries.value = response.entries
if (dirTokens.get(target) !== request) return
dirs.set(target, { loading: false, error: '', entries: response.entries })
} catch (err) {
if (request !== dirRequest) return
entries.value = []
error.value = apiErrorMessage(err)
} finally {
if (request === dirRequest) loading.value = false
if (dirTokens.get(target) !== request) return
dirs.set(target, {
loading: false,
error: apiErrorMessage(err),
entries: previous?.entries ?? [],
})
}
}
@@ -226,20 +260,18 @@ async function openFile(entry: FileEntry) {
}
}
function openEntry(entry: FileEntry) {
if (entry.kind === 'dir') {
selected.value = null
file.value = null
fileError.value = ''
void loadDir(entry.path)
} else {
void openFile(entry)
function toggleDir(entry: FileEntry) {
if (expanded.has(entry.path)) {
expanded.delete(entry.path)
return
}
expanded.add(entry.path)
void loadDir(entry.path)
}
function goBack() {
const parent = segments.value.slice(0, -1).join('/')
void loadDir(parent)
function activateEntry(entry: FileEntry) {
if (entry.kind === 'dir') toggleDir(entry)
else void openFile(entry)
}
async function loadGit() {
@@ -282,41 +314,41 @@ onBeforeUnmount(() => {
<div class="files-pane">
<div ref="filesEl" class="files" :style="filesStyle">
<div ref="browserEl" class="files-browser">
<div class="crumbs">
<button v-if="hasParent" type="button" class="crumb-back" @click="goBack">
<ChevronLeft :size="12" aria-hidden="true" />
Back
</button>
<div class="crumbs-row">
<button type="button" class="crumb" @click="loadDir('')">Workspace</button>
<template v-for="(segment, index) in segments" :key="crumbPath(index)">
<ChevronRight :size="12" class="faint" aria-hidden="true" />
<button type="button" class="crumb" @click="loadDir(crumbPath(index))">
{{ segment }}
</button>
</template>
</div>
</div>
<div class="tree-head">Workspace</div>
<div class="file-list">
<p v-if="loading" class="meta pane-pad">Loading files.</p>
<p v-else-if="error" class="form-error pane-pad">{{ error }}</p>
<p v-else-if="entries.length === 0" class="empty pane-pad">This folder is empty.</p>
<template v-else>
<div class="file-list" aria-label="Workspace files">
<template v-for="row in rows" :key="row.key">
<p
v-if="row.kind === 'note'"
class="tree-note"
:class="{ error: row.error }"
:style="indent(row.depth)"
>
{{ row.text }}
</p>
<button
v-for="entry in entries"
:key="entry.path"
v-else
type="button"
class="file-row"
:class="{ active: selected?.path === entry.path }"
:title="entry.path"
@click="openEntry(entry)"
:class="{ active: selected?.path === row.entry.path }"
:style="indent(row.depth)"
:title="row.entry.path"
:aria-expanded="row.entry.kind === 'dir' ? expanded.has(row.entry.path) : undefined"
@click="activateEntry(row.entry)"
>
<Folder v-if="entry.kind === 'dir'" :size="14" aria-hidden="true" />
<LinkIcon v-else-if="entry.kind === 'link'" :size="14" aria-hidden="true" />
<ChevronRight
v-if="row.entry.kind === 'dir'"
:size="14"
class="tree-caret"
:class="{ open: expanded.has(row.entry.path) }"
aria-hidden="true"
/>
<span v-else class="tree-caret-gap" aria-hidden="true"></span>
<Folder v-if="row.entry.kind === 'dir'" :size="14" aria-hidden="true" />
<LinkIcon v-else-if="row.entry.kind === 'link'" :size="14" aria-hidden="true" />
<File v-else :size="14" aria-hidden="true" />
<span class="file-row-name">{{ entry.name }}</span>
<span v-if="entry.kind === 'file'" class="file-row-size">{{ formatSize(entry.size) }}</span>
<span class="file-row-name">{{ row.entry.name }}</span>
<span v-if="row.entry.kind === 'file'" class="file-row-size">{{ formatSize(row.entry.size) }}</span>
</button>
</template>
</div>