CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
sagemathinc

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.

GitHub Repository: sagemathinc/cocalc
Path: blob/master/src/packages/next/lib/share/edit-url.ts
Views: 687
1
/*
2
* This file is part of CoCalc: Copyright © 2020 Sagemath, Inc.
3
* License: MS-RSL – see LICENSE.md for details
4
*/
5
6
import { join } from "path";
7
import basePath from "lib/base-path";
8
9
interface AnonymousOptions {
10
id: string;
11
path: string;
12
relativePath: string;
13
type: "anonymous";
14
}
15
16
interface CollaboratorOptions {
17
project_id: string;
18
path?: string; // no path means link to project
19
relativePath?: string;
20
type?: "collaborator";
21
}
22
23
type Options = AnonymousOptions | CollaboratorOptions;
24
25
export default function editURL(options: Options): string {
26
const type = options["type"];
27
switch (type) {
28
case "anonymous":
29
return anonymousURL(options);
30
case "collaborator":
31
default:
32
return collaboratorURL(options);
33
}
34
}
35
36
// needed since we're making a link outside of the nextjs server.
37
function withBasePath(url: string): string {
38
return join(basePath, url);
39
}
40
41
function anonymousURL({ id, path, relativePath }): string {
42
const app = "/static/app.html";
43
return withBasePath(
44
encodeURI(
45
`${app}?anonymous=true&launch=share/${id}/${join(
46
path,
47
relativePath ?? ""
48
)}`
49
)
50
);
51
}
52
53
function collaboratorURL({
54
project_id,
55
path,
56
relativePath,
57
}: {
58
project_id: string;
59
path?: string;
60
relativePath?: string;
61
}): string {
62
const projectURL = join("/projects", project_id);
63
if (!path) {
64
return withBasePath(projectURL);
65
}
66
return withBasePath(join(projectURL, "files", path, relativePath ?? ""));
67
}
68
69