feat: list recently entered workspaces first in the workspace pickers (#12133)

* feat: list recently entered workspaces first in the workspace pickers

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* refactor: share the long workspace list threshold and test recent roots

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* docs: say the admins workspace leads its group, below the recent group

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Guilhem
2026-11-04 09:08:15 +00:00
committed by GitHub
co-authored by Claude Opus 5.5
parent 8dd97a5a2d
commit d8773f4316
5 changed files with 129 additions and 10 deletions
@@ -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<string>() : 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}
<div class="px-3 py-2 text-xs text-secondary">No workspaces match your search</div>
{/if}
{#if firstOlderWorkspaceId}
<div class="px-3 pt-1 pb-0.5 text-2xs text-secondary">Recent</div>
{/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}
<div class="my-1 border-t border-border-light" role="separator"></div>
{/if}
<!-- A search filters out sibling forks, so the divider between the user's own forks
and the others would mark the wrong boundary. -->
{#if separatorBefore && !searching}
@@ -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<string>() : 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 @@
<div class="space-y-4 max-h-[50vh] overflow-auto" bind:this={scrollContainer}>
<!-- Workspace Tree -->
<div class="space-y-2">
{#each rootWorkspaces as workspace (workspace.id)}
{#each rootWorkspaces as workspace, i (workspace.id)}
{#if recentCount > 0 && recentCount < rootWorkspaces.length && (i === 0 || i === recentCount)}
<div class="text-xs text-secondary {i === 0 ? '' : 'pt-2'}">
{i === 0 ? 'Recent' : 'Other workspaces'}
</div>
{/if}
<WorkspaceCard
{workspace}
dev={workspace._dev}
@@ -0,0 +1,22 @@
import { describe, expect, it } from 'vitest'
import { recentWorkspaceRoots } from './recentWorkspaces'
import type { UserWorkspace } from '../stores'
function ws(id: string, parent?: string): UserWorkspace {
return { id, name: id, username: 'u', parent_workspace_id: parent, disabled: false }
}
const roots = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'].map((id) => 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)
})
})
@@ -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<string[]>(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<string> {
const roots = new Set<string>()
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
}
@@ -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.