1 import { logger } from '@root-helpers/logger'
3 function getStoredVolume () {
4 const value = getLocalStorage('volume')
5 if (value !== null && value !== undefined) {
6 const valueNumber = parseFloat(value)
7 if (isNaN(valueNumber)) return undefined
15 function getStoredMute () {
16 const value = getLocalStorage('mute')
17 if (value !== null && value !== undefined) return value === 'true'
22 function getStoredTheater () {
23 const value = getLocalStorage('theater-enabled')
24 if (value !== null && value !== undefined) return value === 'true'
29 function saveVolumeInStore (value: number) {
30 return setLocalStorage('volume', value.toString())
33 function saveMuteInStore (value: boolean) {
34 return setLocalStorage('mute', value.toString())
37 function saveTheaterInStore (enabled: boolean) {
38 return setLocalStorage('theater-enabled', enabled.toString())
41 function saveAverageBandwidth (value: number) {
42 /** used to choose the most fitting resolution */
43 return setLocalStorage('average-bandwidth', value.toString())
46 function getAverageBandwidthInStore () {
47 const value = getLocalStorage('average-bandwidth')
48 if (value !== null && value !== undefined) {
49 const valueNumber = parseInt(value, 10)
50 if (isNaN(valueNumber)) return undefined
58 function saveLastSubtitle (language: string) {
59 return setLocalStorage('last-subtitle', language)
62 function getStoredLastSubtitle () {
63 return getLocalStorage('last-subtitle')
66 function saveVideoWatchHistory (videoUUID: string, duration: number) {
67 return setLocalStorage(`video-watch-history`, JSON.stringify({
68 ...getStoredVideoWatchHistory(),
72 date: `${(new Date()).toISOString()}`
77 function getStoredVideoWatchHistory (videoUUID?: string) {
81 const value = getLocalStorage('video-watch-history')
84 data = JSON.parse(value)
86 logger.error('Cannot parse video watch history from local storage/', error)
91 if (videoUUID) return data[videoUUID]
96 function cleanupVideoWatch () {
97 const data = getStoredVideoWatchHistory()
100 const newData = Object.keys(data).reduce((acc, videoUUID) => {
101 const date = Date.parse(data[videoUUID].date)
103 const diff = Math.ceil(((new Date()).getTime() - date) / (1000 * 3600 * 24))
105 if (diff > 30) return acc
109 [videoUUID]: data[videoUUID]
113 setLocalStorage('video-watch-history', JSON.stringify(newData))
116 // ---------------------------------------------------------------------------
125 saveAverageBandwidth,
126 getAverageBandwidthInStore,
128 getStoredLastSubtitle,
129 saveVideoWatchHistory,
130 getStoredVideoWatchHistory,
134 // ---------------------------------------------------------------------------
136 const KEY_PREFIX = 'peertube-videojs-'
138 function getLocalStorage (key: string) {
140 return localStorage.getItem(KEY_PREFIX + key)
146 function setLocalStorage (key: string, value: string) {
148 localStorage.setItem(KEY_PREFIX + key, value)
149 } catch { /* empty */