Files
hapi/web/src/hooks/useSessionListView.ts
T
db7ace3dc3 feat(web): sort and filter the session list (#2263)
* feat(web): sort and filter the session list

The session list had one fixed order, the user's last message, and
active and archived sessions were interleaved inside each project. With
in-progress pinning off by default, a connected session could sit below
a stack of archived ones, and archived sessions could not be listed on
their own.

A sort and filter button in the list toolbar now sets:
- Sort by: last message (the existing default), date created, or recent
  activity including agent output.
- Active first (on by default): connected sessions lead each project,
  and projects with one lead the list. Manual pins still come first.
- Show: all sessions, active only, or archived only. The open session
  stays visible, and archived-only keeps projects expanded instead of
  auto-collapsing them.

Choices persist and stay in sync across consumers and tabs. The Display
setting "Show active sessions only" is now the Active status, and an
existing true value carries over.

via [HAPI](https://hapi.run)

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

* fix(web): keep the default project order while searching

Relevance ranking keeps the incoming project order for equal scores, so
passing the list view into grouping let Active first and the chosen sort
reorder equally relevant search results. Group with the default
last-message order while a text query is active, as before.

via [HAPI](https://hapi.run)

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

* fix(web): drop the Recent activity session sort

The Hub advances updatedAt only for human messages; agent output moves
an in-memory progress clock that clients never see. Recent activity
therefore ordered like Last message. Ordering by user input is the
agreed contract across clients (see #1512), so remove the option rather
than add a new activity clock. A stored "activity" value falls back to
the default.

via [HAPI](https://hapi.run)

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

---------

Co-authored-by: HAPI <noreply@hapi.run>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-04 15:07:13 +08:00

129 lines
4.9 KiB
TypeScript

import { useCallback, useSyncExternalStore } from 'react'
/**
* Session list view preferences: sort key, status filter and whether
* connected sessions sort ahead of archived ones. One store shared by every
* consumer (sidebar menu, settings page) and synced across tabs.
*/
export type SessionListSort = 'last-message' | 'created'
export type SessionListStatus = 'all' | 'active' | 'archived'
export type SessionListView = {
sort: SessionListSort
status: SessionListStatus
activeFirst: boolean
}
export const DEFAULT_SESSION_LIST_VIEW: SessionListView = {
sort: 'last-message',
status: 'all',
activeFirst: true
}
const SORT_KEY = 'hapi-session-list-sort'
const STATUS_KEY = 'hapi-session-list-status'
const ACTIVE_FIRST_KEY = 'hapi-session-list-active-first'
// Pre-status "Show active sessions only" setting; read once as the default status.
const LEGACY_ACTIVE_ONLY_KEY = 'hapi-show-active-sessions-only'
const SORTS: readonly SessionListSort[] = ['last-message', 'created']
const STATUSES: readonly SessionListStatus[] = ['all', 'active', 'archived']
function safeGetItem(key: string): string | null {
try {
return typeof localStorage === 'undefined' ? null : localStorage.getItem(key)
} catch {
return null
}
}
function safeSetItem(key: string, value: string | null): boolean {
try {
if (typeof localStorage === 'undefined') return false
if (value === null) localStorage.removeItem(key)
else localStorage.setItem(key, value)
return true
} catch {
return false
}
}
export function readSessionListView(): SessionListView {
const sort = safeGetItem(SORT_KEY)
const status = safeGetItem(STATUS_KEY)
const activeFirst = safeGetItem(ACTIVE_FIRST_KEY)
return {
sort: SORTS.includes(sort as SessionListSort) ? sort as SessionListSort : DEFAULT_SESSION_LIST_VIEW.sort,
status: STATUSES.includes(status as SessionListStatus)
? status as SessionListStatus
: safeGetItem(LEGACY_ACTIVE_ONLY_KEY) === 'true' ? 'active' : DEFAULT_SESSION_LIST_VIEW.status,
activeFirst: activeFirst === null ? DEFAULT_SESSION_LIST_VIEW.activeFirst : activeFirst === 'true'
}
}
// Snapshots are cached per raw storage value so useSyncExternalStore sees a
// stable object, while any storage change (another tab, a cleared store) is
// still picked up on the next read.
let current: { raw: string; view: SessionListView } | null = null
// Choices made while storage is unavailable still apply for this page.
let unsaved: SessionListView | null = null
const listeners = new Set<() => void>()
function getSnapshot(): SessionListView {
if (unsaved) return unsaved
const raw = [SORT_KEY, STATUS_KEY, ACTIVE_FIRST_KEY, LEGACY_ACTIVE_ONLY_KEY].map(safeGetItem).join('\u0000')
if (current?.raw !== raw) current = { raw, view: readSessionListView() }
return current.view
}
function emit(): void {
for (const listener of listeners) listener()
}
function subscribe(listener: () => void): () => void {
listeners.add(listener)
const onStorage = (event: StorageEvent) => {
if (event.key !== null && event.key !== SORT_KEY && event.key !== STATUS_KEY
&& event.key !== ACTIVE_FIRST_KEY && event.key !== LEGACY_ACTIVE_ONLY_KEY) return
emit()
}
if (typeof window !== 'undefined') window.addEventListener('storage', onStorage)
return () => {
listeners.delete(listener)
if (typeof window !== 'undefined') window.removeEventListener('storage', onStorage)
}
}
export function updateSessionListView(patch: Partial<SessionListView>): void {
const next = { ...getSnapshot(), ...patch }
// Defaults are stored as absent keys, like the other display preferences.
let saved = true
if (patch.sort !== undefined) {
saved = safeSetItem(SORT_KEY, next.sort === DEFAULT_SESSION_LIST_VIEW.sort ? null : next.sort) && saved
}
if (patch.status !== undefined) {
saved = safeSetItem(STATUS_KEY, next.status === DEFAULT_SESSION_LIST_VIEW.status ? null : next.status) && saved
saved = safeSetItem(LEGACY_ACTIVE_ONLY_KEY, null) && saved
}
if (patch.activeFirst !== undefined) {
saved = safeSetItem(ACTIVE_FIRST_KEY, next.activeFirst === DEFAULT_SESSION_LIST_VIEW.activeFirst ? null : String(next.activeFirst)) && saved
}
unsaved = saved ? null : next
emit()
}
export function isDefaultSessionListView(view: SessionListView): boolean {
return view.sort === DEFAULT_SESSION_LIST_VIEW.sort
&& view.status === DEFAULT_SESSION_LIST_VIEW.status
&& view.activeFirst === DEFAULT_SESSION_LIST_VIEW.activeFirst
}
export function useSessionListView(): {
view: SessionListView
setView: (patch: Partial<SessionListView>) => void
} {
const view = useSyncExternalStore(subscribe, getSnapshot, getSnapshot)
const setView = useCallback((patch: Partial<SessionListView>) => updateSessionListView(patch), [])
return { view, setView }
}