sanity-plugin-mux-input 2.13.0 → 2.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.
Files changed (56) hide show
  1. package/README.md +25 -24
  2. package/dist/index.d.mts +35 -2
  3. package/dist/index.d.ts +35 -2
  4. package/dist/index.js +2176 -461
  5. package/dist/index.js.map +1 -1
  6. package/dist/index.mjs +2178 -463
  7. package/dist/index.mjs.map +1 -1
  8. package/package.json +1 -1
  9. package/src/_exports/index.ts +1 -0
  10. package/src/actions/assets.ts +75 -0
  11. package/src/actions/secrets.ts +6 -1
  12. package/src/actions/upload.ts +1 -1
  13. package/src/components/AddCaptionDialog.tsx +421 -0
  14. package/src/components/CaptionsDialog.tsx +23 -0
  15. package/src/components/ConfigureApi.tsx +51 -5
  16. package/src/components/EditCaptionDialog.tsx +508 -0
  17. package/src/components/InputBrowser.tsx +8 -2
  18. package/src/components/Onboard.tsx +2 -2
  19. package/src/components/PageSelector.tsx +54 -0
  20. package/src/components/Player.styled.tsx +7 -2
  21. package/src/components/PlayerActionsMenu.tsx +14 -6
  22. package/src/components/SelectAsset.tsx +9 -3
  23. package/src/components/StudioTool.tsx +2 -2
  24. package/src/components/TextTracksManager.tsx +781 -0
  25. package/src/components/UploadConfiguration.tsx +104 -343
  26. package/src/components/Uploader.styled.tsx +8 -15
  27. package/src/components/Uploader.tsx +25 -7
  28. package/src/components/VideoDetails/VideoDetails.tsx +43 -7
  29. package/src/components/VideoInBrowser.tsx +53 -6
  30. package/src/components/VideoPlayer.tsx +122 -47
  31. package/src/components/VideoThumbnail.tsx +84 -72
  32. package/src/components/VideosBrowser.tsx +15 -5
  33. package/src/components/uploadConfiguration/PlaybackPolicy.tsx +95 -6
  34. package/src/components/uploadConfiguration/PlaybackPolicyOption.tsx +26 -10
  35. package/src/components/uploadConfiguration/ResolutionTierSelector.tsx +71 -0
  36. package/src/components/uploadConfiguration/StaticRenditionSelector.tsx +179 -0
  37. package/src/context/DrmPlaybackWarningContext.tsx +93 -0
  38. package/src/hooks/useAccessControl.ts +1 -0
  39. package/src/hooks/useDialogState.ts +1 -1
  40. package/src/hooks/useFetchFileSize.ts +54 -0
  41. package/src/hooks/useMediaMetadata.ts +100 -0
  42. package/src/hooks/useSaveSecrets.ts +10 -3
  43. package/src/hooks/useSecretsDocumentValues.ts +9 -1
  44. package/src/hooks/useSecretsFormState.ts +6 -3
  45. package/src/util/asserters.ts +14 -0
  46. package/src/util/createUrlParamsObject.ts +7 -3
  47. package/src/util/generateJwt.ts +11 -2
  48. package/src/util/getPlaybackPolicy.ts +63 -4
  49. package/src/util/getStoryboardSrc.ts +7 -3
  50. package/src/util/getVideoMetadata.ts +4 -1
  51. package/src/util/getVideoSrc.ts +9 -9
  52. package/src/util/readSecrets.ts +3 -1
  53. package/src/util/textTracks.ts +222 -0
  54. package/src/util/tryWithSuspend.ts +22 -0
  55. package/src/util/types.ts +39 -6
  56. package/src/util/getPlaybackId.ts +0 -9
@@ -1,6 +1,6 @@
1
1
  import {ErrorOutlineIcon} from '@sanity/icons'
2
2
  import {Box, Card, CardTone, Spinner, Stack, Text} from '@sanity/ui'
3
- import {useMemo, useRef, useState} from 'react'
3
+ import {Suspense, useMemo, useRef, useState} from 'react'
4
4
  import {styled} from 'styled-components'
5
5
 
6
6
  import {useClient} from '../hooks/useClient'
@@ -8,6 +8,7 @@ import {useInView} from '../hooks/useInView'
8
8
  import {THUMBNAIL_ASPECT_RATIO} from '../util/constants'
9
9
  import {getAnimatedPosterSrc} from '../util/getAnimatedPosterSrc'
10
10
  import {getPosterSrc} from '../util/getPosterSrc'
11
+ import {tryWithSuspend} from '../util/tryWithSuspend'
11
12
  import {AssetThumbnailOptions, MuxAnimatedThumbnailUrl, MuxThumbnailUrl} from '../util/types'
12
13
 
13
14
  const Image = styled.img`
@@ -36,91 +37,102 @@ export default function VideoThumbnail({
36
37
  width?: number
37
38
  staticImage?: boolean
38
39
  }) {
40
+ const posterWidth = width || 250
41
+ const client = useClient()
39
42
  const ref = useRef<HTMLDivElement | null>(null)
40
43
  const inView = useInView(ref)
41
- const posterWidth = width || 250
42
44
 
43
45
  const [status, setStatus] = useState<ImageStatus>('loading')
44
- const client = useClient()
46
+ const [error, setError] = useState<string | null>(null)
45
47
 
46
- const src = useMemo(() => {
47
- try {
48
- let thumbnail: MuxAnimatedThumbnailUrl | MuxThumbnailUrl
49
- if (staticImage) thumbnail = getPosterSrc({asset, client, width: posterWidth})
50
- else thumbnail = getAnimatedPosterSrc({asset, client, width: posterWidth})
48
+ const thumbnailSrc = useMemo(() => {
49
+ return tryWithSuspend(
50
+ () => {
51
+ let thumbnail: MuxAnimatedThumbnailUrl | MuxThumbnailUrl | undefined
51
52
 
52
- return thumbnail
53
- } catch {
54
- if (status !== 'error') setStatus('error')
55
- return undefined
56
- }
57
- }, [asset, client, posterWidth, status, staticImage])
53
+ if (staticImage) thumbnail = getPosterSrc({asset, client, width: posterWidth})
54
+ else thumbnail = getAnimatedPosterSrc({asset, client, width: posterWidth})
55
+ return thumbnail
56
+ },
57
+ (err: Error) => {
58
+ handleError(err.message)
59
+ return undefined
60
+ }
61
+ )
62
+ }, [asset, client, posterWidth, staticImage])
58
63
 
59
64
  function handleLoad() {
60
65
  setStatus('loaded')
61
66
  }
62
67
 
63
- function handleError() {
68
+ function handleError(err?: string) {
64
69
  setStatus('error')
70
+ if (err) {
71
+ setError(err)
72
+ } else {
73
+ setError('Failed loading thumbnail')
74
+ }
65
75
  }
66
76
 
67
77
  return (
68
- <Card
69
- style={{
70
- aspectRatio: THUMBNAIL_ASPECT_RATIO,
71
- position: 'relative',
72
- maxWidth: width ? `${width}px` : undefined,
73
- width: '100%',
74
- flex: 1,
75
- }}
76
- border
77
- radius={2}
78
- ref={ref}
79
- tone={STATUS_TO_TONE[status]}
80
- >
81
- {inView ? (
82
- <>
83
- {status === 'loading' && (
84
- <Box
85
- style={{
86
- position: 'absolute',
87
- left: '50%',
88
- top: '50%',
89
- transform: 'translate(-50%, -50%)',
90
- }}
91
- >
92
- <Spinner />
93
- </Box>
94
- )}
95
- {status === 'error' && (
96
- <Stack
97
- space={4}
98
- style={{
99
- position: 'absolute',
100
- width: '100%',
101
- left: 0,
102
- top: '50%',
103
- transform: 'translateY(-50%)',
104
- justifyItems: 'center',
105
- }}
106
- >
107
- <Text size={4} muted>
108
- <ErrorOutlineIcon style={{fontSize: '1.75em'}} />
109
- </Text>
110
- <Text muted align="center">
111
- Failed loading thumbnail
112
- </Text>
113
- </Stack>
114
- )}
115
- <Image
116
- src={src}
117
- alt={`Preview for ${staticImage ? 'image' : 'video'} ${asset.filename || asset.assetId}`}
118
- onLoad={handleLoad}
119
- onError={handleError}
120
- style={{opacity: status === 'loaded' ? 1 : 0}}
121
- />
122
- </>
123
- ) : null}
124
- </Card>
78
+ <Suspense fallback={<span>Preparing thumbnail</span>}>
79
+ <Card
80
+ style={{
81
+ aspectRatio: THUMBNAIL_ASPECT_RATIO,
82
+ position: 'relative',
83
+ maxWidth: width ? `${width}px` : undefined,
84
+ width: '100%',
85
+ flex: 1,
86
+ }}
87
+ border
88
+ radius={2}
89
+ ref={ref}
90
+ tone={STATUS_TO_TONE[status]}
91
+ >
92
+ {inView ? (
93
+ <>
94
+ {status === 'loading' && (
95
+ <Box
96
+ style={{
97
+ position: 'absolute',
98
+ left: '50%',
99
+ top: '50%',
100
+ transform: 'translate(-50%, -50%)',
101
+ }}
102
+ >
103
+ <Spinner />
104
+ </Box>
105
+ )}
106
+ {status === 'error' && (
107
+ <Stack
108
+ space={4}
109
+ style={{
110
+ position: 'absolute',
111
+ width: '100%',
112
+ left: 0,
113
+ top: '50%',
114
+ transform: 'translateY(-50%)',
115
+ justifyItems: 'center',
116
+ }}
117
+ >
118
+ <Text size={4} muted>
119
+ <ErrorOutlineIcon style={{fontSize: '1.75em'}} />
120
+ </Text>
121
+ <Text muted align="center">
122
+ {error}
123
+ </Text>
124
+ </Stack>
125
+ )}
126
+ <Image
127
+ src={thumbnailSrc ?? undefined}
128
+ alt={`Preview for ${staticImage ? 'image' : 'video'} ${asset.filename || asset.assetId}`}
129
+ onLoad={handleLoad}
130
+ onError={() => handleError()}
131
+ style={{opacity: status === 'loaded' ? 1 : 0}}
132
+ />
133
+ </>
134
+ ) : null}
135
+ </Card>
136
+ </Suspense>
125
137
  )
126
138
  }
@@ -2,10 +2,12 @@ import {SearchIcon} from '@sanity/icons'
2
2
  import {Card, Flex, Grid, Inline, Label, Stack, Text, TextInput} from '@sanity/ui'
3
3
  import {useMemo, useState} from 'react'
4
4
 
5
+ import {DrmPlaybackWarningContextProvider} from '../context/DrmPlaybackWarningContext'
5
6
  import useAssets from '../hooks/useAssets'
6
- import type {VideoAssetDocument} from '../util/types'
7
+ import type {PluginConfig, VideoAssetDocument} from '../util/types'
7
8
  import ConfigureApi from './ConfigureApi'
8
9
  import ImportVideosFromMux from './ImportVideosFromMux'
10
+ import PageSelector from './PageSelector'
9
11
  import ResyncMetadata from './ResyncMetadata'
10
12
  import {SelectSortOptions} from './SelectSortOptions'
11
13
  import SpinnerBox from './SpinnerBox'
@@ -14,20 +16,27 @@ import VideoDetails from './VideoDetails/VideoDetails'
14
16
  import VideoInBrowser from './VideoInBrowser'
15
17
 
16
18
  export interface VideosBrowserProps {
19
+ config: PluginConfig
17
20
  onSelect?: (asset: VideoAssetDocument) => void
18
21
  }
19
22
 
20
- export default function VideosBrowser({onSelect}: VideosBrowserProps) {
23
+ export default function VideosBrowser({onSelect, config}: VideosBrowserProps) {
21
24
  const {assets, isLoading, searchQuery, setSearchQuery, setSort, sort} = useAssets()
25
+ const [page, setPage] = useState<number>(0)
26
+ const pageLimit = 20
27
+ const pageTotal = Math.floor(assets.length / pageLimit) + 1
22
28
  const [editedAsset, setEditedAsset] = useState<VideoDetailsProps['asset'] | null>(null)
23
29
  const freshEditedAsset = useMemo(
24
30
  () => assets.find((a) => a._id === editedAsset?._id) || editedAsset,
25
31
  [editedAsset, assets]
26
32
  )
27
33
 
34
+ const pageStart = page * pageLimit
35
+ const pageEnd = pageStart + pageLimit
36
+
28
37
  const placement = onSelect ? 'input' : 'tool'
29
38
  return (
30
- <>
39
+ <DrmPlaybackWarningContextProvider config={config}>
31
40
  <Stack padding={4} space={4} style={{minHeight: '50vh'}}>
32
41
  <Flex justify="space-between" align="center">
33
42
  <Flex align="center" gap={3}>
@@ -40,6 +49,7 @@ export default function VideosBrowser({onSelect}: VideosBrowserProps) {
40
49
  placeholder="Search videos"
41
50
  />
42
51
  <SelectSortOptions setSort={setSort} sort={sort} />
52
+ <PageSelector page={page} setPage={setPage} total={pageTotal} />
43
53
  </Flex>
44
54
  {placement === 'tool' && (
45
55
  <Inline space={2}>
@@ -62,7 +72,7 @@ export default function VideosBrowser({onSelect}: VideosBrowserProps) {
62
72
  gridTemplateColumns: 'repeat(auto-fill, minmax(250px, 1fr))',
63
73
  }}
64
74
  >
65
- {assets.map((asset) => (
75
+ {assets.slice(pageStart, pageEnd).map((asset) => (
66
76
  <VideoInBrowser
67
77
  key={asset._id}
68
78
  asset={asset}
@@ -85,6 +95,6 @@ export default function VideosBrowser({onSelect}: VideosBrowserProps) {
85
95
  {freshEditedAsset && (
86
96
  <VideoDetails closeDialog={() => setEditedAsset(null)} asset={freshEditedAsset} />
87
97
  )}
88
- </>
98
+ </DrmPlaybackWarningContextProvider>
89
99
  )
90
100
  }
@@ -1,6 +1,8 @@
1
- import {Grid, Text} from '@sanity/ui'
1
+ import {Code, Grid, Text} from '@sanity/ui'
2
+ import {ActionDispatch} from 'react'
2
3
 
3
4
  import {Secrets, UploadConfig} from '../../util/types'
5
+ import {UploadConfigurationStateAction} from '../UploadConfiguration'
4
6
  import PlaybackPolicyOption from './PlaybackPolicyOption'
5
7
  import PlaybackPolicyWarning from './PlaybackPolicyWarning'
6
8
 
@@ -13,9 +15,10 @@ export default function PlaybackPolicy({
13
15
  id: string
14
16
  config: UploadConfig
15
17
  secrets: Secrets
16
- dispatch: any
18
+ dispatch: ActionDispatch<[action: UploadConfigurationStateAction]>
17
19
  }) {
18
- const noPolicySelected = !(config.public_policy || config.signed_policy)
20
+ const noPolicySelected = !(config.public_policy || config.signed_policy || config.drm_policy)
21
+ const drmPolicyDisabled = !secrets.drmConfigId
19
22
  return (
20
23
  <Grid gap={3}>
21
24
  <Text weight="bold">Advanced Playback Policies</Text>
@@ -23,7 +26,14 @@ export default function PlaybackPolicy({
23
26
  id={`${id}--public`}
24
27
  checked={config.public_policy}
25
28
  optionName="Public"
26
- description="Playback IDs are accessible by constructing an HLS URL like https://stream.mux.com/{PLAYBACK_ID}"
29
+ description={
30
+ <>
31
+ <Text size={2} muted>
32
+ Playback IDs are accessible by constructing an HLS URL like
33
+ </Text>
34
+ <Code>{'https://stream.mux.com/{PLAYBACK_ID}'}</Code>
35
+ </>
36
+ }
27
37
  dispatch={dispatch}
28
38
  action="public_policy"
29
39
  />
@@ -32,12 +42,91 @@ export default function PlaybackPolicy({
32
42
  id={`${id}--signed`}
33
43
  checked={config.signed_policy}
34
44
  optionName="Signed"
35
- description="Playback IDs should be used with tokens https://stream.mux.com/{PLAYBACK_ID}?token={TOKEN}.
36
- // See Secure video playback for details about creating tokens."
45
+ description={
46
+ <>
47
+ <Text size={2} muted>
48
+ Playback IDs should be used with tokens
49
+ </Text>
50
+ <Code>{'https://stream.mux.com/{PLAYBACK_ID}?token={TOKEN}'}</Code>
51
+ <Text size={2} muted>
52
+ See{' '}
53
+ <a
54
+ href="https://www.mux.com/docs/guides/secure-video-playback"
55
+ target="_blank"
56
+ rel="noopener noreferrer"
57
+ >
58
+ Secure video playback
59
+ </a>{' '}
60
+ for details about creating tokens.
61
+ </Text>
62
+ </>
63
+ }
64
+ // See Secure video playback for details about creating tokens."
37
65
  dispatch={dispatch}
38
66
  action="signed_policy"
39
67
  />
40
68
  )}
69
+ {drmPolicyDisabled ? (
70
+ <PlaybackPolicyOption
71
+ id={`${id}--drm`}
72
+ checked={false}
73
+ optionName="DRM - Disabled"
74
+ description={
75
+ <>
76
+ <Text size={2} muted>
77
+ To enable DRM add your DRM Configuration Id to your plugin configuration in the API
78
+ Credentials view.{' '}
79
+ <a
80
+ href="https://www.mux.com/support/human"
81
+ target="_blank"
82
+ rel="noopener noreferrer"
83
+ >
84
+ Contact us
85
+ </a>{' '}
86
+ to get started using DRM.
87
+ </Text>
88
+ </>
89
+ }
90
+ dispatch={dispatch}
91
+ disabled
92
+ />
93
+ ) : (
94
+ <PlaybackPolicyOption
95
+ id={`${id}--drm`}
96
+ checked={config.drm_policy}
97
+ optionName="DRM"
98
+ description={
99
+ <>
100
+ <Text size={2} muted>
101
+ Playback IDs should be used with tokens as with Signed playback, but require extra
102
+ configuration.
103
+ </Text>
104
+ <Code>{'https://stream.mux.com/{PLAYBACK_ID}?token={TOKEN}'}</Code>
105
+ <Text size={2} muted>
106
+ See{' '}
107
+ <a
108
+ href="https://www.mux.com/docs/guides/protect-videos-with-drm#play-drm-protected-videos"
109
+ target="_blank"
110
+ rel="noopener noreferrer"
111
+ >
112
+ Protect videos with DRM
113
+ </a>{' '}
114
+ for details about configuring your player for DRM playback and{' '}
115
+ <a
116
+ href="https://www.mux.com/docs/guides/secure-video-playback"
117
+ target="_blank"
118
+ rel="noopener noreferrer"
119
+ >
120
+ Secure video playback
121
+ </a>{' '}
122
+ for details about creating tokens.
123
+ </Text>
124
+ </>
125
+ }
126
+ dispatch={dispatch}
127
+ action="drm_policy"
128
+ />
129
+ )}
41
130
  {noPolicySelected && <PlaybackPolicyWarning />}
42
131
  </Grid>
43
132
  )
@@ -1,5 +1,5 @@
1
- import {Box, Checkbox, Flex, Grid, Stack, Text} from '@sanity/ui'
2
- import {CSSProperties, useState} from 'react'
1
+ import {Checkbox, Flex, Grid, Text} from '@sanity/ui'
2
+ import {ActionDispatch, CSSProperties, ReactNode, useState} from 'react'
3
3
 
4
4
  import {UploadConfigurationStateAction} from '../UploadConfiguration'
5
5
 
@@ -10,13 +10,15 @@ export default function PlaybackPolicyOption({
10
10
  description,
11
11
  dispatch,
12
12
  action,
13
+ disabled,
13
14
  }: {
14
15
  id: string
15
16
  checked: boolean
16
17
  optionName: string
17
- description: string
18
- dispatch: any
19
- action: UploadConfigurationStateAction['action']
18
+ description: string | ReactNode
19
+ dispatch: ActionDispatch<[action: UploadConfigurationStateAction]>
20
+ action?: 'public_policy' | 'signed_policy' | 'drm_policy'
21
+ disabled?: boolean
20
22
  }) {
21
23
  const [scale, setScale] = useState(1)
22
24
 
@@ -24,7 +26,7 @@ export default function PlaybackPolicyOption({
24
26
  outline: '0.01rem solid grey',
25
27
  transform: `scale(${scale})`,
26
28
  transition: 'transform 0.1s ease-in-out',
27
- cursor: 'pointer',
29
+ cursor: disabled ? 'not-allowed' : 'pointer',
28
30
  borderRadius: '0.25rem',
29
31
  }
30
32
 
@@ -36,23 +38,37 @@ export default function PlaybackPolicyOption({
36
38
  }
37
39
 
38
40
  const handleBoxClick = () => {
41
+ if (!action) return
39
42
  triggerAnimation()
40
43
  dispatch({
41
44
  action,
42
45
  value: !checked,
43
46
  })
44
47
  }
48
+
49
+ const descriptionJsx =
50
+ typeof description === 'string' ? (
51
+ <Text size={2} muted>
52
+ {description}
53
+ </Text>
54
+ ) : (
55
+ description
56
+ )
45
57
  return (
46
58
  <label>
47
59
  <Flex gap={3} padding={3} style={boxStyle}>
48
- <Checkbox id={id} required checked={checked} onChange={handleBoxClick} />
60
+ <Checkbox
61
+ id={id}
62
+ required
63
+ checked={checked}
64
+ onChange={handleBoxClick}
65
+ disabled={disabled}
66
+ />
49
67
  <Grid gap={3}>
50
68
  <Text size={3} weight="bold">
51
69
  {optionName}
52
70
  </Text>
53
- <Text size={2} muted>
54
- {description}
55
- </Text>
71
+ {descriptionJsx}
56
72
  </Grid>
57
73
  </Flex>
58
74
  </label>
@@ -0,0 +1,71 @@
1
+ import {Flex, Radio, Text} from '@sanity/ui'
2
+ import {ActionDispatch} from 'react'
3
+ import {FormField} from 'sanity'
4
+
5
+ import {type UploadConfig} from '../../util/types'
6
+ import {UploadConfigurationStateAction} from '../UploadConfiguration'
7
+
8
+ export const RESOLUTION_TIERS = [
9
+ {value: '1080p', label: '1080p'},
10
+ {value: '1440p', label: '1440p (2k)'},
11
+ {value: '2160p', label: '2160p (4k)'},
12
+ ] as const satisfies {value: UploadConfig['max_resolution_tier']; label: string}[]
13
+
14
+ export const ResolutionTierSelector = ({
15
+ id,
16
+ config,
17
+ dispatch,
18
+ maxSupportedResolution,
19
+ }: {
20
+ id: string
21
+ config: UploadConfig
22
+ dispatch: ActionDispatch<[action: UploadConfigurationStateAction]>
23
+ maxSupportedResolution: number
24
+ }) => {
25
+ return (
26
+ <FormField
27
+ title="Resolution Tier"
28
+ description={
29
+ <>
30
+ The maximum{' '}
31
+ <a
32
+ href="https://docs.mux.com/api-reference#video/operation/create-direct-upload"
33
+ target="_blank"
34
+ rel="noopener noreferrer"
35
+ >
36
+ resolution_tier
37
+ </a>{' '}
38
+ your asset is encoded, stored, and streamed at.
39
+ </>
40
+ }
41
+ >
42
+ <Flex gap={3} wrap={'wrap'}>
43
+ {RESOLUTION_TIERS.map(({value, label}, index) => {
44
+ const inputId = `${id}--type-${value}`
45
+
46
+ if (index > maxSupportedResolution) return null
47
+
48
+ return (
49
+ <Flex key={value} align="center" gap={2}>
50
+ <Radio
51
+ checked={config.max_resolution_tier === value}
52
+ name="asset-resolutiontier"
53
+ onChange={(e) =>
54
+ dispatch({
55
+ action: 'max_resolution_tier',
56
+ value: e.currentTarget.value as UploadConfig['max_resolution_tier'],
57
+ })
58
+ }
59
+ value={value}
60
+ id={inputId}
61
+ />
62
+ <Text as="label" htmlFor={inputId}>
63
+ {label}
64
+ </Text>
65
+ </Flex>
66
+ )
67
+ })}
68
+ </Flex>
69
+ </FormField>
70
+ )
71
+ }