Skip to content

Commit 41f960c

Browse files
authored
fix for tabs out of order in tabbar (#1478)
1 parent 3ccdcb6 commit 41f960c

File tree

1 file changed

+53
-19
lines changed

1 file changed

+53
-19
lines changed

frontend/app/tab/tabbar.tsx

Lines changed: 53 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,43 @@ const ConfigErrorIcon = ({ buttonRef }: { buttonRef: React.RefObject<HTMLElement
9999
);
100100
};
101101

102+
function strArrayIsEqual(a: string[], b: string[]) {
103+
// null check
104+
if (a == null && b == null) {
105+
return true;
106+
}
107+
if (a == null || b == null) {
108+
return false;
109+
}
110+
if (a.length !== b.length) {
111+
return false;
112+
}
113+
for (let i = 0; i < a.length; i++) {
114+
if (a[i] !== b[i]) {
115+
return false;
116+
}
117+
}
118+
return true;
119+
}
120+
121+
function setIsEqual(a: Set<string> | null, b: Set<string> | null): boolean {
122+
if (a == null && b == null) {
123+
return true;
124+
}
125+
if (a == null || b == null) {
126+
return false;
127+
}
128+
if (a.size !== b.size) {
129+
return false;
130+
}
131+
for (const item of a) {
132+
if (!b.has(item)) {
133+
return false;
134+
}
135+
}
136+
return true;
137+
}
138+
102139
const TabBar = memo(({ workspace }: TabBarProps) => {
103140
const [tabIds, setTabIds] = useState<string[]>([]);
104141
const [pinnedTabIds, setPinnedTabIds] = useState<Set<string>>(new Set());
@@ -148,25 +185,22 @@ const TabBar = memo(({ workspace }: TabBarProps) => {
148185
}, [tabIds]);
149186

150187
useEffect(() => {
151-
if (workspace) {
152-
// Compare current tabIds with new workspace.tabids
153-
console.log("tabbar workspace", workspace);
154-
const newTabIds = new Set([...(workspace.pinnedtabids ?? []), ...(workspace.tabids ?? [])]);
155-
const newPinnedTabIds = workspace.pinnedtabids ?? [];
156-
157-
const areEqual =
158-
tabIds.length === newTabIds.size &&
159-
tabIds.every((id) => newTabIds.has(id)) &&
160-
newPinnedTabIds.length === pinnedTabIds.size;
161-
162-
if (!areEqual) {
163-
const newPinnedTabIdSet = new Set(newPinnedTabIds);
164-
console.log("newPinnedTabIds", newPinnedTabIds);
165-
const newTabIdList = newPinnedTabIds.concat([...newTabIds].filter((id) => !newPinnedTabIdSet.has(id))); // Corrects for any duplicates between the two lists
166-
console.log("newTabIdList", newTabIdList);
167-
setTabIds(newTabIdList);
168-
setPinnedTabIds(newPinnedTabIdSet);
169-
}
188+
if (!workspace) {
189+
return;
190+
}
191+
// Compare current tabIds with new workspace.tabids
192+
console.log("tabbar workspace", workspace);
193+
194+
const newTabIdsArr = [...(workspace.pinnedtabids ?? []), ...(workspace.tabids ?? [])];
195+
const newPinnedTabSet = new Set(workspace.pinnedtabids ?? []);
196+
197+
const areEqual = strArrayIsEqual(tabIds, newTabIdsArr) && setIsEqual(pinnedTabIds, newPinnedTabSet);
198+
199+
if (!areEqual) {
200+
console.log("newPinnedTabIds", newPinnedTabSet);
201+
console.log("newTabIdList", newTabIdsArr);
202+
setTabIds(newTabIdsArr);
203+
setPinnedTabIds(newPinnedTabSet);
170204
}
171205
}, [workspace, tabIds, pinnedTabIds]);
172206

0 commit comments

Comments
 (0)