Rename some files and variables (#5587)

* Move composer reducers together

* videoUploadState -> videoState

* Inline videoDispatch
This commit is contained in:
dan
2024-10-03 14:57:48 +09:00
committed by Takayuki KUSANO
parent 59589e34a3
commit 475708ea30
10 changed files with 64 additions and 81 deletions
+1 -1
View File
@@ -24,7 +24,7 @@ import {
threadgateAllowUISettingToAllowRecordValue, threadgateAllowUISettingToAllowRecordValue,
writeThreadgateRecord, writeThreadgateRecord,
} from '#/state/queries/threadgate' } from '#/state/queries/threadgate'
import {ComposerState} from '#/view/com/composer/state' import {ComposerState} from '#/view/com/composer/state/composer'
import {LinkMeta} from '../link-meta/link-meta' import {LinkMeta} from '../link-meta/link-meta'
import {uploadBlob} from './upload-blob' import {uploadBlob} from './upload-blob'
+1 -1
View File
@@ -2,8 +2,8 @@ import {getVideoMetaData, Video} from 'react-native-compressor'
import {ImagePickerAsset} from 'expo-image-picker' import {ImagePickerAsset} from 'expo-image-picker'
import {SUPPORTED_MIME_TYPES, SupportedMimeTypes} from '#/lib/constants' import {SUPPORTED_MIME_TYPES, SupportedMimeTypes} from '#/lib/constants'
import {extToMime} from '#/state/queries/video/util'
import {CompressedVideo} from './types' import {CompressedVideo} from './types'
import {extToMime} from './util'
const MIN_SIZE_FOR_COMPRESSION = 25 // 25mb const MIN_SIZE_FOR_COMPRESSION = 25 // 25mb
@@ -7,8 +7,8 @@ import {nanoid} from 'nanoid/non-secure'
import {AbortError} from '#/lib/async/cancelable' import {AbortError} from '#/lib/async/cancelable'
import {ServerError} from '#/lib/media/video/errors' import {ServerError} from '#/lib/media/video/errors'
import {CompressedVideo} from '#/lib/media/video/types' import {CompressedVideo} from '#/lib/media/video/types'
import {createVideoEndpointUrl, mimeToExt} from '#/state/queries/video/util' import {createVideoEndpointUrl, mimeToExt} from './util'
import {getServiceAuthToken, getVideoUploadLimits} from './video-upload.shared' import {getServiceAuthToken, getVideoUploadLimits} from './upload.shared'
export async function uploadVideo({ export async function uploadVideo({
video, video,
@@ -7,8 +7,8 @@ import {nanoid} from 'nanoid/non-secure'
import {AbortError} from '#/lib/async/cancelable' import {AbortError} from '#/lib/async/cancelable'
import {ServerError} from '#/lib/media/video/errors' import {ServerError} from '#/lib/media/video/errors'
import {CompressedVideo} from '#/lib/media/video/types' import {CompressedVideo} from '#/lib/media/video/types'
import {createVideoEndpointUrl, mimeToExt} from '#/state/queries/video/util' import {createVideoEndpointUrl, mimeToExt} from './util'
import {getServiceAuthToken, getVideoUploadLimits} from './video-upload.shared' import {getServiceAuthToken, getVideoUploadLimits} from './upload.shared'
export async function uploadVideo({ export async function uploadVideo({
video, video,
+51 -63
View File
@@ -82,13 +82,6 @@ import {useProfileQuery} from '#/state/queries/profile'
import {Gif} from '#/state/queries/tenor' import {Gif} from '#/state/queries/tenor'
import {ThreadgateAllowUISetting} from '#/state/queries/threadgate' import {ThreadgateAllowUISetting} from '#/state/queries/threadgate'
import {threadgateViewToAllowUISetting} from '#/state/queries/threadgate/util' import {threadgateViewToAllowUISetting} from '#/state/queries/threadgate/util'
import {NO_VIDEO, NoVideoState} from '#/state/queries/video/video'
import {
processVideo,
VideoAction,
VideoState,
VideoState as VideoUploadState,
} from '#/state/queries/video/video'
import {useAgent, useSession} from '#/state/session' import {useAgent, useSession} from '#/state/session'
import {useComposerControls} from '#/state/shell/composer' import {useComposerControls} from '#/state/shell/composer'
import {ComposerOpts} from '#/state/shell/composer' import {ComposerOpts} from '#/state/shell/composer'
@@ -123,7 +116,8 @@ import {EmojiArc_Stroke2_Corner0_Rounded as EmojiSmile} from '#/components/icons
import {TimesLarge_Stroke2_Corner0_Rounded as X} from '#/components/icons/Times' import {TimesLarge_Stroke2_Corner0_Rounded as X} from '#/components/icons/Times'
import * as Prompt from '#/components/Prompt' import * as Prompt from '#/components/Prompt'
import {Text as NewText} from '#/components/Typography' import {Text as NewText} from '#/components/Typography'
import {composerReducer, createComposerState} from './state' import {composerReducer, createComposerState} from './state/composer'
import {NO_VIDEO, NoVideoState, processVideo, VideoState} from './state/video'
const MAX_IMAGES = 4 const MAX_IMAGES = 4
@@ -200,16 +194,10 @@ export const ComposePost = ({
createComposerState, createComposerState,
) )
let videoUploadState: VideoState | NoVideoState = NO_VIDEO let videoState: VideoState | NoVideoState = NO_VIDEO
if (composerState.embed.media?.type === 'video') { if (composerState.embed.media?.type === 'video') {
videoUploadState = composerState.embed.media.video videoState = composerState.embed.media.video
} }
const videoDispatch = useCallback(
(videoAction: VideoAction) => {
dispatch({type: 'embed_update_video', videoAction})
},
[dispatch],
)
const selectVideo = React.useCallback( const selectVideo = React.useCallback(
(asset: ImagePickerAsset) => { (asset: ImagePickerAsset) => {
@@ -217,14 +205,14 @@ export const ComposePost = ({
dispatch({type: 'embed_add_video', asset, abortController}) dispatch({type: 'embed_add_video', asset, abortController})
processVideo( processVideo(
asset, asset,
videoDispatch, videoAction => dispatch({type: 'embed_update_video', videoAction}),
agent, agent,
currentDid, currentDid,
abortController.signal, abortController.signal,
_, _,
) )
}, },
[_, videoDispatch, agent, currentDid], [_, agent, currentDid],
) )
// Whenever we receive an initial video uri, we should immediately run compression if necessary // Whenever we receive an initial video uri, we should immediately run compression if necessary
@@ -235,23 +223,26 @@ export const ComposePost = ({
}, [initVideoUri, selectVideo]) }, [initVideoUri, selectVideo])
const clearVideo = React.useCallback(() => { const clearVideo = React.useCallback(() => {
videoUploadState.abortController.abort() videoState.abortController.abort()
dispatch({type: 'embed_remove_video'}) dispatch({type: 'embed_remove_video'})
}, [videoUploadState.abortController, dispatch]) }, [videoState.abortController, dispatch])
const updateVideoDimensions = useCallback( const updateVideoDimensions = useCallback(
(width: number, height: number) => { (width: number, height: number) => {
videoDispatch({ dispatch({
type: 'embed_update_video',
videoAction: {
type: 'update_dimensions', type: 'update_dimensions',
width, width,
height, height,
signal: videoUploadState.abortController.signal, signal: videoState.abortController.signal,
},
}) })
}, },
[videoUploadState.abortController, videoDispatch], [videoState.abortController],
) )
const hasVideo = Boolean(videoUploadState.asset || videoUploadState.video) const hasVideo = Boolean(videoState.asset || videoState.video)
const [publishOnUpload, setPublishOnUpload] = useState(false) const [publishOnUpload, setPublishOnUpload] = useState(false)
@@ -288,7 +279,7 @@ export const ComposePost = ({
graphemeLength > 0 || graphemeLength > 0 ||
images.length !== 0 || images.length !== 0 ||
extGif || extGif ||
videoUploadState.status !== 'idle' videoState.status !== 'idle'
) { ) {
closeAllDialogs() closeAllDialogs()
Keyboard.dismiss() Keyboard.dismiss()
@@ -303,7 +294,7 @@ export const ComposePost = ({
closeAllDialogs, closeAllDialogs,
discardPromptControl, discardPromptControl,
onClose, onClose,
videoUploadState.status, videoState.status,
]) ])
useImperativeHandle(cancelRef, () => ({onPressCancel})) useImperativeHandle(cancelRef, () => ({onPressCancel}))
@@ -400,8 +391,8 @@ export const ComposePost = ({
if ( if (
!finishedUploading && !finishedUploading &&
videoUploadState.asset && videoState.asset &&
videoUploadState.status !== 'done' videoState.status !== 'done'
) { ) {
setPublishOnUpload(true) setPublishOnUpload(true)
return return
@@ -414,7 +405,7 @@ export const ComposePost = ({
images.length === 0 && images.length === 0 &&
!extLink && !extLink &&
!quote && !quote &&
videoUploadState.status === 'idle' videoState.status === 'idle'
) { ) {
setError(_(msg`Did you want to say anything?`)) setError(_(msg`Did you want to say anything?`))
return return
@@ -442,14 +433,14 @@ export const ComposePost = ({
onStateChange: setProcessingState, onStateChange: setProcessingState,
langs: toPostLanguages(langPrefs.postLanguage), langs: toPostLanguages(langPrefs.postLanguage),
video: video:
videoUploadState.status === 'done' videoState.status === 'done'
? { ? {
blobRef: videoUploadState.pendingPublish.blobRef, blobRef: videoState.pendingPublish.blobRef,
altText: videoAltText, altText: videoAltText,
captions: captions, captions: captions,
aspectRatio: { aspectRatio: {
width: videoUploadState.asset.width, width: videoState.asset.width,
height: videoUploadState.asset.height, height: videoState.asset.height,
}, },
} }
: undefined, : undefined,
@@ -550,20 +541,20 @@ export const ComposePost = ({
setLangPrefs, setLangPrefs,
threadgateAllowUISettings, threadgateAllowUISettings,
videoAltText, videoAltText,
videoUploadState.asset, videoState.asset,
videoUploadState.pendingPublish, videoState.pendingPublish,
videoUploadState.status, videoState.status,
], ],
) )
React.useEffect(() => { React.useEffect(() => {
if (videoUploadState.pendingPublish && publishOnUpload) { if (videoState.pendingPublish && publishOnUpload) {
if (!videoUploadState.pendingPublish.mutableProcessed) { if (!videoState.pendingPublish.mutableProcessed) {
videoUploadState.pendingPublish.mutableProcessed = true videoState.pendingPublish.mutableProcessed = true
onPressPublish(true) onPressPublish(true)
} }
} }
}, [onPressPublish, publishOnUpload, videoUploadState.pendingPublish]) }, [onPressPublish, publishOnUpload, videoState.pendingPublish])
const canPost = useMemo( const canPost = useMemo(
() => graphemeLength <= MAX_GRAPHEME_LENGTH && !isAltTextRequiredAndMissing, () => graphemeLength <= MAX_GRAPHEME_LENGTH && !isAltTextRequiredAndMissing,
@@ -576,10 +567,10 @@ export const ComposePost = ({
const canSelectImages = const canSelectImages =
images.length < MAX_IMAGES && images.length < MAX_IMAGES &&
!extLink && !extLink &&
videoUploadState.status === 'idle' && videoState.status === 'idle' &&
!videoUploadState.video !videoState.video
const hasMedia = const hasMedia =
images.length > 0 || Boolean(extLink) || Boolean(videoUploadState.video) images.length > 0 || Boolean(extLink) || Boolean(videoState.video)
const onEmojiButtonPress = useCallback(() => { const onEmojiButtonPress = useCallback(() => {
openEmojiPicker?.(textInput.current?.getCursorPosition()) openEmojiPicker?.(textInput.current?.getCursorPosition())
@@ -694,9 +685,7 @@ export const ComposePost = ({
size="small" size="small"
style={[a.rounded_full, a.py_sm]} style={[a.rounded_full, a.py_sm]}
onPress={() => onPressPublish()} onPress={() => onPressPublish()}
disabled={ disabled={videoState.status !== 'idle' && publishOnUpload}>
videoUploadState.status !== 'idle' && publishOnUpload
}>
<ButtonText style={[a.text_md]}> <ButtonText style={[a.text_md]}>
{replyTo ? ( {replyTo ? (
<Trans context="action">Reply</Trans> <Trans context="action">Reply</Trans>
@@ -732,7 +721,7 @@ export const ComposePost = ({
)} )}
<ErrorBanner <ErrorBanner
error={error} error={error}
videoUploadState={videoUploadState} videoState={videoState}
clearError={() => setError('')} clearError={() => setError('')}
clearVideo={clearVideo} clearVideo={clearVideo}
/> />
@@ -798,17 +787,17 @@ export const ComposePost = ({
style={[a.w_full, a.mt_lg]} style={[a.w_full, a.mt_lg]}
entering={native(ZoomIn)} entering={native(ZoomIn)}
exiting={native(ZoomOut)}> exiting={native(ZoomOut)}>
{videoUploadState.asset && {videoState.asset &&
(videoUploadState.status === 'compressing' ? ( (videoState.status === 'compressing' ? (
<VideoTranscodeProgress <VideoTranscodeProgress
asset={videoUploadState.asset} asset={videoState.asset}
progress={videoUploadState.progress} progress={videoState.progress}
clear={clearVideo} clear={clearVideo}
/> />
) : videoUploadState.video ? ( ) : videoState.video ? (
<VideoPreview <VideoPreview
asset={videoUploadState.asset} asset={videoState.asset}
video={videoUploadState.video} video={videoState.video}
setDimensions={updateVideoDimensions} setDimensions={updateVideoDimensions}
clear={clearVideo} clear={clearVideo}
/> />
@@ -854,9 +843,8 @@ export const ComposePost = ({
t.atoms.border_contrast_medium, t.atoms.border_contrast_medium,
styles.bottomBar, styles.bottomBar,
]}> ]}>
{videoUploadState.status !== 'idle' && {videoState.status !== 'idle' && videoState.status !== 'done' ? (
videoUploadState.status !== 'done' ? ( <VideoUploadToolbar state={videoState} />
<VideoUploadToolbar state={videoUploadState} />
) : ( ) : (
<ToolbarWrapper style={[a.flex_row, a.align_center, a.gap_xs]}> <ToolbarWrapper style={[a.flex_row, a.align_center, a.gap_xs]}>
<SelectPhotoBtn <SelectPhotoBtn
@@ -1121,12 +1109,12 @@ const styles = StyleSheet.create({
function ErrorBanner({ function ErrorBanner({
error: standardError, error: standardError,
videoUploadState, videoState,
clearError, clearError,
clearVideo, clearVideo,
}: { }: {
error: string error: string
videoUploadState: VideoUploadState | NoVideoState videoState: VideoState | NoVideoState
clearError: () => void clearError: () => void
clearVideo: () => void clearVideo: () => void
}) { }) {
@@ -1134,7 +1122,7 @@ function ErrorBanner({
const {_} = useLingui() const {_} = useLingui()
const videoError = const videoError =
videoUploadState.status === 'error' ? videoUploadState.error : undefined videoState.status === 'error' ? videoState.error : undefined
const error = standardError || videoError const error = standardError || videoError
const onClearError = () => { const onClearError = () => {
@@ -1176,7 +1164,7 @@ function ErrorBanner({
<ButtonIcon icon={X} /> <ButtonIcon icon={X} />
</Button> </Button>
</View> </View>
{videoError && videoUploadState.jobId && ( {videoError && videoState.jobId && (
<NewText <NewText
style={[ style={[
{paddingLeft: 28}, {paddingLeft: 28},
@@ -1185,7 +1173,7 @@ function ErrorBanner({
a.leading_snug, a.leading_snug,
t.atoms.text_contrast_low, t.atoms.text_contrast_low,
]}> ]}>
<Trans>Job ID: {videoUploadState.jobId}</Trans> <Trans>Job ID: {videoState.jobId}</Trans>
</NewText> </NewText>
)} )}
</View> </View>
@@ -1211,7 +1199,7 @@ function ToolbarWrapper({
) )
} }
function VideoUploadToolbar({state}: {state: VideoUploadState}) { function VideoUploadToolbar({state}: {state: VideoState}) {
const t = useTheme() const t = useTheme()
const {_} = useLingui() const {_} = useLingui()
const progress = state.progress const progress = state.progress
+1 -1
View File
@@ -21,7 +21,7 @@ import {ComposerImage, cropImage} from '#/state/gallery'
import {Text} from '#/view/com/util/text/Text' import {Text} from '#/view/com/util/text/Text'
import {useTheme} from '#/alf' import {useTheme} from '#/alf'
import * as Dialog from '#/components/Dialog' import * as Dialog from '#/components/Dialog'
import {ComposerAction} from '../state' import {ComposerAction} from '../state/composer'
import {EditImageDialog} from './EditImageDialog' import {EditImageDialog} from './EditImageDialog'
import {ImageAltTextDialog} from './ImageAltTextDialog' import {ImageAltTextDialog} from './ImageAltTextDialog'
@@ -1,13 +1,8 @@
import {ImagePickerAsset} from 'expo-image-picker' import {ImagePickerAsset} from 'expo-image-picker'
import {ComposerImage, createInitialImages} from '#/state/gallery' import {ComposerImage, createInitialImages} from '#/state/gallery'
import {
createVideoState,
VideoAction,
videoReducer,
VideoState,
} from '#/state/queries/video/video'
import {ComposerOpts} from '#/state/shell/composer' import {ComposerOpts} from '#/state/shell/composer'
import {createVideoState, VideoAction, videoReducer, VideoState} from './video'
type PostRecord = { type PostRecord = {
uri: string uri: string
@@ -4,6 +4,8 @@ import {JobStatus} from '@atproto/api/dist/client/types/app/bsky/video/defs'
import {I18n} from '@lingui/core' import {I18n} from '@lingui/core'
import {msg} from '@lingui/macro' 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 {AbortError} from '#/lib/async/cancelable'
import {compressVideo} from '#/lib/media/video/compress' import {compressVideo} from '#/lib/media/video/compress'
import { import {
@@ -13,8 +15,6 @@ import {
} from '#/lib/media/video/errors' } from '#/lib/media/video/errors'
import {CompressedVideo} from '#/lib/media/video/types' import {CompressedVideo} from '#/lib/media/video/types'
import {logger} from '#/logger' import {logger} from '#/logger'
import {createVideoAgent} from '#/state/queries/video/util'
import {uploadVideo} from '#/state/queries/video/video-upload'
export type VideoAction = export type VideoAction =
| { | {