Skip to content

Commit

Permalink
Update comments count in post page
Browse files Browse the repository at this point in the history
  • Loading branch information
teodorus-nathaniel committed Sep 5, 2023
1 parent f5be6ed commit a0ac798
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 14 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@
"@subsocial/api": "0.8.10",
"@subsocial/definitions": "0.8.10",
"@subsocial/elasticsearch": "0.8.10",
"@subsocial/grill-widget": "^0.0.11",
"@subsocial/grill-widget": "^0.0.12",
"@subsocial/resource-discussions": "^0.0.3",
"@subsocial/utils": "0.8.10",
"@tiptap/extension-highlight": "^2.0.0-beta.33",
Expand Down
13 changes: 8 additions & 5 deletions src/components/chat/ChatIframe.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import grill, { GrillConfig } from '@subsocial/grill-widget'
import grill, { GrillConfig, GrillEventListener } from '@subsocial/grill-widget'
import { Resource } from '@subsocial/resource-discussions'
import { summarize } from '@subsocial/utils'
import clsx from 'clsx'
import { ComponentProps, useEffect } from 'react'
import useWrapInRef from 'src/hooks/useWrapInRef'
import { useSendEvent } from 'src/providers/AnalyticContext'
import { useSetChatTotalMessageCount } from 'src/rtk/app/hooks'
import { useAppSelector } from 'src/rtk/app/store'
import { ChatEntity } from 'src/rtk/features/chat/chatSlice'

Expand All @@ -18,6 +19,7 @@ export default function ChatIframe({ onUnreadCountChange, ...props }: ChatIframe
const entity = useAppSelector(state => state.chat.entity)
const sendEvent = useSendEvent()
const sendEventRef = useWrapInRef(sendEvent)
const setChatTotalMessageCount = useSetChatTotalMessageCount()

useEffect(() => {
if (!entity) return
Expand All @@ -33,10 +35,11 @@ export default function ChatIframe({ onUnreadCountChange, ...props }: ChatIframe
return iframe
}

const listener = onUnreadCountChange
? (count: number) => {
console.log('unread count', count)
onUnreadCountChange(count)
const listener: GrillEventListener | undefined = onUnreadCountChange
? (name, value) => {
const parsedValue = parseInt(value) ?? 0
if (name === 'unread') onUnreadCountChange(parsedValue)
else if (name === 'totalMessage') setChatTotalMessageCount(parsedValue)
}
: undefined
if (listener) {
Expand Down
6 changes: 4 additions & 2 deletions src/components/posts/PostStats.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { nonEmptyStr } from '@subsocial/utils'
import clsx from 'clsx'
import { useState } from 'react'
import { useSetChatOpen } from 'src/rtk/app/hooks'
import { useAppSelector } from 'src/rtk/app/store'
import { idToBn, PostStruct } from 'src/types'
import { MutedSpan } from '../utils/MutedText'
import { Pluralize } from '../utils/Plularize'
Expand All @@ -18,14 +19,15 @@ export const StatsPanel = (props: StatsProps) => {
const setChatOpen = useSetChatOpen()
const [postVotersOpen, setPostVotersOpen] = useState(false)

const { upvotesCount, downvotesCount, repliesCount, sharesCount, id } = post
const totalMessageCount = useAppSelector(state => state.chat.totalMessageCount)
const { upvotesCount, downvotesCount, sharesCount, id } = post
const reactionsCount = upvotesCount + downvotesCount
const showReactionsModal = () => reactionsCount && setPostVotersOpen(true)

const toggleCommentsSection = () => {
setChatOpen(true)
}
const comments = <Pluralize count={repliesCount || 0} singularText='Comment' />
const comments = <Pluralize count={totalMessageCount || 0} singularText='Comment' />

return (
<>
Expand Down
9 changes: 8 additions & 1 deletion src/rtk/features/chat/chatHooks.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
import { useCallback } from 'react'
import { useAppDispatch, useAppSelector } from 'src/rtk/app/store'
import { setChatConfig, setChatOpen } from '../chat/chatSlice'
import { setChatConfig, setChatOpen, setTotalMessageCount } from '../chat/chatSlice'

export function useSetChatTotalMessageCount() {
const dispatch = useAppDispatch()
return useCallback((...params: Parameters<typeof setTotalMessageCount>) => {
dispatch(setTotalMessageCount(...params))
}, [])
}

export function useSetChatEntityConfig() {
const dispatch = useAppDispatch()
Expand Down
8 changes: 7 additions & 1 deletion src/rtk/features/chat/chatSlice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@ type Entity = {
export interface ChatEntity {
isOpen: boolean
entity: Entity
totalMessageCount: number
}

const initialState: ChatEntity = {
isOpen: false,
entity: null,
totalMessageCount: 0,
}

const slice = createSlice({
Expand All @@ -26,10 +28,14 @@ const slice = createSlice({
},
setChatConfig: (state, action: PayloadAction<Entity>) => {
state.entity = action.payload
state.totalMessageCount = 0
},
setTotalMessageCount: (state, action: PayloadAction<number>) => {
state.totalMessageCount = action.payload
},
},
})

export const { setChatConfig, setChatOpen } = slice.actions
export const { setChatConfig, setChatOpen, setTotalMessageCount } = slice.actions

export default slice.reducer
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2395,10 +2395,10 @@
"@elastic/elasticsearch" "7.4.0"
"@subsocial/utils" latest

"@subsocial/grill-widget@^0.0.11":
version "0.0.11"
resolved "https://registry.yarnpkg.com/@subsocial/grill-widget/-/grill-widget-0.0.11.tgz#c77267b1c1344b5535fadfc5c1d7b062862f331f"
integrity sha512-2XOUbd467AeRtPkv2W3oYYfHGJCXAWEDMx6NpXdYs4AMOm1ZtHPjknrQ88/dcZCxDl26XcCRCSysu9n/6qPu2w==
"@subsocial/grill-widget@^0.0.12":
version "0.0.12"
resolved "https://registry.yarnpkg.com/@subsocial/grill-widget/-/grill-widget-0.0.12.tgz#57384850a3adb5342a5505a3f0d67c258d7e3b0e"
integrity sha512-Xed+wN/NEZ+mQlKA1olVVcBviVIQOTyqaCYcl9pGsg8yDdIBsKVjqJUKty1vvBL7gY0A+1osdwBgX2xUsbHOBA==

"@subsocial/resource-discussions@^0.0.3":
version "0.0.3"
Expand Down

0 comments on commit a0ac798

Please sign in to comment.