Skip to content

Commit 3c6c4ad

Browse files
authored
Merge pull request #196 from Pranav-0440/fix/top-level-links-178
Fix #178: Adding top-level links now works correctly
2 parents bd42886 + 3c8d6a4 commit 3c6c4ad

File tree

1 file changed

+13
-5
lines changed

1 file changed

+13
-5
lines changed

src/components/Dialogs/AddLinkTdDialog.tsx

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,14 @@ const AddLinkTdDialog = forwardRef<AddLinkTdDialogRef, AddLinkTdDialogProps>(
8080
};
8181

8282
const addLinksToTd = (link: Link): void => {
83-
tdJSON["links"] = [...(tdJSON["links"] ? tdJSON["links"] : []), link];
84-
context.updateOfflineTD(JSON.stringify(tdJSON, null, 2));
83+
// clone instead of mutating original TD
84+
const updatedTd = structuredClone(context.parsedTD);
85+
// initialize links array if missing
86+
if (!Array.isArray(updatedTd.links)) {
87+
updatedTd.links = [];
88+
}
89+
updatedTd.links.push(link);
90+
context.updateOfflineTD(JSON.stringify(updatedTd, null, 2));
8591
};
8692

8793
const linkingMethodChange = (linkingOption: string): void => {
@@ -243,6 +249,8 @@ const AddLinkTdDialog = forwardRef<AddLinkTdDialogRef, AddLinkTdDialogProps>(
243249
return;
244250
}
245251

252+
const linkedTd: Record<string, any> = {};
253+
246254
const link: Link = {
247255
href:
248256
(
@@ -272,7 +280,7 @@ const AddLinkTdDialog = forwardRef<AddLinkTdDialogRef, AddLinkTdDialogProps>(
272280
) {
273281
try {
274282
var httpRequest = new XMLHttpRequest();
275-
httpRequest.open("GET", href, false);
283+
httpRequest.open("GET", link.href, false);
276284
httpRequest.send();
277285
if (
278286
httpRequest
@@ -286,10 +294,10 @@ const AddLinkTdDialog = forwardRef<AddLinkTdDialogRef, AddLinkTdDialogProps>(
286294
} catch (ex) {
287295
const msg = "We ran into an error trying to fetch your TD.";
288296
console.error(msg, ex);
289-
linkedTd[href] = currentLinkedTd;
297+
linkedTd[link.href] = currentLinkedTd;
290298
}
291299
} else {
292-
linkedTd[href] = currentLinkedTd;
300+
linkedTd[link.href] = currentLinkedTd;
293301
}
294302

295303
if (link.href === "") {

0 commit comments

Comments
 (0)