Skip to content
This repository was archived by the owner on Nov 8, 2022. It is now read-only.

Commit 1a270ea

Browse files
authored
feat(status): add global realtime visitors (#1204)
1 parent 9096c72 commit 1a270ea

File tree

11 files changed

+93
-21
lines changed

11 files changed

+93
-21
lines changed

src/containers/digest/CommunityDigest/ClassicLayout/index.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ const NON_STANDARD_COMMUNITIES = [HCN, 'feedback']
2727

2828
type TProps = {
2929
community: TCommunity
30+
realtimeVisitors: number
3031
descExpand: boolean
3132
activeThread: TThread
3233
layout: TC11NLayout
@@ -35,6 +36,7 @@ type TProps = {
3536

3637
const ClassicLayout: FC<TProps> = ({
3738
community,
39+
realtimeVisitors,
3840
descExpand,
3941
activeThread,
4042
layout,
@@ -62,6 +64,7 @@ const ClassicLayout: FC<TProps> = ({
6264
community={community}
6365
onShowEditorList={onShowEditorList}
6466
onShowSubscriberList={onShowSubscriberList}
67+
realtimeVisitors={realtimeVisitors}
6568
/>
6669
</CommunityBaseInfo>
6770
<TabBarWrapper>

src/containers/digest/CommunityDigest/index.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ const CommunityDigestContainer: FC<TProps> = ({
3333
accountInfo: {
3434
customization: { bannerLayout },
3535
},
36+
realtimeVisitors,
3637
curThread,
3738
curCommunity,
3839
descExpand,
@@ -41,6 +42,7 @@ const CommunityDigestContainer: FC<TProps> = ({
4142
return (
4243
<ClassicLayout
4344
metric={metric}
45+
realtimeVisitors={realtimeVisitors}
4446
community={curCommunity}
4547
activeThread={curThread}
4648
layout={bannerLayout}

src/containers/digest/CommunityDigest/store.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ const CommunityDigest = T.model('CommunityDigest', {
3434
const root = getParent(self) as TRootStore
3535
return toJS(root.viewing)
3636
},
37+
get realtimeVisitors(): number {
38+
const root = getParent(self) as TRootStore
39+
return root.footer.realtimeVisitors
40+
},
3741
get accountInfo(): TAccount {
3842
const root = getParent(self) as TRootStore
3943
return root.accountInfo

src/containers/thread/ArticlesThread/logic.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ export const useInit = (_store: TStore): void =>
166166
useEffect(() => {
167167
store = _store
168168
sub$ = sr71$.data().subscribe($solver(DataSolver, ErrSolver))
169-
console.log('### see me client side?: ', store.isEmpty)
169+
170170
if (store.isEmpty) loadArticles()
171171

172172
return () => {

src/containers/unit/Footer/logic.ts

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
11
import { useEffect } from 'react'
22

33
import type { TMetric } from '@/spec'
4-
import { EVENT, PAYMENT_USAGE } from '@/constant'
4+
import { EVENT } from '@/constant'
55

66
import asyncSuit from '@/utils/async'
77
import { send } from '@/utils/helper'
88
import { buildLog } from '@/utils/logger'
9-
// import S from './schema'
9+
import uid from '@/utils/uid'
10+
import S from './schema'
1011

1112
import type { TStore } from './store'
1213

1314
/* eslint-disable-next-line */
1415
const log = buildLog('L:Footer2')
1516

16-
const { SR71, $solver } = asyncSuit
17+
const { SR71, $solver, asyncRes } = asyncSuit
1718
const sr71$ = new SR71()
1819

1920
let sub$ = null
@@ -25,11 +26,27 @@ export const toggleSponsorHelper = (): void =>
2526
export const onLogin = (): void => store.authWarning({ hideToast: true })
2627
export const queryDoraemon = (data): void => send(EVENT.QUERY_DORAMON, { data })
2728

29+
const getOnlineStatus = (): void => {
30+
sr71$.query(S.onlineStatus, { freshkey: uid.gen() })
31+
32+
setInterval(() => {
33+
sr71$.query(S.onlineStatus, { freshkey: uid.gen() })
34+
}, 10000)
35+
}
36+
2837
// ###############################
2938
// Data & Error handlers
3039
// ###############################
3140

32-
const DataSolver = []
41+
const DataSolver = [
42+
{
43+
match: asyncRes('onlineStatus'),
44+
action: ({ onlineStatus }): void => {
45+
const { realtimeVisitors } = onlineStatus
46+
store.mark({ realtimeVisitors })
47+
},
48+
},
49+
]
3350
const ErrSolver = []
3451

3552
// ###############################
@@ -40,6 +57,7 @@ export const useInit = (_store: TStore, metric: TMetric): void => {
4057
store = _store
4158
store.mark({ metric })
4259
sub$ = sr71$.data().subscribe($solver(DataSolver, ErrSolver))
60+
getOnlineStatus()
4361

4462
return () => {
4563
sub$.unsubscribe()
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { gql } from '@urql/core'
2+
3+
const onlineStatus = gql`
4+
query ($freshkey: String) {
5+
onlineStatus(freshkey: $freshkey) {
6+
realtimeVisitors
7+
}
8+
}
9+
`
10+
11+
const schema = {
12+
onlineStatus,
13+
}
14+
15+
export default schema

src/containers/unit/Footer/store.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ import { markStates, toJS } from '@/utils/mobx'
1414
const FooterStore = T.model('FooterStore', {
1515
showSponsor: T.optional(T.boolean, false),
1616
metric: T.optional(T.string, METRIC.COMMUNITY),
17+
// online-status
18+
realtimeVisitors: T.optional(T.number, 1),
1719
})
1820
.views((self) => ({
1921
get isLogin(): boolean {

src/widgets/CommunityStatesPad/NumberGroup.tsx

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@ import { FC, memo } from 'react'
22

33
import { prettyNum } from '@/utils/helper'
44
import { buildLog } from '@/utils/logger'
5+
import AnimatedCount from '@/widgets/AnimatedCount'
56

67
import {
78
Wrapper,
89
NumberItem,
910
LargeNumberItem,
1011
SubNumberWrapper,
12+
SubNum,
1113
GreenDot,
1214
PlusSign,
1315
} from './styles/number_group'
@@ -32,7 +34,7 @@ const NumberGroup: FC<TProps> = ({
3234
}) => {
3335
return (
3436
<Wrapper>
35-
{subCount ? (
37+
{subCount >= 0 ? (
3638
<NumberItem readOnly={readOnly} onClick={onClick}>
3739
{prettyNum(count)}
3840
</NumberItem>
@@ -42,11 +44,13 @@ const NumberGroup: FC<TProps> = ({
4244
</LargeNumberItem>
4345
)}
4446

45-
{subCount && (
47+
{subCount >= 0 && (
4648
<SubNumberWrapper>
4749
{subPrefix === 'online' && <GreenDot />}
4850
{subPrefix === 'add' && <PlusSign>+</PlusSign>}
49-
{subCount}
51+
<SubNum>
52+
<AnimatedCount count={subCount} size="tiny" />
53+
</SubNum>
5054
</SubNumberWrapper>
5155
)}
5256
</Wrapper>

src/widgets/CommunityStatesPad/index.tsx

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import { FC, memo } from 'react'
99
import type { TCommunity } from '@/spec'
1010
import usePlatform from '@/hooks/usePlatform'
1111
import { buildLog } from '@/utils/logger'
12+
import { getRandomInt } from '@/utils/helper'
13+
import Tooltip from '@/widgets/Tooltip'
1214

1315
import Charger from '@/widgets/Charger'
1416

@@ -22,20 +24,23 @@ import {
2224
ChargeSection,
2325
NumberDivider,
2426
NumberTitle,
27+
PopHint,
2528
} from './styles'
2629

2730
/* eslint-disable-next-line */
2831
const log = buildLog('c:CommunityStatesPad:index')
2932

3033
type TProps = {
3134
community: TCommunity
35+
realtimeVisitors?: number
3236
withoutFounding?: boolean
3337
onShowEditorList?: () => void
3438
onShowSubscriberList?: () => void
3539
}
3640

3741
const CommunityStatesPad: FC<TProps> = ({
3842
community,
43+
realtimeVisitors = 1,
3944
onShowEditorList = log,
4045
onShowSubscriberList = log,
4146
withoutFounding = true,
@@ -48,22 +53,35 @@ const CommunityStatesPad: FC<TProps> = ({
4853
<Wrapper>
4954
<NumberSection>
5055
{!isMobile && <NumberTitle>成员</NumberTitle>}
51-
<NumberGroup
52-
count={subscribersCount}
53-
subCount={12}
54-
onClick={onShowSubscriberList}
55-
subPrefix="online"
56-
/>
56+
<Tooltip
57+
content={
58+
<PopHint>
59+
实时在线人数,后续会单独统计每个子社区的实时在线人数。
60+
</PopHint>
61+
}
62+
placement="bottom"
63+
>
64+
<NumberGroup
65+
count={subscribersCount}
66+
subCount={realtimeVisitors}
67+
onClick={onShowSubscriberList}
68+
subPrefix="online"
69+
/>
70+
</Tooltip>
5771
</NumberSection>
5872
<NumberDivider />
5973
<ContentSection>
6074
<NumberTitle readOnly>内容</NumberTitle>
61-
<NumberGroup
62-
subPrefix="add"
63-
count={contentsCount}
64-
subCount={4}
65-
readOnly
66-
/>
75+
<Tooltip
76+
content={<PopHint>较前一天新增内容,功能开发中</PopHint>}
77+
placement="bottom"
78+
>
79+
<NumberGroup
80+
subPrefix="add"
81+
count={contentsCount}
82+
subCount={getRandomInt(1, 8)}
83+
/>
84+
</Tooltip>
6785
</ContentSection>
6886
<NumberDivider />
6987
<VolunteerSection alignCenter={editorsCount < 99}>

src/widgets/CommunityStatesPad/styles/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,3 +54,7 @@ export const NumberDivider = styled.div`
5454
`};
5555
${css.media.mobile`display: none`};
5656
`
57+
58+
export const PopHint = styled.div`
59+
width: 200px;
60+
`

0 commit comments

Comments
 (0)