Move remaining composer state into reducer (#5623)
Co-authored-by: Mary <git@mary.my.id> Co-authored-by: surfdude29 <149612116+surfdude29@users.noreply.github.com> Co-authored-by: Hailey <me@haileyok.com>
This commit is contained in:
@@ -1,12 +1,17 @@
|
||||
import {ImagePickerAsset} from 'expo-image-picker'
|
||||
import {AppBskyFeedPostgate, RichText} from '@atproto/api'
|
||||
|
||||
import {insertMentionAt} from '#/lib/strings/mention-manip'
|
||||
import {
|
||||
isBskyPostUrl,
|
||||
postUriToRelativePath,
|
||||
toBskyAppUrl,
|
||||
} from '#/lib/strings/url-helpers'
|
||||
import {ComposerImage, createInitialImages} from '#/state/gallery'
|
||||
import {createPostgateRecord} from '#/state/queries/postgate/util'
|
||||
import {Gif} from '#/state/queries/tenor'
|
||||
import {threadgateViewToAllowUISetting} from '#/state/queries/threadgate'
|
||||
import {ThreadgateAllowUISetting} from '#/state/queries/threadgate'
|
||||
import {ComposerOpts} from '#/state/shell/composer'
|
||||
import {createVideoState, VideoAction, videoReducer, VideoState} from './video'
|
||||
|
||||
@@ -41,12 +46,19 @@ export type EmbedDraft = {
|
||||
link: Link | undefined
|
||||
}
|
||||
|
||||
export type ComposerState = {
|
||||
// TODO: Other draft data.
|
||||
export type ComposerDraft = {
|
||||
richtext: RichText
|
||||
labels: string[]
|
||||
postgate: AppBskyFeedPostgate.Record
|
||||
threadgate: ThreadgateAllowUISetting[]
|
||||
embed: EmbedDraft
|
||||
}
|
||||
|
||||
export type ComposerAction =
|
||||
| {type: 'update_richtext'; richtext: RichText}
|
||||
| {type: 'update_labels'; labels: string[]}
|
||||
| {type: 'update_postgate'; postgate: AppBskyFeedPostgate.Record}
|
||||
| {type: 'update_threadgate'; threadgate: ThreadgateAllowUISetting[]}
|
||||
| {type: 'embed_add_images'; images: ComposerImage[]}
|
||||
| {type: 'embed_update_image'; image: ComposerImage}
|
||||
| {type: 'embed_remove_image'; image: ComposerImage}
|
||||
@@ -67,10 +79,34 @@ export type ComposerAction =
|
||||
export const MAX_IMAGES = 4
|
||||
|
||||
export function composerReducer(
|
||||
state: ComposerState,
|
||||
state: ComposerDraft,
|
||||
action: ComposerAction,
|
||||
): ComposerState {
|
||||
): ComposerDraft {
|
||||
switch (action.type) {
|
||||
case 'update_richtext': {
|
||||
return {
|
||||
...state,
|
||||
richtext: action.richtext,
|
||||
}
|
||||
}
|
||||
case 'update_labels': {
|
||||
return {
|
||||
...state,
|
||||
labels: action.labels,
|
||||
}
|
||||
}
|
||||
case 'update_postgate': {
|
||||
return {
|
||||
...state,
|
||||
postgate: action.postgate,
|
||||
}
|
||||
}
|
||||
case 'update_threadgate': {
|
||||
return {
|
||||
...state,
|
||||
threadgate: action.threadgate,
|
||||
}
|
||||
}
|
||||
case 'embed_add_images': {
|
||||
if (action.images.length === 0) {
|
||||
return state
|
||||
@@ -293,12 +329,16 @@ export function composerReducer(
|
||||
}
|
||||
|
||||
export function createComposerState({
|
||||
initText,
|
||||
initMention,
|
||||
initImageUris,
|
||||
initQuoteUri,
|
||||
}: {
|
||||
initText: string | undefined
|
||||
initMention: string | undefined
|
||||
initImageUris: ComposerOpts['imageUris']
|
||||
initQuoteUri: string | undefined
|
||||
}): ComposerState {
|
||||
}): ComposerDraft {
|
||||
let media: ImagesMedia | undefined
|
||||
if (initImageUris?.length) {
|
||||
media = {
|
||||
@@ -317,7 +357,22 @@ export function createComposerState({
|
||||
}
|
||||
}
|
||||
}
|
||||
const initRichText = new RichText({
|
||||
text: initText
|
||||
? initText
|
||||
: initMention
|
||||
? insertMentionAt(
|
||||
`@${initMention}`,
|
||||
initMention.length + 1,
|
||||
`${initMention}`,
|
||||
)
|
||||
: '',
|
||||
})
|
||||
return {
|
||||
richtext: initRichText,
|
||||
labels: [],
|
||||
postgate: createPostgateRecord({post: ''}),
|
||||
threadgate: threadgateViewToAllowUISetting(undefined),
|
||||
embed: {
|
||||
quote,
|
||||
media,
|
||||
|
||||
Reference in New Issue
Block a user