pstream is dead; long live pstream taciturnaxolotl.github.io/pstream-ng/
at main 103 lines 2.9 kB view raw
1import { ProgressItem, ProgressMediaItem } from "@/stores/progress"; 2 3/** 4 * Options for modifying progress item properties 5 */ 6export interface ProgressModificationOptions { 7 /** Update the title of the progress item */ 8 title?: string; 9 /** Update the year of the progress item */ 10 year?: number; 11 /** Update the poster URL of the progress item */ 12 poster?: string; 13 /** Update the overall progress for movies or shows */ 14 progress?: ProgressItem; 15} 16 17/** 18 * Result of a progress modification operation 19 */ 20export interface ProgressModificationResult { 21 /** IDs of progress items that were modified */ 22 modifiedIds: string[]; 23 /** Whether any progress items were actually changed */ 24 hasChanges: boolean; 25} 26 27/** 28 * Modifies a single progress item with the provided options 29 */ 30export function modifyProgressItem( 31 progressItem: ProgressMediaItem, 32 options: ProgressModificationOptions, 33): ProgressMediaItem { 34 const modified = { ...progressItem, updatedAt: Date.now() }; 35 36 if (options.title !== undefined) { 37 modified.title = options.title; 38 } 39 40 if (options.year !== undefined) { 41 modified.year = options.year; 42 } 43 44 if (options.poster !== undefined) { 45 modified.poster = options.poster; 46 } 47 48 if (options.progress !== undefined) { 49 modified.progress = { ...options.progress }; 50 } 51 52 return modified; 53} 54 55/** 56 * Modifies multiple progress items by their IDs 57 */ 58export function modifyProgressItems( 59 progressItems: Record<string, ProgressMediaItem>, 60 progressIds: string[], 61 options: ProgressModificationOptions, 62): { 63 modifiedProgressItems: Record<string, ProgressMediaItem>; 64 result: ProgressModificationResult; 65} { 66 const modifiedProgressItems = { ...progressItems }; 67 const modifiedIds: string[] = []; 68 let hasChanges = false; 69 70 progressIds.forEach((id) => { 71 const original = modifiedProgressItems[id]; 72 if (original) { 73 const modified = modifyProgressItem(original, options); 74 modifiedProgressItems[id] = modified; 75 modifiedIds.push(id); 76 77 // Check if anything actually changed 78 if (!hasChanges) { 79 hasChanges = Object.keys(options).some((key) => { 80 const optionKey = key as keyof ProgressModificationOptions; 81 const optionValue = options[optionKey]; 82 const currentValue = modified[optionKey as keyof ProgressMediaItem]; 83 84 if (optionKey === "progress" && optionValue && currentValue) { 85 return ( 86 (optionValue as ProgressItem).watched !== 87 (currentValue as ProgressItem).watched || 88 (optionValue as ProgressItem).duration !== 89 (currentValue as ProgressItem).duration 90 ); 91 } 92 93 return optionValue !== currentValue; 94 }); 95 } 96 } 97 }); 98 99 return { 100 modifiedProgressItems, 101 result: { modifiedIds, hasChanges: hasChanges && modifiedIds.length > 0 }, 102 }; 103}