Extend composer checks to all posts in a thread (#5955)
* Check all posts in a thread * Use thread for more checks
This commit is contained in:
@@ -167,7 +167,8 @@ export const ComposePost = ({
|
|||||||
)
|
)
|
||||||
|
|
||||||
// TODO: Display drafts for other posts in the thread.
|
// TODO: Display drafts for other posts in the thread.
|
||||||
const draft = composerState.thread.posts[composerState.activePostIndex]
|
const thread = composerState.thread
|
||||||
|
const draft = thread.posts[composerState.activePostIndex]
|
||||||
const dispatch = useCallback((postAction: PostAction) => {
|
const dispatch = useCallback((postAction: PostAction) => {
|
||||||
composerDispatch({
|
composerDispatch({
|
||||||
type: 'update_post',
|
type: 'update_post',
|
||||||
@@ -226,9 +227,12 @@ export const ComposePost = ({
|
|||||||
|
|
||||||
const onPressCancel = useCallback(() => {
|
const onPressCancel = useCallback(() => {
|
||||||
if (
|
if (
|
||||||
draft.shortenedGraphemeLength > 0 ||
|
thread.posts.some(
|
||||||
draft.embed.media ||
|
post =>
|
||||||
draft.embed.link
|
post.shortenedGraphemeLength > 0 ||
|
||||||
|
post.embed.media ||
|
||||||
|
post.embed.link,
|
||||||
|
)
|
||||||
) {
|
) {
|
||||||
closeAllDialogs()
|
closeAllDialogs()
|
||||||
Keyboard.dismiss()
|
Keyboard.dismiss()
|
||||||
@@ -236,7 +240,7 @@ export const ComposePost = ({
|
|||||||
} else {
|
} else {
|
||||||
onClose()
|
onClose()
|
||||||
}
|
}
|
||||||
}, [draft, closeAllDialogs, discardPromptControl, onClose])
|
}, [thread, closeAllDialogs, discardPromptControl, onClose])
|
||||||
|
|
||||||
useImperativeHandle(cancelRef, () => ({onPressCancel}))
|
useImperativeHandle(cancelRef, () => ({onPressCancel}))
|
||||||
|
|
||||||
@@ -261,30 +265,38 @@ export const ComposePost = ({
|
|||||||
}, [onPressCancel, closeAllDialogs, closeAllModals])
|
}, [onPressCancel, closeAllDialogs, closeAllModals])
|
||||||
|
|
||||||
const isAltTextRequiredAndMissing = useMemo(() => {
|
const isAltTextRequiredAndMissing = useMemo(() => {
|
||||||
const media = draft.embed.media
|
if (!requireAltTextEnabled) {
|
||||||
if (!requireAltTextEnabled || !media) {
|
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
if (media.type === 'images' && media.images.some(img => !img.alt)) {
|
return thread.posts.some(post => {
|
||||||
return true
|
const media = post.embed.media
|
||||||
}
|
if (media) {
|
||||||
if (media.type === 'gif' && !media.alt) {
|
if (media.type === 'images' && media.images.some(img => !img.alt)) {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
return false
|
if (media.type === 'gif' && !media.alt) {
|
||||||
}, [draft.embed.media, requireAltTextEnabled])
|
return true
|
||||||
|
}
|
||||||
const isEmptyPost =
|
}
|
||||||
draft.richtext.text.trim().length === 0 &&
|
})
|
||||||
!draft.embed.link &&
|
}, [thread, requireAltTextEnabled])
|
||||||
!draft.embed.media &&
|
|
||||||
!draft.embed.quote
|
|
||||||
|
|
||||||
const canPost =
|
const canPost =
|
||||||
draft.shortenedGraphemeLength <= MAX_GRAPHEME_LENGTH &&
|
|
||||||
!isAltTextRequiredAndMissing &&
|
!isAltTextRequiredAndMissing &&
|
||||||
!isEmptyPost &&
|
thread.posts.every(
|
||||||
videoState.status !== 'error'
|
post =>
|
||||||
|
post.shortenedGraphemeLength <= MAX_GRAPHEME_LENGTH &&
|
||||||
|
!(
|
||||||
|
post.richtext.text.trim().length === 0 &&
|
||||||
|
!post.embed.link &&
|
||||||
|
!post.embed.media &&
|
||||||
|
!post.embed.quote
|
||||||
|
) &&
|
||||||
|
!(
|
||||||
|
post.embed.media?.type === 'video' &&
|
||||||
|
post.embed.media.video.status === 'error'
|
||||||
|
),
|
||||||
|
)
|
||||||
|
|
||||||
const onPressPublish = React.useCallback(
|
const onPressPublish = React.useCallback(
|
||||||
async (finishedUploading: boolean) => {
|
async (finishedUploading: boolean) => {
|
||||||
@@ -298,8 +310,12 @@ export const ComposePost = ({
|
|||||||
|
|
||||||
if (
|
if (
|
||||||
!finishedUploading &&
|
!finishedUploading &&
|
||||||
videoState.asset &&
|
thread.posts.some(
|
||||||
videoState.status !== 'done'
|
post =>
|
||||||
|
post.embed.media?.type === 'video' &&
|
||||||
|
post.embed.media.video.asset &&
|
||||||
|
post.embed.media.video.status !== 'done',
|
||||||
|
)
|
||||||
) {
|
) {
|
||||||
setPublishOnUpload(true)
|
setPublishOnUpload(true)
|
||||||
return
|
return
|
||||||
@@ -308,16 +324,11 @@ export const ComposePost = ({
|
|||||||
setError('')
|
setError('')
|
||||||
setIsPublishing(true)
|
setIsPublishing(true)
|
||||||
|
|
||||||
const imageCount =
|
|
||||||
draft.embed.media?.type === 'images'
|
|
||||||
? draft.embed.media.images.length
|
|
||||||
: 0
|
|
||||||
|
|
||||||
let postUri
|
let postUri
|
||||||
try {
|
try {
|
||||||
postUri = (
|
postUri = (
|
||||||
await apilib.post(agent, queryClient, {
|
await apilib.post(agent, queryClient, {
|
||||||
thread: composerState.thread,
|
thread,
|
||||||
replyTo: replyTo?.uri,
|
replyTo: replyTo?.uri,
|
||||||
onStateChange: setPublishingStage,
|
onStateChange: setPublishingStage,
|
||||||
langs: toPostLanguages(langPrefs.postLanguage),
|
langs: toPostLanguages(langPrefs.postLanguage),
|
||||||
@@ -325,8 +336,8 @@ export const ComposePost = ({
|
|||||||
).uri
|
).uri
|
||||||
try {
|
try {
|
||||||
await whenAppViewReady(agent, postUri, res => {
|
await whenAppViewReady(agent, postUri, res => {
|
||||||
const thread = res.data.thread
|
const postedThread = res.data.thread
|
||||||
return AppBskyFeedDefs.isThreadViewPost(thread)
|
return AppBskyFeedDefs.isThreadViewPost(postedThread)
|
||||||
})
|
})
|
||||||
} catch (waitErr: any) {
|
} catch (waitErr: any) {
|
||||||
logger.error(waitErr, {
|
logger.error(waitErr, {
|
||||||
@@ -337,7 +348,7 @@ export const ComposePost = ({
|
|||||||
} catch (e: any) {
|
} catch (e: any) {
|
||||||
logger.error(e, {
|
logger.error(e, {
|
||||||
message: `Composer: create post failed`,
|
message: `Composer: create post failed`,
|
||||||
hasImages: imageCount > 0,
|
hasImages: thread.posts.some(p => p.embed.media?.type === 'images'),
|
||||||
})
|
})
|
||||||
|
|
||||||
let err = cleanError(e.message)
|
let err = cleanError(e.message)
|
||||||
@@ -353,14 +364,21 @@ export const ComposePost = ({
|
|||||||
return
|
return
|
||||||
} finally {
|
} finally {
|
||||||
if (postUri) {
|
if (postUri) {
|
||||||
logEvent('post:create', {
|
let index = 0
|
||||||
imageCount,
|
for (let post of thread.posts) {
|
||||||
isReply: !!replyTo,
|
logEvent('post:create', {
|
||||||
hasLink: !!draft.embed.link,
|
imageCount:
|
||||||
hasQuote: !!draft.embed.quote,
|
post.embed.media?.type === 'images'
|
||||||
langs: langPrefs.postLanguage,
|
? post.embed.media.images.length
|
||||||
logContext: 'Composer',
|
: 0,
|
||||||
})
|
isReply: index > 0 || !!replyTo,
|
||||||
|
hasLink: !!post.embed.link,
|
||||||
|
hasQuote: !!post.embed.quote,
|
||||||
|
langs: langPrefs.postLanguage,
|
||||||
|
logContext: 'Composer',
|
||||||
|
})
|
||||||
|
index++
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (postUri && !replyTo) {
|
if (postUri && !replyTo) {
|
||||||
@@ -370,10 +388,10 @@ export const ComposePost = ({
|
|||||||
if (initQuote) {
|
if (initQuote) {
|
||||||
// We want to wait for the quote count to update before we call `onPost`, which will refetch data
|
// We want to wait for the quote count to update before we call `onPost`, which will refetch data
|
||||||
whenAppViewReady(agent, initQuote.uri, res => {
|
whenAppViewReady(agent, initQuote.uri, res => {
|
||||||
const thread = res.data.thread
|
const quotedThread = res.data.thread
|
||||||
if (
|
if (
|
||||||
AppBskyFeedDefs.isThreadViewPost(thread) &&
|
AppBskyFeedDefs.isThreadViewPost(quotedThread) &&
|
||||||
thread.post.quoteCount !== initQuote.quoteCount
|
quotedThread.post.quoteCount !== initQuote.quoteCount
|
||||||
) {
|
) {
|
||||||
onPost?.(postUri)
|
onPost?.(postUri)
|
||||||
return true
|
return true
|
||||||
@@ -393,8 +411,7 @@ export const ComposePost = ({
|
|||||||
[
|
[
|
||||||
_,
|
_,
|
||||||
agent,
|
agent,
|
||||||
composerState.thread,
|
thread,
|
||||||
draft,
|
|
||||||
canPost,
|
canPost,
|
||||||
isPublishing,
|
isPublishing,
|
||||||
langPrefs.postLanguage,
|
langPrefs.postLanguage,
|
||||||
@@ -403,8 +420,6 @@ export const ComposePost = ({
|
|||||||
initQuote,
|
initQuote,
|
||||||
replyTo,
|
replyTo,
|
||||||
setLangPrefs,
|
setLangPrefs,
|
||||||
videoState.asset,
|
|
||||||
videoState.status,
|
|
||||||
queryClient,
|
queryClient,
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
|
|||||||
Reference in New Issue
Block a user