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

Commit 807f89f

Browse files
authored
chore(ts-workflow): more js -> ts day2 (#1016)
* chore(ts-workflow): perfer Type instead of Interface * chore(ts-workflow): add @typs/react && improve defaultProps style * chore(ts-workflow): page workflow * chore(ts-workflow): make sure mst-view helper has return type * chore(ts-workflow): add some note for rootStore * chore(ts-workflow): community page -> ts, resolve warnings
1 parent ecdd06b commit 807f89f

File tree

22 files changed

+237
-169
lines changed

22 files changed

+237
-169
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@
143143
"@types/jest": "^26.0.20",
144144
"@types/mocha": "^8.2.1",
145145
"@types/ramda": "^0.27.38",
146+
"@types/react": "^17.0.3",
146147
"@typescript-eslint/eslint-plugin": "^4.17.0",
147148
"@typescript-eslint/parser": "^4.17.0",
148149
"babel-eslint": "^10.0.2",

src/components/AlertBar/index.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@ import { Wrapper } from './styles'
1313
/* eslint-disable-next-line */
1414
const log = buildLog('c:AlertBar:index')
1515

16-
type IProps = {
16+
type TProps = {
1717
children: React.ReactNode
1818
}
1919

20-
const AlertBar: React.FC<IProps> = ({ children }) => {
20+
const AlertBar: React.FC<TProps> = ({ children }) => {
2121
return <Wrapper testid="alertBar">{children}</Wrapper>
2222
}
2323

src/components/ErrorPage/index.js renamed to src/components/ErrorPage/index.tsx

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,8 @@
55
*/
66

77
import React from 'react'
8-
import T from 'prop-types'
98
import { useRouter } from 'next/router'
109
import Link from 'next/link'
11-
import { values } from 'ramda'
1210

1311
import { METRIC } from '@/constant'
1412
import { ICON_BASE } from '@/config'
@@ -35,7 +33,19 @@ import {
3533
/* eslint-disable-next-line */
3634
const log = buildLog('c:ErrorPage:index')
3735

38-
const ErrorPage = ({ testid, errorCode, metric, target }) => {
36+
export type TProps = {
37+
errorCode?: number // 400 | 500 | 404
38+
target?: string
39+
testid?: string
40+
metric?: string
41+
}
42+
43+
const ErrorPage: React.FC<TProps> = ({
44+
testid = 'error-page',
45+
errorCode,
46+
metric = METRIC.COMMUNITY,
47+
target = '',
48+
}) => {
3949
const router = useRouter()
4050

4151
return (
@@ -51,7 +61,7 @@ const ErrorPage = ({ testid, errorCode, metric, target }) => {
5161
<HintWrapper>
5262
<IconsWrapper>
5363
<SpinPlanet />
54-
<CodeSnippets errorCode={errorCode} path={target || router.asPath} />
64+
<CodeSnippets path={target || router.asPath} />
5565
</IconsWrapper>
5666
<TextWrapper>
5767
{/** TODO: */}
@@ -67,19 +77,4 @@ const ErrorPage = ({ testid, errorCode, metric, target }) => {
6777
</Wrapper>
6878
)
6979
}
70-
71-
ErrorPage.propTypes = {
72-
testid: T.string,
73-
errorCode: T.oneOf([404, 500]),
74-
metric: T.oneOf(values(METRIC)),
75-
target: T.string,
76-
}
77-
78-
ErrorPage.defaultProps = {
79-
testid: 'error-page',
80-
errorCode: 404,
81-
metric: METRIC.COMMUNITY,
82-
target: '',
83-
}
84-
8580
export default React.memo(ErrorPage)

src/containers/content/HelpCenterContent/Digest.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@ import {
1515
HelpTitle,
1616
} from './styles/digest'
1717

18-
interface IProps {
18+
type TProps = {
1919
community: TCommunity
20-
metric?: string // METRIC.HELP_CENTER
20+
metric: string
2121
}
2222

23-
const Digest: React.FC<IProps> = ({ metric, community }) => {
23+
const Digest: React.FC<TProps> = ({ metric, community }) => {
2424
return (
2525
<Wrapper metric={metric}>
2626
<InnerWrapper metric={metric}>

src/containers/content/HelpCenterContent/index.tsx

Lines changed: 9 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import React from 'react'
1111
import { METRIC } from '@/constant'
1212
import { pluggedIn, buildLog } from '@/utils'
1313

14-
import { IStore } from './store'
14+
import { TStore } from './store'
1515

1616
import Cover from './Cover'
1717
import Detail from './Detail'
@@ -25,21 +25,16 @@ import { useInit } from './logic'
2525
/* eslint-disable-next-line */
2626
const log = buildLog('C:HelpCenterContent')
2727

28-
interface IProps {
29-
helpCenterContent?: IStore
30-
testid: string
31-
metric: string // METRIC.HELP_CENTER
28+
type TProps = {
29+
helpCenterContent?: TStore
30+
testid?: string
31+
metric?: string
3232
}
3333

34-
const defaultProps: IProps = {
35-
metric: METRIC.HELP_CENTER,
36-
testid: 'help-center-content',
37-
}
38-
39-
const HelpCenterContentContainer: React.FC<IProps> = ({
34+
const HelpCenterContentContainer: React.FC<TProps> = ({
4035
helpCenterContent: store,
41-
testid,
42-
metric,
36+
testid = 'help-center-content',
37+
metric = METRIC.HELP_CENTER,
4338
}) => {
4439
useInit(store)
4540
const { view, curCommunity } = store
@@ -54,16 +49,4 @@ const HelpCenterContentContainer: React.FC<IProps> = ({
5449
)
5550
}
5651

57-
HelpCenterContentContainer.defaultProps = defaultProps
58-
// HelpCenterContentContainer.propTypes = {
59-
// helpCenterContent: T.any.isRequired,
60-
// testid: T.string,
61-
// metric: T.oneOf(values(METRIC)),
62-
// }
63-
64-
// HelpCenterContentContainer.defaultProps = {
65-
// testid: 'help-center-content',
66-
// metric: METRIC.HELP_CENTER,
67-
// }
68-
69-
export default pluggedIn(HelpCenterContentContainer)
52+
export default pluggedIn(HelpCenterContentContainer) as React.FC<TProps>

src/containers/content/HelpCenterContent/logic.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ import { useEffect } from 'react'
44
import { buildLog } from '@/utils'
55
// import S from './service'
66
import { VIEW } from './constant'
7-
import { IStore } from './store'
7+
import { TStore } from './store'
88

9-
let store: IStore | undefined
9+
let store: TStore | undefined
1010

1111
/* eslint-disable-next-line */
1212
const log = buildLog('L:HelpCenterContent')
@@ -22,7 +22,7 @@ export const gotoDetail = (): void => {
2222
// init & uninit handlers
2323
// ###############################
2424

25-
export const useInit = (_store: IStore): void => {
25+
export const useInit = (_store: TStore): void => {
2626
useEffect(() => {
2727
store = _store
2828
log('useInit: ', store)

src/containers/content/HelpCenterContent/store.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66
import { types as T, getParent, Instance } from 'mobx-state-tree'
77
import { values } from 'ramda'
88

9-
import { TCommunity, IRootStore } from '@/types'
9+
import { IRootStore } from '@/stores/RootStore'
10+
11+
import { TCommunity } from '@/types'
1012
import { markStates, buildLog, stripMobx } from '@/utils'
1113

1214
import { VIEW } from './constant'
@@ -31,5 +33,5 @@ export const HelpCenterContent = T.model('HelpCenterContent', {
3133
},
3234
}))
3335

34-
export type IStore = Instance<typeof HelpCenterContent>
36+
export type TStore = Instance<typeof HelpCenterContent>
3537
export default HelpCenterContent

src/containers/layout/GlobalLayout/dynamic.js renamed to src/containers/layout/GlobalLayout/dynamic.tsx

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

4+
import { TProps as TErrorPage } from '@/components/ErrorPage'
5+
46
export const Doraemon = dynamic(() => import('@/containers/tool/Doraemon'), {
57
/* eslint-disable react/display-name */
68
loading: () => <div />,
@@ -18,8 +20,12 @@ export const Footer = dynamic(() => import('@/containers/unit/Footer'), {
1820
loading: () => <div />,
1921
ssr: false,
2022
})
21-
export const ErrorPage = dynamic(() => import('@/components/ErrorPage'), {
22-
/* eslint-disable react/display-name */
23-
loading: () => <div />,
24-
ssr: false,
25-
})
23+
24+
export const ErrorPage = dynamic<TErrorPage>(
25+
() => import('@/components/ErrorPage'),
26+
{
27+
/* eslint-disable react/display-name */
28+
loading: () => <div />,
29+
ssr: false,
30+
},
31+
)

src/containers/layout/GlobalLayout/index.js renamed to src/containers/layout/GlobalLayout/index.tsx

Lines changed: 19 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,12 @@
55
*/
66

77
import React, { useEffect } from 'react'
8-
import T from 'prop-types'
9-
import { values } from 'ramda'
108

11-
import { ANCHOR, METRIC } from '@/constant'
9+
import { ANCHOR } from '@/constant'
1210
import AnalysisService from '@/services/Analysis'
1311
import { useNetwork, useShortcut, usePlatform, useDevice } from '@/hooks'
1412
import { pluggedIn } from '@/utils'
13+
import { Nullable, TSEO } from '@/types'
1514

1615
import ThemePalette from '@/containers/layout/ThemePalette'
1716
import Header from '@/containers/unit/Header'
@@ -20,6 +19,7 @@ import ModeLine from '@/containers/unit/ModeLine'
2019
import Drawer from '@/containers/tool/Drawer'
2120
import CustomScroller from '@/components/CustomScroller'
2221

22+
import { TStore } from './store'
2323
import SEO from './SEO'
2424
import { Doraemon, ErrorBox, Footer, ErrorPage } from './dynamic'
2525

@@ -33,14 +33,26 @@ import {
3333
childrenWithProps,
3434
} from './logic'
3535

36-
const GlobalLayoutContainer = ({
36+
type TProps = {
37+
globalLayout?: TStore
38+
children: React.ReactNode
39+
seoConfig: TSEO
40+
errorCode?: number // 400 | 500 | 404
41+
errorPath?: Nullable<string>
42+
noSidebar?: boolean
43+
noFooter?: boolean
44+
45+
metric: string
46+
}
47+
48+
const GlobalLayoutContainer: React.FC<TProps> = ({
3749
globalLayout: store,
3850
seoConfig,
3951
errorCode,
4052
errorPath,
4153
children,
42-
noSidebar,
43-
noFooter,
54+
noSidebar = false,
55+
noFooter = false,
4456
metric,
4557
}) => {
4658
const { online } = useNetwork()
@@ -116,24 +128,4 @@ const GlobalLayoutContainer = ({
116128
)
117129
}
118130

119-
GlobalLayoutContainer.propTypes = {
120-
children: T.node,
121-
globalLayout: T.object.isRequired,
122-
seoConfig: T.object.isRequired, // TODO:
123-
noSidebar: T.bool,
124-
noFooter: T.bool,
125-
metric: T.oneOf(values(METRIC)),
126-
errorCode: T.oneOf([null, 404, 500]),
127-
errorPath: T.oneOfType([T.string, T.instanceOf(null)]),
128-
}
129-
130-
GlobalLayoutContainer.defaultProps = {
131-
children: <div />,
132-
noSidebar: false,
133-
noFooter: false,
134-
errorCode: null,
135-
errorPath: null,
136-
metric: METRIC.COMMUNITY,
137-
}
138-
139-
export default pluggedIn(GlobalLayoutContainer)
131+
export default pluggedIn(GlobalLayoutContainer) as React.FC<TProps>
Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
/*
2-
* GlobalLayoutStore store
2+
* GlobalLayout store
33
*
44
*/
55

6-
import { types as T, getParent } from 'mobx-state-tree'
6+
import { types as T, getParent, Instance } from 'mobx-state-tree'
77

88
import { markStates, buildLog } from '@/utils'
9+
import { IRootStore, TAccount } from '@/types'
910

1011
/* eslint-disable-next-line */
1112
const log = buildLog('S:GlobalLayoutStore')
@@ -18,31 +19,33 @@ const Platform = T.model('Platform', {
1819
isEdge: T.optional(T.boolean, false),
1920
})
2021

21-
const GlobalLayoutStore = T.model('GlobalLayoutStore', {
22+
const GlobalLayout = T.model('GlobalLayoutStore', {
2223
online: T.optional(T.boolean, true),
2324
platform: T.optional(Platform, {}),
2425
isMobile: T.optional(T.boolean, false),
2526
// follow the mac convention
2627
bodyScrollDirection: T.optional(T.enumeration(['up', 'down']), 'up'),
2728
})
2829
.views((self) => ({
29-
get root() {
30-
return getParent(self)
30+
get accountInfo(): TAccount {
31+
const root = getParent(self) as IRootStore
32+
return root.accountInfo
3133
},
32-
get accountInfo() {
33-
return self.root.accountInfo
34-
},
35-
get sidebarPin() {
36-
return self.root.sidebar.pin
34+
get sidebarPin(): boolean {
35+
const root = getParent(self) as IRootStore
36+
return root.sidebar.pin
3737
},
3838
}))
3939
.actions((self) => ({
40-
openDoraemon() {
41-
self.root.openDoraemon()
40+
openDoraemon(): void {
41+
const root = getParent(self) as IRootStore
42+
root.openDoraemon()
4243
},
4344
mark(sobj) {
4445
markStates(sobj, self)
4546
},
4647
}))
4748

48-
export default GlobalLayoutStore
49+
export type TStore = Instance<typeof GlobalLayout>
50+
51+
export default GlobalLayout

0 commit comments

Comments
 (0)