feat(directory): useHeader to handle fast navigation

This commit is contained in:
Haoyu Xu
2023-06-24 02:23:34 -04:00
parent 83ffa6c85f
commit bf4edf96b9
5 changed files with 53 additions and 43 deletions

View File

@@ -1,5 +1,5 @@
import React, { import React, {
useState useState,
} from 'react' } from 'react'
import PropTypes from 'prop-types'; import PropTypes from 'prop-types';
import classes from './scss/dropdown.module.scss' import classes from './scss/dropdown.module.scss'
@@ -13,7 +13,7 @@ export default function Dropdown(props) {
return ( return (
<> <>
<section className={`${classes.dropdown} ${hidden ? '' : classes.active} ${props.className ? props.className : ''}`} > <section className={`${classes.dropdown} ${hidden ? '' : classes.active} ${props.className ? props.className : ''}`}>
<section <section
className={classes.text} className={classes.text}
onClick={() => toggleDropdown()} onClick={() => toggleDropdown()}

View File

@@ -67,6 +67,7 @@
opacity: 0; opacity: 0;
position: absolute; position: absolute;
background-color: var(--root-background-color); background-color: var(--root-background-color);
min-width: 38.2vw;
width: max-content; width: max-content;
max-height: 61.8vh; max-height: 61.8vh;
max-width: 61.8vw; max-width: 61.8vw;

View File

@@ -284,49 +284,16 @@ HeaderTabsElement.propTypes = {
function HeaderButton() { function HeaderButton() {
const navigate = useNavigate() const navigate = useNavigate()
let location = useLocation();
const { operators } = useConfig()
const { language } = useLanguage()
const { i18n } = useI18n() const { i18n } = useI18n()
const {
fastNavigation
} = useHeader()
const fastNavigateDict = useMemo(() => { if (fastNavigation.length > 0) {
const dict = {}
operators.forEach((item) => {
if (!(item.date in dict)) {
dict[item.date] = []
}
dict[item.date].push({
codename: item.codename,
link: item.link,
})
})
return dict
}, [operators])
const fastNavigateList = useMemo(() => {
const list = []
for (const [key, value] of Object.entries(fastNavigateDict)) {
list.push({
name: key,
value: null,
type: "date",
})
value.forEach((item) => {
list.push({
name: item.codename[language],
value: item.link,
type: "item",
})
})
}
return list
}, [fastNavigateDict, language])
if (location.pathname === routes.find((item) => item.index).path) {
return ( return (
<Border> <Border>
<Dropdown <Dropdown
menu={fastNavigateList} menu={fastNavigation}
altText={i18n("fast_navigation")} altText={i18n("fast_navigation")}
onClick={(item) => { onClick={(item) => {
navigate(item.value) navigate(item.value)

View File

@@ -33,13 +33,15 @@ export default function Home() {
setTitle, setTitle,
setTabs, setTabs,
currentTab, currentTab,
setHeaderIcon setHeaderIcon,
setFastNavigation
} = useHeader() } = useHeader()
const { config } = useConfig() const { config, operators } = useConfig()
const [content, setContent] = useState([]) const [content, setContent] = useState([])
const [voiceOn] = useAtom(voiceOnAtom) const [voiceOn] = useAtom(voiceOnAtom)
const [voiceSrc, setVoiceSrc] = useState(null) const [voiceSrc, setVoiceSrc] = useState(null)
const [voiceReplay, setVoiceReplay] = useState(false) const [voiceReplay, setVoiceReplay] = useState(false)
const { language } = useLanguage()
useEffect(() => { useEffect(() => {
setTitle('dynamic_compile') setTitle('dynamic_compile')
@@ -66,6 +68,43 @@ export default function Home() {
const isShown = useCallback((type) => currentTab === 'all' || currentTab === type, [currentTab]) const isShown = useCallback((type) => currentTab === 'all' || currentTab === type, [currentTab])
const fastNavigateDict = useMemo(() => {
const dict = {}
operators.forEach((item) => {
if (!(item.date in dict)) {
dict[item.date] = []
}
dict[item.date].push({
codename: item.codename,
link: item.link,
type: item.type
})
})
return dict
}, [operators])
useEffect(() => {
const list = []
for (const [key, value] of Object.entries(fastNavigateDict)) {
const newValue = value.filter((item) => isShown(item.type))
if (newValue.length > 0) {
list.push({
name: key,
value: null,
type: "date",
})
newValue.forEach((item) => {
list.push({
name: item.codename[language],
value: item.link,
type: "item",
})
})
}
}
setFastNavigation(list)
}, [currentTab, fastNavigateDict, isShown, language, setFastNavigation])
const handleVoicePlay = useCallback((src) => { const handleVoicePlay = useCallback((src) => {
if (!voiceOn) { if (!voiceOn) {
setVoiceSrc(null) setVoiceSrc(null)

View File

@@ -8,6 +8,7 @@ const tabsAtom = atom([]);
const currentTabAtom = atom(null); const currentTabAtom = atom(null);
const appbarExtraAreaAtom = atom([]); const appbarExtraAreaAtom = atom([]);
const headerIconAtom = atom(null); const headerIconAtom = atom(null);
const fastNaviationAtom = atom([]);
export function useHeader() { export function useHeader() {
const [key, setTitle] = useAtom(keyAtom); const [key, setTitle] = useAtom(keyAtom);
@@ -16,6 +17,7 @@ export function useHeader() {
const [currentTab, setCurrentTab] = useAtom(currentTabAtom); const [currentTab, setCurrentTab] = useAtom(currentTabAtom);
const [appbarExtraArea, setAppbarExtraArea] = useAtom(appbarExtraAreaAtom); const [appbarExtraArea, setAppbarExtraArea] = useAtom(appbarExtraAreaAtom);
const [headerIcon, setHeaderIcon] = useAtom(headerIconAtom); const [headerIcon, setHeaderIcon] = useAtom(headerIconAtom);
const [fastNavigation, setFastNavigation] = useAtom(fastNaviationAtom);
const { i18n } = useI18n() const { i18n } = useI18n()
useEffect(() => { useEffect(() => {
@@ -29,6 +31,7 @@ export function useHeader() {
tabs, setTabs, tabs, setTabs,
currentTab, setCurrentTab, currentTab, setCurrentTab,
appbarExtraArea, setAppbarExtraArea, appbarExtraArea, setAppbarExtraArea,
headerIcon, setHeaderIcon headerIcon, setHeaderIcon,
fastNavigation, setFastNavigation,
} }
} }