Path: blob/master/src/packages/frontend/collaborators/handle-project-invite.ts
1496 views
/*1* This file is part of CoCalc: Copyright © 2020 Sagemath, Inc.2* License: MS-RSL – see LICENSE.md for details3*/45import { QueryParams } from "../misc/query-params";6import { webapp_client } from "../webapp-client";7import { alert_message } from "../alerts";8import { redux } from "../app-framework";9import { delay } from "awaiting";1011export const PROJECT_INVITE_QUERY_PARAM = "project-invite";1213async function handleProjectInviteToken() {14const token_id = QueryParams.get(PROJECT_INVITE_QUERY_PARAM);15if (!token_id) {16return;17}18QueryParams.remove(PROJECT_INVITE_QUERY_PARAM);19const account_id = webapp_client.account_id;20if (!account_id) return;21addSelfToProjectUsingInviteToken(token_id);22}2324export async function init() {25await delay(0); // has to be after page loads...26webapp_client.once("signed_in", handleProjectInviteToken);27}2829async function addSelfToProjectUsingInviteToken(token_id) {30if (webapp_client.account_id == null) return;3132const actions = redux.getActions("page");33if (34!(await actions.popconfirm({35title: "Would you like to accept this project invitation?",36description:37"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.",38okText: "Yes, accept invitation",39}))40) {41return;42}43try {44const resp = await webapp_client.project_collaborators.add_collaborator({45account_id: webapp_client.account_id,46token_id,47});48console.log({ resp });49const project_id = resp.project_id;50if (typeof project_id == "string") {51alert_message({52type: "info",53message: "You have been successfully added to the project!",54timeout: 10,55});56// Wait until the project is available in the store:57const store = redux.getStore("projects");58await store.async_wait({59until: () => store.getIn(["project_map", project_id]),60timeout: 120,61});62// Now actually open it.63redux.getActions("projects").open_project({ project_id });64}65} catch (err) {66alert_message({ type: "error", message: err.toString(), timeout: 30 });67}68}697071