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

Commit 09ef539

Browse files
authored
refactor: useViewing for toggle (#1319)
1 parent 875c267 commit 09ef539

File tree

12 files changed

+114
-17
lines changed

12 files changed

+114
-17
lines changed

src/containers/tool/Drawer/Viewer/DesktopView.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ const DesktopView: FC<TProps> = ({
2929
articleNavi,
3030
children,
3131
}) => {
32-
console.log('--> visible --> ', visible)
3332
return (
3433
<Fragment>
3534
<DrawerOverlay

src/hooks/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ export { default as useOutsideClick } from './useOutsideClick'
88
export { default as useLangPress } from './useLangPress'
99
export { default as useTrans } from './useTrans'
1010
export { default as useAccount } from './useAccount'
11+
export { default as useViewing } from './useViewing'
1112

1213
export { default as useNetwork } from 'react-use/lib/useNetwork'
1314
export { default as useCopyToClipboard } from 'react-use/lib/useCopyToClipboard'
1415
export { default as useInterval } from 'react-use/lib/useInterval'
1516

1617
export { useSwipeable as useSwipe } from 'react-swipeable'
17-
export { useViewing } from '@/stores/init'

src/hooks/useAccount.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { useState, useEffect } from 'react'
22
// import { merge } from 'ramda'
33

4+
import { EVENT } from '@/constant'
45
import type { TAccount } from '@/spec'
56

67
import BStore from '@/utils/bstore'
@@ -21,10 +22,10 @@ const useAccount = (): TAccount | null => {
2122
}
2223
}
2324

24-
Global.addEventListener('storage', checkAccount)
25+
Global.addEventListener(EVENT.SESSION_CHANGED, checkAccount)
2526

2627
return () => {
27-
Global.removeEventListener('storage', checkAccount)
28+
Global.removeEventListener(EVENT.SESSION_CHANGED, checkAccount)
2829
}
2930
}, [])
3031

src/hooks/useViewing.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { useState, useEffect } from 'react'
2+
// import { merge } from 'ramda'
3+
4+
import { EVENT } from '@/constant'
5+
import type { TID } from '@/spec'
6+
7+
import BStore from '@/utils/bstore'
8+
import { Global } from '@/utils/helper'
9+
10+
const initState = ''
11+
12+
const useViewing = (): TID | null => {
13+
const [viewing, setViewing] = useState(initState)
14+
15+
/* eslint-disable */
16+
useEffect(() => {
17+
const checkViewing = () => {
18+
const item = BStore.get('viewingInfo')
19+
setViewing(item)
20+
}
21+
22+
Global.addEventListener(EVENT.VIEWING_CHANGED, checkViewing)
23+
24+
return () => {
25+
Global.removeEventListener(EVENT.VIEWING_CHANGED, checkViewing)
26+
}
27+
}, [])
28+
29+
return viewing
30+
}
31+
32+
export default useViewing

src/stores/ViewingStore/index.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import type {
1616
} from '@/spec'
1717
import { THREAD, ARTICLE_THREAD } from '@/constant'
1818
import { markStates } from '@/utils/mobx'
19+
import { viewingChanged } from '@/utils/helper'
1920
import { User, Community, Post, Blog, Job, Radar, Works } from '@/model'
2021

2122
const ViewingStore = T.model('ViewingStore', {
@@ -63,8 +64,9 @@ const ViewingStore = T.model('ViewingStore', {
6364
}))
6465
.actions((self) => ({
6566
setViewing(sobj): void {
66-
const { mark } = self as TStore
67+
const { mark, viewingArticle } = self as TStore
6768
mark(sobj)
69+
viewingChanged(viewingArticle.id)
6870
},
6971
changeCommunity(raw): void {
7072
self.community.raw = raw
@@ -75,6 +77,7 @@ const ViewingStore = T.model('ViewingStore', {
7577
resetViewing(): void {
7678
const { mark, viewingThread } = self as TStore
7779
mark({ [viewingThread]: {}, viewingThread: null })
80+
viewingChanged(null)
7881
},
7982
updateUpvote(viewerHasUpvoted: boolean): void {
8083
const { currentThread } = self as TStore

src/stores/init.ts

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
* the entry of the App root store
33
*/
44

5-
import { useMemo, useState, useEffect } from 'react'
5+
import { useMemo } from 'react'
66
import { applySnapshot } from 'mobx-state-tree'
77

8-
import type { TRootStore, TAccountStore, TViewingStore } from '@/spec'
8+
import type { TRootStore } from '@/spec'
99
import RootStore from './RootStore'
1010

1111
let clientSideRootStore: TRootStore | undefined
@@ -32,9 +32,4 @@ export const useStore = (initialState = {}): TRootStore => {
3232
return store
3333
}
3434

35-
export const useViewing = (): TViewingStore => {
36-
const [substore, setSubstore] = useState(useStore({}).viewing)
37-
useEffect(() => setSubstore(clientSideRootStore.viewing), [])
38-
39-
return substore
40-
}
35+
export const holder = 1
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { FC, memo } from 'react'
2+
3+
import type { TPost } from '@/spec'
4+
import useViewing from '@/hooks/useViewing'
5+
6+
import { Wrapper, ViewIcon } from '../styles/desktop_view/viewing_sign'
7+
8+
type TProps = {
9+
article: TPost
10+
}
11+
12+
const ViewingSign: FC<TProps> = ({ article }) => {
13+
const viewingId = useViewing()
14+
15+
if (article.id !== viewingId) return null
16+
17+
return (
18+
<Wrapper>
19+
<ViewIcon />
20+
</Wrapper>
21+
)
22+
}
23+
24+
export default memo(ViewingSign)

src/widgets/PostItem/DesktopView/index.tsx

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,17 @@ import { UPVOTE_LAYOUT } from '@/constant'
66

77
import { upvoteOnArticleList } from '@/utils/helper'
88
import TheAvatar from '@/widgets/TheAvatar'
9+
import ViewingSign from './ViewingSign'
910

1011
import Header from './Header'
1112
import Body from './Body'
1213

13-
import { AvatarWrapper, UpvoteWrapper, Main } from '../styles/desktop_view'
14+
import {
15+
Wrapper,
16+
AvatarWrapper,
17+
UpvoteWrapper,
18+
Main,
19+
} from '../styles/desktop_view'
1420

1521
let Upvote = null
1622
let ArticleReadLabel = null
@@ -45,13 +51,14 @@ const DigestView: FC<TProps> = ({ curCommunity, entry }) => {
4551
}, [])
4652

4753
return (
48-
<Fragment>
54+
<Wrapper>
4955
{loaded && (
5056
<Fragment>
5157
<ArticleReadLabel entry={entry} />
5258
<ArticlePinLabel entry={entry} />
5359
</Fragment>
5460
)}
61+
<ViewingSign article={entry} />
5562
<AvatarWrapper>
5663
<TheAvatar user={entry.author} />
5764
<UpvoteWrapper>
@@ -71,7 +78,7 @@ const DigestView: FC<TProps> = ({ curCommunity, entry }) => {
7178
<Header item={entry} />
7279
<Body item={entry} curCommunity={curCommunity} />
7380
</Main>
74-
</Fragment>
81+
</Wrapper>
7582
)
7683
}
7784

src/widgets/PostItem/styles/desktop_view/index.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@ import styled from 'styled-components'
33
import css, { theme } from '@/utils/css'
44
import Img from '@/Img'
55

6+
export const Wrapper = styled.div`
7+
${css.flex()};
8+
width: 100%;
9+
position: relative;
10+
`
611
export const Main = styled.div`
712
${css.flexColumnGrow()};
813
margin-left: 6px;
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import styled from 'styled-components'
2+
3+
import css, { animate, theme } from '@/utils/css'
4+
import ViewSVG from '@/icons/View'
5+
6+
export const Wrapper = styled.div`
7+
${css.flex()};
8+
position: absolute;
9+
left: -28px;
10+
top: 11px;
11+
animation: ${animate.fadeInRight} 0.25s linear;
12+
`
13+
export const ViewIcon = styled(ViewSVG)`
14+
${css.size(10)};
15+
fill: ${theme('thread.articleTitle')};
16+
`
17+
export const Title = styled.div``

0 commit comments

Comments
 (0)