Always bind post actions to an ID

This commit is contained in:
Dan Abramov
2024-10-27 01:00:27 +01:00
parent 101281e650
commit 405f55fb1d
2 changed files with 27 additions and 28 deletions
+23 -21
View File
@@ -55,6 +55,7 @@ import {until} from '#/lib/async/until'
import {MAX_GRAPHEME_LENGTH} from '#/lib/constants' import {MAX_GRAPHEME_LENGTH} from '#/lib/constants'
import {useAnimatedScrollHandler} from '#/lib/hooks/useAnimatedScrollHandler_FIXED' import {useAnimatedScrollHandler} from '#/lib/hooks/useAnimatedScrollHandler_FIXED'
import {useIsKeyboardVisible} from '#/lib/hooks/useIsKeyboardVisible' import {useIsKeyboardVisible} from '#/lib/hooks/useIsKeyboardVisible'
import {useNonReactiveCallback} from '#/lib/hooks/useNonReactiveCallback'
import {usePalette} from '#/lib/hooks/usePalette' import {usePalette} from '#/lib/hooks/usePalette'
import {useWebMediaQueries} from '#/lib/hooks/useWebMediaQueries' import {useWebMediaQueries} from '#/lib/hooks/useWebMediaQueries'
import {logEvent} from '#/lib/statsig/statsig' import {logEvent} from '#/lib/statsig/statsig'
@@ -169,16 +170,19 @@ export const ComposePost = ({
// TODO: Display drafts for other posts in the thread. // TODO: Display drafts for other posts in the thread.
const thread = composerState.thread const thread = composerState.thread
const draft = thread.posts[composerState.activePostIndex] const draft = thread.posts[composerState.activePostIndex]
const dispatch = useCallback((postAction: PostAction) => { const dispatch = useCallback(
composerDispatch({ (postAction: PostAction) => {
type: 'update_post', composerDispatch({
postId: undefined, // Active post type: 'update_post',
postAction, postId: draft.id, // Active post
}) postAction,
}, []) })
},
[draft.id],
)
const selectVideo = React.useCallback( const selectVideo = React.useCallback(
(postId: string | undefined, asset: ImagePickerAsset) => { (postId: string, asset: ImagePickerAsset) => {
const abortController = new AbortController() const abortController = new AbortController()
composerDispatch({ composerDispatch({
type: 'update_post', type: 'update_post',
@@ -210,21 +214,19 @@ export const ComposePost = ({
[_, agent, currentDid, composerDispatch], [_, agent, currentDid, composerDispatch],
) )
// Whenever we receive an initial video uri, we should immediately run compression if necessary const onInitVideo = useNonReactiveCallback(() => {
useEffect(() => {
if (initVideoUri) { if (initVideoUri) {
selectVideo(undefined, initVideoUri) selectVideo(draft.id, initVideoUri)
} }
}, [initVideoUri, selectVideo]) })
useEffect(() => {
onInitVideo()
}, [onInitVideo])
const clearVideo = React.useCallback( const clearVideo = React.useCallback(
(postId: string | undefined) => { (postId: string) => {
let post: PostDraft | undefined const post = thread.posts.find(p => p.id === postId)
if (postId !== undefined) {
post = thread.posts.find(p => p.id === postId)
} else {
post = thread.posts[composerState.activePostIndex]
}
const postMedia = post?.embed.media const postMedia = post?.embed.media
if (postMedia?.type === 'video') { if (postMedia?.type === 'video') {
postMedia.video.abortController.abort() postMedia.video.abortController.abort()
@@ -237,7 +239,7 @@ export const ComposePost = ({
}) })
} }
}, },
[composerState, thread, composerDispatch], [thread, composerDispatch],
) )
const [publishOnUpload, setPublishOnUpload] = useState(false) const [publishOnUpload, setPublishOnUpload] = useState(false)
@@ -569,7 +571,7 @@ export const ComposePost = ({
dispatch={dispatch} dispatch={dispatch}
onError={setError} onError={setError}
onEmojiButtonPress={onEmojiButtonPress} onEmojiButtonPress={onEmojiButtonPress}
onSelectVideo={asset => selectVideo(undefined, asset)} onSelectVideo={asset => selectVideo(draft.id, asset)}
/> />
</View> </View>
+4 -7
View File
@@ -93,7 +93,7 @@ export type ComposerAction =
| {type: 'update_threadgate'; threadgate: ThreadgateAllowUISetting[]} | {type: 'update_threadgate'; threadgate: ThreadgateAllowUISetting[]}
| { | {
type: 'update_post' type: 'update_post'
postId: string | undefined // If undefined, updates active post postId: string
postAction: PostAction postAction: PostAction
} }
@@ -124,12 +124,9 @@ export function composerReducer(
} }
case 'update_post': { case 'update_post': {
let nextPosts = state.thread.posts let nextPosts = state.thread.posts
let postIndex = -1 const postIndex = state.thread.posts.findIndex(
if (action.postId !== undefined) { p => p.id === action.postId,
postIndex = state.thread.posts.findIndex(p => p.id === action.postId) )
} else {
postIndex = state.activePostIndex
}
if (postIndex !== -1) { if (postIndex !== -1) {
nextPosts = state.thread.posts.slice() nextPosts = state.thread.posts.slice()
nextPosts[postIndex] = postReducer( nextPosts[postIndex] = postReducer(