39 lines
1.5 KiB
TypeScript
39 lines
1.5 KiB
TypeScript
import type { OfficialSheetAdminSummary } from '@faunosteo/supabase'
|
|
import { matchesSheetName } from '@/catalogue/useCatalogue'
|
|
import type { Locale } from '@/i18n/types'
|
|
|
|
// shortcut: search and sort run in the browser on the whole list; they move to
|
|
// the server with the list's paging (RR-13, TODO.md).
|
|
|
|
/** The display orders of the admin list; `modified` is the default (D7). */
|
|
export const SHEET_SORTS = ['modified', 'name'] as const
|
|
export type SheetSort = (typeof SHEET_SORTS)[number]
|
|
|
|
type Sheet = OfficialSheetAdminSummary
|
|
|
|
/** Search on the sheet name only, as the catalogue does (D7). */
|
|
export function searchSheets(sheets: readonly Sheet[], query: string): Sheet[] {
|
|
return sheets.filter((sheet) => matchesSheetName(sheet.name, query))
|
|
}
|
|
|
|
/**
|
|
* Display order only: a new array, the input untouched. `modified` puts the
|
|
* newest `last_modified_at` first; equal dates, and `name`, follow the name in
|
|
* the interface language, then the id, so the order never depends on the
|
|
* server's.
|
|
*/
|
|
export function sortSheets(
|
|
sheets: readonly Sheet[],
|
|
sort: SheetSort,
|
|
locale: Locale,
|
|
): Sheet[] {
|
|
const collator = new Intl.Collator(locale, { sensitivity: 'base', numeric: true })
|
|
const time = (sheet: Sheet) => Date.parse(sheet.last_modified_at)
|
|
const byName = (a: Sheet, b: Sheet) =>
|
|
collator.compare(a.name[locale], b.name[locale]) ||
|
|
a.sheet_id.localeCompare(b.sheet_id)
|
|
return [...sheets].sort(
|
|
(a, b) => (sort === 'modified' ? time(b) - time(a) : 0) || byName(a, b),
|
|
)
|
|
}
|