From 569eb5f5af364d9577cc7d48995619292ab3448f Mon Sep 17 00:00:00 2001 From: yijie4188 Date: Fri, 10 Nov 2023 01:11:09 +0800 Subject: [PATCH] feat(plugins/mpa): load tabs from atom store --- core/src/plugins/mpa/atoms.ts | 9 ++++++++ core/src/plugins/mpa/topbar/Files.tsx | 32 +++++++++++++-------------- 2 files changed, 25 insertions(+), 16 deletions(-) create mode 100644 core/src/plugins/mpa/atoms.ts diff --git a/core/src/plugins/mpa/atoms.ts b/core/src/plugins/mpa/atoms.ts new file mode 100644 index 00000000..d3843bc6 --- /dev/null +++ b/core/src/plugins/mpa/atoms.ts @@ -0,0 +1,9 @@ +import { atom } from 'jotai' + +export const tabsAtom = atom<{ + id: string + icon?: string + title: string + closeable?: boolean + active?: boolean +}[]>([]) diff --git a/core/src/plugins/mpa/topbar/Files.tsx b/core/src/plugins/mpa/topbar/Files.tsx index e8208edd..29ff6003 100644 --- a/core/src/plugins/mpa/topbar/Files.tsx +++ b/core/src/plugins/mpa/topbar/Files.tsx @@ -1,33 +1,33 @@ import './Files.scss' -import type { ReactNode } from 'react' -import { useState } from 'react' +import { useEffect, useMemo } from 'react' +import { useAtom, useStore } from 'jotai' import { classnames } from '../../../utils' import type { BarItemProps } from '../..' +import { tabsAtom } from '../atoms' const prefix = 'mpa__topbar__files' -interface Tab { - id: string - icon?: string | ReactNode - title: ReactNode -} - export const Files: React.ComponentType = () => { - const [tabs, setTabs] = useState([ - { id: 'index.ts', title: 'index.ts', icon: 'file' }, - { id: 'index.spec.ts', title: 'index.spec.ts', icon: 'beaker' } - ]) - const [activeTabId, setActiveTabId] = useState(tabs[0]?.id) + const [ + tabs, setTabs + ] = useAtom(tabsAtom, { store: useStore() }) + useEffect(() => { + if (tabs.length === 0) { + // TODO only editor mounted? + setTabs([ + { id: 'index.ts', title: 'index.ts', icon: 'file', active: true } + ]) + } + }, [setTabs, tabs.length]) + const activeTabId = useMemo(() => tabs.find(tab => tab.active)?.id, [tabs]) return
{tabs.map(tab =>
- {tab.icon && typeof tab.icon === 'string' - ? - : tab.icon} + {tab.icon && } {tab.title}
)}