Skip to content

Commit 87bdde8

Browse files
Add the shareable ID to a notebook's URL on the first time it is shared (#243)
* Set sharing URL in notebooks on first share * Change variable name `newReadable` to `notebookID` Co-authored-by: Michał Krassowski <5832902+krassowski@users.noreply.github.com> --------- Co-authored-by: Michał Krassowski <5832902+krassowski@users.noreply.github.com>
1 parent 30dba60 commit 87bdde8

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

src/index.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,19 @@ function generateShareURL(notebookID: string): string {
4444
return `${baseUrl}?notebook=${notebookID}`;
4545
}
4646

47+
/**
48+
* Sets or updates the 'notebook' query parameter in the current URL to the given notebookID.
49+
* If the parameter already exists with the same value, no change is made.
50+
* @param notebookID - The ID of the notebook to set in the URL.
51+
*/
52+
function setNotebookParamInUrl(notebookID: string): void {
53+
const url = new URL(window.location.href);
54+
if (url.searchParams.get('notebook') !== notebookID) {
55+
url.searchParams.set('notebook', notebookID);
56+
window.history.replaceState({}, '', url.toString());
57+
}
58+
}
59+
4760
const manuallySharing = new WeakSet<NotebookPanel | ViewOnlyNotebookPanel>();
4861

4962
/**
@@ -134,6 +147,17 @@ async function handleNotebookSharing(
134147

135148
notebookPanel.context.model.fromJSON(notebookContent);
136149
await notebookPanel.context.save();
150+
151+
try {
152+
const notebookID =
153+
(notebookContent.metadata?.readableId as string | undefined) ??
154+
(notebookContent.metadata?.sharedId as string | undefined);
155+
if (notebookID) {
156+
setNotebookParamInUrl(notebookID);
157+
}
158+
} catch (e) {
159+
console.warn('Failed to update URL with shareable notebook ID:', e);
160+
}
137161
}
138162

139163
if (manual) {

0 commit comments

Comments
 (0)