diff --git a/web/src/directoryOrder.ts b/web/src/directoryOrder.ts index f7cc72b..acefc39 100644 --- a/web/src/directoryOrder.ts +++ b/web/src/directoryOrder.ts @@ -1,7 +1,31 @@ -import type { Asset, LedgerState } from './types.ts'; +import type { Asset, BackupRecord, LedgerState } from './types.ts'; import { latestBackupForAsset } from './selectors.ts'; -export type DirectorySort = 'manual' | 'name-asc' | 'name-desc' | 'newest' | 'oldest'; +import { buildHierarchy } from './hierarchy.ts'; +import type { Hierarchy } from './hierarchy.ts'; +export type DirectorySort = 'manual' | 'name-asc' | 'name-desc' | 'newest' | 'oldest' | 'notes-asc' | 'notes-desc'; +export type DirectorySortColumn = 'name' | 'backup' | 'notes'; +const columnSorts = { + name: ['name-asc', 'name-desc'], + backup: ['oldest', 'newest'], + notes: ['notes-asc', 'notes-desc'], +} as const; +export function directoryColumnSort(mode: DirectorySort, column: DirectorySortColumn) { + const [ascending, descending] = columnSorts[column]; + const direction = mode === ascending ? 'ascending' : mode === descending ? 'descending' : 'none'; + return {direction, next: mode === ascending ? descending : ascending} as const; +} +export function latestDirectoryBackup(state: LedgerState, assetId: string, tree: Hierarchy = buildHierarchy(state.assets)): BackupRecord | undefined { + if (tree.isLeaf(assetId)) return latestBackupForAsset(state, assetId); + let latest: BackupRecord | undefined; + for (const child of tree.leafDescendants(assetId)) { + if (child.backup_target !== true) continue; + const record = latestBackupForAsset(state, child.id); + if (record && (!latest || Date.parse(record.backed_up_at) > Date.parse(latest.backed_up_at))) latest = record; + } + return latest; +} const byName = (a: Asset, b: Asset) => a.name.localeCompare(b.name, 'zh-CN') || a.id.localeCompare(b.id); +const compareNotes = new Intl.Collator('zh-CN', {numeric: true}).compare; function portableTextOrder(left:string,right:string):number { const a=Array.from(left),b=Array.from(right); for(let index=0;index(); - if (mode === 'newest' || mode === 'oldest') for (const asset of assets) { - const latest = latestBackupForAsset(state, asset.id); - times.set(asset.id, latest ? Date.parse(latest.backed_up_at) : -Infinity); + const notes = new Map(); + if (mode === 'newest' || mode === 'oldest' || mode === 'notes-asc' || mode === 'notes-desc') { + const tree = buildHierarchy(state.assets); + for (const asset of assets) { + if (mode === 'notes-asc' || mode === 'notes-desc') { + notes.set(asset.id, asset.backup_target === true && tree.isLeaf(asset.id) ? latestBackupForAsset(state, asset.id)?.notes?.trim() ?? '' : ''); + } else { + const latest = latestDirectoryBackup(state, asset.id, tree); + times.set(asset.id, latest ? Date.parse(latest.backed_up_at) : -Infinity); + } + } } return [...assets].sort((a, b) => { if (mode === 'manual') { @@ -25,6 +57,11 @@ export function sortDirectoryAssets(state: LedgerState, assets: Asset[], mode: D } if (mode === 'name-desc') return byName(b,a); if (mode === 'name-asc') return byName(a,b); + if (mode === 'notes-asc' || mode === 'notes-desc') { + const left = notes.get(a.id)!, right = notes.get(b.id)!; + if (!left || !right) return Number(!left) - Number(!right) || byName(a,b); + return (mode === 'notes-asc' ? compareNotes(left,right) : compareNotes(right,left)) || byName(a,b); + } const difference = mode === 'newest' ? times.get(b.id)! - times.get(a.id)! : times.get(a.id)! - times.get(b.id)!; return difference || byName(a,b); }); diff --git a/web/src/features/Overview.tsx b/web/src/features/Overview.tsx index a89f3f5..ab05e03 100644 --- a/web/src/features/Overview.tsx +++ b/web/src/features/Overview.tsx @@ -1,16 +1,26 @@ import { useEffect, useMemo, useRef, useState } from 'react'; -import { ArrowUp, ChevronRight, File, FilePlus2, Folder, FolderPlus, GripVertical, ListPlus, Search } from 'lucide-react'; +import type { ReactNode } from 'react'; +import { ArrowDown, ArrowUp, ArrowUpDown, ChevronRight, File, FilePlus2, Folder, FolderPlus, GripVertical, ListPlus, Search } from 'lucide-react'; import { buildHierarchy } from '../hierarchy'; -import { formatTime, latestBackupForAsset } from '../selectors'; +import { formatTime } from '../selectors'; import type { Asset, LedgerState } from '../types'; import { EmptyState, PageHeading } from '../components/ui'; import { AssetDetail } from './AssetDetail'; import { ActionMenu } from '../components/ActionMenu'; -import { advanceDirectoryDrag, finishDirectoryDrag, moveDirectoryAsset, sortDirectoryAssets, startDirectoryDrag } from '../directoryOrder'; -import type { DirectoryDrag, DirectorySort } from '../directoryOrder'; +import { advanceDirectoryDrag, directoryColumnSort, finishDirectoryDrag, latestDirectoryBackup, moveDirectoryAsset, sortDirectoryAssets, startDirectoryDrag } from '../directoryOrder'; +import type { DirectoryDrag, DirectorySort, DirectorySortColumn } from '../directoryOrder'; import { directoryNavigation, searchDirectoryAssets } from '../directoryTools'; import type { DirectorySearchScope } from '../directoryTools'; +function SortHeading({column, label, sort, disabled, onSort, children}: { + column: DirectorySortColumn; label: string; sort: DirectorySort; disabled: boolean; + onSort: (sort: DirectorySort) => void; children?: ReactNode; +}) { + const {direction, next} = directoryColumnSort(sort, column); + const Icon = direction === 'ascending' ? ArrowUp : direction === 'descending' ? ArrowDown : ArrowUpDown; + return
{children}
; +} + interface OverviewActions { state: LedgerState; folderId: string | null; onFolder: (id: string | null) => void; @@ -66,13 +76,11 @@ export function Overview(actions: OverviewActions) { const showBackupNotes = visibleAssets.some(asset => asset.backup_target === true && tree.isLeaf(asset.id)); const rows = visibleAssets.map(asset => { const leaf = tree.isLeaf(asset.id); - const latest = leaf ? latestBackupForAsset(state, asset.id) : tree.leafDescendants(asset.id) - .filter(entry => entry.backup_target === true) - .flatMap(entry => { const record = latestBackupForAsset(state, entry.id); return record ? [record] : []; }) - .sort((a, b) => Date.parse(b.backed_up_at) - Date.parse(a.backed_up_at))[0]; + const latest = latestDirectoryBackup(state, asset.id, tree); return {asset, leaf, latest}; }); const canReorder = sort === 'manual' && !keyword.trim() && !pending && !bulkSelecting; + const changeSort = (mode: DirectorySort) => { cancelDrag(); setSort(mode); }; const selectableIds=rows.filter(({asset,leaf})=>leaf?asset.backup_target===true:tree.leafDescendants(asset.id).some(child=>child.backup_target===true)).map(({asset})=>asset.id); const selectedVisible=selectableIds.filter(id=>selected.includes(id)); const allSelected=selectableIds.length>0&&selectedVisible.length===selectableIds.length; @@ -107,7 +115,7 @@ export function Overview(actions: OverviewActions) { }; return <>{actions.onBulk&&!showingRecords?bulkSelecting?<>{actions.onPaste?:null}::null}{!bulkSelecting?<>:null} : undefined} />
- {showingRecords ? null :
}
+ {showingRecords ? null :
} {orderError ?
{orderError}
: null} {showingRecords ? actions.onEditRecords(folder.id)} onAddRecord={() => actions.onAddRecord(folder.id)} /> :
{!rows.length ? keyword ? { setKeyword(''); setSelected([]); }}>清除搜索} /> :
: undefined} /> : @@ -116,7 +124,12 @@ export function Overview(actions: OverviewActions) { {showBackupNotes ? : null} - {showBackupNotes ? : null}{rows.map(({asset, leaf, latest}) => { + + {actions.onBulk&&bulkSelecting?{if(node)node.indeterminate=selectedVisible.length>0&&!allSelected;}} onChange={()=>setSelected(current=>allSelected?current.filter(id=>!selectableIds.includes(id)):[...new Set([...current,...selectableIds])])}/>:null} + + {showBackupNotes ? : null} + + {rows.map(({asset, leaf, latest}) => { const Icon = asset.kind === 'folder' ? Folder : File; const marked = leaf && asset.backup_target === true; const index=siblings.findIndex(entry=>entry.id===asset.id); diff --git a/web/src/styles.css b/web/src/styles.css index c3f3a75..8d056ce 100644 --- a/web/src/styles.css +++ b/web/src/styles.css @@ -88,6 +88,14 @@ td { padding: 16px; border-bottom: 1px solid var(--border); line-height: 1.5; ve tbody tr:last-child td { border-bottom: 0; } tbody tr:hover { background: #fcfdff; } .explorer-table { table-layout: fixed; } +.directory-sort-heading { display: flex; align-items: center; justify-content: center; gap: 8px; } +.explorer-table th:first-child .directory-sort-heading { justify-content: flex-start; } +.directory-sort-button { display: inline-flex; align-items: center; gap: 6px; padding: 4px 0; border: 0; background: transparent; color: inherit; font: inherit; cursor: pointer; white-space: nowrap; } +.directory-sort-button svg { color: var(--muted); flex-shrink: 0; } +.directory-sort-button:hover, .explorer-table th[aria-sort="ascending"] .directory-sort-button, .explorer-table th[aria-sort="descending"] .directory-sort-button { color: var(--accent); } +.explorer-table th[aria-sort="ascending"] .directory-sort-button svg, .explorer-table th[aria-sort="descending"] .directory-sort-button svg { color: currentColor; } +.directory-sort-button:focus-visible { outline: 2px solid var(--accent); outline-offset: 4px; border-radius: 3px; } +.directory-sort-button:disabled { opacity: .5; cursor: wait; } .explorer-table th:first-child { border-radius: 12px 0 0 0; } .explorer-table th:last-child { border-radius: 0 12px 0 0; } .explorer-table td { padding: 16px; height: 66px; } diff --git a/web/tests/directory-order.test.mjs b/web/tests/directory-order.test.mjs index 18d22d0..8553db6 100644 --- a/web/tests/directory-order.test.mjs +++ b/web/tests/directory-order.test.mjs @@ -11,6 +11,38 @@ test('manual legacy fallback uses portable Unicode order for privacy slot preser assert.deepEqual(order.sortDirectoryAssets({...state,assets:items},items.slice(0,3),'name-asc').map(x=>x.id),['a','c','b']); }); test('name modes and actual backup time modes include all versions and never-backed-up first for oldest',()=>{assert.equal(typeof order.sortDirectoryAssets,'function');for(const [mode, ids] of [['name-asc',['a','b','c']],['name-desc',['c','b','a']],['newest',['a','b','c']],['oldest',['c','b','a']]])assert.deepEqual(order.sortDirectoryAssets(state,state.assets,mode).map(x=>x.id),ids);}); + +test('column clicks start ascending and toggle only the active column',()=>{ + assert.equal(typeof order.directoryColumnSort,'function'); + for(const [column,ascending,descending] of [['name','name-asc','name-desc'],['backup','oldest','newest'],['notes','notes-asc','notes-desc']]){ + assert.deepEqual(order.directoryColumnSort('manual',column),{direction:'none',next:ascending}); + assert.deepEqual(order.directoryColumnSort(ascending,column),{direction:'ascending',next:descending}); + assert.deepEqual(order.directoryColumnSort(descending,column),{direction:'descending',next:ascending}); + } + assert.deepEqual(order.directoryColumnSort('notes-desc','name'),{direction:'none',next:'name-asc'}); +}); + +test('backup notes sort naturally using only the latest visible description and keep blanks last',()=>{ + const items=[asset('a','A','folder',{backup_target:true}),asset('b','B','folder',{backup_target:true}),asset('c','C','folder',{backup_target:true}),asset('d','D'),asset('p','Parent','folder',{backup_target:true}),asset('child','Child','file',{parent_id:'p',backup_target:true})]; + const versions=items.map(item=>({id:'v-'+item.id,asset_id:item.id})); + const records=items.map((item,index)=>({id:'r-'+item.id,version_id:'v-'+item.id,location_id:'disk',backed_up_at:'2026-10-24T00:00:00Z',notes:['2','10',' ','1','0','0'][index]})); + records.push({id:'older',version_id:'v-a',location_id:'disk',backed_up_at:'2026-10-23T00:00:00Z',notes:'999'}); + const noteState={assets:items,versions,records,locations:[]}; + const roots=items.filter(item=>!item.parent_id); + assert.deepEqual(order.sortDirectoryAssets(noteState,roots,'notes-asc').map(x=>x.id),['a','b','c','d','p']); + assert.deepEqual(order.sortDirectoryAssets(noteState,roots,'notes-desc').map(x=>x.id),['b','a','c','d','p']); + assert.deepEqual(roots.map(x=>x.id),['a','b','c','d','p']); +}); + +test('backup time sorting matches parent rows that display the latest marked descendant backup',()=>{ + const items=[asset('pa','A','folder'),asset('pb','B','folder'),asset('single','C'),asset('ca','CA','file',{parent_id:'pa',backup_target:true}),asset('cb','CB','file',{parent_id:'pb',backup_target:true}),asset('ignored','Ignored','file',{parent_id:'pa'})]; + const versions=items.map(item=>({id:'v-'+item.id,asset_id:item.id})); + const records=[['pa','2026-12-01'],['ca','2026-10-01'],['cb','2026-10-20'],['single','2026-10-10'],['ignored','2026-11-01']].map(([id,date])=>({id:'r-'+id,version_id:'v-'+id,location_id:'disk',backed_up_at:date+'T00:00:00Z'})); + const grouped={assets:items,versions,records,locations:[]}; + const roots=items.filter(item=>!item.parent_id); + assert.deepEqual(order.sortDirectoryAssets(grouped,roots,'newest').map(x=>x.id),['pb','single','pa']); + assert.deepEqual(order.sortDirectoryAssets(grouped,roots,'oldest').map(x=>x.id),['pa','single','pb']); +}); test('move produces complete sibling order without mutating input and rejects foreign ids',()=>{assert.equal(typeof order.moveDirectoryAsset,'function');const ids=['a','b','c'];assert.deepEqual(order.moveDirectoryAsset(ids,'c','a'),['c','a','b']);assert.deepEqual(ids,['a','b','c']);assert.equal(order.moveDirectoryAsset(ids,'foreign','a'),null);assert.equal(order.moveDirectoryAsset(ids,'a','a'),null);}); const {parseImportPreview}=await import('../src/importData.ts'); diff --git a/web/tests/frontend-targets.test.mjs b/web/tests/frontend-targets.test.mjs index ac918a9..4eb5b17 100644 --- a/web/tests/frontend-targets.test.mjs +++ b/web/tests/frontend-targets.test.mjs @@ -43,7 +43,7 @@ const render = (component, extra = {}) => renderToStaticMarkup(createElement(com test('ordinary directory row has marker and child controls but no new-registration control or deadline', () => { const html = render(Overview); assert.doesNotMatch(html, />状态<|data-label="状态"|>文件夹/g)??[]).length,3); + assert.equal((html.match(/]*)?>/g)??[]).length,3); assert.doesNotMatch(html, /个备份对象|个普通条目|备份对象最新记录/); assert.match(html, /设为备份对象/); assert.equal((html.match(/aria-haspopup="menu"/g) ?? []).length, 1); @@ -53,6 +53,15 @@ test('ordinary directory row has marker and child controls but no new-registrati assert.doesNotMatch(html, />登记<|待首次备份/); }); +test('directory headers expose independent accessible sort controls and mobile note options', () => { + const marked={...ordinary,backup_target:true}; + const html=render(Overview,{state:{...state,assets:[marked]}}); + for(const name of ['名称','最新备份','备份说明']) assert.match(html,new RegExp('aria-label="按'+name+'升序排序"')); + assert.equal((html.match(/aria-sort="none"/g)??[]).length,3); + assert.match(html,/
{actions.onBulk&&bulkSelecting?:'名称'}最新备份备份说明操作
操作