Skip to content

Commit

Permalink
Merge branch 'epic-base' into deploy/epic
Browse files Browse the repository at this point in the history
  • Loading branch information
samchuk-vlad committed Jul 16, 2024
2 parents 6dcabc8 + a82c471 commit 5ce2d8e
Show file tree
Hide file tree
Showing 9 changed files with 74 additions and 62 deletions.
12 changes: 12 additions & 0 deletions src/components/chats/ChatList/ChatList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,18 @@ function ChatListContent({
onlyDisplayUnapprovedMessages,
})

const filteredIdsCount = messageIds.length
const unfilteredIdsCount = unfilteredMessageIds.length
useEffect(() => {
if (!scrollContainerRef.current) return
if (
scrollContainerRef.current.scrollHeight <=
scrollContainerRef.current.clientHeight
) {
loadMore()
}
}, [filteredIdsCount, unfilteredIdsCount, loadMore, scrollContainerRef])

const lastFocusedTime = useLastFocusedMessageTime(chatId, messageIds[0] ?? '')

useEffect(() => {
Expand Down
41 changes: 21 additions & 20 deletions src/components/chats/UnapprovedMemeCount.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import ForbiddenImage from '@/assets/emojis/forbidden.png'
import TimeImage from '@/assets/emojis/time.png'
import { env } from '@/env.mjs'
import { getBlockedResourcesQuery } from '@/services/datahub/moderation/query'
import { getUnapprovedMemesCountQuery } from '@/services/datahub/posts/query'
import { getUserPostedMemesForCountQuery } from '@/services/datahub/posts/query'
import { cx } from '@/utils/class-names'
import Image from 'next/image'
import { useMemo } from 'react'
Expand All @@ -17,8 +17,8 @@ export default function UnapprovedMemeCount({
chatId: string
className?: string
}) {
const { data: count, isLoading: loadingUnapproved } =
getUnapprovedMemesCountQuery.useQuery({
const { data, isLoading: loadingMemes } =
getUserPostedMemesForCountQuery.useQuery({
address,
chatId,
})
Expand All @@ -30,25 +30,26 @@ export default function UnapprovedMemeCount({
})

const isLoading =
loadingUnapproved || loadingBlockedInApp || loadingBlockedInContest
const blockedCount = useMemo(() => {
if (isLoading) return null
const blockedInAppCount =
blockedInApp?.blockedResources.postId?.filter((id) =>
count?.ids.includes(id)
).length ?? 0
const blockedInChatCount =
blockedInChat?.blockedResources.postId?.filter((id) =>
count?.ids.includes(id)
).length ?? 0
return blockedInAppCount + blockedInChatCount
}, [count, blockedInApp, blockedInChat, isLoading])
loadingMemes || loadingBlockedInApp || loadingBlockedInContest
const { blocked, approved, unapproved } = useMemo(() => {
if (isLoading) return { blocked: 0, approved: 0, unapproved: 0 }
let blocked = 0
let approved = 0
let unapproved = 0
data?.forEach((meme) => {
if (
blockedInApp?.blockedResources.postId.includes(meme.id) ||
blockedInChat?.blockedResources.postId.includes(meme.id)
) {
blocked++
} else if (meme.approvedInRootPost) approved++
else unapproved++
})
return { blocked, approved, unapproved }
}, [data, blockedInApp, blockedInChat, isLoading])

if (isLoading || loadingBlockedInApp || loadingBlockedInContest) return null

const approved = count?.approved ?? 0
const unapproved = count?.unapproved ?? 0

return (
<div
className={cx(
Expand All @@ -59,7 +60,7 @@ export default function UnapprovedMemeCount({
<div className='flex items-center gap-1'>
<div className='flex items-center gap-1'>
<Image src={ForbiddenImage} className='h-4 w-4' alt='' />
<span>{blockedCount}</span>
<span>{blocked}</span>
</div>
<span>/</span>
<div className='flex items-center gap-1'>
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 @@ -209,7 +209,7 @@ export default function CommonChatItem({
</div>
)}

{showApproveButton && inView && (
{showApproveButton && !isMyMessage && inView && (
<UnapprovedMemeCount
className='absolute bottom-14 right-1.5 z-10 bg-black/50 text-white'
address={ownerId}
Expand Down
6 changes: 3 additions & 3 deletions src/components/modals/MemeOnReviewModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import Check from '@/assets/emojis/check.png'
import Time from '@/assets/emojis/time.png'
import { MIN_MEME_FOR_REVIEW } from '@/constants/chat'
import { getTokenomicsMetadataQuery } from '@/services/datahub/content-staking/query'
import { getUnapprovedMemesCountQuery } from '@/services/datahub/posts/query'
import { getUserPostedMemesForCountQuery } from '@/services/datahub/posts/query'
import { useMyMainAddress } from '@/stores/my-account'
import Image from 'next/image'
import Button from '../Button'
Expand All @@ -14,13 +14,13 @@ export default function MemeOnReviewModal({
}: ModalFunctionalityProps & { chatId: string }) {
const myAddress = useMyMainAddress() ?? ''
const { data: tokenomics } = getTokenomicsMetadataQuery.useQuery(null)
const { data: count } = getUnapprovedMemesCountQuery.useQuery(
const { data: postedMemes } = getUserPostedMemesForCountQuery.useQuery(
{ address: myAddress, chatId },
{
enabled: props.isOpen,
}
)
const sentMeme = (count?.unapproved ?? 0) + (count?.approved ?? 0)
const sentMeme = postedMemes?.length ?? 0
const remaining = MIN_MEME_FOR_REVIEW - sentMeme

const description =
Expand Down
1 change: 1 addition & 0 deletions src/pages/_app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,7 @@ function SessionAccountChecker() {
function DatahubHealthChecker() {
const { data } = getDatahubHealthQuery.useQuery(null, {
refetchInterval: 10_000,
retry: false,
})
const currentId = useRef<string | number>('')
useEffect(() => {
Expand Down
23 changes: 14 additions & 9 deletions src/services/datahub/events/subscription.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import {
} from '../generated-query'
import { getSocialProfileQuery } from '../identity/query'
import { callIdToPostIdMap } from '../posts/mutation'
import { getUnapprovedMemesCountQuery } from '../posts/query'
import { getUserPostedMemesForCountQuery } from '../posts/query'
import { getProfileQuery } from '../profiles/query'
import { getGamificationTasksErrorQuery } from '../tasks/query'
import { datahubSubscription } from '../utils'
Expand Down Expand Up @@ -236,7 +236,9 @@ async function processSubscriptionEvent(
if (eventData.meta.callName === SocialCallName.SynthSetPostApproveStatus) {
const extension = eventData.meta.extension
const creatorAddress = extension?.creatorAddress ?? ''
const postId = extension?.postId ?? ''
const rootPostId = extension?.rootPostId ?? ''
const newStatus = extension?.newStatus ?? false
const profile = getProfileQuery.getQueryData(client, creatorAddress)
toast.custom((t) => (
<Toast
Expand All @@ -247,16 +249,19 @@ async function processSubscriptionEvent(
}`}
/>
))
getUnapprovedMemesCountQuery.setQueryData(
getUserPostedMemesForCountQuery.setQueryData(
client,
{ address: creatorAddress, chatId: rootPostId },
(oldCount) => {
if (!oldCount) return oldCount
return {
...oldCount,
approved: oldCount.approved + 1,
unapproved: oldCount.unapproved - 1,
}
(oldData) => {
if (!oldData) return oldData
const index = oldData.findIndex((meme) => meme.id === postId)
if (index === -1) return oldData
const meme = oldData[index]
return [
...oldData.slice(0, index),
{ ...meme, approvedInRootPost: newStatus },
...oldData.slice(index + 1),
]
}
)
return
Expand Down
6 changes: 0 additions & 6 deletions src/services/datahub/moderation/subscription.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import {
SubscribeOrganizationSubscription,
} from '../generated-query'
import { isPersistentId } from '../posts/fetcher'
import { getUnapprovedMemesCountQuery } from '../posts/query'
import { datahubSubscription, isDatahubAvailable } from '../utils'
import {
getBlockedInAppDetailedQuery,
Expand Down Expand Up @@ -313,11 +312,6 @@ async function processBlockedResources(
getPostQuery.fetchQuery(queryClient, entity.rootPostId),
] as const)

getUnapprovedMemesCountQuery.invalidate(queryClient, {
chatId: entity.rootPostId,
address: message?.struct.ownerId ?? '',
})

if (message?.struct.ownerId === myAddress) {
toast.custom((t) => (
<Toast
Expand Down
16 changes: 5 additions & 11 deletions src/services/datahub/posts/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -545,7 +545,7 @@ export const getTimeLeftUntilCanPostQuery = createQuery({
}),
})

const GET_UNAPPROVED_MEMES_COUNT = gql`
const GET_USER_POSTED_MEMES_FOR_COUNT = gql`
query GetUnapprovedMemesCount($address: String!, $postId: String!) {
posts(
args: {
Expand All @@ -564,23 +564,17 @@ const GET_UNAPPROVED_MEMES_COUNT = gql`
}
}
`
export const getUnapprovedMemesCountQuery = createQuery({
key: 'unapprovedMemesCount',
export const getUserPostedMemesForCountQuery = createQuery({
key: 'userPostedMemesForCount',
fetcher: async ({ address, chatId }: { chatId: string; address: string }) => {
const res = await datahubQueryRequest<
GetUnapprovedMemesCountQuery,
GetUnapprovedMemesCountQueryVariables
>({
document: GET_UNAPPROVED_MEMES_COUNT,
document: GET_USER_POSTED_MEMES_FOR_COUNT,
variables: { address, postId: chatId },
})
let unapproved = 0
let approved = 0
res.posts.data.forEach((post) => {
if (post.approvedInRootPost) approved++
else unapproved++
})
return { unapproved, approved, ids: res.posts.data.map((post) => post.id) }
return res.posts.data
},
defaultConfigGenerator: (params) => ({
enabled: !!params?.address && !!params.chatId,
Expand Down
29 changes: 17 additions & 12 deletions src/services/datahub/posts/subscription.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import {
getPaginatedPostIdsByPostId,
getPostMetadataQuery,
getTimeLeftUntilCanPostQuery,
getUnapprovedMemesCountQuery,
getUserPostedMemesForCountQuery,
} from './query'

// Note: careful when using this in several places, if you have 2 places, the first one will be the one subscribing
Expand Down Expand Up @@ -212,22 +212,27 @@ async function processMessage(
processUnapprovedMeme()
async function processUnapprovedMeme() {
if (ownerId) {
const cachedCount = getUnapprovedMemesCountQuery.getQueryData(
const cached = getUserPostedMemesForCountQuery.getQueryData(
queryClient,
{ address: ownerId, chatId: rootPostId ?? '' }
)
if (cachedCount) {
getUnapprovedMemesCountQuery.setQueryData(
if (cached) {
getUserPostedMemesForCountQuery.setQueryData(
queryClient,
{ address: ownerId, chatId: rootPostId ?? '' },
(count) => ({
ids: [...(count?.ids ?? []), entity.id],
unapproved: (count?.unapproved ?? 0) + 1,
approved: count?.approved ?? 0,
})
(old) => {
if (!old) return old
return [
...old,
{
id: entity.id,
approvedInRootPost: entity.approvedInRootPost,
},
]
}
)
} else if (isCurrentOwner) {
await getUnapprovedMemesCountQuery.fetchQuery(queryClient, {
await getUserPostedMemesForCountQuery.fetchQuery(queryClient, {
address: ownerId,
chatId: rootPostId ?? '',
})
Expand All @@ -238,11 +243,11 @@ async function processMessage(
getTimeLeftUntilCanPostQuery.setQueryData(queryClient, myAddress, 0)
lastSentMessageStorage.remove()

const count = getUnapprovedMemesCountQuery.getQueryData(
const postedMeme = getUserPostedMemesForCountQuery.getQueryData(
queryClient,
{ address: myAddress, chatId: rootPostId ?? '' }
)
const sentMeme = count.approved + count.unapproved
const sentMeme = postedMeme.length
if (sentMeme === 1 || sentMeme === 3) {
useMessageData.getState().setOpenMessageModal('on-review')
} else {
Expand Down

0 comments on commit 5ce2d8e

Please sign in to comment.