-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(videodetail): add continue watching and watch history
- Loading branch information
Showing
6 changed files
with
156 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/** | ||
* | ||
* Service to persist either locally or remotely | ||
* To localStorage or API | ||
* | ||
*/ | ||
|
||
import type { WatchHistoryItem } from '../store/WatchHistoryStore'; | ||
|
||
const LOCAL_STORAGE_KEY_WATCH_HISTORY = `watchhistory${window.configId ? `-${window.configId}` : ''}`; | ||
|
||
const setItem = (key: string, value: string) => { | ||
try { | ||
window.localStorage.setItem(key, value); | ||
} catch (error: unknown) { | ||
console.error(error); | ||
} | ||
}; | ||
|
||
const getItem = (key: string) => { | ||
try { | ||
return window.localStorage.getItem(key); | ||
} catch (error: unknown) { | ||
console.error(error); | ||
} | ||
}; | ||
|
||
const parseJSON = (value?: string | null): unknown[] | undefined => { | ||
if (!value) return; | ||
try { | ||
const ret = JSON.parse(value); | ||
|
||
return ret; | ||
} catch (error: unknown) { | ||
return; | ||
} | ||
}; | ||
|
||
const loadWatchHistory = (): WatchHistoryItem[] => { | ||
const localHistory = parseJSON(getItem(LOCAL_STORAGE_KEY_WATCH_HISTORY)); | ||
const watchHistory: WatchHistoryItem[] = localHistory ? (localHistory as WatchHistoryItem[]) : []; | ||
return watchHistory; | ||
}; | ||
|
||
const storeWatchHistory = (watchHistory: WatchHistoryItem[]): void => { | ||
setItem(LOCAL_STORAGE_KEY_WATCH_HISTORY, JSON.stringify(watchHistory)); | ||
}; | ||
|
||
export { loadWatchHistory, storeWatchHistory }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import { Store } from 'pullstate'; | ||
import { useEffect } from 'react'; | ||
|
||
import * as persist from '../services/persist.service'; | ||
|
||
export type WatchHistoryItem = { | ||
mediaid: string; | ||
position: number; | ||
}; | ||
|
||
type WatchHistoryStore = { | ||
watchHistory: WatchHistoryItem[]; | ||
}; | ||
|
||
export const WatchHistoryStore = new Store<WatchHistoryStore>({ | ||
watchHistory: [], | ||
}); | ||
WatchHistoryStore.subscribe( | ||
(state) => state.watchHistory, | ||
(watchHistory) => persist.storeWatchHistory(watchHistory), | ||
); | ||
|
||
export const loadWatchHistory = () => { | ||
const watchHistory = persist.loadWatchHistory(); | ||
|
||
return ( | ||
watchHistory && | ||
WatchHistoryStore.update((state) => { | ||
state.watchHistory = watchHistory; | ||
}) | ||
); | ||
}; | ||
|
||
export const useWatchHistoryUpdater = (createWatchHistoryItem: () => WatchHistoryItem | undefined): void => { | ||
useEffect(() => { | ||
const savePosition = () => { | ||
const { watchHistory } = WatchHistoryStore.getRawState(); | ||
const item = createWatchHistoryItem(); | ||
if (!item) return; | ||
|
||
const index = watchHistory.findIndex((historyItem) => historyItem.mediaid === item.mediaid); | ||
if (index > -1) { | ||
WatchHistoryStore.update((state) => { | ||
state.watchHistory[index] = item; | ||
}); | ||
} else { | ||
WatchHistoryStore.update((state) => { | ||
state.watchHistory.push(item); | ||
}); | ||
} | ||
}; | ||
const visibilityListener = () => document.visibilityState === 'hidden' && savePosition(); | ||
|
||
window.addEventListener('beforeunload', savePosition); | ||
window.addEventListener('visibilitychange', visibilityListener); | ||
|
||
return () => { | ||
savePosition(); | ||
window.removeEventListener('beforeunload', savePosition); | ||
window.removeEventListener('visibilitychange', visibilityListener); | ||
}; | ||
}, []); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
interface Window { | ||
configLocation: configLocation; | ||
configId: string; | ||
jwplayer: (id: string) => unknown; | ||
} |