1+ import type { Mark } from '@/db/marks'
2+
3+ type RecordTimePreset = 'all' | 'today' | 'last7Days' | 'last30Days'
4+
5+ type RecordFiltersLike = {
6+ search : string
7+ selectedTypes : Mark [ 'type' ] [ ]
8+ timePreset : RecordTimePreset
9+ tagId : number | 'all'
10+ }
11+
112const DAY_IN_MS = 24 * 60 * 60 * 1000
2- const VALID_TYPES = new Set ( [ 'scan' , 'text' , 'image' , 'link' , 'file' , 'recording' , 'todo' ] )
3- const VALID_TIME_PRESETS = new Set ( [ 'all' , 'today' , 'last7Days' , 'last30Days' ] )
13+ const VALID_TYPES = new Set < Mark [ 'type' ] > ( [ 'scan' , 'text' , 'image' , 'link' , 'file' , 'recording' , 'todo' ] )
14+ const VALID_TIME_PRESETS = new Set < RecordTimePreset > ( [ 'all' , 'today' , 'last7Days' , 'last30Days' ] )
415
5- function normalizeText ( value ) {
16+ function normalizeText ( value ?: string | null ) : string {
617 return ( value || '' ) . trim ( ) . toLowerCase ( )
718}
819
9- function matchesTimePreset ( createdAt , timePreset , now ) {
20+ function matchesTimePreset ( createdAt : number , timePreset : RecordTimePreset , now : string | Date ) : boolean {
1021 if ( timePreset === 'all' ) {
1122 return true
1223 }
@@ -38,7 +49,7 @@ function matchesTimePreset(createdAt, timePreset, now) {
3849 return true
3950}
4051
41- function matchesSearch ( mark , search ) {
52+ function matchesSearch ( mark : Pick < Mark , 'content' | 'desc' | 'url' > , search : string ) : boolean {
4253 if ( ! search ) {
4354 return true
4455 }
@@ -50,14 +61,16 @@ function matchesSearch(mark, search) {
5061 return haystack . includes ( search )
5162}
5263
53- export function normalizeRecordFilters ( filters ) {
64+ export function normalizeRecordFilters ( filters ?: Partial < RecordFiltersLike > ) : RecordFiltersLike {
5465 const search = typeof filters ?. search === 'string' ? filters . search : ''
5566 const selectedTypes = Array . isArray ( filters ?. selectedTypes )
56- ? filters . selectedTypes . filter ( ( type ) => VALID_TYPES . has ( type ) )
67+ ? filters . selectedTypes . filter ( ( type ) : type is Mark [ 'type' ] => VALID_TYPES . has ( type ) )
5768 : [ ]
58- const timePreset = VALID_TIME_PRESETS . has ( filters ? .timePreset ) ? filters . timePreset : 'all'
69+ const timePreset : RecordTimePreset = filters ?. timePreset && VALID_TIME_PRESETS . has ( filters . timePreset ) ? filters . timePreset : 'all'
5970 const parsedTagId = typeof filters ?. tagId === 'string' ? Number ( filters . tagId ) : filters ?. tagId
60- const tagId = Number . isInteger ( parsedTagId ) && parsedTagId > 0 ? parsedTagId : 'all'
71+ const tagId : RecordFiltersLike [ 'tagId' ] = typeof parsedTagId === 'number' && Number . isInteger ( parsedTagId ) && parsedTagId > 0
72+ ? parsedTagId
73+ : 'all'
6174
6275 return {
6376 search,
@@ -67,7 +80,16 @@ export function normalizeRecordFilters(filters) {
6780 }
6881}
6982
70- export function buildRecordFilterSummary ( filters ) {
83+ export function getTrashRecordFilters ( ) : RecordFiltersLike {
84+ return {
85+ search : '' ,
86+ selectedTypes : [ ] ,
87+ timePreset : 'all' ,
88+ tagId : 'all' ,
89+ }
90+ }
91+
92+ export function buildRecordFilterSummary ( filters ?: Partial < RecordFiltersLike > ) {
7193 const normalized = normalizeRecordFilters ( filters )
7294
7395 return {
@@ -84,7 +106,10 @@ export function buildRecordFilterSummary(filters) {
84106 }
85107}
86108
87- export function filterMarks ( marks , filters ) {
109+ export function filterMarks (
110+ marks : Mark [ ] ,
111+ filters ?: Partial < RecordFiltersLike > & { now ?: string | Date }
112+ ) {
88113 const normalizedFilters = normalizeRecordFilters ( filters )
89114 const search = normalizeText ( normalizedFilters . search )
90115 const selectedTypes = new Set ( normalizedFilters . selectedTypes )
0 commit comments