@soffits/dsh-media-hub 0.0.0-stage → 0.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +97 -0
- package/LICENSE +21 -0
- package/README.md +78 -2
- package/client.js +223 -0
- package/cordis.patch.yml +11 -0
- package/dist/hls.mjs +41383 -0
- package/dist/index.js +15224 -0
- package/dist/tools.js +6884 -0
- package/icon.svg +7 -0
- package/locale/en.json +276 -0
- package/locale/fr.json +276 -0
- package/locale/zh.json +276 -0
- package/package.json +83 -4
- package/src/captions.js +74 -0
- package/src/formats.js +25 -0
- package/src/i18n.js +69 -0
- package/src/sync.js +84 -0
- package/web/about-view.js +32 -0
- package/web/avatars.js +46 -0
- package/web/books-view.js +192 -0
- package/web/continue-card.js +131 -0
- package/web/continue.js +59 -0
- package/web/details.js +103 -0
- package/web/discover-view.js +202 -0
- package/web/follow.js +29 -0
- package/web/format.js +57 -0
- package/web/known.js +19 -0
- package/web/library-view.js +234 -0
- package/web/nav.js +115 -0
- package/web/page.js +104 -0
- package/web/panel.css +363 -0
- package/web/player.js +736 -0
- package/web/presence-view.js +95 -0
- package/web/readiness.js +55 -0
- package/web/result-card.js +114 -0
- package/web/room-view.js +188 -0
- package/web/rooms-view.js +64 -0
- package/web/sort.js +30 -0
- package/web/ui.js +301 -0
- package/web/watching-view.js +100 -0
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Discover: pick a source (All sources, every configured one, then Link for
|
|
3
|
+
* pasting the address of a video or playlist on any site yt-dlp reads), under
|
|
4
|
+
* it that source's own categories, then search; Link looks the address up
|
|
5
|
+
* and shows it as one result. Book categories add a filter row (language, format, years, size).
|
|
6
|
+
* Results are cards: a show or audiobook (web/result-card.js) is watched
|
|
7
|
+
* together right away, saved to the library (the episodes being watched and
|
|
8
|
+
* the next few are kept on disk), or downloaded whole to keep offline; a
|
|
9
|
+
* book (web/books-view.js) is downloaded in a format onto the person's shelf.
|
|
10
|
+
*
|
|
11
|
+
* The source and category come from the URL (web/nav.js) and are
|
|
12
|
+
* remembered per browser. Only the newest search or category click may show
|
|
13
|
+
* its results: an older answer that arrives late is dropped, and the results
|
|
14
|
+
* area shows a loading state until the newest one answers.
|
|
15
|
+
*/
|
|
16
|
+
import { BookCard, BookFilters } from './books-view.js'
|
|
17
|
+
import { browserStorage, isWebAddress, pickKind, pickSource, readMemory, remember, takesUrl, withAll } from './nav.js'
|
|
18
|
+
import { Result } from './result-card.js'
|
|
19
|
+
import { sortModes, sortResults } from './sort.js'
|
|
20
|
+
import { infoOf } from './details.js'
|
|
21
|
+
import { Icon, Spinner, api, asPerson, fill, h, nameOf, query, React } from './ui.js'
|
|
22
|
+
import { languageChoices } from './i18n.js'
|
|
23
|
+
|
|
24
|
+
export const kindLabel = (t, kind) => nameOf(t, `kind.${kind.id}`, kind.label) || kind.id
|
|
25
|
+
|
|
26
|
+
export function DiscoverView({ t, nav, onNav, onOpen, onSaved, onBack }) {
|
|
27
|
+
const [configured, setConfigured] = React.useState(null)
|
|
28
|
+
const [text, setText] = React.useState('')
|
|
29
|
+
const [results, setResults] = React.useState(null)
|
|
30
|
+
const [loading, setLoading] = React.useState(null)
|
|
31
|
+
const [error, setError] = React.useState(null)
|
|
32
|
+
const [sort, setSort] = React.useState('default')
|
|
33
|
+
const [more, setMore] = React.useState(false)
|
|
34
|
+
const [filters, setFilters] = React.useState({ language: '', format: '', yearFrom: '', yearTo: '', maxMb: '' })
|
|
35
|
+
/** The newest request: its number and abort controller; every older answer is dropped. */
|
|
36
|
+
const latest = React.useRef({ id: 0, controller: null, key: null })
|
|
37
|
+
|
|
38
|
+
React.useEffect(() => {
|
|
39
|
+
api('sources').then(answer => setConfigured(answer.sources ?? []))
|
|
40
|
+
return () => latest.current.controller?.abort()
|
|
41
|
+
}, [])
|
|
42
|
+
|
|
43
|
+
const sources = React.useMemo(() => withAll((configured ?? []).map(one => ({ ...one, label: nameOf(t, `source.name.${one.id}`, one.label) || one.id })), t('source.all')), [configured, t])
|
|
44
|
+
const sourceOf = id => sources.find(one => one.id === id)
|
|
45
|
+
const storage = browserStorage()
|
|
46
|
+
const memory = readMemory(storage)
|
|
47
|
+
const source = configured === null ? null : pickSource(sources, nav.source, memory.source)
|
|
48
|
+
const kind = source === null ? null : pickKind(source, nav.kind, memory.kinds[source.id])
|
|
49
|
+
const isLink = takesUrl(source)
|
|
50
|
+
const isBook = (kindId, from = source) => from?.kinds.find(k => k.id === kindId)?.media === 'book'
|
|
51
|
+
/** A request for the source on screen is on its way. */
|
|
52
|
+
const searching = loading !== null && loading.source === source?.id
|
|
53
|
+
|
|
54
|
+
// The URL shows what is picked, even when it fell back to the remembered or first choice.
|
|
55
|
+
React.useEffect(() => {
|
|
56
|
+
if (source === null) return
|
|
57
|
+
if (nav.source !== source.id || nav.kind !== kind) onNav({ source: source.id, kind, work: nav.source === source.id ? nav.work : null }, { replace: true })
|
|
58
|
+
remember(storage, { source: source.id, kind })
|
|
59
|
+
}, [source?.id, kind, nav.source, nav.kind])
|
|
60
|
+
|
|
61
|
+
const run = async ({ nextSource = source, nextKind = kind, from = 'search', nextFilters = filters } = {}) => {
|
|
62
|
+
const q = text.trim()
|
|
63
|
+
if (q === '' || nextSource === null) return undefined
|
|
64
|
+
const filtering = isBook(nextKind, nextSource) ? Object.fromEntries(Object.entries(nextFilters).filter(([, value]) => value !== '')) : {}
|
|
65
|
+
const key = `${nextSource.id}|${nextKind}|${q}|${JSON.stringify(filtering)}`
|
|
66
|
+
// The same question already on its way or on screen: nothing new to ask.
|
|
67
|
+
if (from === 'pick' && latest.current.key === key) return undefined
|
|
68
|
+
latest.current.controller?.abort()
|
|
69
|
+
const request = { id: latest.current.id + 1, controller: new AbortController(), key }
|
|
70
|
+
latest.current = request
|
|
71
|
+
const stale = () => latest.current.id !== request.id
|
|
72
|
+
setLoading({ from, source: nextSource.id, kind: nextKind })
|
|
73
|
+
setError(null)
|
|
74
|
+
const link = takesUrl(nextSource)
|
|
75
|
+
if (link && !isWebAddress(q)) {
|
|
76
|
+
setLoading(null)
|
|
77
|
+
latest.current.key = null
|
|
78
|
+
return setError(t('link.invalid'))
|
|
79
|
+
}
|
|
80
|
+
const answer = link
|
|
81
|
+
? await asPerson(`info?${query({ source: nextSource.id, id: q, language: t.language })}`, undefined, { signal: request.controller.signal })
|
|
82
|
+
: await api(`search?${query({ source: nextSource.id, q, kind: nextKind ?? '', ...filtering })}`, { signal: request.controller.signal })
|
|
83
|
+
if (stale()) return undefined
|
|
84
|
+
setLoading(null)
|
|
85
|
+
if (answer.error) {
|
|
86
|
+
latest.current.key = null
|
|
87
|
+
return setError(answer.error)
|
|
88
|
+
}
|
|
89
|
+
// Link's answer is the one work at that address.
|
|
90
|
+
setResults({ source: nextSource, kind: nextKind, items: link ? [answer] : answer.results, next: link ? null : (answer.next ?? null), query: q, failed: answer.failed ?? [], filtering })
|
|
91
|
+
return undefined
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/** The next page of the same search, added below; a page with nothing new ends the list. */
|
|
95
|
+
const loadMore = async () => {
|
|
96
|
+
if (results === null || results.next === null || more) return
|
|
97
|
+
setMore(true)
|
|
98
|
+
const answer = await api(`search?${query({ source: results.source.id, q: results.query, kind: results.kind ?? '', offset: results.next, ...results.filtering })}`)
|
|
99
|
+
setMore(false)
|
|
100
|
+
if (answer.error) return setError(answer.error)
|
|
101
|
+
setResults((current) => {
|
|
102
|
+
if (current === null || current.query !== results.query || current.kind !== results.kind || current.source.id !== results.source.id) return current
|
|
103
|
+
const known = new Set(current.items.map(item => item.id))
|
|
104
|
+
const fresh = answer.results.filter(item => !known.has(item.id))
|
|
105
|
+
return { ...current, items: [...current.items, ...fresh], next: fresh.length > 0 ? (answer.next ?? null) : null }
|
|
106
|
+
})
|
|
107
|
+
return undefined
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
const again = results !== null || loading !== null
|
|
111
|
+
const pickSourceId = (next) => {
|
|
112
|
+
const nextKind = pickKind(next, null, memory.kinds[next.id])
|
|
113
|
+
onNav({ source: next.id, kind: nextKind, work: null })
|
|
114
|
+
setError(null)
|
|
115
|
+
if (again && !takesUrl(next)) run({ nextSource: next, nextKind, from: 'pick' })
|
|
116
|
+
}
|
|
117
|
+
const pickKindId = (id) => {
|
|
118
|
+
onNav({ source: source.id, kind: id, work: null })
|
|
119
|
+
if (again) run({ nextKind: id, from: 'pick' })
|
|
120
|
+
}
|
|
121
|
+
const filter = (next) => {
|
|
122
|
+
setFilters(next)
|
|
123
|
+
if (again) run({ nextFilters: next, from: 'pick' })
|
|
124
|
+
}
|
|
125
|
+
const type = (value) => {
|
|
126
|
+
setText(value)
|
|
127
|
+
if (value === '') setResults(null)
|
|
128
|
+
// A web address pasted into a source's search box belongs to Link.
|
|
129
|
+
else if (!isLink && isWebAddress(value)) {
|
|
130
|
+
const link = sources.find(takesUrl)
|
|
131
|
+
if (link !== undefined) onNav({ source: link.id, kind: null, work: null })
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
const onFocus = work => onNav({ work: work.id })
|
|
136
|
+
const focused = nav.work !== null && source !== null && !isLink
|
|
137
|
+
const modes = results === null ? ['default'] : sortModes(results.items)
|
|
138
|
+
const activeSort = modes.includes(sort) ? sort : 'default'
|
|
139
|
+
const sorted = results === null ? [] : sortResults(results.items, activeSort)
|
|
140
|
+
const shown = results !== null && source !== null && results.source.id === source.id && results.kind === kind
|
|
141
|
+
|
|
142
|
+
return h('div', { className: 'vsync-tabPanel' },
|
|
143
|
+
focused && h(FocusedWork, { key: `${source.id}/${nav.work}`, t, source, id: nav.work, onOpen, onSaved, onFocus, onBack }),
|
|
144
|
+
h('div', { hidden: focused },
|
|
145
|
+
source !== null && h('div', { className: 'vsync-picker' },
|
|
146
|
+
h('div', { className: 'vsync-segment', role: 'tablist', 'aria-label': t('discover.source') },
|
|
147
|
+
sources.map(s => h('button', { key: s.id, type: 'button', role: 'tab', className: 'vsync-seg', 'aria-selected': String(s.id === source.id), onClick: () => pickSourceId(s) }, takesUrl(s) ? h('span', { className: 'vsync-segIcon' }, h(Icon.Link, { size: 14 })) : null, s.label))),
|
|
148
|
+
source.kinds.length > 0 && !isLink && h('div', { className: 'vsync-tabs', role: 'tablist', 'aria-label': t('discover.kind') },
|
|
149
|
+
source.kinds.map(k => h('button', {
|
|
150
|
+
key: k.id,
|
|
151
|
+
type: 'button',
|
|
152
|
+
role: 'tab',
|
|
153
|
+
className: 'vsync-chip',
|
|
154
|
+
'aria-selected': String(k.id === kind),
|
|
155
|
+
'aria-busy': String(searching && k.id === loading.kind),
|
|
156
|
+
'data-loading': String(searching && k.id === loading.kind),
|
|
157
|
+
onClick: () => pickKindId(k.id),
|
|
158
|
+
}, searching && k.id === loading.kind ? h(Spinner) : null, kindLabel(t, k))))),
|
|
159
|
+
h('form', { className: 'vsync-search', role: 'search', onSubmit: (event) => { event.preventDefault(); run({ from: 'search' }) } },
|
|
160
|
+
h('span', { className: 'vsync-searchIcon' }, isLink ? h(Icon.Link) : h(Icon.Search)),
|
|
161
|
+
h('input', {
|
|
162
|
+
className: 'vsync-field vsync-searchField',
|
|
163
|
+
type: isLink ? 'text' : 'search',
|
|
164
|
+
inputMode: isLink ? 'url' : undefined,
|
|
165
|
+
value: text,
|
|
166
|
+
placeholder: isLink ? t('link.placeholder') : fill(t('search.placeholder'), { source: source?.label ?? '' }),
|
|
167
|
+
'aria-label': t('search.label'),
|
|
168
|
+
onChange: event => type(event.target.value),
|
|
169
|
+
}),
|
|
170
|
+
h('button', { type: 'submit', className: 'vsync-btn vsync-btnLarge', disabled: (searching && loading.from === 'search') || text.trim() === '' || source === null }, searching && loading.from === 'search' ? h(Spinner) : null, isLink ? t('link.go') : t('search.go'))),
|
|
171
|
+
!isLink && isBook(kind) && h(BookFilters, { t, filters, languages: languageChoices(t.language, shown ? results.items : [], filters.language), onChange: filter }),
|
|
172
|
+
error && h('p', { className: 'vsync-error', role: 'alert' }, error),
|
|
173
|
+
!searching && shown && results.failed.length > 0 && h('p', { className: 'vsync-note', role: 'status' }, results.failed.map(one => fill(t('search.sourceFailed'), { source: sourceOf(one.source)?.label ?? one.source, message: one.message })).join(' ')),
|
|
174
|
+
searching && h('div', { className: 'vsync-skeleton', role: 'status', 'aria-busy': 'true', 'aria-label': t('search.loading') },
|
|
175
|
+
[0, 1, 2].map(n => h('span', { key: n, className: 'vsync-skeletonRow' }))),
|
|
176
|
+
!searching && shown && results.items.length > 1 && modes.length > 1 && h('div', { className: 'vsync-tabs vsync-sort', role: 'group', 'aria-label': t('sort.label') },
|
|
177
|
+
modes.map(mode => h('button', { key: mode, type: 'button', className: 'vsync-chip', 'aria-pressed': String(mode === activeSort), onClick: () => setSort(mode) }, t(`sort.${mode}`)))),
|
|
178
|
+
!searching && shown && (results.items.length === 0
|
|
179
|
+
? h('p', { className: 'vsync-empty' }, t('search.none'))
|
|
180
|
+
: h('ul', { className: 'vsync-rows vsync-results' }, sorted.map((result) => {
|
|
181
|
+
const from = sourceOf(result.source) ?? results.source
|
|
182
|
+
return isBook(result.kind, from) || isBook(results.kind, results.source)
|
|
183
|
+
? h(BookCard, { key: `${from.id}:${result.id}`, t, source: from, result })
|
|
184
|
+
: h(Result, { key: `${from.id}:${result.id}`, t, source: from, result, onOpen, onSaved, onFocus })
|
|
185
|
+
}))),
|
|
186
|
+
!searching && shown && results.next !== null && h('button', { type: 'button', className: 'vsync-btn vsync-more', disabled: more, onClick: loadMore }, more ? h(Spinner) : null, t('search.more'))))
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
/** One work opened from a related list: its card with the details open (their related works, one level), and a way back. */
|
|
190
|
+
function FocusedWork({ t, source, id, onOpen, onSaved, onFocus, onBack }) {
|
|
191
|
+
const [work, setWork] = React.useState(null)
|
|
192
|
+
React.useEffect(() => {
|
|
193
|
+
let live = true
|
|
194
|
+
infoOf(source.id, id, t.language).then(value => { if (live) setWork(value) })
|
|
195
|
+
return () => { live = false }
|
|
196
|
+
}, [source.id, id])
|
|
197
|
+
return h('div', { className: 'vsync-focus' },
|
|
198
|
+
h('button', { type: 'button', className: 'vsync-btn vsync-back', onClick: onBack }, h(Icon.Back, { size: 14 }), t('discover.back')),
|
|
199
|
+
work === null && h('p', { className: 'vsync-note', role: 'status' }, h(Spinner), ' ', t('search.loading')),
|
|
200
|
+
work?.error && h('p', { className: 'vsync-error', role: 'alert' }, fill(t('discover.workFailed'), { message: work.error })),
|
|
201
|
+
work && !work.error && h('ul', { className: 'vsync-rows' }, h(Result, { t, source, result: work, onOpen, onSaved, onFocus, defaultOpen: true })))
|
|
202
|
+
}
|
package/web/follow.js
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Following a work: the host looks for its next season (and new episodes)
|
|
3
|
+
* now and then and adds a new season to the library by itself. A work not in
|
|
4
|
+
* the library is added as an empty show when followed.
|
|
5
|
+
*/
|
|
6
|
+
import { useKnownWork } from './known.js'
|
|
7
|
+
import { Btn, K, Known, h, post, React } from './ui.js'
|
|
8
|
+
|
|
9
|
+
/** `compact`: the bell alone, its words on hover and for screen readers. */
|
|
10
|
+
export function FollowButton({ t, source, work, compact = false }) {
|
|
11
|
+
const known = useKnownWork(source, work.id)
|
|
12
|
+
const { reload } = React.useContext(Known)
|
|
13
|
+
const [busy, setBusy] = React.useState(false)
|
|
14
|
+
const [error, setError] = React.useState(null)
|
|
15
|
+
const following = known.show?.follow != null
|
|
16
|
+
const toggle = async () => {
|
|
17
|
+
setBusy(true)
|
|
18
|
+
setError(null)
|
|
19
|
+
const answer = await post('follow', { source, id: work.id, follow: !following })
|
|
20
|
+
setBusy(false)
|
|
21
|
+
if (answer.error) setError(answer.error)
|
|
22
|
+
reload()
|
|
23
|
+
}
|
|
24
|
+
return h(React.Fragment, null,
|
|
25
|
+
compact
|
|
26
|
+
? h(Btn, { 'aria-pressed': String(following), 'aria-label': t(following ? 'follow.following' : 'follow.follow'), title: t(following ? 'follow.following' : 'follow.hint'), disabled: busy, onClick: toggle, icon: h(K.Icon, { name: following ? 'bell-ring' : 'bell-plus', size: 16 }) })
|
|
27
|
+
: h('button', { type: 'button', className: `vsync-btn${following ? ' vsync-following' : ''}`, 'aria-pressed': String(following), disabled: busy, title: t('follow.hint'), onClick: toggle }, t(following ? 'follow.following' : 'follow.follow')),
|
|
28
|
+
error && h('span', { className: 'vsync-error', role: 'alert' }, error))
|
|
29
|
+
}
|
package/web/format.js
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Words for the descriptive fields of a search result (docs/source-protocol.md).
|
|
3
|
+
* Sources send raw counts and codes; the page language decides how they read.
|
|
4
|
+
* No DOM here: it also runs in Node.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
/** Counts a source may label that the panel has words for (`pop.<label>` in the locale files). */
|
|
8
|
+
export const POPULARITY_LABELS = Object.freeze(['plays', 'readers', 'listeners', 'viewers', 'followers', 'heat'])
|
|
9
|
+
/** `meta` keys the panel has a translated label for (`meta.<key>`). */
|
|
10
|
+
export const META_KEYS = Object.freeze(['heat', 'followers', 'cast', 'duration', 'updated', 'publisher', 'narrator', 'words', 'studio'])
|
|
11
|
+
export const STATUSES = Object.freeze(['finished', 'ongoing', 'stopped'])
|
|
12
|
+
export const RELATIONS = Object.freeze(['season', 'same-title', 'related'])
|
|
13
|
+
|
|
14
|
+
/** A count the way the page language writes it short, by Intl (36.7K, 3.7万, 36,7 k). */
|
|
15
|
+
export function formatCount(count, locale) {
|
|
16
|
+
if (!Number.isFinite(count) || count < 0) return ''
|
|
17
|
+
return new Intl.NumberFormat(locale, { notation: 'compact', maximumFractionDigits: 1 }).format(count)
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/** "36.7万次播放" / "36.7K plays": the count with what it counts, or null when there is no count. */
|
|
21
|
+
export function popularityText(t, popularity) {
|
|
22
|
+
if (popularity === null || popularity === undefined) return null
|
|
23
|
+
const number = formatCount(popularity.count, t.language)
|
|
24
|
+
if (POPULARITY_LABELS.includes(popularity.label)) return t(`pop.${popularity.label}`).replace('{n}', number)
|
|
25
|
+
return popularity.text || `${number} ${popularity.label}`
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/** The label of a `meta` row: the panel's translation of its key, else the source's own label. */
|
|
29
|
+
export const metaLabel = (t, row) => (META_KEYS.includes(row.key) ? t(`meta.${row.key}`) : row.label)
|
|
30
|
+
|
|
31
|
+
/** A template with its `{name}` slots filled. */
|
|
32
|
+
export const fill = (template, values) => template.replace(/\{(\w+)\}/g, (_, key) => String(values[key] ?? ''))
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Whether something has episodes to speak of: from two on. A film, one video or a single link has none,
|
|
36
|
+
* so it gets no picker, no strip and no "Episode 1 of 1". `n` (1-based, optional) past the first means there are.
|
|
37
|
+
*/
|
|
38
|
+
export const hasEpisodes = (count, n = 1) => (Number.isInteger(count) && count > 1) || n > 1
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* The button that picks up where someone is: "Join Ana · episode 7" or "Join Ana", "Continue · episode 7",
|
|
42
|
+
* "Continue at 12:34", or "Play" for a single one not started yet.
|
|
43
|
+
* @param {{ count: number | null, n: number, position?: number | null, at?: string, names?: string }} where -
|
|
44
|
+
* `count` episodes in all (null when unknown), `n` the 1-based one it continues at, `at` the position as read
|
|
45
|
+
*/
|
|
46
|
+
export function resumeLabel(t, { count, n, position = null, at = '', names = '' }) {
|
|
47
|
+
const many = hasEpisodes(count, n)
|
|
48
|
+
if (names) return fill(t(many ? 'continue.join' : 'continue.joinOne'), { names, n })
|
|
49
|
+
if (many) return fill(t('continue.continue'), { n })
|
|
50
|
+
return position > 5 ? fill(t('continue.continueAt'), { time: at }) : t('continue.play')
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/** Where someone stopped, for a list line: "continue from ep 7 at 12:34", or "continue at 12:34" for a single one; null when there is nothing to say. */
|
|
54
|
+
export function stoppedText(t, { count, n, position = null, at = '' }) {
|
|
55
|
+
if (hasEpisodes(count, n)) return fill(t(position > 5 ? 'library.continueFromAt' : 'library.continueFrom'), { n, time: at })
|
|
56
|
+
return position > 5 ? fill(t('library.continueAt'), { time: at }) : null
|
|
57
|
+
}
|
package/web/known.js
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Where a work from a source stands here: in the library (its show), in an
|
|
3
|
+
* open room. Looked up by source id and work id, so every place a work
|
|
4
|
+
* appears can say so instead of offering to save it again.
|
|
5
|
+
*/
|
|
6
|
+
import { Known, React } from './ui.js'
|
|
7
|
+
|
|
8
|
+
/** @returns {{ show: object | null, room: object | null }} */
|
|
9
|
+
export function lookupWork(library, rooms, sourceId, workId) {
|
|
10
|
+
const show = library?.shows?.find(entry => entry.source?.source === sourceId && entry.source?.id === workId) ?? null
|
|
11
|
+
const room = show === null ? null : (rooms ?? []).find(entry => entry.show === show.path) ?? null
|
|
12
|
+
return { show, room }
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/** The known state of one work (from the page's polled library and rooms). */
|
|
16
|
+
export function useKnownWork(sourceId, workId) {
|
|
17
|
+
const { library, rooms } = React.useContext(Known)
|
|
18
|
+
return lookupWork(library, rooms, sourceId, workId)
|
|
19
|
+
}
|
|
@@ -0,0 +1,234 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The library: every show with its poster, episodes, size, download and
|
|
3
|
+
* watch state; starting or continuing a room; renaming, keeping and
|
|
4
|
+
* deleting a show or one episode. Deleting asks first, in place.
|
|
5
|
+
*/
|
|
6
|
+
import { Picture, WorkDetails } from './details.js'
|
|
7
|
+
import { hasEpisodes, stoppedText } from './format.js'
|
|
8
|
+
import { Confirm, DAY_MS, EPISODE_BYTES, Icon, Menu, React, api, asPerson, bytes, clock, day, displayName, fill, h, plural, post, query, watchMark } from './ui.js'
|
|
9
|
+
|
|
10
|
+
const titleOf = name => name.replace(/\.[^.]+$/, '')
|
|
11
|
+
|
|
12
|
+
/** A show's poster: its work's cover, else a frame of its first episode. */
|
|
13
|
+
export function Poster({ show }) {
|
|
14
|
+
return h(Picture, {
|
|
15
|
+
pictures: [show.work?.cover, show.frame],
|
|
16
|
+
className: `vsync-poster${show.work?.cover ? ' vsync-posterCover' : ''}`,
|
|
17
|
+
emptyClassName: 'vsync-poster vsync-posterEmpty',
|
|
18
|
+
icon: show.media === 'audio' ? h(Icon.Audio, { size: 18 }) : h(Icon.Film, { size: 18 }),
|
|
19
|
+
})
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/** Library actions say who did them, for the watch log. */
|
|
23
|
+
const asViewer = async (t, body) => ({ ...body, by: await displayName(t('guest')) })
|
|
24
|
+
|
|
25
|
+
/** The countdown of a show about to be deleted, as a quiet button: keep it, or restart the countdown. */
|
|
26
|
+
function Countdown({ t, show, days, onChanged }) {
|
|
27
|
+
const left = Math.ceil((show.deletesAt - Date.now()) / DAY_MS)
|
|
28
|
+
const text = left <= 0 ? t('library.deletesSoon') : fill(t(left === 1 ? 'library.deletesInDay' : 'library.deletesIn'), { n: left })
|
|
29
|
+
const act = path => async () => { await post(path, await asViewer(t, { path: show.path })); onChanged() }
|
|
30
|
+
return h(Menu, {
|
|
31
|
+
label: text,
|
|
32
|
+
title: fill(t('library.deletesOn'), { date: day(t, show.deletesAt) }),
|
|
33
|
+
items: [
|
|
34
|
+
{ label: t('library.keep'), onSelect: act('library/keep') },
|
|
35
|
+
{ label: fill(t('library.restartCountdown'), { n: days }), onSelect: act('library/countdown') },
|
|
36
|
+
],
|
|
37
|
+
}, text)
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* What a library row says: `lead` is the short line (how good it is, how far along, whether it is followed), `rest` the muted one
|
|
42
|
+
* (what is on disk, downloads, who is in it, where the others stopped).
|
|
43
|
+
*/
|
|
44
|
+
/** How many episodes a show has in all: its source's count, else the files on disk. */
|
|
45
|
+
export const episodeCount = show => show.total ?? show.episodes
|
|
46
|
+
|
|
47
|
+
export function showMeta(t, show) {
|
|
48
|
+
const lead = [...workGlance(t, show)]
|
|
49
|
+
if (show.watchedAt) lead.push(t('library.watchedAll'))
|
|
50
|
+
else {
|
|
51
|
+
if (show.finished > 0 && show.episodes > 1) lead.push(fill(t('library.watchedCount'), { n: show.finished, total: show.total ?? show.episodes }))
|
|
52
|
+
const stopped = show.resume && stoppedText(t, { count: episodeCount(show), n: show.resume.index + 1, position: show.resume.position, at: clock(show.resume.position) })
|
|
53
|
+
if (stopped) lead.push(stopped)
|
|
54
|
+
}
|
|
55
|
+
if (show.follow) lead.push(t('follow.following'))
|
|
56
|
+
if (show.seasons > 1) lead.push(fill(t('follow.season'), { n: show.season, total: show.seasons }))
|
|
57
|
+
if (show.newEpisodes) lead.push(plural(t, 'follow.newEpisodes', show.newEpisodes.n))
|
|
58
|
+
if (show.newSeason) lead.push(t('follow.newSeason'))
|
|
59
|
+
|
|
60
|
+
const rest = []
|
|
61
|
+
if (hasEpisodes(episodeCount(show))) {
|
|
62
|
+
rest.push(show.sourced
|
|
63
|
+
? fill(t('library.cachedOf'), { n: show.episodes, total: show.total })
|
|
64
|
+
: plural(t, 'library.episodes', show.episodes))
|
|
65
|
+
}
|
|
66
|
+
if (show.pinned) rest.push(t('library.offline'))
|
|
67
|
+
rest.push(bytes(t, show.size))
|
|
68
|
+
const download = show.download
|
|
69
|
+
if (download?.status === 'running') rest.push(t('library.downloading'))
|
|
70
|
+
else if (download?.status === 'queued') rest.push(t('library.queued'))
|
|
71
|
+
else if (download?.status === 'failed') rest.push(fill(t('library.downloadFailed'), { message: download.reason ? t(`error.${download.reason}`) : download.error ?? '' }))
|
|
72
|
+
if (show.inUse) rest.push(t('library.inRoom'))
|
|
73
|
+
const many = hasEpisodes(episodeCount(show))
|
|
74
|
+
if (!show.watchedAt) for (const stop of show.stops ?? []) rest.push(fill(t(many || stop.index > 0 ? 'library.viewerStopped' : 'library.viewerStoppedAt'), { name: stop.viewer, n: stop.index + 1, time: clock(stop.position) }))
|
|
75
|
+
if (show.keep) rest.push(t('library.kept'))
|
|
76
|
+
return { lead, rest }
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/** What the source said about the show at a glance: score, status, how long it is. */
|
|
80
|
+
function workGlance(t, show) {
|
|
81
|
+
const work = show.work
|
|
82
|
+
if (!work) return []
|
|
83
|
+
const status = { finished: 'search.finished', ongoing: 'search.ongoing', stopped: 'search.stopped' }[work.status]
|
|
84
|
+
return [work.score && fill(t('search.score'), { score: work.score.toFixed(1) }), status && t(status)].filter(Boolean)
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
function Episodes({ t, show, onStart, onChanged }) {
|
|
88
|
+
const [episodes, setEpisodes] = React.useState(null)
|
|
89
|
+
const [confirming, setConfirming] = React.useState(null)
|
|
90
|
+
const [error, setError] = React.useState(null)
|
|
91
|
+
const load = React.useCallback(() => api(`library/episodes?${query({ path: show.path })}`).then(answer => setEpisodes(answer.episodes ?? [])), [show.path])
|
|
92
|
+
React.useEffect(() => { load() }, [load, show.episodes])
|
|
93
|
+
if (episodes === null) return null
|
|
94
|
+
return h('ol', { className: 'vsync-subrows' },
|
|
95
|
+
error && h('li', { className: 'vsync-error', role: 'alert' }, error),
|
|
96
|
+
episodes.map((episode, index) => {
|
|
97
|
+
const mark = watchMark(t, episode.watch)
|
|
98
|
+
const title = episode.title ?? fill(t('library.episodeN'), { n: episode.n })
|
|
99
|
+
return h('li', { key: episode.n, className: 'vsync-subrow', 'data-watch': mark.state, 'data-cached': String(episode.cached) },
|
|
100
|
+
h('button', { type: 'button', className: 'vsync-subrowMain', title: episode.cached ? t('library.playFrom') : t('library.playFromFetch'), onClick: () => onStart({ index, position: 0 }) },
|
|
101
|
+
h('span', { className: 'vsync-episodeNo' }, index + 1),
|
|
102
|
+
h('span', { className: 'vsync-episodeMain' },
|
|
103
|
+
h('span', { className: 'vsync-episodeTitle' }, title),
|
|
104
|
+
mark.bar),
|
|
105
|
+
mark.note,
|
|
106
|
+
h('span', { className: 'vsync-episodeState' }, episode.cached ? bytes(t, episode.size) : t('library.notCached'))),
|
|
107
|
+
!episode.cached ? null : confirming === episode.name
|
|
108
|
+
? h(Confirm, {
|
|
109
|
+
t,
|
|
110
|
+
question: t('library.deleteEpisodeQuestion'),
|
|
111
|
+
confirmLabel: t('action.delete'),
|
|
112
|
+
onCancel: () => setConfirming(null),
|
|
113
|
+
onConfirm: async () => {
|
|
114
|
+
const answer = await api(`library?${query({ path: show.path, episode: episode.name, by: await displayName(t('guest')) })}`, { method: 'DELETE' })
|
|
115
|
+
setConfirming(null)
|
|
116
|
+
setError(answer.error ?? null)
|
|
117
|
+
load()
|
|
118
|
+
onChanged()
|
|
119
|
+
},
|
|
120
|
+
})
|
|
121
|
+
: h('button', { type: 'button', className: 'vsync-icon vsync-subrowDelete', 'aria-label': fill(t('library.deleteEpisode'), { episode: title }), title: t('action.delete'), disabled: show.inUse, onClick: () => setConfirming(episode.name) }, h(Icon.Close)))
|
|
122
|
+
}))
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
function ShowRow({ t, show, days, onOpen, onChanged, onOpenWork }) {
|
|
126
|
+
const { lead, rest } = showMeta(t, show)
|
|
127
|
+
const [mode, setMode] = React.useState('idle')
|
|
128
|
+
const [details, setDetails] = React.useState(false)
|
|
129
|
+
const [name, setName] = React.useState(show.name)
|
|
130
|
+
const [open, setOpen] = React.useState(false)
|
|
131
|
+
const [error, setError] = React.useState(null)
|
|
132
|
+
|
|
133
|
+
const act = async (request) => {
|
|
134
|
+
const answer = await request()
|
|
135
|
+
setError(answer.error ?? null)
|
|
136
|
+
onChanged()
|
|
137
|
+
return answer
|
|
138
|
+
}
|
|
139
|
+
const start = async (startAt) => {
|
|
140
|
+
setError(null)
|
|
141
|
+
const by = await displayName(t('guest'))
|
|
142
|
+
const answer = await asPerson('rooms', { source: 'local', path: show.path, by, ...(startAt ? { start: startAt } : {}) })
|
|
143
|
+
if (answer.error) return setError(answer.error)
|
|
144
|
+
return onOpen(answer.room.id)
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
let actions
|
|
148
|
+
if (mode === 'delete') {
|
|
149
|
+
actions = h(Confirm, {
|
|
150
|
+
t,
|
|
151
|
+
question: fill(t('library.deleteQuestion'), { size: bytes(t, show.size) }),
|
|
152
|
+
confirmLabel: t('action.delete'),
|
|
153
|
+
onCancel: () => setMode('idle'),
|
|
154
|
+
onConfirm: async () => {
|
|
155
|
+
await act(async () => api(`library?${query({ path: show.path, by: await displayName(t('guest')) })}`, { method: 'DELETE' }))
|
|
156
|
+
setMode('idle')
|
|
157
|
+
},
|
|
158
|
+
})
|
|
159
|
+
} else if (mode === 'pin') {
|
|
160
|
+
const missing = show.total - show.episodes
|
|
161
|
+
actions = h(Confirm, {
|
|
162
|
+
t,
|
|
163
|
+
danger: false,
|
|
164
|
+
question: fill(t('library.downloadAllQuestion'), { n: missing, size: bytes(t, missing * (EPISODE_BYTES[show.media === 'audio' ? 'audio' : 'drama'])) }),
|
|
165
|
+
confirmLabel: t('library.downloadAll'),
|
|
166
|
+
onCancel: () => setMode('idle'),
|
|
167
|
+
onConfirm: async () => {
|
|
168
|
+
await act(async () => post('library/pin', await asViewer(t, { path: show.path, pinned: true })))
|
|
169
|
+
setMode('idle')
|
|
170
|
+
},
|
|
171
|
+
})
|
|
172
|
+
} else if (mode === 'rename') {
|
|
173
|
+
actions = h('span', { className: 'vsync-actions' },
|
|
174
|
+
h('button', { type: 'button', className: 'vsync-btn', onClick: () => { setMode('idle'); setName(show.name) } }, t('action.cancel')),
|
|
175
|
+
h('button', { type: 'submit', form: `vsync-rename-${show.path}`, className: 'vsync-btn vsync-primary' }, t('action.save')))
|
|
176
|
+
} else {
|
|
177
|
+
actions = h('span', { className: 'vsync-actions' },
|
|
178
|
+
(show.episodes > 0 || show.sourced) && h('button', { type: 'button', className: 'vsync-btn', onClick: () => start(null) }, show.resume ? t('library.continue') : t('library.watch')),
|
|
179
|
+
h(Menu, {
|
|
180
|
+
label: t('library.more'),
|
|
181
|
+
items: [
|
|
182
|
+
{ label: t('library.rename'), onSelect: () => setMode('rename') },
|
|
183
|
+
show.sourced && !show.pinned && show.episodes < show.total && { label: t('library.downloadAll'), onSelect: () => setMode('pin') },
|
|
184
|
+
show.pinned && { label: t('library.stopOffline'), onSelect: () => act(async () => post('library/pin', await asViewer(t, { path: show.path, pinned: false }))) },
|
|
185
|
+
days > 0 && show.deletesAt === null && { label: fill(t('library.startCountdown'), { n: days }), onSelect: () => act(async () => post('library/countdown', await asViewer(t, { path: show.path }))) },
|
|
186
|
+
show.source?.source && show.sourced && { label: t(show.follow ? 'follow.stop' : 'follow.follow'), onSelect: () => act(() => post('follow', { source: show.source.source, id: show.source.id, follow: !show.follow })) },
|
|
187
|
+
show.download?.status === 'failed' && { label: t('library.retry'), onSelect: () => act(() => post('library/retry', { path: show.path })) },
|
|
188
|
+
show.download?.active && { label: t('library.stopDownload'), onSelect: () => act(() => post('library/cancel', { path: show.path })) },
|
|
189
|
+
{ label: t('action.delete'), danger: true, onSelect: () => (show.inUse ? setError(t('library.inUseError')) : setMode('delete')) },
|
|
190
|
+
],
|
|
191
|
+
}))
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
return h('li', { className: 'vsync-show' },
|
|
195
|
+
h('div', { className: 'vsync-row' },
|
|
196
|
+
hasEpisodes(episodeCount(show))
|
|
197
|
+
? h('button', { type: 'button', className: 'vsync-expand', 'aria-expanded': String(open), 'aria-label': t('library.episodesToggle'), onClick: () => setOpen(!open) }, h(Icon.Chevron, { open }))
|
|
198
|
+
: h('span', { className: 'vsync-expand', 'aria-hidden': true }),
|
|
199
|
+
h(Poster, { show }),
|
|
200
|
+
h('span', { className: 'vsync-rowText' },
|
|
201
|
+
mode === 'rename'
|
|
202
|
+
? h('form', {
|
|
203
|
+
id: `vsync-rename-${show.path}`,
|
|
204
|
+
onSubmit: async (event) => {
|
|
205
|
+
event.preventDefault()
|
|
206
|
+
const answer = await act(async () => post('library/rename', await asViewer(t, { path: show.path, name })))
|
|
207
|
+
if (!answer.error) setMode('idle')
|
|
208
|
+
},
|
|
209
|
+
}, h('input', { className: 'vsync-field vsync-inline', value: name, autoFocus: true, 'aria-label': t('library.rename'), onChange: event => setName(event.target.value), onKeyDown: (event) => { if (event.key === 'Escape') { setMode('idle'); setName(show.name) } } }))
|
|
210
|
+
: h('button', { type: 'button', className: 'vsync-rowTitle vsync-titleButton', onClick: () => setOpen(!open) }, show.kind === 'file' ? titleOf(show.name) : show.name),
|
|
211
|
+
h('span', { className: 'vsync-rowMeta vsync-rowLead' }, lead.map((part, n) => h('span', { key: n }, part))),
|
|
212
|
+
h('span', { className: 'vsync-rowMeta vsync-rowMuted' },
|
|
213
|
+
rest.map((part, n) => h('span', { key: n }, part)),
|
|
214
|
+
show.deletesAt !== null && h('span', { key: 'countdown' }, h(Countdown, { t, show, days, onChanged }))),
|
|
215
|
+
show.work?.tags?.length > 0 && h('span', { className: 'vsync-tags' }, show.work.tags.slice(0, 5).map(tag => h('span', { key: tag, className: 'vsync-tag' }, tag))),
|
|
216
|
+
show.work && h('button', { type: 'button', className: 'vsync-metaButton vsync-detailsToggle', 'aria-expanded': String(details), onClick: () => setDetails(!details) }, h(Icon.Chevron, { size: 12, open: details }), ' ', t(details ? 'search.hideDetails' : 'search.details')),
|
|
217
|
+
error && h('span', { className: 'vsync-error', role: 'alert' }, error)),
|
|
218
|
+
actions),
|
|
219
|
+
details && show.work && h('div', { className: 'vsync-showDetails' }, h(WorkDetails, { t, info: show.work, source: show.source.source, onFocus: work => onOpenWork(show.source.source, work) })),
|
|
220
|
+
open && h(Episodes, { t, show, onStart: start, onChanged }))
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
export function LibrarySection({ t, library, onOpen, onChanged, onOpenWork }) {
|
|
224
|
+
const shows = library?.shows ?? []
|
|
225
|
+
return h('section', { className: 'vsync-section', 'aria-labelledby': 'vsync-library' },
|
|
226
|
+
h('div', { className: 'vsync-sectionHead' },
|
|
227
|
+
h('h2', { id: 'vsync-library' }, t('start.library')),
|
|
228
|
+
library && h('span', { className: 'vsync-sectionMeta' }, [
|
|
229
|
+
shows.length > 0 && bytes(t, library.total),
|
|
230
|
+
library.free !== null && fill(t('library.free'), { size: bytes(t, library.free) }),
|
|
231
|
+
].filter(Boolean).join(' · '))),
|
|
232
|
+
library && shows.length === 0 && h('p', { className: 'vsync-empty' }, t('start.libraryEmpty')),
|
|
233
|
+
h('ul', { className: 'vsync-rows' }, shows.map(show => h(ShowRow, { key: show.path, t, show, days: library.autoDeleteDays, onOpen, onChanged, onOpenWork }))))
|
|
234
|
+
}
|