Skip to content

Commit

Permalink
Move caption and altText state into video reducer
Browse files Browse the repository at this point in the history
  • Loading branch information
gaearon committed Oct 4, 2024
1 parent 282db85 commit b38f789
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 20 deletions.
37 changes: 26 additions & 11 deletions src/view/com/composer/Composer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -184,9 +184,6 @@ export const ComposePost = ({
initQuote,
)

const [videoAltText, setVideoAltText] = useState('')
const [captions, setCaptions] = useState<{lang: string; file: File}[]>([])

// TODO: Move more state here.
const [composerState, dispatch] = useReducer(
composerReducer,
Expand Down Expand Up @@ -437,8 +434,8 @@ export const ComposePost = ({
videoState.status === 'done'
? {
blobRef: videoState.pendingPublish.blobRef,
altText: videoAltText,
captions: captions,
altText: videoState.altText,
captions: videoState.captions,
aspectRatio: {
width: videoState.asset.width,
height: videoState.asset.height,
Expand Down Expand Up @@ -522,7 +519,6 @@ export const ComposePost = ({
[
_,
agent,
captions,
composerState,
extLink,
images,
Expand All @@ -541,10 +537,11 @@ export const ComposePost = ({
setExtLink,
setLangPrefs,
threadgateAllowUISettings,
videoAltText,
videoState.asset,
videoState.pendingPublish,
videoState.status,
videoState.altText,
videoState.captions,
],
)

Expand Down Expand Up @@ -811,10 +808,28 @@ export const ComposePost = ({
/>
) : null)}
<SubtitleDialogBtn
defaultAltText={videoAltText}
saveAltText={setVideoAltText}
captions={captions}
setCaptions={setCaptions}
defaultAltText={videoState.altText}
saveAltText={altText =>
dispatch({
type: 'embed_update_video',
videoAction: {
type: 'update_alt_text',
altText,
signal: videoState.abortController.signal,
},
})
}
captions={videoState.captions}
setCaptions={updater => {
dispatch({
type: 'embed_update_video',
videoAction: {
type: 'update_captions',
updater,
signal: videoState.abortController.signal,
},
})
}}
/>
</Animated.View>
)}
Expand Down
48 changes: 46 additions & 2 deletions src/view/com/composer/state/video.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import {JobStatus} from '@atproto/api/dist/client/types/app/bsky/video/defs'
import {I18n} from '@lingui/core'
import {msg} from '@lingui/macro'

import {createVideoAgent} from '#/lib/media/video/util'
import {uploadVideo} from '#/lib/media/video/upload'
import {AbortError} from '#/lib/async/cancelable'
import {compressVideo} from '#/lib/media/video/compress'
import {
Expand All @@ -14,8 +12,12 @@ import {
VideoTooLargeError,
} from '#/lib/media/video/errors'
import {CompressedVideo} from '#/lib/media/video/types'
import {uploadVideo} from '#/lib/media/video/upload'
import {createVideoAgent} from '#/lib/media/video/util'
import {logger} from '#/logger'

type CaptionsTrack = {lang: string; file: File}

export type VideoAction =
| {
type: 'compressing_to_uploading'
Expand All @@ -40,6 +42,16 @@ export type VideoAction =
height: number
signal: AbortSignal
}
| {
type: 'update_alt_text'
altText: string
signal: AbortSignal
}
| {
type: 'update_captions'
updater: (prev: CaptionsTrack[]) => CaptionsTrack[]
signal: AbortSignal
}
| {
type: 'update_job_status'
jobStatus: AppBskyVideoDefs.JobStatus
Expand All @@ -57,6 +69,8 @@ export const NO_VIDEO = Object.freeze({
video: undefined,
jobId: undefined,
pendingPublish: undefined,
altText: '',
captions: [],
})

export type NoVideoState = typeof NO_VIDEO
Expand All @@ -70,6 +84,8 @@ type ErrorState = {
jobId: string | null
error: string
pendingPublish?: undefined
altText: string
captions: CaptionsTrack[]
}

type CompressingState = {
Expand All @@ -80,6 +96,8 @@ type CompressingState = {
video?: undefined
jobId?: undefined
pendingPublish?: undefined
altText: string
captions: CaptionsTrack[]
}

type UploadingState = {
Expand All @@ -90,6 +108,8 @@ type UploadingState = {
video: CompressedVideo
jobId?: undefined
pendingPublish?: undefined
altText: string
captions: CaptionsTrack[]
}

type ProcessingState = {
Expand All @@ -101,6 +121,8 @@ type ProcessingState = {
jobId: string
jobStatus: AppBskyVideoDefs.JobStatus | null
pendingPublish?: undefined
altText: string
captions: CaptionsTrack[]
}

type DoneState = {
Expand All @@ -111,6 +133,8 @@ type DoneState = {
video: CompressedVideo
jobId?: undefined
pendingPublish: {blobRef: BlobRef; mutableProcessed: boolean}
altText: string
captions: CaptionsTrack[]
}

export type VideoState =
Expand All @@ -129,6 +153,8 @@ export function createVideoState(
progress: 0,
abortController,
asset,
altText: '',
captions: [],
}
}

Expand All @@ -149,6 +175,8 @@ export function videoReducer(
asset: state.asset ?? null,
video: state.video ?? null,
jobId: state.jobId ?? null,
altText: state.altText,
captions: state.captions,
}
} else if (action.type === 'update_progress') {
if (state.status === 'compressing' || state.status === 'uploading') {
Expand All @@ -164,6 +192,16 @@ export function videoReducer(
asset: {...state.asset, width: action.width, height: action.height},
}
}
} else if (action.type === 'update_alt_text') {
return {
...state,
altText: action.altText,
}
} else if (action.type === 'update_captions') {
return {
...state,
captions: action.updater(state.captions),
}
} else if (action.type === 'compressing_to_uploading') {
if (state.status === 'compressing') {
return {
Expand All @@ -172,6 +210,8 @@ export function videoReducer(
abortController: state.abortController,
asset: state.asset,
video: action.video,
altText: state.altText,
captions: state.captions,
}
}
return state
Expand All @@ -185,6 +225,8 @@ export function videoReducer(
video: state.video,
jobId: action.jobId,
jobStatus: null,
altText: state.altText,
captions: state.captions,
}
}
} else if (action.type === 'update_job_status') {
Expand All @@ -210,6 +252,8 @@ export function videoReducer(
blobRef: action.blobRef,
mutableProcessed: false,
},
altText: state.altText,
captions: state.captions,
}
}
}
Expand Down
12 changes: 5 additions & 7 deletions src/view/com/composer/videos/SubtitleDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ import {SubtitleFilePicker} from './SubtitleFilePicker'

const MAX_NUM_CAPTIONS = 1

type CaptionsTrack = {lang: string; file: File}

interface Props {
defaultAltText: string
captions: {lang: string; file: File}[]
captions: CaptionsTrack[]
saveAltText: (altText: string) => void
setCaptions: React.Dispatch<
React.SetStateAction<{lang: string; file: File}[]>
>
setCaptions: (updater: (prev: CaptionsTrack[]) => CaptionsTrack[]) => void
}

export function SubtitleDialogBtn(props: Props) {
Expand Down Expand Up @@ -198,9 +198,7 @@ function SubtitleFileRow({
language: string
file: File
otherLanguages: {code2: string; code3: string; name: string}[]
setCaptions: React.Dispatch<
React.SetStateAction<{lang: string; file: File}[]>
>
setCaptions: (updater: (prev: CaptionsTrack[]) => CaptionsTrack[]) => void
style: StyleProp<ViewStyle>
}) {
const {_} = useLingui()
Expand Down

0 comments on commit b38f789

Please sign in to comment.