diff --git a/frontend/src/lib/components/sidebar/WorkspacePickerBody.svelte b/frontend/src/lib/components/sidebar/WorkspacePickerBody.svelte index b2357beece..85ea04d84a 100644 --- a/frontend/src/lib/components/sidebar/WorkspacePickerBody.svelte +++ b/frontend/src/lib/components/sidebar/WorkspacePickerBody.svelte @@ -50,6 +50,11 @@ import { canCreateFork } from '$lib/utils/editInFork' import { workspaceRootId } from '$lib/components/sessions/sessionScope.svelte' import DevWorkspaceBadge from '$lib/components/DevWorkspaceBadge.svelte' + import { + LONG_WORKSPACE_LIST, + recentWorkspaceRoots, + visitedWorkspaces + } from '$lib/utils/recentWorkspaces' interface Props { // The melt item builder of the menu (or submenu) hosting this list. Melt @@ -182,8 +187,7 @@ return ancestors.at(-1) ?? $userWorkspaces?.find((w) => w.id === id) }) - const SEARCH_THRESHOLD = 8 - const showSearch = $derived(($userWorkspaces?.length ?? 0) > SEARCH_THRESHOLD) + const showSearch = $derived(($userWorkspaces?.length ?? 0) > LONG_WORKSPACE_LIST) let searchFilter = $state('') let searchMatches: (UserWorkspace & { marked?: string })[] | undefined = $state() const searching = $derived(showSearch && searchFilter.trim() !== '') @@ -232,18 +236,30 @@ return !!rootId && isExpanded(rootId) }) // The family the user is standing in leads the list, its forks travelling with it: the - // workspace they are in is the one they look for first, and the one the tick is on. + // workspace they are in is the one they look for first, and the one the tick is on. The + // other recently entered families follow, still alphabetical, then everything else. const activeRoot = lineageRoot?.id - if (!activeRoot) return visible + if (!activeRoot && recentRoots.size === 0) return visible const families: { root: string | undefined; rows: typeof visible }[] = [] for (const h of visible) { if (h.depth === 0 || families.length === 0) families.push({ root: h.workspace.id, rows: [h] }) else families[families.length - 1].rows.push(h) } - const at = families.findIndex((f) => f.root === activeRoot) - if (at <= 0) return visible - const [active] = families.splice(at, 1) - return [active, ...families].flatMap((f) => f.rows) + const rank = (root: string | undefined) => + root === activeRoot ? 0 : root && recentRoots.has(root) ? 1 : 2 + return families.sort((a, b) => rank(a.root) - rank(b.root)).flatMap((f) => f.rows) + }) + + // A search ranks by match, so the recent group only orders the unfiltered list. + const recentRoots = $derived( + searching ? new Set() : recentWorkspaceRoots($visitedWorkspaces, $userWorkspaces ?? []) + ) + // First row below the recent group, which a divider separates from it. + const firstOlderWorkspaceId = $derived.by(() => { + if (recentRoots.size === 0) return undefined + const isRecent = (id: string) => id === lineageRoot?.id || recentRoots.has(id) + if (!isRecent(familyWorkspaces[0]?.workspace.id ?? '')) return undefined + return familyWorkspaces.find((h) => h.depth === 0 && !isRecent(h.workspace.id))?.workspace.id }) // With the search field shown, focus stays in it: the arrows move this highlight rather than @@ -444,9 +460,15 @@ {#if searching && familyWorkspaces.length === 0}
No workspaces match your search
{/if} + {#if firstOlderWorkspaceId} +
Recent
+ {/if} {#each familyWorkspaces as { workspace, depth, isForked, parentName, separatorBefore } (workspace.id)} {@const isActive = $workspaceStore === workspace.id} {@const forkAccent = isForked ? forkAccentStyle(workspace.color) : undefined} + {#if firstOlderWorkspaceId === workspace.id} + + {/if} {#if separatorBefore && !searching} diff --git a/frontend/src/lib/components/workspace/WorkspaceTreeView.svelte b/frontend/src/lib/components/workspace/WorkspaceTreeView.svelte index c74a5222e5..96ef3b6eb8 100644 --- a/frontend/src/lib/components/workspace/WorkspaceTreeView.svelte +++ b/frontend/src/lib/components/workspace/WorkspaceTreeView.svelte @@ -4,6 +4,7 @@ import WorkspaceCard from './WorkspaceCard.svelte' import SearchItems from '$lib/components/SearchItems.svelte' import type { UserWorkspace } from '$lib/stores' + import { recentWorkspaceRoots, visitedWorkspaces } from '$lib/utils/recentWorkspaces' interface ExtendedWorkspace extends UserWorkspace { _children?: ExtendedWorkspace[] @@ -79,6 +80,11 @@ return { ...manualExpansionStates, ...autoExpanded } }) + // A search ranks by match, so the recent group only orders the unfiltered list. + let recentRoots = $derived( + searchFilter ? new Set() : recentWorkspaceRoots($visitedWorkspaces, workspaces ?? []) + ) + // Build nested hierarchy correctly - always use full workspace list for hierarchy let rootWorkspaces = $derived.by(() => { if (!workspaces) return [] @@ -167,7 +173,8 @@ return visibleRoots .map((workspace) => buildWorkspaceWithChildren(workspace)) .sort((a, b) => { - // Admin workspace always goes first + if (recentRoots.has(a.id) !== recentRoots.has(b.id)) return recentRoots.has(a.id) ? -1 : 1 + // Admin workspace leads its group: the recent group stays above it if (a.id === 'admins') return -1 if (b.id === 'admins') return 1 // Then sort alphabetically by name @@ -175,6 +182,8 @@ }) }) + let recentCount = $derived(rootWorkspaces.filter((w) => recentRoots.has(w.id)).length) + function handleToggleExpand(workspaceId: string) { const currentState = expansionStates[workspaceId] ?? false manualExpansionStates = { ...manualExpansionStates, [workspaceId]: !currentState } @@ -401,7 +410,12 @@
- {#each rootWorkspaces as workspace (workspace.id)} + {#each rootWorkspaces as workspace, i (workspace.id)} + {#if recentCount > 0 && recentCount < rootWorkspaces.length && (i === 0 || i === recentCount)} +
+ {i === 0 ? 'Recent' : 'Other workspaces'} +
+ {/if} ws(id)) +const workspaces = [...roots, ws('wm-fork-x', 'c'), ws('wm-fork-y', 'wm-fork-x')] + +describe('recentWorkspaceRoots', () => { + it('counts a fork visit for its root and keeps the first families visited', () => { + const visited = ['wm-fork-y', 'gone', 'c', 'e', 'a', 'b'] + expect([...recentWorkspaceRoots(visited, workspaces)]).toEqual(['c', 'e', 'a']) + }) + + it('leaves a short list without a recent group', () => { + expect(recentWorkspaceRoots(['a'], roots).size).toBe(0) + }) +}) diff --git a/frontend/src/lib/utils/recentWorkspaces.ts b/frontend/src/lib/utils/recentWorkspaces.ts new file mode 100644 index 0000000000..1ebfaf1b0f --- /dev/null +++ b/frontend/src/lib/utils/recentWorkspaces.ts @@ -0,0 +1,59 @@ +import { BROWSER } from 'esm-env' +import { writable } from 'svelte/store' +import type { UserWorkspace } from '$lib/stores' +import { findWorkspaceAncestors } from './workspaceHierarchy' + +const STORAGE_KEY = 'recentWorkspaces' +const MAX_REMEMBERED = 20 + +/** Above this many workspaces a list gets a search field and a recent group. A shorter list stays + * alphabetical: a recent group there only moves rows the user can already see. */ +export const LONG_WORKSPACE_LIST = 8 +export const RECENT_WORKSPACES_COUNT = 3 + +function readVisited(): string[] { + try { + const parsed = JSON.parse(localStorage.getItem(STORAGE_KEY) ?? '[]') + return Array.isArray(parsed) ? parsed.filter((id) => typeof id === 'string') : [] + } catch { + return [] + } +} + +/** Workspace ids, most recently entered first. Per browser, like the persisted `workspace` key. */ +export const visitedWorkspaces = writable(BROWSER ? readVisited() : []) + +export function recordWorkspaceVisit(workspaceId: string) { + visitedWorkspaces.update((visited) => { + if (visited[0] === workspaceId) return visited + const next = [workspaceId, ...visited.filter((id) => id !== workspaceId)].slice( + 0, + MAX_REMEMBERED + ) + try { + localStorage.setItem(STORAGE_KEY, JSON.stringify(next)) + } catch {} + return next + }) +} + +/** + * The ids of the root workspaces of the `count` most recently entered families. Lists group forks + * under their root, so a visit to a fork counts for its family. Empty for a list too short to + * need a recent group. + */ +export function recentWorkspaceRoots( + visited: string[], + workspaces: UserWorkspace[], + count = RECENT_WORKSPACES_COUNT +): Set { + const roots = new Set() + if (workspaces.length <= LONG_WORKSPACE_LIST) return roots + for (const id of visited) { + if (roots.size >= count) break + const workspace = workspaces.find((w) => w.id === id) + if (!workspace) continue + roots.add((findWorkspaceAncestors(id, workspaces).at(-1) ?? workspace).id) + } + return roots +} diff --git a/frontend/src/routes/(root)/(logged)/+layout.svelte b/frontend/src/routes/(root)/(logged)/+layout.svelte index fadc29d97f..b3cec74c90 100644 --- a/frontend/src/routes/(root)/(logged)/+layout.svelte +++ b/frontend/src/routes/(root)/(logged)/+layout.svelte @@ -145,6 +145,7 @@ import { useDbManagerUriState } from '$lib/components/dbManagerDrawerModel.svelte' import Modal2 from '$lib/components/common/modal/Modal2.svelte' import { recordForkParent, rememberForkParent } from '$lib/forkParentMemory' + import { recordWorkspaceVisit } from '$lib/utils/recentWorkspaces' import { homeSnapshotShown } from '$lib/homeSnapshot' import { persistedValue } from '$lib/persistedResource.svelte' interface Props { @@ -413,6 +414,7 @@ } catch (e) { console.error('Could not persist workspace to local storage', e) } + recordWorkspaceVisit(String(workspace)) } // Rendering as the user whoami last confirmed for this session saves its round trip on // a reload; whoami below confirms or replaces them. See lastSession.ts for the binding.