Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ui: add progress bar when switching pages #587

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions ui/dashboardApp/layout/main/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Root } from '@lib/components'
import { useLocalStorageState } from '@umijs/hooks'
import { HashRouter as Router } from 'react-router-dom'
import { useSpring, animated } from 'react-spring'
import { TopLoadingBar } from '@lib/components'

import Sider from './Sider'
import styles from './index.module.less'
Expand Down Expand Up @@ -70,6 +71,7 @@ export default function App({ registry }) {
return (
<Root>
<Router>
<TopLoadingBar />
<animated.div className={styles.container} style={transContainer}>
<Sider
registry={registry}
Expand Down
28 changes: 16 additions & 12 deletions ui/dashboardApp/layout/signin/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as singleSpa from 'single-spa'
import { Root } from '@lib/components'
import React, { useState, useEffect, useRef } from 'react'
import { HashRouter as Router } from 'react-router-dom'
import {
DownOutlined,
GlobalOutlined,
Expand All @@ -10,7 +11,7 @@ import {
import { Form, Input, Button, message } from 'antd'
import { motion } from 'framer-motion'
import { useTranslation } from 'react-i18next'
import LanguageDropdown from '@lib/components/LanguageDropdown'
import { LanguageDropdown, TopLoadingBar } from '@lib/components'
import client from '@lib/client'
import * as auth from '@lib/utils/auth'

Expand Down Expand Up @@ -172,19 +173,22 @@ function TiDBSignInForm({ registry }) {
function App({ registry }) {
return (
<Root>
<div className={styles.container}>
<div className={styles.dialogContainer}>
<div className={styles.dialog}>
<TiDBSignInForm registry={registry} />
<Router>
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After testing, the TopLoadingBar can't receive the single-spa:before-routing-event event if it isn't wrapped by the Router component.

<TopLoadingBar />
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about making it another SingleSPA app that always exists on the page, or simply put it outside everything mounted by SingleSPA. In this way, it can listen SingleSPA global events gracefully without worrying about its own lifecycle.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It may be a solution, let me have a try.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Implement it by this way in PR #661 , much more graceful! will close this PR.

<div className={styles.container}>
<div className={styles.dialogContainer}>
<div className={styles.dialog}>
<TiDBSignInForm registry={registry} />
</div>
</div>
<motion.div
initial={{ opacity: 0 }}
animate={{ opacity: 1 }}
transition={{ ease: 'easeOut', duration: 0.5 }}
className={styles.landing}
></motion.div>
</div>
<motion.div
initial={{ opacity: 0 }}
animate={{ opacity: 1 }}
transition={{ ease: 'easeOut', duration: 0.5 }}
className={styles.landing}
></motion.div>
</div>
</Router>
</Root>
)
}
Expand Down
34 changes: 34 additions & 0 deletions ui/lib/components/TopLoadingBar/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import React, { useRef, useEffect } from 'react'
import LoadingBar from 'react-top-loading-bar'

const useLoadingBar = () => {
const loadingBar = useRef<LoadingBar>(null)

useEffect(() => {
function startLoading() {
loadingBar?.current?.continuousStart()
}
window.addEventListener('single-spa:before-routing-event', startLoading)
return () =>
window.removeEventListener(
'single-spa:before-routing-event',
startLoading
)
}, [])

useEffect(() => {
function completeLoading() {
loadingBar?.current?.complete()
}
window.addEventListener('single-spa:routing-event', completeLoading)
return () =>
window.removeEventListener('single-spa:routing-event', completeLoading)
}, [])

return loadingBar
}

export default function TopLoadinngBar() {
const loadingBar = useLoadingBar()
return <LoadingBar ref={loadingBar} />
}
1 change: 1 addition & 0 deletions ui/lib/components/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,4 @@ export { default as ShortValueWithTooltip } from './ShortValueWithTooltip'

export { default as LanguageDropdown } from './LanguageDropdown'
export { default as ParamsPageWrapper } from './ParamsPageWrapper'
export { default as TopLoadingBar } from './TopLoadingBar'
1 change: 1 addition & 0 deletions ui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"react-router-dom": "^6.0.0-alpha.3",
"react-spring": "^8.0.27",
"react-syntax-highlighter": "^12.2.1",
"react-top-loading-bar": "^1.2.0",
"react-use": "^14.2.0",
"single-spa": "^5.3.4",
"single-spa-react": "^2.14.0",
Expand Down
5 changes: 5 additions & 0 deletions ui/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -11372,6 +11372,11 @@ react-syntax-highlighter@^12.2.1:
prismjs "^1.8.4"
refractor "^2.4.1"

react-top-loading-bar@^1.2.0:
version "1.2.0"
resolved "https://registry.yarnpkg.com/react-top-loading-bar/-/react-top-loading-bar-1.2.0.tgz#fcbca3c2e462bee7b1c0d1850e8b584cd7bd5a26"
integrity sha512-5oNdy+DfD5JK06bcc/gsnnXHmml+d8eaBe3C8KQ3eLiH/BD8+FcwsgbAwqgOaRjuSeVQXdYN2JC2G1uVFtCLfA==

react-universal-interface@^0.6.0:
version "0.6.0"
resolved "https://registry.yarnpkg.com/react-universal-interface/-/react-universal-interface-0.6.0.tgz#b65cbf7d71a2f3f7dd9705d8e4f06748539bd465"
Expand Down