Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemathinc
GitHub Repository: sagemathinc/cocalc
Path: blob/master/src/packages/frontend/collaborators/handle-project-invite.ts
1496 views
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 { QueryParams } from "../misc/query-params";
7
import { webapp_client } from "../webapp-client";
8
import { alert_message } from "../alerts";
9
import { redux } from "../app-framework";
10
import { delay } from "awaiting";
11
12
export const PROJECT_INVITE_QUERY_PARAM = "project-invite";
13
14
async function handleProjectInviteToken() {
15
const token_id = QueryParams.get(PROJECT_INVITE_QUERY_PARAM);
16
if (!token_id) {
17
return;
18
}
19
QueryParams.remove(PROJECT_INVITE_QUERY_PARAM);
20
const account_id = webapp_client.account_id;
21
if (!account_id) return;
22
addSelfToProjectUsingInviteToken(token_id);
23
}
24
25
export async function init() {
26
await delay(0); // has to be after page loads...
27
webapp_client.once("signed_in", handleProjectInviteToken);
28
}
29
30
async function addSelfToProjectUsingInviteToken(token_id) {
31
if (webapp_client.account_id == null) return;
32
33
const actions = redux.getActions("page");
34
if (
35
!(await actions.popconfirm({
36
title: "Would you like to accept this project invitation?",
37
description:
38
"If you are visiting a link from somebody you trust, click 'Yes, accept invitation'. If this seems suspicious, click 'No'. You can always open the invite link again if you change your mind.",
39
okText: "Yes, accept invitation",
40
}))
41
) {
42
return;
43
}
44
try {
45
const resp = await webapp_client.project_collaborators.add_collaborator({
46
account_id: webapp_client.account_id,
47
token_id,
48
});
49
console.log({ resp });
50
const project_id = resp.project_id;
51
if (typeof project_id == "string") {
52
alert_message({
53
type: "info",
54
message: "You have been successfully added to the project!",
55
timeout: 10,
56
});
57
// Wait until the project is available in the store:
58
const store = redux.getStore("projects");
59
await store.async_wait({
60
until: () => store.getIn(["project_map", project_id]),
61
timeout: 120,
62
});
63
// Now actually open it.
64
redux.getActions("projects").open_project({ project_id });
65
}
66
} catch (err) {
67
alert_message({ type: "error", message: err.toString(), timeout: 30 });
68
}
69
}
70
71