Skip to content

Commit

Permalink
remove duplicated id and tabId
Browse files Browse the repository at this point in the history
  • Loading branch information
boyazeng committed Apr 25, 2024
1 parent 3cf3e77 commit 64c55e4
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 15 deletions.
14 changes: 5 additions & 9 deletions public/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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()}`;
Expand All @@ -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,
Expand All @@ -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()}`;

Expand All @@ -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,
Expand All @@ -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 }, () => {
Expand Down
5 changes: 2 additions & 3 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -92,16 +92,15 @@ 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) {
if (updatedTabsByCategory.hasOwnProperty(category)) {
updatedTabsByCategory[category] = updatedTabsByCategory[category].filter(tab => tab.id !== mainId);
}
}
setTabsByCategory(updatedTabsByCategory);
setFullTabsByCategory(updatedTabsByCategory);
});
});
Expand Down
5 changes: 2 additions & 3 deletions src/components/TabsItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ export interface Tab {
id: number;
text: string;
url: string;
tabId: number;
time: string;
}

Expand All @@ -19,7 +18,7 @@ export const TabsItem: React.FC<TabsItemProps> = ({ tab, onDelete }) => {
const handleUrlClick = () => {
chrome.runtime.sendMessage({
action: 'navigateToTab',
tabId: tab.tabId,
tabId: tab.id,
});
};

Expand All @@ -31,7 +30,7 @@ export const TabsItem: React.FC<TabsItemProps> = ({ tab, onDelete }) => {
</span>
<div className="tab-timestamp">{timestamp}</div>
</div>
<button onClick={() => onDelete(tab.tabId)}>Delete</button>
<button onClick={() => onDelete(tab.id)}>Delete</button>
</li>
);
};

0 comments on commit 64c55e4

Please sign in to comment.