@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
package/web/avatars.js
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Who is watching, as overlapping round avatars (the first few, then +N):
|
|
3
|
+
* the person's picture when the sign-in plugin has one for this browser's
|
|
4
|
+
* user, else initials on a colour taken from the name. Pressing the stack
|
|
5
|
+
* opens a small list with each person's name and state.
|
|
6
|
+
*/
|
|
7
|
+
import { h, profile, React } from './ui.js'
|
|
8
|
+
|
|
9
|
+
const SHOWN = 3
|
|
10
|
+
|
|
11
|
+
const hueOf = name => [...name].reduce((sum, char) => (sum * 31 + char.codePointAt(0)) % 360, 7)
|
|
12
|
+
const initialsOf = name => [...name.trim()].slice(0, 2).join('').toUpperCase() || '?'
|
|
13
|
+
|
|
14
|
+
function Avatar({ name, picture }) {
|
|
15
|
+
if (picture) return h('img', { className: 'vsync-avatar', src: picture, alt: '', title: name, referrerPolicy: 'no-referrer' })
|
|
16
|
+
return h('span', { className: 'vsync-avatar', title: name, style: { background: `hsl(${hueOf(name)} 45% 42%)` } }, initialsOf(name))
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* @param {{ key?: string, name: string, state?: string | null, self?: boolean }[]} members
|
|
21
|
+
*/
|
|
22
|
+
export function AvatarStack({ t, members }) {
|
|
23
|
+
const [open, setOpen] = React.useState(false)
|
|
24
|
+
const [mine, setMine] = React.useState(null)
|
|
25
|
+
const root = React.useRef(null)
|
|
26
|
+
React.useEffect(() => { profile().then(setMine) }, [])
|
|
27
|
+
React.useEffect(() => {
|
|
28
|
+
if (!open) return undefined
|
|
29
|
+
const away = (event) => { if (!root.current?.contains(event.target)) setOpen(false) }
|
|
30
|
+
const key = (event) => { if (event.key === 'Escape') setOpen(false) }
|
|
31
|
+
document.addEventListener('pointerdown', away)
|
|
32
|
+
document.addEventListener('keydown', key)
|
|
33
|
+
return () => { document.removeEventListener('pointerdown', away); document.removeEventListener('keydown', key) }
|
|
34
|
+
}, [open])
|
|
35
|
+
if (members.length === 0) return null
|
|
36
|
+
const pictureOf = member => (member.self && mine?.picture ? mine.picture : null)
|
|
37
|
+
const label = members.map(member => member.name).join(', ')
|
|
38
|
+
return h('span', { className: 'vsync-avatarWrap', ref: root },
|
|
39
|
+
h('button', { type: 'button', className: 'vsync-avatarStack', 'aria-label': label, title: label, 'aria-haspopup': 'true', 'aria-expanded': String(open), onClick: () => setOpen(!open) },
|
|
40
|
+
members.slice(0, SHOWN).map((member, n) => h(Avatar, { key: member.key ?? `${member.name}-${n}`, name: member.name, picture: pictureOf(member) })),
|
|
41
|
+
members.length > SHOWN && h('span', { className: 'vsync-avatar vsync-avatarMore' }, `+${members.length - SHOWN}`)),
|
|
42
|
+
open && h('ul', { className: 'vsync-avatarMenu', role: 'list' }, members.map((member, n) => h('li', { key: member.key ?? `${member.name}-${n}`, className: 'vsync-avatarItem' },
|
|
43
|
+
h(Avatar, { name: member.name, picture: pictureOf(member) }),
|
|
44
|
+
h('span', { className: 'vsync-avatarName' }, member.self ? `${member.name} (${t('room.you')})` : member.name),
|
|
45
|
+
member.state && h('span', { className: 'vsync-avatarState' }, member.state)))))
|
|
46
|
+
}
|
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Books: the filter row of a book search, a book result as a card (pick a
|
|
3
|
+
* format, download it onto your shelf, then save it to this device), and My
|
|
4
|
+
* books, the signed-in person's shelf in the Library tab.
|
|
5
|
+
*
|
|
6
|
+
* Downloads go through the shelf (src/shelf-routes.js, under DSH's /api,
|
|
7
|
+
* where the person is known): the Host fetches the file through its source,
|
|
8
|
+
* converts it when the format asked for is not one the source has, and keeps
|
|
9
|
+
* it on that person's shelf. Saving to the device is a plain link the person
|
|
10
|
+
* taps, so phones treat it as a download they started (Safari offers Apple
|
|
11
|
+
* Books for an epub).
|
|
12
|
+
*/
|
|
13
|
+
import { Cover } from './details.js'
|
|
14
|
+
import { offeredFormats } from './formats.js'
|
|
15
|
+
import { Confirm, Icon, Spinner, bytes, canChat, chatAbout, fill, h, languageName, React, shelf, shelfUrl } from './ui.js'
|
|
16
|
+
|
|
17
|
+
const SHELF_POLL_MS = 2000
|
|
18
|
+
|
|
19
|
+
export const FORMATS = ['', 'epub', 'pdf', 'txt', 'azw3', 'mobi']
|
|
20
|
+
export const SIZES = ['', '5', '20', '50', '200']
|
|
21
|
+
|
|
22
|
+
/** What the shelf says about itself (conversion on or off, use of the disk), shared by every card on the page. */
|
|
23
|
+
let shelfInfo = null
|
|
24
|
+
const loadShelf = () => {
|
|
25
|
+
shelfInfo ??= shelf().then(answer => (answer.error ? (shelfInfo = null, { entries: [], convert: false, error: answer.error }) : answer))
|
|
26
|
+
return shelfInfo
|
|
27
|
+
}
|
|
28
|
+
const forgetShelf = () => { shelfInfo = null }
|
|
29
|
+
|
|
30
|
+
/** The filter row of a book search; `languages` are the codes to offer (src/i18n.js `languageChoices`), `onChange` gets the whole set. */
|
|
31
|
+
export function BookFilters({ t, filters, languages, onChange }) {
|
|
32
|
+
const [open, setOpen] = React.useState(false)
|
|
33
|
+
const active = Object.values(filters).filter(value => value !== '').length
|
|
34
|
+
const set = patch => onChange({ ...filters, ...patch })
|
|
35
|
+
const select = (label, value, options, render, key) => h('label', { className: 'vsync-filter' },
|
|
36
|
+
h('span', null, label),
|
|
37
|
+
h('select', { className: 'vsync-field', value, onChange: event => set({ [key]: event.target.value }) },
|
|
38
|
+
options.map(option => h('option', { key: option, value: option }, option === '' ? t('filter.any') : render(option)))))
|
|
39
|
+
const year = (label, key) => h('label', { className: 'vsync-filter' },
|
|
40
|
+
h('span', null, label),
|
|
41
|
+
h('input', { className: 'vsync-field vsync-year', type: 'number', inputMode: 'numeric', min: 1, max: 9999, placeholder: '—', value: filters[key], onChange: event => set({ [key]: event.target.value }) }))
|
|
42
|
+
return h('div', { className: 'vsync-filterBox', 'data-open': String(open) },
|
|
43
|
+
// On phones the filters fold behind this button; wider screens always show them.
|
|
44
|
+
h('button', { type: 'button', className: 'vsync-btn vsync-filterToggle', 'aria-expanded': String(open), onClick: () => setOpen(!open) },
|
|
45
|
+
h(Icon.Chevron, { size: 12, open }), active > 0 ? fill(t('filter.labelActive'), { n: active }) : t('filter.label')),
|
|
46
|
+
h('div', { className: 'vsync-filters', role: 'group', 'aria-label': t('filter.label') },
|
|
47
|
+
select(t('filter.language'), filters.language, ['', ...languages], code => languageName(t, code), 'language'),
|
|
48
|
+
select(t('filter.format'), filters.format, FORMATS, format => format.toUpperCase(), 'format'),
|
|
49
|
+
year(t('filter.yearFrom'), 'yearFrom'),
|
|
50
|
+
year(t('filter.yearTo'), 'yearTo'),
|
|
51
|
+
select(t('filter.size'), filters.maxMb, SIZES, mb => fill(t('filter.upTo'), { size: bytes(t, Number(mb) * 1e6) }), 'maxMb')))
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/** The facts of a book in a row: author, year, language, publisher, where the file comes from. */
|
|
55
|
+
function bookFacts(t, book, sourceLabel) {
|
|
56
|
+
return [book.author, book.year, languageName(t, book.language), book.publisher, book.origin || sourceLabel].filter(Boolean)
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export function startChat(t, book, sourceLabel) {
|
|
60
|
+
chatAbout({ title: book.title, lines: [bookFacts(t, book, sourceLabel).join(' · '), book.blurb], context: { source: book.source, id: book.id, kind: book.kind } })
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/** "Ask": a new conversation about the item. On phones only its icon shows; its name stays for screen readers. */
|
|
64
|
+
export function ChatButton({ t, onClick }) {
|
|
65
|
+
return canChat() && h('button', { type: 'button', className: 'vsync-btn vsync-ask', title: t('chat.about'), 'aria-label': t('chat.about'), onClick }, h(Icon.Chat, { size: 14 }), h('span', { className: 'vsync-askLabel' }, t('chat.ask')))
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/** Waits until the shelf entry is no longer being fetched. */
|
|
69
|
+
function useEntry(entryId) {
|
|
70
|
+
const [entry, setEntry] = React.useState(null)
|
|
71
|
+
React.useEffect(() => {
|
|
72
|
+
if (entryId === null) return undefined
|
|
73
|
+
let live = true
|
|
74
|
+
let timer
|
|
75
|
+
const tick = async () => {
|
|
76
|
+
const answer = await shelf()
|
|
77
|
+
if (!live) return
|
|
78
|
+
const found = answer.entries?.find(one => one.entry === entryId) ?? null
|
|
79
|
+
setEntry(found)
|
|
80
|
+
if (found?.state === 'fetching') timer = setTimeout(tick, SHELF_POLL_MS)
|
|
81
|
+
else forgetShelf()
|
|
82
|
+
}
|
|
83
|
+
tick()
|
|
84
|
+
return () => { live = false; clearTimeout(timer) }
|
|
85
|
+
}, [entryId])
|
|
86
|
+
return entry
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
export const fileLink = entry => `${shelfUrl('/file').href}?entry=${encodeURIComponent(entry.entry)}`
|
|
90
|
+
|
|
91
|
+
export function SaveLink({ t, entry, primary = true }) {
|
|
92
|
+
return h('a', { className: `vsync-btn${primary ? ' vsync-primary' : ''}`, href: fileLink(entry), download: entry.name ?? '' }, h(Icon.Download, { size: 14 }), fill(t('book.save'), { format: entry.format.toUpperCase() }))
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/** One book search result: facts, the formats it comes in, and a download in the one picked. */
|
|
96
|
+
export function BookCard({ t, source, result }) {
|
|
97
|
+
const [convert, setConvert] = React.useState(false)
|
|
98
|
+
React.useEffect(() => { loadShelf().then(info => setConvert(Boolean(info.convert))) }, [])
|
|
99
|
+
const offered = offeredFormats(result.formats, convert)
|
|
100
|
+
const [format, setFormat] = React.useState(null)
|
|
101
|
+
const chosen = format ?? offered[0]?.format ?? 'epub'
|
|
102
|
+
const [request, setRequest] = React.useState({ step: 'idle' })
|
|
103
|
+
const entry = useEntry(request.entry ?? null)
|
|
104
|
+
|
|
105
|
+
const download = async () => {
|
|
106
|
+
setRequest({ step: 'adding' })
|
|
107
|
+
const answer = await shelf('/add', { source: source.id, id: result.id, format: chosen, title: result.title, author: result.author })
|
|
108
|
+
if (answer.error) return setRequest({ step: 'error', message: answer.error })
|
|
109
|
+
return setRequest({ step: 'added', entry: answer.entry.entry })
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
let status = null
|
|
113
|
+
if (request.step === 'adding' || (request.step === 'added' && (entry === null || entry.state === 'fetching'))) {
|
|
114
|
+
status = h('span', { className: 'vsync-pending', role: 'status' }, h(Spinner), t(offered.find(one => one.format === chosen)?.converted ? 'book.converting' : 'book.fetching'))
|
|
115
|
+
} else if (entry?.state === 'ready') {
|
|
116
|
+
status = h(React.Fragment, null, h('span', { className: 'vsync-badge vsync-badgeHere' }, t('book.onShelf')), h(SaveLink, { t, entry }))
|
|
117
|
+
} else if (entry?.state === 'failed' || request.step === 'error') {
|
|
118
|
+
status = h('span', { className: 'vsync-error', role: 'alert' }, entry?.error ?? request.message)
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
const formatLabel = one => `${one.format.toUpperCase()}${one.bytes ? ` · ${bytes(t, one.bytes)}` : ''}${one.converted ? ` · ${t('book.converted')}` : ''}`
|
|
122
|
+
return h('li', { className: 'vsync-card vsync-book' },
|
|
123
|
+
h(Cover, { result: { ...result, media: 'book' } }),
|
|
124
|
+
h('div', { className: 'vsync-cardBody' },
|
|
125
|
+
h('div', { className: 'vsync-cardHead' }, h('span', { className: 'vsync-rowTitle' }, result.title)),
|
|
126
|
+
h('span', { className: 'vsync-rowMeta' }, bookFacts(t, result, source.label).map((part, n) => h('span', { key: n }, part))),
|
|
127
|
+
offered.length > 0 && h('span', { className: 'vsync-tags' }, offered.filter(one => !one.converted).map(one => h('span', { key: one.format, className: 'vsync-tag' }, formatLabel(one)))),
|
|
128
|
+
result.blurb && h('span', { className: 'vsync-blurb' }, result.blurb),
|
|
129
|
+
h('div', { className: 'vsync-cardFoot' },
|
|
130
|
+
h('span', { className: 'vsync-actions' },
|
|
131
|
+
h(ChatButton, { t, onClick: () => startChat(t, { ...result, source: source.id }, source.label) }),
|
|
132
|
+
status,
|
|
133
|
+
entry?.state !== 'ready' && request.step !== 'adding' && !(request.step === 'added' && entry?.state !== 'failed') && h(React.Fragment, null,
|
|
134
|
+
offered.length > 1 && h('select', { className: 'vsync-field vsync-pick', 'aria-label': t('book.format'), value: chosen, onChange: event => setFormat(event.target.value) },
|
|
135
|
+
offered.map(one => h('option', { key: one.format, value: one.format }, formatLabel(one)))),
|
|
136
|
+
h('button', { type: 'button', className: 'vsync-btn vsync-primary', onClick: download }, h(Icon.Download, { size: 14 }), offered.length > 1 ? t('book.download') : fill(t('book.downloadAs'), { format: chosen.toUpperCase() })))))))
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/** My books: what the signed-in person downloaded, to save again, fetch again or delete. */
|
|
140
|
+
export function ShelfSection({ t }) {
|
|
141
|
+
const [state, setState] = React.useState(null)
|
|
142
|
+
const [confirm, setConfirm] = React.useState(null)
|
|
143
|
+
const load = React.useCallback(async () => {
|
|
144
|
+
const answer = await shelf()
|
|
145
|
+
setState(answer)
|
|
146
|
+
return answer
|
|
147
|
+
}, [])
|
|
148
|
+
React.useEffect(() => {
|
|
149
|
+
let live = true
|
|
150
|
+
let timer
|
|
151
|
+
const tick = async () => {
|
|
152
|
+
const answer = await load()
|
|
153
|
+
if (live && answer.entries?.some(entry => entry.state === 'fetching')) timer = setTimeout(tick, SHELF_POLL_MS)
|
|
154
|
+
}
|
|
155
|
+
tick()
|
|
156
|
+
return () => { live = false; clearTimeout(timer) }
|
|
157
|
+
}, [load])
|
|
158
|
+
|
|
159
|
+
const act = async (path, entry) => {
|
|
160
|
+
setConfirm(null)
|
|
161
|
+
await shelf(path, { entry: entry.entry })
|
|
162
|
+
forgetShelf()
|
|
163
|
+
const answer = await load()
|
|
164
|
+
if (answer.entries?.some(one => one.state === 'fetching')) setTimeout(load, SHELF_POLL_MS)
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
if (state === null) return null
|
|
168
|
+
if (state.error) return h('section', { className: 'vsync-section' }, h('h2', null, t('shelf.title')), h('p', { className: 'vsync-error', role: 'alert' }, state.error))
|
|
169
|
+
return h('section', { className: 'vsync-section vsync-shelf' },
|
|
170
|
+
h('div', { className: 'vsync-sectionHead' },
|
|
171
|
+
h('h2', null, t('shelf.title')),
|
|
172
|
+
h('span', { className: 'vsync-note' }, state.capBytes > 0 ? fill(t('shelf.usedOf'), { used: bytes(t, state.usedBytes), cap: bytes(t, state.capBytes) }) : bytes(t, state.usedBytes))),
|
|
173
|
+
state.entries.length === 0
|
|
174
|
+
? h('p', { className: 'vsync-empty' }, t('shelf.empty'))
|
|
175
|
+
: h('ul', { className: 'vsync-rows' }, state.entries.map(entry => h('li', { key: entry.entry, className: 'vsync-card vsync-book vsync-shelfEntry' },
|
|
176
|
+
h(Cover, { result: { ...entry, media: 'book' }, small: true }),
|
|
177
|
+
h('div', { className: 'vsync-cardBody' },
|
|
178
|
+
h('div', { className: 'vsync-cardHead' }, h('span', { className: 'vsync-rowTitle' }, entry.title || entry.id)),
|
|
179
|
+
h('span', { className: 'vsync-rowMeta' }, [entry.author, entry.format.toUpperCase(), entry.bytes ? bytes(t, entry.bytes) : null, entry.converted ? t('book.converted') : null].filter(Boolean).map((part, n) => h('span', { key: n }, part))),
|
|
180
|
+
entry.state === 'failed' && h('span', { className: 'vsync-error', role: 'alert' }, entry.error),
|
|
181
|
+
entry.state === 'removed' && h('span', { className: 'vsync-note' }, t('shelf.removed')),
|
|
182
|
+
h('div', { className: 'vsync-cardFoot' },
|
|
183
|
+
h('span', { className: 'vsync-actions' },
|
|
184
|
+
h(ChatButton, { t, onClick: () => startChat(t, entry, entry.source) }),
|
|
185
|
+
confirm === entry.entry
|
|
186
|
+
? h(Confirm, { t, danger: true, question: t('shelf.deleteQuestion'), confirmLabel: t('shelf.delete'), onCancel: () => setConfirm(null), onConfirm: () => act('/remove', entry) })
|
|
187
|
+
: h(React.Fragment, null,
|
|
188
|
+
h('button', { type: 'button', className: 'vsync-btn', onClick: () => setConfirm(entry.entry) }, t('shelf.delete')),
|
|
189
|
+
entry.state === 'fetching' && h('span', { className: 'vsync-pending', role: 'status' }, h(Spinner), t('book.fetching')),
|
|
190
|
+
(entry.state === 'failed' || entry.state === 'removed') && h('button', { type: 'button', className: 'vsync-btn vsync-primary', onClick: () => act('/retry', entry) }, t('shelf.again')),
|
|
191
|
+
entry.state === 'ready' && h(SaveLink, { t, entry })))))))))
|
|
192
|
+
}
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The Continue card at the top of Watching: the show someone is in now, or
|
|
3
|
+
* the one watched last, over its blurred cover. Who is in the room, the
|
|
4
|
+
* title, where it stands, an episode strip, joining or continuing alone,
|
|
5
|
+
* watching together, the link, asking about it, a recap without spoilers
|
|
6
|
+
* (when the `recap` part is on and one was written) and suggested next steps.
|
|
7
|
+
*/
|
|
8
|
+
import { AvatarStack } from './avatars.js'
|
|
9
|
+
import { Picture } from './details.js'
|
|
10
|
+
import { currentEpisode, episodeStrip, stripCounts } from './continue.js'
|
|
11
|
+
import { hasEpisodes, resumeLabel } from './format.js'
|
|
12
|
+
import { Btn, Icon, K, P, api, asPerson, base, canChat, chatAbout, clock, copyText, displayName, fill, h, nameOf, plural, post, React, roomLink } from './ui.js'
|
|
13
|
+
|
|
14
|
+
const names = (t, list) => new Intl.ListFormat(t.language, { type: 'conjunction' }).format(list)
|
|
15
|
+
const picture = key => (key ? new URL(key, base()).href : undefined)
|
|
16
|
+
|
|
17
|
+
/** The show's episodes from the library, for the strip; refreshed when where it stands changes. */
|
|
18
|
+
function useEpisodes(path, version) {
|
|
19
|
+
const [episodes, setEpisodes] = React.useState(null)
|
|
20
|
+
React.useEffect(() => {
|
|
21
|
+
if (!path) return undefined
|
|
22
|
+
let live = true
|
|
23
|
+
api(`library/episodes?path=${encodeURIComponent(path)}`).then((answer) => { if (live && Array.isArray(answer.episodes)) setEpisodes(answer.episodes) })
|
|
24
|
+
return () => { live = false }
|
|
25
|
+
}, [path, version])
|
|
26
|
+
return path ? episodes : null
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function Strip({ t, episodes, current }) {
|
|
30
|
+
if (!episodes || episodes.length < 2) return null
|
|
31
|
+
const counts = stripCounts(episodes, current)
|
|
32
|
+
const note = [counts.watched > 0 && plural(t, 'continue.watched', counts.watched), counts.ahead > 0 && plural(t, 'continue.onDisk', counts.ahead)].filter(Boolean).join(' · ')
|
|
33
|
+
const items = episodeStrip(episodes, current).map(item => ({ state: item.state, title: fill(t('continue.episode'), { n: item.n }) }))
|
|
34
|
+
return h('div', { className: 'vsync-ccStrip' }, h(K.Segments, { items, end: note || undefined, label: note || fill(t('continue.episode'), { n: current }) }))
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export function ContinueCard({ t, subject, presence, sources, onOpen, onChanged }) {
|
|
38
|
+
const { room, show } = subject
|
|
39
|
+
const work = show?.work ?? null
|
|
40
|
+
const current = currentEpisode(subject)
|
|
41
|
+
const total = show?.total ?? (show?.sourced ? null : show?.episodes) ?? room?.episodes ?? null
|
|
42
|
+
const episodes = useEpisodes(show?.path, `${current}:${show?.finished}:${show?.episodes}`)
|
|
43
|
+
const [busy, setBusy] = React.useState(false)
|
|
44
|
+
const [error, setError] = React.useState(null)
|
|
45
|
+
const [copied, setCopied] = React.useState(false)
|
|
46
|
+
|
|
47
|
+
const title = work?.title ?? room?.title ?? show?.name ?? ''
|
|
48
|
+
const sourceId = show?.source?.source ?? null
|
|
49
|
+
const source = sourceId === null ? '' : nameOf(t, `source.name.${sourceId}`, sources.find(entry => entry.id === sourceId)?.label ?? '')
|
|
50
|
+
const meta = [
|
|
51
|
+
hasEpisodes(total) ? fill(t('rooms.episode'), { n: current, total }) : null,
|
|
52
|
+
work?.tags?.[0] ?? null,
|
|
53
|
+
Number.isFinite(work?.score) ? fill(t('continue.score'), { score: work.score }) : null,
|
|
54
|
+
source ? fill(t('continue.from'), { source }) : null,
|
|
55
|
+
].filter(Boolean)
|
|
56
|
+
const members = room?.members ?? []
|
|
57
|
+
const position = room ? room.position : show?.resume?.position ?? null
|
|
58
|
+
const cover = work?.cover ?? room?.cover ?? show?.frame ?? room?.frame ?? null
|
|
59
|
+
|
|
60
|
+
const run = async (action) => {
|
|
61
|
+
setBusy(true)
|
|
62
|
+
setError(null)
|
|
63
|
+
const failure = await action()
|
|
64
|
+
setBusy(false)
|
|
65
|
+
if (failure) setError(failure)
|
|
66
|
+
}
|
|
67
|
+
/** A room for this show: the shared one when there is one, else a new one for this person alone. */
|
|
68
|
+
const openRoom = async () => {
|
|
69
|
+
if (room) return room.id
|
|
70
|
+
const answer = await asPerson('rooms', { source: 'local', path: show.path, by: await displayName(t('guest')) })
|
|
71
|
+
if (answer.error) throw new Error(answer.error)
|
|
72
|
+
return answer.room.id
|
|
73
|
+
}
|
|
74
|
+
const go = () => run(async () => {
|
|
75
|
+
try { onOpen(await openRoom()) } catch (failure) { return failure.message }
|
|
76
|
+
return null
|
|
77
|
+
})
|
|
78
|
+
const together = () => run(async () => {
|
|
79
|
+
try {
|
|
80
|
+
const id = await openRoom()
|
|
81
|
+
const answer = await post(`rooms/${encodeURIComponent(id)}/together`, {})
|
|
82
|
+
if (answer.error) return answer.error
|
|
83
|
+
await copyText(roomLink(id), t('room.copy'))
|
|
84
|
+
onChanged()
|
|
85
|
+
onOpen(id)
|
|
86
|
+
} catch (failure) { return failure.message }
|
|
87
|
+
return null
|
|
88
|
+
})
|
|
89
|
+
const copy = async () => {
|
|
90
|
+
if (await copyText(roomLink(room.id), t('room.copy'))) {
|
|
91
|
+
setCopied(true)
|
|
92
|
+
setTimeout(() => setCopied(false), 2000)
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
const about = { title, lines: [meta.join(' · '), work?.blurb ?? ''], context: { show: show?.path ?? null, room: room?.id ?? null, episode: current } }
|
|
96
|
+
const recap = presence.recap ? show?.recap ?? null : null
|
|
97
|
+
const started = show?.stepsStarted ?? {}
|
|
98
|
+
const states = Object.fromEntries(Object.keys(started).map(id => [id, 'done']))
|
|
99
|
+
|
|
100
|
+
return h(K.Surface, { as: 'section', backdrop: picture(cover), className: 'vsync-cc', 'aria-label': title },
|
|
101
|
+
h(Picture, { pictures: [cover], className: 'vsync-ccPoster', emptyClassName: 'vsync-ccPoster vsync-ccPosterEmpty', icon: h(Icon.Film, { size: 28 }) }),
|
|
102
|
+
h('div', { className: 'vsync-ccBody' },
|
|
103
|
+
members.length > 0 && h('p', { className: 'vsync-ccLive' },
|
|
104
|
+
h(P.StateDot, { state: 'done', size: 8 }),
|
|
105
|
+
h('span', null, fill(t(members.length === 1 ? 'continue.inRoomOne' : 'continue.inRoomMany'), { names: names(t, members) })),
|
|
106
|
+
h(AvatarStack, { t, members: members.map(name => ({ name })) })),
|
|
107
|
+
h('h2', { className: 'vsync-ccTitle' }, title),
|
|
108
|
+
meta.length > 0 && h('p', { className: 'vsync-ccMeta' }, meta.join(' · ')),
|
|
109
|
+
h(Strip, { t, episodes, current }),
|
|
110
|
+
recap && h(K.Surface, { className: 'vsync-ccRecap' },
|
|
111
|
+
h('p', { className: 'vsync-ccRecapHead' }, h(K.Icon, { name: 'book-open', size: 14 }), t('continue.recap')),
|
|
112
|
+
h('p', { className: 'vsync-ccRecapText' }, recap.text)),
|
|
113
|
+
h('div', { className: 'vsync-ccActions' },
|
|
114
|
+
h(Btn, { variant: 'primary', disabled: busy || (!room && !show), onClick: go, icon: h(P.IconPlayOutlineRegular, { size: 16 }) },
|
|
115
|
+
resumeLabel(t, { count: total, n: current, position, at: clock(position), names: members.length > 0 ? names(t, members) : '' })),
|
|
116
|
+
room
|
|
117
|
+
? h(Btn, { onClick: copy, icon: h(P.IconLinkOutlineRegular, { size: 16 }) }, t(copied ? 'room.copied' : 'room.copy'))
|
|
118
|
+
: show && h(Btn, { disabled: busy, onClick: together, icon: h(P.IconUsersOutlineRegular, { size: 16 }) }, t('room.watchTogether')),
|
|
119
|
+
canChat() && h(Btn, { onClick: () => chatAbout(about), icon: h(P.IconNewChatOutlineRegular, { size: 16 }) }, t('chat.ask'))),
|
|
120
|
+
error && h('p', { className: 'vsync-error', role: 'alert' }, error),
|
|
121
|
+
show?.steps && h(K.Suggested, {
|
|
122
|
+
actions: show.steps,
|
|
123
|
+
about: { source: '@soffits/dsh-media-hub', ...about },
|
|
124
|
+
states,
|
|
125
|
+
onPress: async (action) => {
|
|
126
|
+
const answer = await asPerson('steps', { path: show.path, id: action.id })
|
|
127
|
+
onChanged()
|
|
128
|
+
if (answer.error) throw new Error(answer.error)
|
|
129
|
+
},
|
|
130
|
+
})))
|
|
131
|
+
}
|
package/web/continue.js
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
/** What the Watching tab offers to continue, and the Continue card's episode strip. Pure. */
|
|
2
|
+
|
|
3
|
+
/** Shows someone has started and not finished, most recently watched first. */
|
|
4
|
+
export function startedShows(shows) {
|
|
5
|
+
const started = shows.filter(show => !show.watchedAt && (show.episodes > 0 || show.sourced) && (show.resume || show.finished > 0))
|
|
6
|
+
// Array.prototype.sort is stable: shows without a time keep the library's order (newest added first).
|
|
7
|
+
return started.sort((a, b) => (b.lastWatchedAt ?? 0) - (a.lastWatchedAt ?? 0))
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
/** Shows someone has started and not finished, no room is playing, most recently watched first. */
|
|
11
|
+
export const continueWatching = shows => startedShows(shows).filter(show => !show.inUse)
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* What the Continue card is about: a shared room someone is in now (with the library show it plays, if any),
|
|
15
|
+
* else the show watched last (with its shared room, if one is open), else null.
|
|
16
|
+
* @param {object[] | undefined} rooms - shared rooms
|
|
17
|
+
* @param {object[]} shows - library shows
|
|
18
|
+
* @returns {{ room: object | null, show: object | null } | null}
|
|
19
|
+
*/
|
|
20
|
+
export function continueSubject(rooms, shows) {
|
|
21
|
+
const busy = (rooms ?? []).find(room => room.members.length > 0)
|
|
22
|
+
if (busy) return { room: busy, show: shows.find(show => show.path === busy.show) ?? null }
|
|
23
|
+
const [show] = startedShows(shows)
|
|
24
|
+
if (show === undefined) return null
|
|
25
|
+
return { room: (rooms ?? []).find(room => room.show === show.path) ?? null, show }
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/** The episode (1-based) the card offers: where the room is, else where the show's viewers stopped, else the one after the last finished. */
|
|
29
|
+
export function currentEpisode({ room, show }) {
|
|
30
|
+
if (room) return room.index + 1
|
|
31
|
+
if (show?.resume) return show.resume.index + 1
|
|
32
|
+
const total = show?.total ?? show?.episodes ?? 1
|
|
33
|
+
return Math.min(Math.max(1, total), (show?.finished ?? 0) + 1)
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** How many segments the strip shows at most, and how many of them come before the current one. */
|
|
37
|
+
export const STRIP_MAX = 18
|
|
38
|
+
const STRIP_BEFORE = 6
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* The strip's segments around the current episode: watched (`done`), `current`, on disk (`ready`), or not yet (`todo`).
|
|
42
|
+
* @param {{ n: number, cached: boolean, watch: { finished: boolean } | null }[]} episodes - the show's episodes in order
|
|
43
|
+
* @param {number} current - 1-based
|
|
44
|
+
*/
|
|
45
|
+
export function episodeStrip(episodes, current, max = STRIP_MAX) {
|
|
46
|
+
const start = Math.max(0, Math.min(current - 1 - STRIP_BEFORE, episodes.length - max))
|
|
47
|
+
return episodes.slice(start, start + max).map(episode => ({
|
|
48
|
+
n: episode.n,
|
|
49
|
+
state: episode.n === current ? 'current' : episode.watch?.finished ? 'done' : episode.cached ? 'ready' : 'todo',
|
|
50
|
+
}))
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/** What the strip's note says: episodes watched, and how many after the current one are on disk. */
|
|
54
|
+
export function stripCounts(episodes, current) {
|
|
55
|
+
return {
|
|
56
|
+
watched: episodes.filter(episode => episode.watch?.finished).length,
|
|
57
|
+
ahead: episodes.filter(episode => episode.n > current && episode.cached).length,
|
|
58
|
+
}
|
|
59
|
+
}
|
package/web/details.js
ADDED
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What the page knows about a work, shown the same wherever a work is shown
|
|
3
|
+
* (a search result, a work opened from another's related list): the full
|
|
4
|
+
* blurb, every field the source gives, and the other seasons and linked
|
|
5
|
+
* works as a plain list. A related work is a row that opens it (onFocus),
|
|
6
|
+
* never a card with details of its own, so details never nest.
|
|
7
|
+
*/
|
|
8
|
+
import { hasEpisodes, metaLabel, popularityText } from './format.js'
|
|
9
|
+
import { useKnownWork } from './known.js'
|
|
10
|
+
import { Icon, Spinner, asPerson, base, clock, day, fill, h, plural, query, React } from './ui.js'
|
|
11
|
+
|
|
12
|
+
const INFO_CONCURRENCY = 3
|
|
13
|
+
|
|
14
|
+
/** Fetch work details a few at a time, so a page of results doesn't start ten downloads of metadata at once. */
|
|
15
|
+
function createInfoQueue() {
|
|
16
|
+
const cache = new Map()
|
|
17
|
+
const waiting = []
|
|
18
|
+
let running = 0
|
|
19
|
+
const pump = () => {
|
|
20
|
+
while (running < INFO_CONCURRENCY && waiting.length > 0) {
|
|
21
|
+
const { key, path, resolve } = waiting.shift()
|
|
22
|
+
running += 1
|
|
23
|
+
asPerson(path).then((info) => {
|
|
24
|
+
if (info.error) cache.delete(key)
|
|
25
|
+
resolve(info)
|
|
26
|
+
}).finally(() => { running -= 1; pump() })
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
/** `language` is the viewer's: a work's subtitles are chosen for it. */
|
|
30
|
+
return (source, id, language = '') => {
|
|
31
|
+
const key = `${source}/${id}/${language}`
|
|
32
|
+
if (!cache.has(key)) cache.set(key, new Promise(resolve => { waiting.push({ key, path: `info?${query({ source, id, language })}`, resolve }); pump() }))
|
|
33
|
+
return cache.get(key)
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
export const infoOf = createInfoQueue()
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* A picture from the host (`covers/<key>` routes), trying each in order: one
|
|
40
|
+
* that fails to load (it could not be made) gives way to the next, and a
|
|
41
|
+
* placeholder icon shows only when none loads.
|
|
42
|
+
*/
|
|
43
|
+
export function Picture({ pictures, className, emptyClassName, icon }) {
|
|
44
|
+
const candidates = pictures.filter(Boolean)
|
|
45
|
+
const [failed, setFailed] = React.useState(0)
|
|
46
|
+
const key = candidates.join('|')
|
|
47
|
+
React.useEffect(() => setFailed(0), [key])
|
|
48
|
+
const src = candidates[failed]
|
|
49
|
+
if (src !== undefined) return h('img', { key: src, className, src: new URL(src, base()).href, alt: '', loading: 'lazy', onError: () => setFailed(failed + 1) })
|
|
50
|
+
return h('span', { className: emptyClassName }, icon)
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/** A work's cover, or a quiet placeholder. */
|
|
54
|
+
export function Cover({ result, small = false }) {
|
|
55
|
+
const size = small ? ' vsync-coverSmall' : ''
|
|
56
|
+
const icon = result.media === 'book' ? Icon.Book : result.kind === 'audio' ? Icon.Audio : Icon.Film
|
|
57
|
+
return h(Picture, { pictures: [result.cover], className: `vsync-cover${size}`, emptyClassName: `vsync-cover vsync-coverEmpty${size}`, icon: h(icon, { size: 22 }) })
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/** What to show about a work at a glance: the fields in a row, skipping what is missing. */
|
|
61
|
+
export function glance(t, result) {
|
|
62
|
+
const status = { finished: 'search.finished', ongoing: 'search.ongoing', stopped: 'search.stopped' }[result.status]
|
|
63
|
+
return [
|
|
64
|
+
popularityText(t, result.popularity),
|
|
65
|
+
hasEpisodes(result.episodes) && plural(t, result.kind === 'audio' ? 'search.chapters' : 'search.episodes', result.episodes),
|
|
66
|
+
result.duration > 0 && clock(result.duration),
|
|
67
|
+
status && t(status),
|
|
68
|
+
result.author,
|
|
69
|
+
].filter(Boolean)
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
/** A related work: a small cover, title, how it relates, score and the glance; pressing it opens that work. */
|
|
74
|
+
function RelatedWork({ t, source, work, onFocus }) {
|
|
75
|
+
const known = useKnownWork(source, work.id)
|
|
76
|
+
return h('li', { className: 'vsync-related' },
|
|
77
|
+
h('button', { type: 'button', className: 'vsync-relatedButton', title: t('search.openWork'), onClick: () => onFocus(work) },
|
|
78
|
+
h(Cover, { result: work, small: true }),
|
|
79
|
+
h('span', { className: 'vsync-relatedText' },
|
|
80
|
+
h('span', { className: 'vsync-cardHead' },
|
|
81
|
+
h('span', { className: 'vsync-rowTitle' }, work.title),
|
|
82
|
+
work.relation && h('span', { className: 'vsync-badge' }, t(`relation.${work.relation}`)),
|
|
83
|
+
known.show !== null && h('span', { className: 'vsync-badge vsync-badgeHere' }, t(known.show.pinned ? 'search.offline' : 'search.inLibrary')),
|
|
84
|
+
work.score && h('span', { className: 'vsync-score' }, fill(t('search.score'), { score: work.score.toFixed(1) }))),
|
|
85
|
+
h('span', { className: 'vsync-rowMeta' }, glance(t, work).map((part, n) => h('span', { key: n }, part)))),
|
|
86
|
+
h(Icon.Chevron, { size: 14 })))
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/** `info` is the work's details (null while they load); onFocus opens a related work. */
|
|
90
|
+
export function WorkDetails({ t, info, source, onFocus }) {
|
|
91
|
+
if (info === null) return h('p', { className: 'vsync-note', role: 'status' }, h(Spinner), ' ', t('search.relatedLoading'))
|
|
92
|
+
const rows = info.meta ?? []
|
|
93
|
+
const related = info.related ?? []
|
|
94
|
+
return h('div', { className: 'vsync-details' },
|
|
95
|
+
info.offline && h('p', { className: 'vsync-note', role: 'status' }, fill(t('library.offlineCopy'), { date: info.at ? day(t, info.at) : '?' })),
|
|
96
|
+
info.blurb && h('p', { className: 'vsync-fullBlurb' }, info.blurb),
|
|
97
|
+
info.latest && h('p', { className: 'vsync-note' }, fill(t('search.latest'), { title: info.latest })),
|
|
98
|
+
rows.length > 0 && h('dl', { className: 'vsync-facts' }, rows.flatMap(row => [h('dt', { key: `${row.key}-k` }, metaLabel(t, row)), h('dd', { key: `${row.key}-v` }, row.value)])),
|
|
99
|
+
h('h3', { className: 'vsync-subheading' }, t('search.related')),
|
|
100
|
+
related.length === 0
|
|
101
|
+
? h('p', { className: 'vsync-note' }, t('search.noRelated'))
|
|
102
|
+
: h('ul', { className: 'vsync-relatedList' }, related.map(work => h(RelatedWork, { key: work.id, t, source, work, onFocus }))))
|
|
103
|
+
}
|