From d746ff8ae1222534eee7602bdb2d8b92c05f3f05 Mon Sep 17 00:00:00 2001 From: atinylittleshell <3233006+atinylittleshell@users.noreply.github.com> Date: Fri, 27 Oct 2023 11:51:59 -0700 Subject: [PATCH] Features/keybind (#19) * temp keybind implementation * react to keybinds --- .../src/nativeBridge/modules/common/logger.ts | 4 +- apps/terminal/app/page.tsx | 46 +++-- .../components/ClientSideProviders/index.tsx | 7 +- apps/terminal/components/Terminal/index.tsx | 8 +- apps/terminal/hooks/ConfigContext.tsx | 2 +- apps/terminal/hooks/KeybindContext.tsx | 178 ++++++++++++++++++ apps/terminal/package.json | 1 + package-lock.json | 6 + package.json | 2 +- packages/types/config.ts | 9 + packages/types/defaultConfig.ts | 11 ++ 11 files changed, 256 insertions(+), 18 deletions(-) create mode 100644 apps/terminal/hooks/KeybindContext.tsx diff --git a/apps/app/src/nativeBridge/modules/common/logger.ts b/apps/app/src/nativeBridge/modules/common/logger.ts index d59d53b..0485955 100644 --- a/apps/app/src/nativeBridge/modules/common/logger.ts +++ b/apps/app/src/nativeBridge/modules/common/logger.ts @@ -23,21 +23,23 @@ export class Logger extends EventEmitter { this.logPath = path.join(getAppDirs().userData, 'logs.log'); this.winstonLogger = winston.createLogger({ - level: 'info', format: logFormat, transports: app.isPackaged ? [ new winston.transports.File({ + level: 'info', filename: this.logPath, format: winston.format.combine(logFormat, winston.format.json()), }), ] : [ new winston.transports.File({ + level: 'info', filename: this.logPath, format: winston.format.combine(logFormat, winston.format.json()), }), new winston.transports.Console({ + level: 'debug', format: winston.format.combine( winston.format.colorize(), logFormat, diff --git a/apps/terminal/app/page.tsx b/apps/terminal/app/page.tsx index fd07512..05a52ea 100644 --- a/apps/terminal/app/page.tsx +++ b/apps/terminal/app/page.tsx @@ -4,12 +4,13 @@ import { DEFAULT_CONFIG } from '@terminalone/types'; import _ from 'lodash'; import dynamic from 'next/dynamic'; -import { useEffect, useState } from 'react'; +import { useCallback, useEffect, useState } from 'react'; import { FiMenu, FiPlus, FiX } from 'react-icons/fi'; import SettingsPage from '../components/SettingsPage'; import ShellSelector from '../components/ShellSelector'; import { useConfigContext } from '../hooks/ConfigContext'; +import { useKeybindContext } from '../hooks/KeybindContext'; const TitleBar = dynamic(() => import('../components/TitleBar'), { ssr: false, @@ -25,10 +26,39 @@ type UserTab = { const Page = () => { const { config, loading } = useConfigContext(); + const { commands } = useKeybindContext(); const [tabId, setTabId] = useState(0); const [userTabs, setUserTabs] = useState([]); + const createTab = useCallback(() => { + const newTabId = (_.max(userTabs.map((t) => t.tabId)) || 0) + 1; + setUserTabs([ + ...userTabs, + { + tabId: newTabId, + shellName: config.shells.length === 1 ? config.defaultShellName : null, + }, + ]); + setTabId(newTabId); + }, [userTabs, config]); + + const closeTab = useCallback(() => { + const newTabs = userTabs.filter((t) => t.tabId !== tabId); + setUserTabs(newTabs); + setTabId(_.max(newTabs.map((t) => t.tabId)) || 0); + }, [userTabs, tabId]); + + useEffect(() => { + commands.on('createTab', createTab); + commands.on('closeTab', closeTab); + + return () => { + commands.off('createTab', createTab); + commands.off('closeTab', closeTab); + }; + }, [commands, createTab, closeTab]); + useEffect(() => { if (!loading && userTabs.length === 0) { setTabId(1); @@ -84,9 +114,7 @@ const Page = () => {