Skip to content

Commit

Permalink
feat(plugins/mpa): load tabs from atom store
Browse files Browse the repository at this point in the history
  • Loading branch information
NWYLZW committed Nov 10, 2023
1 parent 9d4f84f commit 569eb5f
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 16 deletions.
9 changes: 9 additions & 0 deletions core/src/plugins/mpa/atoms.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { atom } from 'jotai'

export const tabsAtom = atom<{
id: string
icon?: string
title: string
closeable?: boolean
active?: boolean
}[]>([])
32 changes: 16 additions & 16 deletions core/src/plugins/mpa/topbar/Files.tsx
Original file line number Diff line number Diff line change
@@ -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<BarItemProps> = () => {
const [tabs, setTabs] = useState<Tab[]>([
{ id: 'index.ts', title: 'index.ts', icon: 'file' },
{ id: 'index.spec.ts', title: 'index.spec.ts', icon: 'beaker' }
])
const [activeTabId, setActiveTabId] = useState<string | undefined>(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 <div className={prefix}>
{tabs.map(tab => <div
key={tab.id}
className={classnames(`${prefix}-tab`, { active: tab.id === activeTabId })}
>
{tab.icon && typeof tab.icon === 'string'
? <span className={`cldr codicon codicon-${tab.icon}`} />
: tab.icon}
{tab.icon && <span className={`cldr codicon codicon-${tab.icon}`} />}
{tab.title}
<span className='cldr codicon codicon-close' />
</div>)}
Expand Down

0 comments on commit 569eb5f

Please sign in to comment.