@soffits/dsh-media-hub 0.13.0 → 0.15.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/web/room-view.js CHANGED
@@ -4,9 +4,12 @@
4
4
  */
5
5
  import { About, nextSeason } from './about-view.js'
6
6
  import { AvatarStack } from './avatars.js'
7
+ import { Comments } from './comments-view.js'
8
+ import { DanmakuLayer, useDanmakuSettings } from './danmaku-layer.js'
7
9
  import { Cover } from './details.js'
8
10
  import { useKnownWork } from './known.js'
9
- import { Confirm, Icon, ProgressBar, React, api, asPersonPaying, base, captionText, clock, copyText, displayName, fill, h, post, progressFraction, progressText, roomLink, watchMark } from './ui.js'
11
+ import { PlayerBar } from './player-bar.js'
12
+ import { Changes, Confirm, Icon, ProgressBar, React, api, asPersonPaying, base, captionText, clock, copyText, displayName, fill, h, post, progressFraction, progressText, roomLink, watchMark } from './ui.js'
10
13
 
11
14
  const TOAST_MS = 4000
12
15
  /** A wait shorter than this (a seek into what is already loaded) shows nothing. */
@@ -43,8 +46,38 @@ function WaitPanel({ t, view, item, onSkip, onContinue }) {
43
46
  view.wait.canSkip && others.length > 0 && view.wait.held && h('button', { type: 'button', className: 'vsync-btn', onClick: onSkip }, fill(t('room.startWithout'), { names })))
44
47
  }
45
48
 
49
+ /** No danmaku yet: the same empty list each time, so the layer is not rebuilt. */
50
+ const NO_DANMAKU = Object.freeze([])
51
+
52
+ /**
53
+ * The player filling the screen with the danmaku and the bar on top: the browser's full screen for the player's box
54
+ * where it has one, else (iPhone, which only lets a bare <video> go full screen) the box covering the page.
55
+ */
56
+ function useFullScreen(box) {
57
+ const [covering, setCovering] = React.useState(false)
58
+ const [native, setNative] = React.useState(false)
59
+ React.useEffect(() => {
60
+ const changed = () => setNative(document.fullscreenElement === box.current && box.current !== null)
61
+ document.addEventListener('fullscreenchange', changed)
62
+ return () => document.removeEventListener('fullscreenchange', changed)
63
+ }, [])
64
+ const toggle = () => {
65
+ if (native) return void document.exitFullscreen()
66
+ if (covering) return setCovering(false)
67
+ if (typeof box.current?.requestFullscreen === 'function' && document.fullscreenEnabled) return void box.current.requestFullscreen().catch(() => setCovering(true))
68
+ return setCovering(true)
69
+ }
70
+ return { full: native || covering, covering, toggle }
71
+ }
72
+
46
73
  export function RoomView({ t, roomId, onLeave, onOpen }) {
47
74
  const stage = React.useRef(null)
75
+ const playerBox = React.useRef(null)
76
+ const { shows } = React.useContext(Changes)
77
+ const [danmakuSettings, setDanmakuSettings] = useDanmakuSettings()
78
+ const [danmaku, setDanmaku] = React.useState({ index: null, items: NO_DANMAKU })
79
+ const [qualityBusy, setQualityBusy] = React.useState(false)
80
+ const screen = useFullScreen(playerBox)
48
81
  const session = React.useRef(null)
49
82
  const [view, setView] = React.useState({ connection: 'connecting', room: null, members: [], needsGesture: false })
50
83
  const [toasts, setToasts] = React.useState([])
@@ -84,14 +117,34 @@ export function RoomView({ t, roomId, onLeave, onOpen }) {
84
117
  }
85
118
  }, [roomId])
86
119
 
120
+ // The show's notes change (another viewer picked a quality, the work was read again): read them again.
87
121
  React.useEffect(() => {
88
122
  let live = true
89
123
  api(`rooms/${encodeURIComponent(roomId)}/work`).then((answer) => { if (live && answer.work) setAbout(answer) })
90
124
  return () => { live = false }
91
- }, [roomId])
125
+ }, [roomId, shows])
92
126
 
93
127
  const room = view.room
94
128
  const item = room?.items[room.index]
129
+ const index = room?.index ?? null
130
+ const wantsDanmaku = about?.danmaku === true && danmakuSettings.on
131
+ React.useEffect(() => {
132
+ if (!wantsDanmaku || index === null) return undefined
133
+ let live = true
134
+ setDanmaku({ index, items: NO_DANMAKU })
135
+ api(`rooms/${encodeURIComponent(roomId)}/danmaku/${index}`).then((answer) => {
136
+ if (live && Array.isArray(answer.items)) setDanmaku({ index, items: answer.items })
137
+ })
138
+ return () => { live = false }
139
+ }, [roomId, index, wantsDanmaku])
140
+ const choices = about?.work?.qualities ?? []
141
+ const pickQuality = async (quality) => {
142
+ setQualityBusy(true)
143
+ const answer = await post(`rooms/${encodeURIComponent(roomId)}/quality`, { quality })
144
+ setQualityBusy(false)
145
+ if (answer.error) toast(answer.error)
146
+ else setAbout(current => ({ ...current, quality: answer.quality }))
147
+ }
95
148
  const copy = async () => {
96
149
  if (await copyText(roomLink(roomId), t('room.copy'))) {
97
150
  setCopied(true)
@@ -111,6 +164,8 @@ export function RoomView({ t, roomId, onLeave, onOpen }) {
111
164
  onLeave()
112
165
  }
113
166
 
167
+ const tabs = [room && room.items.length > 1 && 'episodes', about && 'about', about?.comments && 'comments'].filter(Boolean)
168
+ const shownSide = tabs.includes(side) ? side : tabs[0]
114
169
  const season = about ? nextSeason(about.work) : null
115
170
  const seasonKnown = useKnownWork(about?.source, season?.id)
116
171
  const watchSeason = async () => {
@@ -150,40 +205,52 @@ export function RoomView({ t, roomId, onLeave, onOpen }) {
150
205
  room?.together && h('button', { type: 'button', className: 'vsync-icon', 'aria-label': copied ? t('room.copied') : t('room.copy'), title: copied ? t('room.copied') : t('room.copy'), onClick: copy }, h(Icon.Link)),
151
206
  h('button', { type: 'button', className: 'vsync-icon', 'aria-label': t('room.close'), title: t('room.close'), onClick: () => setClosing(true) }, h(Icon.Close)))),
152
207
  h('div', { className: 'vsync-body' },
153
- h('div', { className: 'vsync-stageWrap' },
154
- h('div', { className: 'vsync-stage', ref: stage }),
155
- overlay && h('div', { className: 'vsync-overlay' }, overlay),
156
- h('div', { className: 'vsync-toasts', role: 'status', 'aria-live': 'polite' }, toasts.map(item => h('span', { key: item.id, className: 'vsync-toast' }, item.text)))),
157
- room && (room.items.length > 1 || about) && h('aside', { className: 'vsync-episodes', 'aria-label': t('room.episodes') },
158
- about && room.items.length > 1 && h('div', { className: 'vsync-sideTabs', role: 'tablist' },
159
- ['episodes', 'about'].map(tab => h('button', { key: tab, type: 'button', role: 'tab', className: 'vsync-chip', 'aria-selected': String(side === tab), onClick: () => setSide(tab) }, t(`room.tab.${tab}`)))),
160
- (side === 'about' || room.items.length === 1) && about
161
- ? h(About, { t, work: about.work, source: about.source, workId: about.id })
162
- : h(React.Fragment, null,
163
- !about && h('h2', null, t('room.episodes')),
164
- h('ol', { className: 'vsync-episodeList' }, room.items.map((episode, index) => {
165
- const mark = watchMark(t, episode.watch, index === room.index)
166
- const title = episode.title ?? fill(t('room.episodeN'), { n: index + 1 })
167
- return h('li', { key: index },
168
- h('button', {
169
- type: 'button',
170
- className: 'vsync-episode',
171
- 'aria-current': String(index === room.index),
172
- 'data-watch': mark.state,
173
- 'data-cached': String(episode.cached),
174
- title: episode.cached ? undefined : t('room.notCached'),
175
- disabled: episode.status === 'failed' && episode.cached,
176
- onClick: () => session.current?.select(index),
177
- },
178
- h('span', { className: 'vsync-episodeNo' }, index + 1),
179
- h('span', { className: 'vsync-episodeMain' },
180
- h('span', { className: 'vsync-episodeTitle' }, title),
181
- mark.bar),
182
- episode.status === 'preparing'
183
- ? h('span', { className: 'vsync-episodeState' }, progressFraction(episode.progress) === null ? '…' : `${Math.floor(progressFraction(episode.progress) * 100)}%`)
184
- : mark.note))
185
- })),
186
- season && h('div', { className: 'vsync-nextSeason', 'data-last': String(room.index === room.items.length - 1) },
187
- h('span', { className: 'vsync-nextSeasonText' }, fill(t('room.nextSeason'), { title: season.title })),
188
- h('button', { type: 'button', className: 'vsync-btn vsync-primary', disabled: seasonBusy, onClick: watchSeason }, seasonBusy ? t('search.opening') : seasonKnown.room !== null ? t('search.joinRoom') : seasonKnown.show !== null ? t(seasonKnown.show.resume ? 'search.continue' : 'search.open') : t('search.watch')))))))
208
+ h('div', { className: 'vsync-player', ref: playerBox, 'data-covering': String(screen.covering) },
209
+ h('div', { className: 'vsync-stageWrap' },
210
+ h('div', { className: 'vsync-stage', ref: stage }),
211
+ wantsDanmaku && danmaku.index === index && h(DanmakuLayer, { media: session.current?.media ?? null, items: danmaku.items, settings: danmakuSettings }),
212
+ overlay && h('div', { className: 'vsync-overlay' }, overlay),
213
+ h('div', { className: 'vsync-toasts', role: 'status', 'aria-live': 'polite' }, toasts.map(item => h('span', { key: item.id, className: 'vsync-toast' }, item.text)))),
214
+ room && h(PlayerBar, {
215
+ t,
216
+ settings: danmakuSettings,
217
+ onSettings: setDanmakuSettings,
218
+ danmaku: about?.danmaku === true,
219
+ count: danmaku.index === index && danmaku.items !== NO_DANMAKU ? danmaku.items.length : null,
220
+ quality: choices.length > 1 ? { value: choices.includes(about.quality) ? about.quality : choices[0], choices, onChange: pickQuality, busy: qualityBusy } : null,
221
+ full: screen.full,
222
+ onFull: screen.toggle,
223
+ })),
224
+ tabs.length > 0 && h('aside', { className: 'vsync-episodes', 'aria-label': t('room.episodes') },
225
+ tabs.length > 1 && h('div', { className: 'vsync-sideTabs', role: 'tablist' },
226
+ tabs.map(tab => h('button', { key: tab, type: 'button', role: 'tab', className: 'vsync-chip', 'aria-selected': String(shownSide === tab), onClick: () => setSide(tab) }, t(`room.tab.${tab}`)))),
227
+ shownSide === 'about' && h(About, { t, work: about.work, source: about.source, workId: about.id }),
228
+ shownSide === 'comments' && h(Comments, { t, roomId }),
229
+ shownSide === 'episodes' && h(React.Fragment, null,
230
+ !about && h('h2', null, t('room.episodes')),
231
+ h('ol', { className: 'vsync-episodeList' }, room.items.map((episode, index) => {
232
+ const mark = watchMark(t, episode.watch, index === room.index)
233
+ const title = episode.title ?? fill(t('room.episodeN'), { n: index + 1 })
234
+ return h('li', { key: index },
235
+ h('button', {
236
+ type: 'button',
237
+ className: 'vsync-episode',
238
+ 'aria-current': String(index === room.index),
239
+ 'data-watch': mark.state,
240
+ 'data-cached': String(episode.cached),
241
+ title: episode.cached ? undefined : t('room.notCached'),
242
+ disabled: episode.status === 'failed' && episode.cached,
243
+ onClick: () => session.current?.select(index),
244
+ },
245
+ h('span', { className: 'vsync-episodeNo' }, index + 1),
246
+ h('span', { className: 'vsync-episodeMain' },
247
+ h('span', { className: 'vsync-episodeTitle' }, title),
248
+ mark.bar),
249
+ episode.status === 'preparing'
250
+ ? h('span', { className: 'vsync-episodeState' }, progressFraction(episode.progress) === null ? '…' : `${Math.floor(progressFraction(episode.progress) * 100)}%`)
251
+ : mark.note))
252
+ })),
253
+ season && h('div', { className: 'vsync-nextSeason', 'data-last': String(room.index === room.items.length - 1) },
254
+ h('span', { className: 'vsync-nextSeasonText' }, fill(t('room.nextSeason'), { title: season.title })),
255
+ h('button', { type: 'button', className: 'vsync-btn vsync-primary', disabled: seasonBusy, onClick: watchSeason }, seasonBusy ? t('search.opening') : seasonKnown.room !== null ? t('search.joinRoom') : seasonKnown.show !== null ? t(seasonKnown.show.resume ? 'search.continue' : 'search.open') : t('search.watch')))))))
189
256
  }
package/web/ui.js CHANGED
@@ -70,21 +70,21 @@ export const post = (path, body) => api(path, { method: 'POST', body: JSON.strin
70
70
  const personUrl = path => new URL(`api/media-hub/${path}`, document.baseURI)
71
71
  export const asPerson = (path, body, init = {}) => request(personUrl(path), body === undefined ? init : { ...init, method: 'POST', body: JSON.stringify(body) })
72
72
 
73
- /** Where the page offers an extra the person's points pay for (page.js `PointsOffer`): resolves true to buy it. */
74
- let offer = null
75
- export function setOffer(value) { offer = value }
73
+ /** The points plugin's offer (ctx.points from @soffits/dsh-points, set by client.js; `available` is false without it). */
74
+ let points = null
75
+ export function setPoints(value) { points = value }
76
76
 
77
77
  /**
78
78
  * An action that may need a ticket (a new series, a binge night; src/points.js): when the hub answers `points`, the
79
- * page offers the purchase where the person is, and on yes asks again buying it. A no answers `{ declined: true }`.
79
+ * points plugin offers it where the person is, and after a yes the action is asked again. A no answers `{ declined: true }`.
80
80
  */
81
81
  export async function asPersonPaying(path, body) {
82
- const buy = []
82
+ const offered = new Set()
83
83
  for (;;) {
84
- const answer = await asPerson(path, buy.length === 0 ? body : { ...body, buy })
85
- if (answer.code !== 'points' || !answer.points || offer === null || buy.includes(answer.points.item)) return answer
86
- if (!(await offer(answer.points))) return { declined: true }
87
- buy.push(answer.points.item)
84
+ const answer = await asPerson(path, body)
85
+ if (answer.code !== 'points' || !answer.points || points?.available() !== true || offered.has(answer.points.item)) return answer
86
+ offered.add(answer.points.item)
87
+ if (!(await points.offer(answer.points).catch(() => false))) return { declined: true }
88
88
  }
89
89
  }
90
90
 
@@ -239,6 +239,10 @@ export const Icon = {
239
239
  Download: ({ size = 16 }) => svg(size, h('path', { d: 'M8 2.5v8M4.75 7.5 8 10.75l3.25-3.25M3 13.5h10', ...stroke })),
240
240
  Book: ({ size = 16 }) => svg(size, h('path', { d: 'M3 3.5A1.5 1.5 0 0 1 4.5 2H13v10H4.5A1.5 1.5 0 0 0 3 13.5Zm0 10A1.5 1.5 0 0 0 4.5 15H13v-3', ...stroke })),
241
241
  Check: ({ size = 16 }) => svg(size, h('path', { d: 'M3.5 8.5 6.5 11.5 12.5 4.5', ...stroke })),
242
+ Danmaku: ({ size = 16 }) => svg(size, h('rect', { x: 1.75, y: 3, width: 12.5, height: 10, rx: 2, ...stroke }), h('path', { d: 'M4.5 6.5h4M7 9.5h4.5', ...stroke })),
243
+ Settings: ({ size = 16 }) => svg(size, h('path', { d: 'M2.5 4.5h6M11.5 4.5h2M2.5 11.5h2M7.5 11.5h6', ...stroke }), h('circle', { cx: 10, cy: 4.5, r: 1.5, ...stroke }), h('circle', { cx: 6, cy: 11.5, r: 1.5, ...stroke })),
244
+ Expand: ({ size = 16 }) => svg(size, h('path', { d: 'M2.5 6V2.5H6M10 2.5h3.5V6M13.5 10v3.5H10M6 13.5H2.5V10', ...stroke })),
245
+ Shrink: ({ size = 16 }) => svg(size, h('path', { d: 'M6 2.5V6H2.5M13.5 6H10V2.5M10 13.5V10h3.5M2.5 10H6v3.5', ...stroke })),
242
246
  Chevron: ({ size = 16, open = false }) => svg(size, h('path', { d: open ? 'M4 6l4 4 4-4' : 'M6 4l4 4-4 4', ...stroke })),
243
247
  }
244
248
 
@@ -288,17 +292,12 @@ export function Confirm({ t, question, confirmLabel, onConfirm, onCancel, danger
288
292
  }, confirmLabel))
289
293
  }
290
294
 
291
- /**
292
- * A button with a small menu of actions; closes on outside click or Escape.
293
- * Without children it is the "more" icon; with children, a quiet text button.
294
- */
295
- export function Menu({ label, items, children, title }) {
296
- const [open, setOpen] = React.useState(false)
297
- const root = React.useRef(null)
295
+ /** Close something open on a press outside `root` or on Escape. */
296
+ function useDismiss(open, close, root) {
298
297
  React.useEffect(() => {
299
298
  if (!open) return undefined
300
- const away = (event) => { if (!root.current?.contains(event.target)) setOpen(false) }
301
- const key = (event) => { if (event.key === 'Escape') setOpen(false) }
299
+ const away = (event) => { if (!root.current?.contains(event.target)) close() }
300
+ const key = (event) => { if (event.key === 'Escape') close() }
302
301
  document.addEventListener('pointerdown', away)
303
302
  document.addEventListener('keydown', key)
304
303
  return () => {
@@ -306,6 +305,16 @@ export function Menu({ label, items, children, title }) {
306
305
  document.removeEventListener('keydown', key)
307
306
  }
308
307
  }, [open])
308
+ }
309
+
310
+ /**
311
+ * A button with a small menu of actions; closes on outside click or Escape.
312
+ * Without children it is the "more" icon; with children, a quiet text button.
313
+ */
314
+ export function Menu({ label, items, children, title }) {
315
+ const [open, setOpen] = React.useState(false)
316
+ const root = React.useRef(null)
317
+ useDismiss(open, () => setOpen(false), root)
309
318
  return h('span', { className: 'vsync-menuWrap', ref: root },
310
319
  h('button', { type: 'button', className: children ? 'vsync-metaButton' : 'vsync-icon', 'aria-label': label, title: title ?? label, 'aria-haspopup': 'menu', 'aria-expanded': String(open), onClick: () => setOpen(!open) }, children ?? h(Icon.More)),
311
320
  open && h('div', { className: 'vsync-menu', role: 'menu' }, items.filter(Boolean).map(item => h('button', {
@@ -319,3 +328,16 @@ export function Menu({ label, items, children, title }) {
319
328
  },
320
329
  }, item.label))))
321
330
  }
331
+
332
+ /** An icon button that opens a small panel of settings above it; closes on outside click or Escape. */
333
+ export function Popover({ label, icon, children }) {
334
+ const [open, setOpen] = React.useState(false)
335
+ const root = React.useRef(null)
336
+ useDismiss(open, () => setOpen(false), root)
337
+ return h('span', { className: 'vsync-menuWrap', ref: root },
338
+ h('button', { type: 'button', className: 'vsync-icon', 'aria-label': label, title: label, 'aria-haspopup': 'dialog', 'aria-expanded': String(open), onClick: () => setOpen(!open) }, icon),
339
+ open && h('div', { className: 'vsync-popover', role: 'dialog', 'aria-label': label }, children))
340
+ }
341
+
342
+ /** The words for a quality a source offers (src/quality.js): auto, audio, or a height. */
343
+ export const qualityLabel = (t, quality) => (/^\d+$/.test(quality) ? fill(t('quality.height'), { n: quality }) : t(`quality.${quality}`))