From b83bc8fb0d420fea382c3e1e0fbb07c19e0eaeaf Mon Sep 17 00:00:00 2001 From: zhouteng Date: Wed, 9 Jun 2021 18:39:38 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E7=AD=9B=E9=80=89bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/pages/crontab/index.tsx | 47 +++++++++++++++++++++++++------------ src/pages/crontab/store.ts | 20 ++++++++++++++++ 2 files changed, 52 insertions(+), 15 deletions(-) create mode 100644 src/pages/crontab/store.ts diff --git a/src/pages/crontab/index.tsx b/src/pages/crontab/index.tsx index 876390e8..e374615c 100644 --- a/src/pages/crontab/index.tsx +++ b/src/pages/crontab/index.tsx @@ -40,11 +40,12 @@ import { request } from '@/utils/http'; import CronModal from './modal'; import CronLogModal from './logModal'; import AddFilterModal from './addFilterModal'; +import { store } from './store'; const { Text } = Typography; const { Search } = Input; -type Pane = { title: string; key: string }; +export type Pane = { title: string; key: string }; enum CrontabStatus { 'running', @@ -200,7 +201,7 @@ const Crontab = () => { const [isLogModalVisible, setIsLogModalVisible] = useState(false); const [logCron, setLogCron] = useState(); const [selectedRowIds, setSelectedRowIds] = useState([]); - const [panes, setPanes] = useState([ + const [paneList, setPaneList] = useState([ { title: '内置命令', key: 'ql', @@ -209,11 +210,19 @@ const Crontab = () => { const [activeKey, setActiveKey] = useState(KEY_ALL); const addFilterModalRef = useRef(null); + const setPanesAndStore = useCallback( + (panes: Pane[]) => { + setPaneList(panes); + store.savePanes(panes); + }, + [setPaneList], + ); + const handleAddFilter = useCallback( (values) => { - setPanes([...panes, values]); + setPanesAndStore([...paneList, values]); }, - [panes, setPanes], + [paneList, setPanesAndStore], ); const getCrons = () => { @@ -541,25 +550,28 @@ const Crontab = () => { const remove = useCallback( (targetKey) => { - setPanes(panes.filter((pane) => pane.key !== targetKey)); + setPanesAndStore(paneList.filter((pane) => pane.key !== targetKey)); }, - [panes, setPanes], + [paneList, setPanesAndStore], ); const add = useCallback(() => { (addFilterModalRef.current as any).showModal(); }, []); - const onEdit = useCallback((targetKey: string, action: string) => { - if (action === 'remove') { - remove(targetKey); - } else { - add(); - } - }, []); + const onEdit = useCallback( + (targetKey: string, action: string) => { + if (action === 'remove') { + remove(targetKey); + } else { + add(); + } + }, + [remove], + ); const onTabChange = useCallback((activeKey: string) => { - activeKey !== KEY_ALL && onSearch(activeKey); + onSearch(activeKey !== KEY_ALL ? activeKey : ''); setActiveKey(activeKey); }, []); @@ -574,6 +586,11 @@ const Crontab = () => { getCrons(); }, [searchText]); + useEffect(() => { + const panes = store.getPanes(); + panes && setPaneList(panes); + }, []); + useEffect(() => { if (document.body.clientWidth < 768) { setWidth('auto'); @@ -658,7 +675,7 @@ const Crontab = () => { onChange={onTabChange} > - {panes.map((pane) => { + {paneList.map((pane) => { return ; })} diff --git a/src/pages/crontab/store.ts b/src/pages/crontab/store.ts new file mode 100644 index 00000000..f14f807b --- /dev/null +++ b/src/pages/crontab/store.ts @@ -0,0 +1,20 @@ +import type { Pane } from './index'; + +const KEY_PANES = '__panes__'; + +export const store = { + savePanes(panes: Pane[]) { + localStorage.setItem(KEY_PANES, JSON.stringify({ data: panes })); + }, + + getPanes() { + const panesStr = localStorage.getItem(KEY_PANES); + try { + if (panesStr) { + let panes = JSON.parse(panesStr); + return panes.data; + } + } catch (e) {} + return null; + }, +};