mirror of
https://github.com/dergigi/boris.git
synced 2026-01-01 22:14:20 +01:00
refactor: make code more DRY by extracting shared utilities
- Create readingProgressUtils.ts with filterByReadingProgress function - Create readingDataProcessor.ts with shared processing functions: - processReadingPositions - processMarkedAsRead - filterValidItems - sortByReadingActivity - Refactor readsService.ts to use shared utilities - Refactor linksService.ts to use shared utilities - Eliminate 100+ lines of duplicated code - Simplify Me.tsx filter logic to 2 lines Benefits: - Single source of truth for reading progress filtering - Easier to maintain and modify - Less code duplication across services - More testable with isolated utility functions
This commit is contained in:
30
src/utils/readingProgressUtils.ts
Normal file
30
src/utils/readingProgressUtils.ts
Normal file
@@ -0,0 +1,30 @@
|
||||
import { ReadItem } from '../services/readsService'
|
||||
import { ReadingProgressFilterType } from '../components/ReadingProgressFilters'
|
||||
|
||||
/**
|
||||
* Filters ReadItems by reading progress
|
||||
*/
|
||||
export function filterByReadingProgress(
|
||||
items: ReadItem[],
|
||||
filter: ReadingProgressFilterType
|
||||
): ReadItem[] {
|
||||
return items.filter((item) => {
|
||||
const progress = item.readingProgress || 0
|
||||
const isMarked = item.markedAsRead || false
|
||||
|
||||
switch (filter) {
|
||||
case 'unopened':
|
||||
return progress === 0 && !isMarked
|
||||
case 'started':
|
||||
return progress > 0 && progress <= 0.10 && !isMarked
|
||||
case 'reading':
|
||||
return progress > 0.10 && progress <= 0.94 && !isMarked
|
||||
case 'completed':
|
||||
return progress >= 0.95 || isMarked
|
||||
case 'all':
|
||||
default:
|
||||
return true
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user