From 64c55e42628bdddb461598775680b66a22f544e3 Mon Sep 17 00:00:00 2001 From: Boya Zeng Date: Thu, 25 Apr 2024 16:14:54 -0400 Subject: [PATCH] remove duplicated id and tabId --- public/background.js | 14 +++++--------- src/App.tsx | 5 ++--- src/components/TabsItem.tsx | 5 ++--- 3 files changed, 9 insertions(+), 15 deletions(-) diff --git a/public/background.js b/public/background.js index 2942086..9e62bf3 100644 --- a/public/background.js +++ b/public/background.js @@ -22,9 +22,8 @@ chrome.contextMenus.onClicked.addListener((info, tab) => { // Create a new tab object with a unique ID, the selected text, and tab/window IDs const newTab = { - id: Date.now(), + id: tab.id, text: selectedText, - tabId: tab.id, // Associate the tab with the tab it was created in windowId: tab.windowId, // Associate the tab with the window it was created in time: Date.now(), url: tab.url, @@ -65,9 +64,8 @@ function handleTabCreation(tab, isNewWindow) { const readableDate = currentDate.toLocaleString(); const newTab = { - id: Date.now(), + id: tab.id, text: `${tab.url}\nCreated at: ${readableDate}`, - tabId: tab.id, windowId: tab.windowId, time: Date.now(), url: tab.url, @@ -116,7 +114,7 @@ chrome.tabs.onActivated.addListener(activeInfo => { return; } - let found = tabs.find(tab => tab.tabId === tabId); + let found = tabs.find(tab => tab.id === tabId); if (found) { found.text = `${tab.title}\nActivated at: ${currentTime.toLocaleString()}`; @@ -125,7 +123,6 @@ chrome.tabs.onActivated.addListener(activeInfo => { tabs.push({ id: tabId, text: `${tab.title}\nActivated at: ${currentTime.toLocaleString()}`, - tabId: tabId, windowId: activeInfo.windowId, time: Date.now(), url: tab.url, @@ -146,7 +143,7 @@ chrome.tabs.onUpdated.addListener((tabId, changeInfo, tab) => { chrome.storage.local.get({ tabs: [] }, (result) => { let tabs = result.tabs; tabs.sort((a, b) => a.time - b.time); - let found = tabs.find(tab => tab.tabId === tabId); + let found = tabs.find(tab => tab.id === tabId); const tabText = `${tab.title}\nUpdated at: ${currentTime.toLocaleString()}`; @@ -156,7 +153,6 @@ chrome.tabs.onUpdated.addListener((tabId, changeInfo, tab) => { tabs.push({ id: tabId, text: tabText, - tabId: tabId, windowId: tab.windowId, time: Date.now(), url: tab.url, @@ -174,7 +170,7 @@ chrome.tabs.onRemoved.addListener(tabId => { tabIdToDelete = tabId; chrome.storage.local.get({ tabs: [] }, (result) => { let tabs = result.tabs; - const index = tabs.findIndex(tab => tab.tabId === tabId); + const index = tabs.findIndex(tab => tab.id === tabId); if (index !== -1) { tabs.splice(index, 1); chrome.storage.local.set({ tabs }, () => { diff --git a/src/App.tsx b/src/App.tsx index 5f973e4..4862003 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -92,8 +92,8 @@ const handleDeleteTab = (id: number) => { const updatedTabs = result.tabs.filter((tab: Tab) => tab.id !== mainId); chrome.storage.local.set({ tabs: updatedTabs }, () => { // After deleting from local storage, remove the associated tab - chrome.tabs.remove(tabId, () => { - console.log(`Tab ${tabId} removed.`); + chrome.tabs.remove(id, () => { + console.log(`Tab ${id} removed.`); // Update tabsByCategory state after deletion const updatedTabsByCategory = { ...tabsByCategory }; for (const category in updatedTabsByCategory) { @@ -101,7 +101,6 @@ const handleDeleteTab = (id: number) => { updatedTabsByCategory[category] = updatedTabsByCategory[category].filter(tab => tab.id !== mainId); } } - setTabsByCategory(updatedTabsByCategory); setFullTabsByCategory(updatedTabsByCategory); }); }); diff --git a/src/components/TabsItem.tsx b/src/components/TabsItem.tsx index fb4c177..fab6144 100644 --- a/src/components/TabsItem.tsx +++ b/src/components/TabsItem.tsx @@ -4,7 +4,6 @@ export interface Tab { id: number; text: string; url: string; - tabId: number; time: string; } @@ -19,7 +18,7 @@ export const TabsItem: React.FC = ({ tab, onDelete }) => { const handleUrlClick = () => { chrome.runtime.sendMessage({ action: 'navigateToTab', - tabId: tab.tabId, + tabId: tab.id, }); }; @@ -31,7 +30,7 @@ export const TabsItem: React.FC = ({ tab, onDelete }) => {
{timestamp}
- + ); };