diff --git a/src/components/topbar/menubar/Menu.tsx b/src/components/topbar/menubar/Menu.tsx index fe47cbb7..6c3ba74d 100644 --- a/src/components/topbar/menubar/Menu.tsx +++ b/src/components/topbar/menubar/Menu.tsx @@ -11,7 +11,7 @@ export const Menu = ({ menu }: MenuProps) => { return (
- {Object.keys(menu).map((key) => ( + {Object.keys(menu || {}).map((key) => ( <> { - const [currentAppMenus] = useAtom(menuBarMenusStore); const [activeMenu, setActiveMenu] = useAtom(activeMenuStore); + const [currentAppMenus] = useRightMenusForRightApp(); const parentRef = useRef(); @@ -56,3 +58,40 @@ export const MenuBar = () => {
); }; + +const useRightMenusForRightApp = () => { + const [activeApp] = useAtom(activeAppStore); + const [openApps] = useAtom(openAppsStore); + + const [currentAppMenus, setCurrentAppMenus] = useAtom(menuBarMenusStore); + + // Store previoussly active app to see if it is open now or now + const prevActiveApp = usePrevious(activeApp || 'finder'); + + useLayoutEffect(() => { + async function main() { + // If previous app was closed, revert to Finder + if (prevActiveApp !== 'finder' && !openApps[prevActiveApp]) { + return void setCurrentAppMenus(finderMenuConfig); + } + + let config = finderMenuConfig; + + const allConfigs = import.meta.glob('../../../data/menu/*.menu.config.ts'); + + const pathOfConfig = `../../../data/menu/${activeApp}.menu.config.ts`; + + try { + config = (await allConfigs[pathOfConfig]()).default; + } catch (e) { + console.log(e); + } + + setCurrentAppMenus(config); + } + + main(); + }, [activeApp, openApps]); + + return [currentAppMenus] as const; +}; diff --git a/src/data/menu/calculator.menu.config.ts b/src/data/menu/calculator.menu.config.ts new file mode 100644 index 00000000..73470887 --- /dev/null +++ b/src/data/menu/calculator.menu.config.ts @@ -0,0 +1,57 @@ +import { createMenuConfig } from '__/helpers/create-menu-config'; + +export const vscodeMenuConfig = createMenuConfig({ + default: { + title: 'Code', + menu: {}, + }, + + file: { + title: 'File', + menu: { + 'new-file': { + title: 'New File', + }, + 'new-window': { + title: 'New Window', + breakAfter: true, + }, + + 'open-file': { + title: 'Open File', + }, + 'open-folder': { + title: 'Open Folder', + }, + 'open-workspace': { + title: 'Open Workspace', + }, + 'open-recent': { + title: 'Open recent', + breakAfter: true, + }, + + 'add-folder-to-workspace': { + title: 'Add Folder to Workspace', + }, + 'save-workspace-as': { + title: 'Add Folder to Workspace as...', + breakAfter: true, + }, + + save: { + title: 'Save', + }, + 'save-as': { + title: 'Save as...', + }, + 'save-all': { + title: 'Save All', + breakAfter: true, + disabled: true, + }, + }, + }, +}); + +export default vscodeMenuConfig; diff --git a/src/data/menu/finder.menu.config.ts b/src/data/menu/finder.menu.config.ts index bd202e02..31a667b8 100644 --- a/src/data/menu/finder.menu.config.ts +++ b/src/data/menu/finder.menu.config.ts @@ -384,3 +384,5 @@ export const finderMenuConfig = createMenuConfig({ }, }, }); + +export default finderMenuConfig; diff --git a/src/hooks/index.ts b/src/hooks/index.ts index 5b3757ee..0c80f8bb 100644 --- a/src/hooks/index.ts +++ b/src/hooks/index.ts @@ -4,3 +4,4 @@ export { useTheme } from './use-theme'; export { useTimeout } from './use-timeout'; export { useContextMenu } from './use-context-menu'; export { useFocusOutside } from './use-focus-outside'; +export { usePrevious } from './use-previous'; diff --git a/src/hooks/use-previous.ts b/src/hooks/use-previous.ts new file mode 100644 index 00000000..21d6e66a --- /dev/null +++ b/src/hooks/use-previous.ts @@ -0,0 +1,11 @@ +import { useEffect, useRef } from 'preact/hooks'; + +export function usePrevious(value: T) { + const ref = useRef(); + + useEffect(() => { + ref.current = value; + }); + + return ref.current; +} diff --git a/src/stores/menubar.store.ts b/src/stores/menubar.store.ts index f4212c01..1d725e09 100644 --- a/src/stores/menubar.store.ts +++ b/src/stores/menubar.store.ts @@ -1,12 +1,9 @@ import { atom } from 'jotai'; import { finderMenuConfig } from '__/data/menu/finder.menu.config'; -const menuConfigs = { finder: finderMenuConfig }; - -export const menuBarMenusStore = atom( - // Uncomment when all apps get their own menus - // (get) => menuConfigs[get(activeAppStore) as keyof typeof menuConfigs], - menuConfigs.finder, +export const menuBarMenusStore = atom>( + // All apps will load their own configs, to support code splitting + {}, ); export const activeMenuStore = atom('');