-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
128 lines (113 loc) · 3.92 KB
/
background.js
File metadata and controls
128 lines (113 loc) · 3.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
const openCategoriesPage = () =>
chrome.tabs.create({
url: chrome.runtime.getURL("/categories/categories.html"),
});
chrome.runtime.onMessage.addListener((message, _, sendResponse) => {
switch (message.type) {
case "openExtensionPage":
openCategoriesPage();
break;
case "getCategories":
chrome.storage.local.get({ categories: [] }).then(({ categories }) => {
sendResponse(categories);
});
break;
case "addToCategory":
chrome.storage.local.get({ categories: [] }).then(({ categories }) => {
const categoryIndex = categories.findIndex(
(category) => category.id === message.categoryId,
);
if (categoryIndex === -1) {
sendResponse({ success: false, error: "Category not found" });
return;
}
// Check if chat already exists in this category
const existingChat = categories[categoryIndex].chats.find(
(chat) => chat.id === message.chat.id,
);
if (existingChat) {
sendResponse({
success: false,
error: "Chat already in this category",
errorCode: "already_in_category",
});
return;
}
categories[categoryIndex].chats.push(message.chat);
chrome.storage.local.set({ categories }).then(() => {
sendResponse({ success: true });
});
});
break;
case "deleteCategory":
const { id } = message;
chrome.storage.local.get({ categories: [] }).then(({ categories }) => {
const updatedCategories = categories.filter(
(category) => category.id !== id,
);
chrome.storage.local.set({ categories: updatedCategories }).then(() => {
sendResponse(updatedCategories);
});
});
break;
case "createCategory":
const { name } = message;
chrome.storage.local.get({ categories: [] }).then(({ categories }) => {
const newId = self.crypto.randomUUID();
const newCategory = [...categories, { id: newId, name, chats: [] }];
chrome.storage.local.set({ categories: newCategory }).then(() => {
sendResponse(newCategory);
});
});
break;
case "removeFromCategory":
const { categoryId, chatId } = message;
chrome.storage.local.get({ categories: [] }).then(({ categories }) => {
const categoryIndex = categories.findIndex(
(category) => category.id === categoryId,
);
if (categoryIndex !== -1) {
categories[categoryIndex].chats = categories[
categoryIndex
].chats.filter((chat) => chat.id !== chatId);
chrome.storage.local.set({ categories }).then(() => {
sendResponse({ success: true });
});
}
});
break;
case "openChat":
const { url } = message;
chrome.tabs.create({
url: url,
});
break;
case "syncDeleteChat":
const { chatId: syncChatId } = message;
chrome.storage.local.get({ categories: [] }).then(({ categories }) => {
let wasModified = false;
// Go through each category and filter out the deleted chat
const updatedCategories = categories.map((category) => {
const originalLength = category.chats.length;
category.chats = category.chats.filter(
(chat) => chat.id !== syncChatId,
);
// If the length changed, it means we removed something
if (category.chats.length !== originalLength) {
wasModified = true;
}
return category;
});
// Only update storage if a change actually occurred
if (wasModified) {
chrome.storage.local.set({ categories: updatedCategories });
}
});
// No need for sendResponse, this is a fire-and-forget sync
break;
}
return true;
});
chrome.action.onClicked.addListener(() => {
openCategoriesPage();
});