From a137a552b4f0d3b1d403c5c9f9acfbcfa1fafa20 Mon Sep 17 00:00:00 2001 From: Dan Abramov Date: Thu, 3 Oct 2024 21:48:30 +0900 Subject: [PATCH] Implement the reducer --- src/view/com/composer/Composer.tsx | 2 +- src/view/com/composer/state/composer.ts | 97 ++++++++++++++++++++++--- 2 files changed, 86 insertions(+), 13 deletions(-) diff --git a/src/view/com/composer/Composer.tsx b/src/view/com/composer/Composer.tsx index aaab588c91..b5f2d84d4f 100644 --- a/src/view/com/composer/Composer.tsx +++ b/src/view/com/composer/Composer.tsx @@ -337,9 +337,9 @@ export const ComposePost = ({ const onNewLink = useCallback( (uri: string) => { + dispatch({type: 'embed_add_uri', uri}) if (extLink != null) return setExtLink({uri, isLoading: true}) - dispatch({type: 'embed_add_uri', uri}) }, [extLink, setExtLink], ) diff --git a/src/view/com/composer/state/composer.ts b/src/view/com/composer/state/composer.ts index 0e583b8509..769a0521d4 100644 --- a/src/view/com/composer/state/composer.ts +++ b/src/view/com/composer/state/composer.ts @@ -1,5 +1,6 @@ import {ImagePickerAsset} from 'expo-image-picker' +import {isBskyPostUrl} from '#/lib/strings/url-helpers' import {ComposerImage, createInitialImages} from '#/state/gallery' import {Gif} from '#/state/queries/tenor' import {ComposerOpts} from '#/state/shell/composer' @@ -187,28 +188,100 @@ export function composerReducer( } } case 'embed_add_uri': { - // TODO - return state + const prevQuote = state.embed.quote + const prevLink = state.embed.link + let nextQuote = prevQuote + let nextLink = prevLink + if (isBskyPostUrl(action.uri)) { + if (!prevQuote) { + nextQuote = { + type: 'link', + uri: action.uri, + } + } + } else { + if (!prevLink) { + nextLink = { + type: 'link', + uri: action.uri, + } + } + } + return { + ...state, + embed: { + ...state.embed, + quote: nextQuote, + link: nextLink, + }, + } } case 'embed_remove_link': { - // TODO - return state + return { + ...state, + embed: { + ...state.embed, + link: undefined, + }, + } } case 'embed_remove_quote': { - // TODO - return state + return { + ...state, + embed: { + ...state.embed, + quote: undefined, + }, + } } case 'embed_add_gif': { - // TODO - return state + const prevMedia = state.embed.media + let nextMedia = prevMedia + if (!prevMedia) { + nextMedia = { + type: 'gif', + gif: action.gif, + alt: '', + } + } + return { + ...state, + embed: { + ...state.embed, + media: nextMedia, + }, + } } case 'embed_update_gif': { - // TODO - return state + const prevMedia = state.embed.media + let nextMedia = prevMedia + if (prevMedia?.type === 'gif') { + nextMedia = { + ...prevMedia, + alt: action.alt, + } + } + return { + ...state, + embed: { + ...state.embed, + media: nextMedia, + }, + } } case 'embed_remove_gif': { - // TODO - return state + const prevMedia = state.embed.media + let nextMedia = prevMedia + if (prevMedia?.type === 'gif') { + nextMedia = undefined + } + return { + ...state, + embed: { + ...state.embed, + media: nextMedia, + }, + } } default: return state