Skip to content

Commit

Permalink
Derive images state from composer state
Browse files Browse the repository at this point in the history
Co-authored-by: Mary <[email protected]>
  • Loading branch information
gaearon and mary-ext committed Oct 1, 2024
1 parent 5345800 commit 02378dd
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 24 deletions.
19 changes: 10 additions & 9 deletions src/view/com/composer/Composer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ import {logger} from '#/logger'
import {isAndroid, isIOS, isNative, isWeb} from '#/platform/detection'
import {useDialogStateControlContext} from '#/state/dialogs'
import {emitPostCreated} from '#/state/events'
import {ComposerImage, createInitialImages, pasteImage} from '#/state/gallery'
import {ComposerImage, pasteImage} from '#/state/gallery'
import {useModalControls} from '#/state/modals'
import {useModals} from '#/state/modals'
import {useRequireAltTextEnabled} from '#/state/preferences'
Expand Down Expand Up @@ -128,6 +128,8 @@ type CancelRef = {
onPressCancel: () => void
}

const NO_IMAGES: ComposerImage[] = []

type Props = ComposerOpts
export const ComposePost = ({
replyTo,
Expand Down Expand Up @@ -215,16 +217,16 @@ export const ComposePost = ({
)
const [postgate, setPostgate] = useState(createPostgateRecord({post: ''}))

const [images, setImages] = useState<ComposerImage[]>(() =>
createInitialImages(initImageUris),
)

// Not used yet.
// TODO: Move more state here.
const [composerState, dispatch] = useReducer(
composerReducer,
{initImageUris},
createComposerState,
)
let images = NO_IMAGES
if (composerState.embed.media?.type === 'images') {
images = composerState.embed.media.images
}

const onClose = useCallback(() => {
closeComposer()
Expand Down Expand Up @@ -311,13 +313,12 @@ export const ComposePost = ({

const onImageAdd = useCallback(
(next: ComposerImage[]) => {
setImages(prev => prev.concat(next.slice(0, MAX_IMAGES - prev.length)))
dispatch({
type: 'embed_add_images',
images: next,
})
},
[setImages, dispatch],
[dispatch],
)

const onPhotoPasted = useCallback(
Expand Down Expand Up @@ -733,7 +734,7 @@ export const ComposePost = ({
/>
</View>

<Gallery images={images} onChange={setImages} dispatch={dispatch} />
<Gallery images={images} dispatch={dispatch} />
{images.length === 0 && extLink && (
<View style={a.relative}>
<ExternalEmbed
Expand Down
17 changes: 2 additions & 15 deletions src/view/com/composer/photos/Gallery.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ const IMAGE_GAP = 8

interface GalleryProps {
images: ComposerImage[]
onChange: (next: ComposerImage[]) => void
dispatch: (action: ComposerAction) => void
}

Expand Down Expand Up @@ -58,12 +57,7 @@ interface GalleryInnerProps extends GalleryProps {
containerInfo: Dimensions
}

const GalleryInner = ({
images,
containerInfo,
onChange,
dispatch,
}: GalleryInnerProps) => {
const GalleryInner = ({images, containerInfo, dispatch}: GalleryInnerProps) => {
const {isMobile} = useWebMediaQueries()

const {altTextControlStyle, imageControlsStyle, imageStyle} =
Expand Down Expand Up @@ -103,7 +97,7 @@ const GalleryInner = ({
return images.length !== 0 ? (
<>
<View testID="selectedPhotosView" style={styles.gallery}>
{images.map((image, index) => {
{images.map(image => {
return (
<GalleryItem
key={image.source.id}
Expand All @@ -113,16 +107,9 @@ const GalleryInner = ({
imageStyle={imageStyle}
onChange={next => {
dispatch({type: 'embed_update_image', image: next})
onChange(
images.map(i => (i.source === image.source ? next : i)),
)
}}
onRemove={() => {
const next = images.slice()
next.splice(index, 1)

dispatch({type: 'embed_remove_image', image})
onChange(next)
}}
/>
)
Expand Down

0 comments on commit 02378dd

Please sign in to comment.