|
| 1 | +import {formatMediumDate} from '../../../shared/util/date-format'; |
| 2 | +import {formatFileSizeKb} from '../../../shared/util/file-size'; |
| 3 | +import {type TableColumnPreference} from '../../settings/user-management/user.service'; |
| 4 | +import {type BookQuerySortKey} from '../data/book-query-params'; |
| 5 | +import {type BookSummary} from '../data/book-response.models'; |
| 6 | +import { |
| 7 | + COLUMNS, |
| 8 | + COLUMNS_BY_KEY, |
| 9 | + COLUMN_GROUP_ORDER, |
| 10 | + bookReadStatusLabelKey, |
| 11 | + type BookBrowseColumnGroupId, |
| 12 | + type BookColumnKey, |
| 13 | + type BookColumnKind, |
| 14 | + type BookColumnValue, |
| 15 | +} from './book-browse-fields'; |
| 16 | + |
| 17 | +export const BOOK_EMPTY_VALUE = '—'; |
| 18 | + |
| 19 | +const DEFAULT_COLUMN_MIN_WIDTH = 84; |
| 20 | +const DEFAULT_COLUMN_MAX_WIDTH = 420; |
| 21 | +const NUMBER_FORMAT = new Intl.NumberFormat(); |
| 22 | + |
| 23 | +export interface BookColumnSizing { |
| 24 | + readonly size: number; |
| 25 | + readonly minSize: number; |
| 26 | + readonly maxSize: number; |
| 27 | +} |
| 28 | + |
| 29 | +export interface BookColumnOption { |
| 30 | + readonly field: BookColumnKey; |
| 31 | + readonly labelKey: string; |
| 32 | + readonly visible: boolean; |
| 33 | + readonly hideable: boolean; |
| 34 | +} |
| 35 | + |
| 36 | +export interface BookBrowseColumnVisibilityChange { |
| 37 | + readonly field: string; |
| 38 | + readonly visible: boolean; |
| 39 | +} |
| 40 | + |
| 41 | +export interface BookColumnSection { |
| 42 | + readonly id: BookBrowseColumnGroupId; |
| 43 | + readonly columns: readonly BookColumnOption[]; |
| 44 | +} |
| 45 | + |
| 46 | +export function normalizeBookColumnPreferences( |
| 47 | + saved: readonly TableColumnPreference[] | undefined, |
| 48 | +): TableColumnPreference[] { |
| 49 | + const savedByField = new Map(saved?.map(preference => [preference.field, preference])); |
| 50 | + return COLUMNS.map(({key, column}, order) => { |
| 51 | + const preference = savedByField.get(key); |
| 52 | + return { |
| 53 | + field: key, |
| 54 | + visible: column.hideable === false || (preference?.visible ?? column.defaultVisible), |
| 55 | + order: preference?.order ?? order, |
| 56 | + }; |
| 57 | + }); |
| 58 | +} |
| 59 | + |
| 60 | +export function bookColumnOptions( |
| 61 | + preferences: readonly TableColumnPreference[], |
| 62 | +): BookColumnOption[] { |
| 63 | + const visibleByField = new Map( |
| 64 | + preferences.map(preference => [preference.field, preference.visible]), |
| 65 | + ); |
| 66 | + return COLUMNS.map(({key, labelKey, column}) => ({ |
| 67 | + field: key, |
| 68 | + labelKey, |
| 69 | + visible: visibleByField.get(key) === true, |
| 70 | + hideable: column.hideable !== false, |
| 71 | + })); |
| 72 | +} |
| 73 | + |
| 74 | +export function bookVisibleColumnOptions( |
| 75 | + preferences: readonly TableColumnPreference[], |
| 76 | +): BookColumnOption[] { |
| 77 | + const orderByField = new Map(preferences.map(preference => [preference.field, preference.order])); |
| 78 | + const orderOf = (option: BookColumnOption) => |
| 79 | + option.field === 'title' ? -1 : orderByField.get(option.field) ?? Number.MAX_SAFE_INTEGER; |
| 80 | + return bookColumnOptions(preferences) |
| 81 | + .filter(option => option.visible) |
| 82 | + .sort((first, second) => orderOf(first) - orderOf(second)); |
| 83 | +} |
| 84 | + |
| 85 | +export function bookColumnSections( |
| 86 | + options: readonly BookColumnOption[], |
| 87 | +): BookColumnSection[] { |
| 88 | + const optionsByField = new Map(options.map(option => [option.field, option])); |
| 89 | + return COLUMN_GROUP_ORDER.map(id => ({ |
| 90 | + id, |
| 91 | + columns: COLUMNS.flatMap(({key, column}) => { |
| 92 | + const option = column.group === id ? optionsByField.get(key) : undefined; |
| 93 | + return option ? [option] : []; |
| 94 | + }), |
| 95 | + })); |
| 96 | +} |
| 97 | + |
| 98 | +export function bookColumnSortKey(field: string): BookQuerySortKey | undefined { |
| 99 | + return COLUMNS_BY_KEY.get(field)?.sort?.key; |
| 100 | +} |
| 101 | + |
| 102 | +export function bookColumnKind(field: string): BookColumnKind { |
| 103 | + return COLUMNS_BY_KEY.get(field)?.column.kind ?? 'text'; |
| 104 | +} |
| 105 | + |
| 106 | +export function bookColumnSizing(field: BookColumnKey): BookColumnSizing { |
| 107 | + const {defaultWidth, minWidth, maxWidth} = COLUMNS_BY_KEY.get(field)!.column; |
| 108 | + return { |
| 109 | + size: defaultWidth, |
| 110 | + minSize: minWidth ?? DEFAULT_COLUMN_MIN_WIDTH, |
| 111 | + maxSize: maxWidth ?? DEFAULT_COLUMN_MAX_WIDTH, |
| 112 | + }; |
| 113 | +} |
| 114 | + |
| 115 | +export function bookColumnValue( |
| 116 | + book: BookSummary, |
| 117 | + field: string, |
| 118 | +): BookColumnValue { |
| 119 | + return COLUMNS_BY_KEY.get(field)?.column.value(book); |
| 120 | +} |
| 121 | + |
| 122 | +export function formatBookValue( |
| 123 | + kind: BookColumnKind, |
| 124 | + value: BookColumnValue, |
| 125 | + translate: (key: string) => string, |
| 126 | +): string { |
| 127 | + if (value == null || value === '') { |
| 128 | + return BOOK_EMPTY_VALUE; |
| 129 | + } |
| 130 | + switch (kind) { |
| 131 | + case 'date': |
| 132 | + return formatMediumDate(String(value)) || BOOK_EMPTY_VALUE; |
| 133 | + case 'fileSize': |
| 134 | + return formatFileSizeKb(Number(value)); |
| 135 | + case 'rating': |
| 136 | + return Number(value).toFixed(1); |
| 137 | + case 'number': |
| 138 | + return NUMBER_FORMAT.format(Number(value)); |
| 139 | + case 'readStatus': { |
| 140 | + const labelKey = typeof value === 'string' ? bookReadStatusLabelKey(value) : null; |
| 141 | + return labelKey ? translate(labelKey) : BOOK_EMPTY_VALUE; |
| 142 | + } |
| 143 | + case 'text': |
| 144 | + return String(value); |
| 145 | + } |
| 146 | +} |
0 commit comments