Derive shadow like count (#2616)

zio/stable
dan 2024-01-25 21:21:07 +00:00 committed by GitHub
parent a588b0d548
commit 3b26b32f7f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 27 additions and 31 deletions

View File

@ -12,9 +12,7 @@ export type {Shadow} from './types'
export interface PostShadow { export interface PostShadow {
likeUri: string | undefined likeUri: string | undefined
likeCount: number | undefined
repostUri: string | undefined repostUri: string | undefined
repostCount: number | undefined
isDeleted: boolean isDeleted: boolean
} }
@ -62,11 +60,31 @@ function mergeShadow(
if (shadow.isDeleted) { if (shadow.isDeleted) {
return POST_TOMBSTONE return POST_TOMBSTONE
} }
const wasLiked = !!post.viewer?.like
const isLiked = !!shadow.likeUri
let likeCount = post.likeCount ?? 0
if (wasLiked && !isLiked) {
likeCount--
} else if (!wasLiked && isLiked) {
likeCount++
}
likeCount = Math.max(0, likeCount)
const wasReposted = !!post.viewer?.repost
const isReposted = !!shadow.repostUri
let repostCount = post.repostCount ?? 0
if (wasReposted && !isReposted) {
repostCount--
} else if (!wasReposted && isReposted) {
repostCount++
}
repostCount = Math.max(0, repostCount)
return castAsShadow({ return castAsShadow({
...post, ...post,
likeCount: 'likeCount' in shadow ? shadow.likeCount : post.likeCount, likeCount: likeCount,
repostCount: repostCount: repostCount,
'repostCount' in shadow ? shadow.repostCount : post.repostCount,
viewer: { viewer: {
...(post.viewer || {}), ...(post.viewer || {}),
like: 'likeUri' in shadow ? shadow.likeUri : post.viewer?.like, like: 'likeUri' in shadow ? shadow.likeUri : post.viewer?.like,

View File

@ -59,13 +59,12 @@ export function usePostLikeMutation() {
return useMutation< return useMutation<
{uri: string}, // responds with the uri of the like {uri: string}, // responds with the uri of the like
Error, Error,
{uri: string; cid: string; likeCount: number} // the post's uri, cid, and likes {uri: string; cid: string} // the post's uri and cid
>({ >({
mutationFn: post => getAgent().like(post.uri, post.cid), mutationFn: post => getAgent().like(post.uri, post.cid),
onMutate(variables) { onMutate(variables) {
// optimistically update the post-shadow // optimistically update the post-shadow
updatePostShadow(variables.uri, { updatePostShadow(variables.uri, {
likeCount: variables.likeCount + 1,
likeUri: 'pending', likeUri: 'pending',
}) })
}, },
@ -79,7 +78,6 @@ export function usePostLikeMutation() {
onError(error, variables) { onError(error, variables) {
// revert the optimistic update // revert the optimistic update
updatePostShadow(variables.uri, { updatePostShadow(variables.uri, {
likeCount: variables.likeCount,
likeUri: undefined, likeUri: undefined,
}) })
}, },
@ -87,11 +85,7 @@ export function usePostLikeMutation() {
} }
export function usePostUnlikeMutation() { export function usePostUnlikeMutation() {
return useMutation< return useMutation<void, Error, {postUri: string; likeUri: string}>({
void,
Error,
{postUri: string; likeUri: string; likeCount: number}
>({
mutationFn: async ({likeUri}) => { mutationFn: async ({likeUri}) => {
await getAgent().deleteLike(likeUri) await getAgent().deleteLike(likeUri)
track('Post:Unlike') track('Post:Unlike')
@ -99,14 +93,12 @@ export function usePostUnlikeMutation() {
onMutate(variables) { onMutate(variables) {
// optimistically update the post-shadow // optimistically update the post-shadow
updatePostShadow(variables.postUri, { updatePostShadow(variables.postUri, {
likeCount: variables.likeCount - 1,
likeUri: undefined, likeUri: undefined,
}) })
}, },
onError(error, variables) { onError(error, variables) {
// revert the optimistic update // revert the optimistic update
updatePostShadow(variables.postUri, { updatePostShadow(variables.postUri, {
likeCount: variables.likeCount,
likeUri: variables.likeUri, likeUri: variables.likeUri,
}) })
}, },
@ -117,13 +109,12 @@ export function usePostRepostMutation() {
return useMutation< return useMutation<
{uri: string}, // responds with the uri of the repost {uri: string}, // responds with the uri of the repost
Error, Error,
{uri: string; cid: string; repostCount: number} // the post's uri, cid, and reposts {uri: string; cid: string} // the post's uri and cid
>({ >({
mutationFn: post => getAgent().repost(post.uri, post.cid), mutationFn: post => getAgent().repost(post.uri, post.cid),
onMutate(variables) { onMutate(variables) {
// optimistically update the post-shadow // optimistically update the post-shadow
updatePostShadow(variables.uri, { updatePostShadow(variables.uri, {
repostCount: variables.repostCount + 1,
repostUri: 'pending', repostUri: 'pending',
}) })
}, },
@ -137,7 +128,6 @@ export function usePostRepostMutation() {
onError(error, variables) { onError(error, variables) {
// revert the optimistic update // revert the optimistic update
updatePostShadow(variables.uri, { updatePostShadow(variables.uri, {
repostCount: variables.repostCount,
repostUri: undefined, repostUri: undefined,
}) })
}, },
@ -145,11 +135,7 @@ export function usePostRepostMutation() {
} }
export function usePostUnrepostMutation() { export function usePostUnrepostMutation() {
return useMutation< return useMutation<void, Error, {postUri: string; repostUri: string}>({
void,
Error,
{postUri: string; repostUri: string; repostCount: number}
>({
mutationFn: async ({repostUri}) => { mutationFn: async ({repostUri}) => {
await getAgent().deleteRepost(repostUri) await getAgent().deleteRepost(repostUri)
track('Post:Unrepost') track('Post:Unrepost')
@ -157,14 +143,12 @@ export function usePostUnrepostMutation() {
onMutate(variables) { onMutate(variables) {
// optimistically update the post-shadow // optimistically update the post-shadow
updatePostShadow(variables.postUri, { updatePostShadow(variables.postUri, {
repostCount: variables.repostCount - 1,
repostUri: undefined, repostUri: undefined,
}) })
}, },
onError(error, variables) { onError(error, variables) {
// revert the optimistic update // revert the optimistic update
updatePostShadow(variables.postUri, { updatePostShadow(variables.postUri, {
repostCount: variables.repostCount,
repostUri: variables.repostUri, repostUri: variables.repostUri,
}) })
}, },

View File

@ -73,20 +73,17 @@ let PostCtrls = ({
postLikeMutation.mutate({ postLikeMutation.mutate({
uri: post.uri, uri: post.uri,
cid: post.cid, cid: post.cid,
likeCount: post.likeCount || 0,
}) })
} else { } else {
postUnlikeMutation.mutate({ postUnlikeMutation.mutate({
postUri: post.uri, postUri: post.uri,
likeUri: post.viewer.like, likeUri: post.viewer.like,
likeCount: post.likeCount || 0,
}) })
} }
}, [ }, [
post.viewer?.like, post.viewer?.like,
post.uri, post.uri,
post.cid, post.cid,
post.likeCount,
postLikeMutation, postLikeMutation,
postUnlikeMutation, postUnlikeMutation,
]) ])
@ -98,20 +95,17 @@ let PostCtrls = ({
postRepostMutation.mutate({ postRepostMutation.mutate({
uri: post.uri, uri: post.uri,
cid: post.cid, cid: post.cid,
repostCount: post.repostCount || 0,
}) })
} else { } else {
postUnrepostMutation.mutate({ postUnrepostMutation.mutate({
postUri: post.uri, postUri: post.uri,
repostUri: post.viewer.repost, repostUri: post.viewer.repost,
repostCount: post.repostCount || 0,
}) })
} }
}, [ }, [
post.uri, post.uri,
post.cid, post.cid,
post.viewer?.repost, post.viewer?.repost,
post.repostCount,
closeModal, closeModal,
postRepostMutation, postRepostMutation,
postUnrepostMutation, postUnrepostMutation,