Skip to content

Commit

Permalink
Separate unapproved meme count between different chatIds
Browse files Browse the repository at this point in the history
  • Loading branch information
teodorus-nathaniel committed Jul 10, 2024
1 parent 5434cfc commit b09e543
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 22 deletions.
14 changes: 11 additions & 3 deletions src/components/chats/UnapprovedMemeCount.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
import { getUnapprovedMemesCountQuery } from '@/services/datahub/posts/query'

export default function UnapprovedMemeCount({ address }: { address: string }) {
const { data: count, isLoading } =
getUnapprovedMemesCountQuery.useQuery(address)
export default function UnapprovedMemeCount({
address,
chatId,
}: {
address: string
chatId: string
}) {
const { data: count, isLoading } = getUnapprovedMemesCountQuery.useQuery({
address,
chatId,
})
if (isLoading) return null

return (
Expand Down
2 changes: 1 addition & 1 deletion src/components/extensions/common/CommonChatItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ export default function CommonChatItem({
className={cx('text-sm font-medium text-text-secondary')}
/>
{showApproveButton && inView && (
<UnapprovedMemeCount address={ownerId} />
<UnapprovedMemeCount address={ownerId} chatId={chatId} />
)}
{/* <SubTeamLabel address={ownerId} /> */}
{othersMessage.checkMark === 'top' &&
Expand Down
9 changes: 7 additions & 2 deletions src/services/datahub/generated-query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2853,6 +2853,7 @@ export type GetLastPostedMemeQuery = {

export type GetUnapprovedMemesCountQueryVariables = Exact<{
address: Scalars['String']['input']
postId: Scalars['String']['input']
}>

export type GetUnapprovedMemesCountQuery = {
Expand Down Expand Up @@ -3624,10 +3625,14 @@ export const GetLastPostedMeme = gql`
}
`
export const GetUnapprovedMemesCount = gql`
query GetUnapprovedMemesCount($address: String!) {
query GetUnapprovedMemesCount($address: String!, $postId: String!) {
posts(
args: {
filter: { createdByAccountAddress: $address, approvedInRootPost: true }
filter: {
createdByAccountAddress: $address
approvedInRootPost: false
rootPostId: $postId
}
}
) {
total
Expand Down
16 changes: 10 additions & 6 deletions src/services/datahub/posts/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -537,10 +537,14 @@ export const getTimeLeftUntilCanPostQuery = createQuery({
})

const GET_UNAPPROVED_MEMES_COUNT = gql`
query GetUnapprovedMemesCount($address: String!) {
query GetUnapprovedMemesCount($address: String!, $postId: String!) {
posts(
args: {
filter: { createdByAccountAddress: $address, approvedInRootPost: false }
filter: {
createdByAccountAddress: $address
approvedInRootPost: false
rootPostId: $postId
}
}
) {
total
Expand All @@ -549,17 +553,17 @@ const GET_UNAPPROVED_MEMES_COUNT = gql`
`
export const getUnapprovedMemesCountQuery = createQuery({
key: 'unapprovedMemesCount',
fetcher: async (address: string) => {
fetcher: async ({ address, chatId }: { chatId: string; address: string }) => {
const res = await datahubQueryRequest<
GetUnapprovedMemesCountQuery,
GetUnapprovedMemesCountQueryVariables
>({
document: GET_UNAPPROVED_MEMES_COUNT,
variables: { address },
variables: { address, postId: chatId },
})
return res.posts.total ?? 0
},
defaultConfigGenerator: (address) => ({
enabled: !!address,
defaultConfigGenerator: (params) => ({
enabled: !!params?.address && !!params.chatId,
}),
})
23 changes: 13 additions & 10 deletions src/services/datahub/posts/subscription.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,11 @@ async function processMessage(

const newPost = getPostQuery.getQueryData(queryClient, newestId)
const myAddress = getMyMainAddress()
const isCurrentOwner = newPost?.struct.ownerId === myAddress

const rootPostId = entity.rootPost?.persistentId
const ownerId = newPost?.struct.ownerId
const isCurrentOwner = ownerId === myAddress

if (isCreationEvent) {
const tokenomics = await getTokenomicsMetadataQuery.fetchQuery(
queryClient,
Expand All @@ -204,22 +208,22 @@ async function processMessage(
// to not wait for another query to run the other synchronous actions below
processUnapprovedMeme(newPost)
async function processUnapprovedMeme(newPost: PostData) {
if (newPost.struct.ownerId) {
if (ownerId) {
const cachedCount = getUnapprovedMemesCountQuery.getQueryData(
queryClient,
newPost.struct.ownerId
{ address: ownerId, chatId: rootPostId ?? '' }
)
if (typeof cachedCount === 'number') {
getUnapprovedMemesCountQuery.setQueryData(
queryClient,
newPost.struct.ownerId,
{ address: ownerId, chatId: rootPostId ?? '' },
(count) => (count ?? 0) + 1
)
} else if (isCurrentOwner) {
await getUnapprovedMemesCountQuery.fetchQuery(
queryClient,
myAddress
)
await getUnapprovedMemesCountQuery.fetchQuery(queryClient, {
address: ownerId,
chatId: rootPostId ?? '',
})
}
}
if (isCurrentOwner) {
Expand All @@ -229,7 +233,7 @@ async function processMessage(

const count = getUnapprovedMemesCountQuery.getQueryData(
queryClient,
myAddress
{ address: myAddress, chatId: rootPostId ?? '' }
)
const remaining = Math.max(MIN_MEME_FOR_REVIEW - (count ?? 0), 0)
const title =
Expand All @@ -256,7 +260,6 @@ async function processMessage(
}
}

const rootPostId = entity.rootPost?.persistentId
if (!rootPostId) return

if (isCreationEvent && !entity.approvedInRootPost && isCurrentOwner) {
Expand Down

0 comments on commit b09e543

Please sign in to comment.