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

Commit e72da11

Browse files
committed
chore: Merge branch 'hooks-rocks' into dev
2 parents 73a752f + dd1b2d4 commit e72da11

File tree

10 files changed

+116
-21
lines changed

10 files changed

+116
-21
lines changed

.babelrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
"alias": {
5353
"@containers":"./containers",
5454
"@components":"./components",
55+
"@hooks":"./components/Hooks",
5556
"@config":"./config",
5657
"@stores":"./stores",
5758
"@model":"./stores/SharedModel",

components/Hooks/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
export { default as useShortcut } from './useShortcut'
22
export { default as useMedia } from './useMedia'
33
export { default as usePlatform } from './usePlatform'
4+
export { default as useScript } from './useScript'

components/Hooks/useScript.js

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import { useState, useEffect } from 'react'
2+
3+
const cachedScripts = []
4+
5+
const useScript = src => {
6+
// Keeping track of script loaded and error state
7+
const [state, setState] = useState({
8+
loaded: false,
9+
error: false,
10+
})
11+
12+
useEffect(
13+
() => {
14+
// If cachedScripts array already includes src that means another instance ...
15+
// ... of this hook already loaded this script, so no need to load again.
16+
if (cachedScripts.includes(src)) {
17+
setState({
18+
loaded: true,
19+
error: false,
20+
})
21+
} else {
22+
cachedScripts.push(src)
23+
24+
// Create script
25+
const script = document.createElement('script')
26+
script.src = src
27+
script.async = true
28+
29+
// Script event listener callbacks for load and error
30+
const onScriptLoad = () => {
31+
setState({
32+
loaded: true,
33+
error: false,
34+
})
35+
}
36+
37+
const onScriptError = () => {
38+
// Remove from cachedScripts we can try loading again
39+
const index = cachedScripts.indexOf(src)
40+
if (index >= 0) cachedScripts.splice(index, 1)
41+
script.remove()
42+
43+
setState({
44+
loaded: true,
45+
error: true,
46+
})
47+
}
48+
49+
script.addEventListener('load', onScriptLoad)
50+
script.addEventListener('error', onScriptError)
51+
52+
// Add script to document body
53+
document.body.appendChild(script)
54+
55+
// Remove event listeners on cleanup
56+
return () => {
57+
script.removeEventListener('load', onScriptLoad)
58+
script.removeEventListener('error', onScriptError)
59+
}
60+
}
61+
},
62+
[src] // Only re-run effect if script src changes
63+
)
64+
65+
return [state.loaded, state.error]
66+
}
67+
68+
export default useScript

containers/DocUploader/index.js

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,21 +43,27 @@ class DocUploaderContainer extends React.Component {
4343

4444
state = {
4545
ossClient: null,
46+
ossScriptAnchor: null,
47+
ossScriptLoaded: false,
4648
// use unique id to init the file input, otherwise it will be the some instance
4749
uniqueId: uid.gen(),
4850
initTimestamp: Date.now() / 1000,
4951
}
5052

5153
componentDidMount() {
54+
this.loadScriptAndInitOSS()
55+
5256
const { docUploader, pasteImage } = this.props
5357
init(docUploader)
5458

55-
this.initOssClient()
59+
// if (ossScriptLoaded) this.initOssClient()
5660
if (pasteImage) this.initPasteWatcher()
5761
}
5862

5963
componentWillUnmount() {
6064
log('componentWillUnmount')
65+
const { ossScriptLoaded, ossScriptAnchor } = this.state
66+
6167
/* eslint-disable */
6268
delete this.state.ossClient
6369
/* eslint-enable */
@@ -66,6 +72,31 @@ class DocUploaderContainer extends React.Component {
6672
Global.removeEventListener('paste', this.handlePaste.bind(this), true)
6773
}
6874
uninit()
75+
76+
if (ossScriptLoaded) {
77+
ossScriptAnchor.removeEventListener('load', () => {
78+
this.setState({ ossScriptLoaded: false })
79+
})
80+
}
81+
}
82+
83+
loadScriptAndInitOSS() {
84+
let { ossScriptAnchor } = this.state
85+
86+
ossScriptAnchor = document.createElement('script')
87+
ossScriptAnchor.src =
88+
'https://gosspublic.alicdn.com/aliyun-oss-sdk-5.2.0.min.js'
89+
ossScriptAnchor.async = true
90+
document.getElementsByTagName('head')[0].appendChild(ossScriptAnchor)
91+
92+
ossScriptAnchor.addEventListener('load', () => {
93+
log('load done')
94+
this.initOssClient()
95+
this.setState({ ossScriptLoaded: true, ossScriptAnchor })
96+
})
97+
ossScriptAnchor.addEventListener('error', () => {
98+
this.setState({ ossScriptLoaded: false, ossScriptAnchor: null })
99+
})
69100
}
70101

71102
initPasteWatcher() {
@@ -116,6 +147,8 @@ class DocUploaderContainer extends React.Component {
116147
}
117148

118149
doUploadFile(file) {
150+
const { ossScriptLoaded } = this.state
151+
if (!ossScriptLoaded) return alert('脚本未加载...')
119152
if (!file || !R.startsWith('image/', file.type)) return false
120153

121154
const fileSize = file.size / 1024 / 1024

containers/Doraemon/InputEditor.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
import React from 'react'
88

9-
import { useShortcut } from '@components/Hooks'
9+
import { useShortcut } from '@hooks'
1010
import InputPrefix from './InputPrefix'
1111

1212
import { EditorBar, InputBar, PrefixWraper } from './styles/input_editor'

containers/ErrorBox/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import React from 'react'
99
import { connectStore, buildLog } from '@utils'
1010

1111
import Modal from '@components/Modal'
12-
import { useShortcut } from '@components/Hooks'
12+
import { useShortcut } from '@hooks'
1313

1414
import Header from './Header'
1515
import Details from './Details'

containers/GlobalLayout/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import useNetwork from 'react-use/lib/useNetwork'
1111
import { ICON_CMD } from '@config'
1212
import { connectStore } from '@utils'
1313

14-
import { useShortcut, useMedia, usePlatform } from '@components/Hooks'
14+
import { useShortcut, useMedia, usePlatform } from '@hooks'
1515

1616
import { Wrapper, SubCommunitiesExpander, ExpanderIcon } from './styles'
1717
import { useInit, openDoraemon, queryDoraemon } from './logic'

containers/Preview/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
import React from 'react'
88

99
import { connectStore, buildLog } from '@utils'
10+
import { useShortcut } from '@hooks'
1011

11-
import { useShortcut } from '@components/Hooks'
1212
import SliderPreview from './SliderPreview'
1313
import ModalPreview from './ModalPreview'
1414
import Viewer from './Viewer'

containers/UsersThread/index.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import React from 'react'
88
import dynamic from 'next/dynamic'
99

1010
import { connectStore, buildLog } from '@utils'
11+
import { useScript } from '@hooks'
1112

1213
import NumDashboard from './NumDashboard'
1314
import MapLoading from './MapLoading'
@@ -26,6 +27,11 @@ const GeoMapSSR = dynamic({
2627
})
2728

2829
const UsersThreadContainer = ({ usersThread }) => {
30+
/* load g2 from CDN, it's too big for dynamic import, and i am poor ..' */
31+
const [g2ScriptLoaded] = useScript(
32+
'https://a.alipayobjects.com/g/datavis/g2/2.3.13/index.js'
33+
)
34+
2935
useInit(usersThread)
3036

3137
const {
@@ -36,7 +42,7 @@ const UsersThreadContainer = ({ usersThread }) => {
3642
showNums,
3743
} = usersThread
3844

39-
const ready = GeoMapSSR !== null && !geoDataLoading
45+
const ready = g2ScriptLoaded && GeoMapSSR !== null && !geoDataLoading
4046

4147
return (
4248
<Wrapper>

pages/_document.js

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,6 @@ export default class DocumentPage extends Document {
4848
content="width=device-width, initial-scale=1, viewport-fit=cover"
4949
/>
5050

51-
{/* load g2 from CDN, it's too big for dynamic import, and i am poor ..' */}
52-
<script
53-
async
54-
src="https://a.alipayobjects.com/g/datavis/g2/2.3.13/index.js"
55-
/>
56-
{/* https://cps-oss.oss-cn-shanghai.aliyuncs.com/antd-3.8.4-mini.css */}
5751
<link href="/antd-3.8.4-mini.css" rel="stylesheet" />
5852
<link
5953
href="https://fonts.googleapis.com/css?family=Orbitron"
@@ -63,9 +57,7 @@ export default class DocumentPage extends Document {
6357

6458
<script
6559
async
66-
src={`https://www.googletagmanager.com/gtag/js?id=${
67-
process.env.GA_TRACING_ID
68-
}`}
60+
src={`https://www.googletagmanager.com/gtag/js?id=${process.env.GA_TRACING_ID}`}
6961
/>
7062
<script
7163
dangerouslySetInnerHTML={{
@@ -94,12 +86,6 @@ export default class DocumentPage extends Document {
9486
href="https://cdn.staticfile.org/izitoast/1.4.0/css/iziToast.css"
9587
rel="stylesheet"
9688
/>
97-
{/* the ali-oss-sdk es6 import support sucks */}
98-
{/* import from cdn is fine, it's not my money anyway */}
99-
<script
100-
async
101-
src="https://gosspublic.alicdn.com/aliyun-oss-sdk-5.2.0.min.js"
102-
/>
10389
<noscript>
10490
You need to enable JavaScript to get this website runing.
10591
</noscript>

0 commit comments

Comments
 (0)