Bluesky app fork with some witchin' additions 💫

Derive shadow like count (#2616)

authored by danabra.mov and committed by

GitHub 3b26b32f a588b0d5

+27 -31
+23 -5
src/state/cache/post-shadow.ts
··· 12 12 13 13 export interface PostShadow { 14 14 likeUri: string | undefined 15 - likeCount: number | undefined 16 15 repostUri: string | undefined 17 - repostCount: number | undefined 18 16 isDeleted: boolean 19 17 } 20 18 ··· 62 60 if (shadow.isDeleted) { 63 61 return POST_TOMBSTONE 64 62 } 63 + 64 + const wasLiked = !!post.viewer?.like 65 + const isLiked = !!shadow.likeUri 66 + let likeCount = post.likeCount ?? 0 67 + if (wasLiked && !isLiked) { 68 + likeCount-- 69 + } else if (!wasLiked && isLiked) { 70 + likeCount++ 71 + } 72 + likeCount = Math.max(0, likeCount) 73 + 74 + const wasReposted = !!post.viewer?.repost 75 + const isReposted = !!shadow.repostUri 76 + let repostCount = post.repostCount ?? 0 77 + if (wasReposted && !isReposted) { 78 + repostCount-- 79 + } else if (!wasReposted && isReposted) { 80 + repostCount++ 81 + } 82 + repostCount = Math.max(0, repostCount) 83 + 65 84 return castAsShadow({ 66 85 ...post, 67 - likeCount: 'likeCount' in shadow ? shadow.likeCount : post.likeCount, 68 - repostCount: 69 - 'repostCount' in shadow ? shadow.repostCount : post.repostCount, 86 + likeCount: likeCount, 87 + repostCount: repostCount, 70 88 viewer: { 71 89 ...(post.viewer || {}), 72 90 like: 'likeUri' in shadow ? shadow.likeUri : post.viewer?.like,
+4 -20
src/state/queries/post.ts
··· 59 59 return useMutation< 60 60 {uri: string}, // responds with the uri of the like 61 61 Error, 62 - {uri: string; cid: string; likeCount: number} // the post's uri, cid, and likes 62 + {uri: string; cid: string} // the post's uri and cid 63 63 >({ 64 64 mutationFn: post => getAgent().like(post.uri, post.cid), 65 65 onMutate(variables) { 66 66 // optimistically update the post-shadow 67 67 updatePostShadow(variables.uri, { 68 - likeCount: variables.likeCount + 1, 69 68 likeUri: 'pending', 70 69 }) 71 70 }, ··· 79 78 onError(error, variables) { 80 79 // revert the optimistic update 81 80 updatePostShadow(variables.uri, { 82 - likeCount: variables.likeCount, 83 81 likeUri: undefined, 84 82 }) 85 83 }, ··· 87 85 } 88 86 89 87 export function usePostUnlikeMutation() { 90 - return useMutation< 91 - void, 92 - Error, 93 - {postUri: string; likeUri: string; likeCount: number} 94 - >({ 88 + return useMutation<void, Error, {postUri: string; likeUri: string}>({ 95 89 mutationFn: async ({likeUri}) => { 96 90 await getAgent().deleteLike(likeUri) 97 91 track('Post:Unlike') ··· 99 93 onMutate(variables) { 100 94 // optimistically update the post-shadow 101 95 updatePostShadow(variables.postUri, { 102 - likeCount: variables.likeCount - 1, 103 96 likeUri: undefined, 104 97 }) 105 98 }, 106 99 onError(error, variables) { 107 100 // revert the optimistic update 108 101 updatePostShadow(variables.postUri, { 109 - likeCount: variables.likeCount, 110 102 likeUri: variables.likeUri, 111 103 }) 112 104 }, ··· 117 109 return useMutation< 118 110 {uri: string}, // responds with the uri of the repost 119 111 Error, 120 - {uri: string; cid: string; repostCount: number} // the post's uri, cid, and reposts 112 + {uri: string; cid: string} // the post's uri and cid 121 113 >({ 122 114 mutationFn: post => getAgent().repost(post.uri, post.cid), 123 115 onMutate(variables) { 124 116 // optimistically update the post-shadow 125 117 updatePostShadow(variables.uri, { 126 - repostCount: variables.repostCount + 1, 127 118 repostUri: 'pending', 128 119 }) 129 120 }, ··· 137 128 onError(error, variables) { 138 129 // revert the optimistic update 139 130 updatePostShadow(variables.uri, { 140 - repostCount: variables.repostCount, 141 131 repostUri: undefined, 142 132 }) 143 133 }, ··· 145 135 } 146 136 147 137 export function usePostUnrepostMutation() { 148 - return useMutation< 149 - void, 150 - Error, 151 - {postUri: string; repostUri: string; repostCount: number} 152 - >({ 138 + return useMutation<void, Error, {postUri: string; repostUri: string}>({ 153 139 mutationFn: async ({repostUri}) => { 154 140 await getAgent().deleteRepost(repostUri) 155 141 track('Post:Unrepost') ··· 157 143 onMutate(variables) { 158 144 // optimistically update the post-shadow 159 145 updatePostShadow(variables.postUri, { 160 - repostCount: variables.repostCount - 1, 161 146 repostUri: undefined, 162 147 }) 163 148 }, 164 149 onError(error, variables) { 165 150 // revert the optimistic update 166 151 updatePostShadow(variables.postUri, { 167 - repostCount: variables.repostCount, 168 152 repostUri: variables.repostUri, 169 153 }) 170 154 },
-6
src/view/com/util/post-ctrls/PostCtrls.tsx
··· 73 73 postLikeMutation.mutate({ 74 74 uri: post.uri, 75 75 cid: post.cid, 76 - likeCount: post.likeCount || 0, 77 76 }) 78 77 } else { 79 78 postUnlikeMutation.mutate({ 80 79 postUri: post.uri, 81 80 likeUri: post.viewer.like, 82 - likeCount: post.likeCount || 0, 83 81 }) 84 82 } 85 83 }, [ 86 84 post.viewer?.like, 87 85 post.uri, 88 86 post.cid, 89 - post.likeCount, 90 87 postLikeMutation, 91 88 postUnlikeMutation, 92 89 ]) ··· 98 95 postRepostMutation.mutate({ 99 96 uri: post.uri, 100 97 cid: post.cid, 101 - repostCount: post.repostCount || 0, 102 98 }) 103 99 } else { 104 100 postUnrepostMutation.mutate({ 105 101 postUri: post.uri, 106 102 repostUri: post.viewer.repost, 107 - repostCount: post.repostCount || 0, 108 103 }) 109 104 } 110 105 }, [ 111 106 post.uri, 112 107 post.cid, 113 108 post.viewer?.repost, 114 - post.repostCount, 115 109 closeModal, 116 110 postRepostMutation, 117 111 postUnrepostMutation,