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

Commit 5b3821b

Browse files
authored
refactor(article-sticker): convert to ts (#1054)
1 parent 85216e6 commit 5b3821b

File tree

10 files changed

+125
-84
lines changed

10 files changed

+125
-84
lines changed

src/containers/tool/ArticleSticker/ArticleSticker.js renamed to src/containers/tool/ArticleSticker/ArticleSticker.tsx

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import React from 'react'
22

3+
import type { TArticle } from '@/spec'
34
import { ICON } from '@/config'
45
import { Br } from '@/components/Common'
56

@@ -14,7 +15,12 @@ import {
1415
Text,
1516
} from './styles/article_sticker'
1617

17-
const CommonSticker = ({ show, viewing }) => {
18+
type TProps = {
19+
show: boolean
20+
viewing: TArticle
21+
}
22+
23+
const ArticleSticker: React.FC<TProps> = ({ show, viewing }) => {
1824
return (
1925
<Wrapper show={show}>
2026
<ItemWrapper>
@@ -37,4 +43,4 @@ const CommonSticker = ({ show, viewing }) => {
3743
)
3844
}
3945

40-
export default React.memo(CommonSticker)
46+
export default React.memo(ArticleSticker)

src/containers/tool/ArticleSticker/CommentSticker.js renamed to src/containers/tool/ArticleSticker/CommentSticker.tsx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,12 @@ import {
2929
/* eslint-disable-next-line */
3030
const log = buildLog('c:CommentSticker:index')
3131

32-
const CommentSticker = ({
32+
type TProps = {
33+
show: boolean
34+
data?: any // TODO
35+
}
36+
37+
const CommentSticker: React.FC<TProps> = ({
3338
show,
3439
data: { pagedCommentsParticipators: users },
3540
}) => {

src/containers/tool/ArticleSticker/CommunitySticker.js renamed to src/containers/tool/ArticleSticker/CommunitySticker.tsx

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import React from 'react'
22

3+
import type { TViewing } from '@/spec'
34
import { ICON_BASE } from '@/config'
45
import { Button } from '@/components/Buttons'
56

@@ -10,7 +11,12 @@ import {
1011
Divider,
1112
} from './styles/community_sticker'
1213

13-
const CommunitySticker = () => {
14+
type TProps = {
15+
data?: TViewing
16+
show?: boolean
17+
}
18+
19+
const CommunitySticker: React.FC<TProps> = ({ show, data }) => {
1420
return (
1521
<React.Fragment>
1622
<ItemWrapper>

src/containers/tool/ArticleSticker/LeftSticker/Toc.js renamed to src/containers/tool/ArticleSticker/LeftSticker/Toc.tsx

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,14 @@ import {
1414
} from '../styles/left_sticker/toc'
1515
import { toggleTocMenu } from '../logic'
1616

17-
const Toc = ({ show }) => {
17+
type TProps = {
18+
show: boolean
19+
testid?: string
20+
}
21+
22+
const Toc: React.FC<TProps> = ({ show, testid = 'article-sticker-toc' }) => {
1823
return (
19-
<Wrapper>
24+
<Wrapper testid={testid}>
2025
<HeaderWrapper onClick={toggleTocMenu}>
2126
<TitleWrapper>
2227
<TocIcon src={`${ICON}/article/outline.svg`} />

src/containers/tool/ArticleSticker/LeftSticker/index.js renamed to src/containers/tool/ArticleSticker/LeftSticker/index.tsx

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,21 @@ import {
1313
BackText,
1414
} from '../styles/left_sticker'
1515

16-
const LeftSticker = ({ show, title, isTocMenuOpened }) => {
16+
type TProps = {
17+
show: boolean
18+
title: string
19+
isTocMenuOpened: boolean
20+
testid?: string
21+
}
22+
23+
const LeftSticker: React.FC<TProps> = ({
24+
show,
25+
title,
26+
isTocMenuOpened,
27+
testid = 'article-sticker-left-sidebar',
28+
}) => {
1729
return (
18-
<Wrapper show={show}>
30+
<Wrapper show={show} testid={testid}>
1931
<BackWrapper>
2032
<ArrowIcon src={`${ICON}/shape/arrow-simple.svg`} />
2133
<BackText>Elixir 社区</BackText>

src/containers/tool/ArticleSticker/index.js renamed to src/containers/tool/ArticleSticker/index.tsx

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,14 @@
77
*/
88

99
import React from 'react'
10-
import T from 'prop-types'
1110

1211
import { pluggedIn, buildLog } from '@/utils'
1312

1413
import Sticky from '@/components/Sticky'
1514
import GotoTop from '@/components/GotoTop'
1615

16+
import type { TStore } from './store'
17+
1718
import LeftSticker from './LeftSticker/index'
1819
import CommunitySticker from './CommunitySticker'
1920
import ArticleSticker from './ArticleSticker'
@@ -25,7 +26,15 @@ import { useInit } from './logic'
2526
/* eslint-disable-next-line */
2627
const log = buildLog('C:ArticleSticker')
2728

28-
const ArticleStickerContainer = ({ articleSticker: store, testid }) => {
29+
type TProps = {
30+
articleSticker?: TStore
31+
testid?: string
32+
}
33+
34+
const ArticleStickerContainer: React.FC<TProps> = ({
35+
articleSticker: store,
36+
testid = 'article-sticker',
37+
}) => {
2938
useInit(store)
3039

3140
const {
@@ -65,13 +74,4 @@ const ArticleStickerContainer = ({ articleSticker: store, testid }) => {
6574
)
6675
}
6776

68-
ArticleStickerContainer.propTypes = {
69-
articleSticker: T.any.isRequired,
70-
testid: T.string,
71-
}
72-
73-
ArticleStickerContainer.defaultProps = {
74-
testid: 'article-sticker',
75-
}
76-
77-
export default pluggedIn(ArticleStickerContainer)
77+
export default pluggedIn(ArticleStickerContainer) as React.FC<TProps>

src/containers/tool/ArticleSticker/logic.js renamed to src/containers/tool/ArticleSticker/logic.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@ import { useEffect } from 'react'
33

44
import { buildLog } from '@/utils'
55
// import S from './service'
6+
import type { TStore } from './store'
67

7-
let store = null
8+
let store: TStore | undefined
89

910
/* eslint-disable-next-line */
1011
const log = buildLog('L:ArticleSticker')
1112

12-
export const toggleTocMenu = () => {
13+
export const toggleTocMenu = (): void => {
1314
const isTocMenuOpened = !store.isTocMenuOpened
1415
const isLeftStickerLocked = isTocMenuOpened
1516

@@ -20,7 +21,7 @@ export const toggleTocMenu = () => {
2021
// init & uninit handlers
2122
// ###############################
2223

23-
export const useInit = (_store) => {
24+
export const useInit = (_store: TStore): void => {
2425
useEffect(() => {
2526
store = _store
2627
log('useInit: ', store)

src/containers/tool/ArticleSticker/store.js

Lines changed: 0 additions & 61 deletions
This file was deleted.
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
* ArticleSticker store
3+
*
4+
*/
5+
6+
import { types as T, getParent, Instance } from 'mobx-state-tree'
7+
// import {} from 'ramda'
8+
9+
import type { TRootStore, TViewing, TScrollDirection } from '@/spec'
10+
import { markStates, buildLog } from '@/utils'
11+
/* eslint-disable-next-line */
12+
const log = buildLog('S:ArticleSticker')
13+
14+
const ArticleSticker = T.model('ArticleSticker', {
15+
isTocMenuOpened: T.optional(T.boolean, false),
16+
// is TOC is opend, then lock the lefsidebar
17+
isLeftStickerLocked: T.optional(T.boolean, false),
18+
})
19+
.views((self) => ({
20+
get viewingData(): TViewing {
21+
const root = getParent(self) as TRootStore
22+
return root.viewingData
23+
},
24+
get bodyScrollDirection(): TScrollDirection {
25+
const root = getParent(self) as TRootStore
26+
return root.globalLayout.bodyScrollDirection
27+
},
28+
get isArticleDigestInViewport(): boolean {
29+
const root = getParent(self) as TRootStore
30+
return root.articleDigest.inViewport
31+
},
32+
get isArticleInViewport(): boolean {
33+
const root = getParent(self) as TRootStore
34+
const { articleInViewport } = root.postContent
35+
36+
return articleInViewport
37+
},
38+
get showLeftSticker(): boolean {
39+
const {
40+
isArticleDigestInViewport,
41+
isLeftStickerLocked,
42+
bodyScrollDirection,
43+
} = self as TStore
44+
45+
if (isArticleDigestInViewport) return false
46+
if (isLeftStickerLocked) return true
47+
48+
return bodyScrollDirection === 'down'
49+
},
50+
get showCommunity(): boolean {
51+
const { isArticleDigestInViewport, isArticleInViewport } = self as TStore
52+
return !isArticleDigestInViewport && isArticleInViewport
53+
},
54+
get showCommentSticker(): boolean {
55+
const { isArticleInViewport } = self as TStore
56+
return !isArticleInViewport
57+
},
58+
}))
59+
.actions((self) => ({
60+
mark(sobj: Record<string, unknown>): void {
61+
markStates(sobj, self)
62+
},
63+
}))
64+
65+
export type TStore = Instance<typeof ArticleSticker>
66+
export default ArticleSticker

src/spec/article.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ export type TArticle = {
1212
nickname: string
1313
avatar: string
1414
}
15+
starredCount?: number
1516
origialCommunity?: TCommunity
1617
commentsParticipators?: TUser
1718
insertedAt?: string

0 commit comments

Comments
 (0)