From 31f4b3621a4b63971468497932eb8dfbd025d98c Mon Sep 17 00:00:00 2001 From: atinylittleshell <3233006+atinylittleshell@users.noreply.github.com> Date: Thu, 8 Aug 2024 15:59:35 -0700 Subject: [PATCH] Add support for renaming a tab Add support for renaming tabs in the terminal application. * **State Management:** - Add a new state variable `tabTitles` in `packages/terminal/app/page.tsx` to store tab names. - Modify the `createTab` function to initialize the tab name. - Modify the `closeTab` function to remove the tab name. - Pass the `tabName` prop to the `Tab` component. * **Tab Component:** - Accept a `tabName` prop in `packages/terminal/components/Tab/index.tsx`. - Display the `tabName` in the tab. - Add an input field to allow users to rename the tab. - Handle the input change event to update the tab name. * **TitleBar Component:** - Update the title bar in `packages/terminal/components/TitleBar/index.tsx` to reflect the renamed tab. - Use the `tabName` prop to display the tab name in the title bar. * **TabContext:** - Add a function to handle tab renaming in `packages/terminal/hooks/TabContext.tsx`. - Update the context to include the tab renaming function. --- For more details, open the [Copilot Workspace session](https://copilot-workspace.githubnext.com/atinylittleshell/TerminalOne?shareId=XXXX-XXXX-XXXX-XXXX). --- packages/terminal/app/page.tsx | 14 ++++++++++- packages/terminal/components/Tab/index.tsx | 17 ++++++++++++++ .../terminal/components/TitleBar/index.tsx | 6 +++-- packages/terminal/hooks/TabContext.tsx | 23 +++++++++++++++++++ 4 files changed, 57 insertions(+), 3 deletions(-) diff --git a/packages/terminal/app/page.tsx b/packages/terminal/app/page.tsx index 83143d0..846b76f 100644 --- a/packages/terminal/app/page.tsx +++ b/packages/terminal/app/page.tsx @@ -27,6 +27,7 @@ const Page = () => { const [tabId, setTabId] = useState(0); const [userTabs, setUserTabs] = useState([]); + const [tabTitles, setTabTitles] = useState<{ [key: number]: string }>({}); const createTab = useCallback(() => { let newTabId = (_.max(userTabs.map((t) => t.tabId)) || 0) + 1; @@ -46,6 +47,10 @@ const Page = () => { }, ].sort((a, b) => a.tabId - b.tabId), ); + setTabTitles((prevTabTitles) => ({ + ...prevTabTitles, + [newTabId]: `Tab ${newTabId}`, + })); setTabId(newTabId); }, [userTabs, config]); @@ -58,6 +63,11 @@ const Page = () => { setUserTabs(newTabs); setTabId(_.max(newTabs.map((t) => t.tabId)) || 0); } + setTabTitles((prevTabTitles) => { + const newTabTitles = { ...prevTabTitles }; + delete newTabTitles[targetTabId]; + return newTabTitles; + }); }, [userTabs], ); @@ -189,6 +199,7 @@ const Page = () => { tabId: 1, }, ]); + setTabTitles({ 1: 'Tab 1' }); } }, [loading, config, userTabs]); @@ -234,7 +245,7 @@ const Page = () => { setTabId(userTab.tabId); }} > - {userTab.tabId} + {tabTitles[userTab.tabId] || userTab.tabId} {userTab.tabId === tabId && (