-
Notifications
You must be signed in to change notification settings - Fork 462
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(admin): Improve import projects from prod
GitOrigin-RevId: 73c3fd0eea6899150f16b1d1a8d6ba3c1830ead4
- Loading branch information
1 parent
bfc5625
commit c1f4396
Showing
8 changed files
with
298 additions
and
125 deletions.
There are no files selected for viewing
163 changes: 163 additions & 0 deletions
163
platform/wab/src/wab/client/components/pages/admin/AdminImportProjectsFromProd.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,163 @@ | ||
import { NonAuthCtx, useNonAuthCtx } from "@/wab/client/app-ctx"; | ||
import { Modal } from "@/wab/client/components/widgets/Modal"; | ||
import { WorkspaceId } from "@/wab/shared/ApiSchema"; | ||
import { Bundle } from "@/wab/shared/bundles"; | ||
import { DevFlagsType } from "@/wab/shared/devflags"; | ||
import { Button } from "antd"; | ||
import React, { useState } from "react"; | ||
|
||
type ProjectInfo = { | ||
projectId: string; | ||
bundle: string; | ||
name: string; | ||
}; | ||
|
||
function useImportFromProdListener( | ||
onListen: (devflags: DevFlagsType, info: ProjectInfo[]) => Promise<void> | ||
) { | ||
React.useEffect(() => { | ||
const listener = async (event: MessageEvent) => { | ||
try { | ||
const data = JSON.parse(event.data); | ||
if (data.source === "import-project-from-prod") { | ||
const { devflags, projectsInfo } = data; | ||
await onListen(devflags, projectsInfo); | ||
} | ||
} catch {} | ||
}; | ||
window.addEventListener("message", listener); | ||
return () => window.removeEventListener("message", listener); | ||
}, [onListen]); | ||
} | ||
|
||
async function setupHostlessWorkspace( | ||
nonAuthCtx: NonAuthCtx, | ||
hostLessWorkspaceId: WorkspaceId | ||
) { | ||
try { | ||
// If it doesn't throw an error, it means the workspace exists | ||
await nonAuthCtx.api.getWorkspace(hostLessWorkspaceId); | ||
} catch (e) { | ||
async function getTeamId() { | ||
const { teams } = await nonAuthCtx.api.listTeams(); | ||
const nonPersonalTeam = teams.find((t) => !t.personalTeamOwnerId); | ||
if (nonPersonalTeam) { | ||
return nonPersonalTeam.id; | ||
} | ||
const { team } = await nonAuthCtx.api.createTeam("Hostless Team"); | ||
return team.id; | ||
} | ||
|
||
const teamId = await getTeamId(); | ||
|
||
await nonAuthCtx.api.adminCreateWorkspace({ | ||
id: hostLessWorkspaceId, | ||
name: "Hostless Workspace", | ||
description: "Workspace for hostless projects", | ||
teamId: teamId, | ||
}); | ||
} | ||
} | ||
|
||
async function resetProjects( | ||
nonAuthCtx: NonAuthCtx, | ||
projectsInfo: ProjectInfo[] | ||
) { | ||
console.log("## Deleting existing projects..."); | ||
|
||
const projectIds = projectsInfo.flatMap((info) => { | ||
const parsedBundles = JSON.parse(info.bundle) as Array<[string, Bundle]>; | ||
const dependenciesProjectIds = parsedBundles.flatMap(([_, bundle]) => | ||
Object.values(bundle.map) | ||
.filter((inst) => inst.__type === "ProjectDependency") | ||
.map((inst) => inst.projectId) | ||
); | ||
|
||
return [info.projectId, ...dependenciesProjectIds]; | ||
}); | ||
|
||
await Promise.all( | ||
projectIds.map((projectId) => | ||
nonAuthCtx.api.deleteProjectAndRevisions(projectId) | ||
) | ||
); | ||
|
||
console.log("## Uploading new projects..."); | ||
// We have to do it sync, since we can end up trying to insert the same project twice, concurrently and that will fail. | ||
for (const bundle of projectsInfo) { | ||
await nonAuthCtx.api.importProject(bundle.bundle, { | ||
keepProjectIdsAndNames: true, | ||
projectName: bundle.name, | ||
}); | ||
} | ||
} | ||
|
||
export function AdminImportProjectsFromProd() { | ||
const nonAuthCtx = useNonAuthCtx(); | ||
const [modalVisible, setModalVisible] = useState(false); | ||
const ref = React.createRef<HTMLIFrameElement>(); | ||
|
||
const onListenProjectsInfo = React.useCallback( | ||
async (devflags: DevFlagsType, info: ProjectInfo[]) => { | ||
if (devflags.hostLessWorkspaceId) { | ||
await setupHostlessWorkspace(nonAuthCtx, devflags.hostLessWorkspaceId); | ||
} | ||
|
||
devflags.hideBlankStarter = false; | ||
|
||
await nonAuthCtx.api.setDevFlagOverrides( | ||
JSON.stringify(devflags, null, 2) | ||
); | ||
|
||
await resetProjects(nonAuthCtx, info); | ||
|
||
console.log("## Posting message to window..."); | ||
ref.current!.contentWindow?.postMessage( | ||
JSON.stringify({ | ||
source: "import-project-from-prod", | ||
done: true, | ||
}) | ||
); | ||
|
||
setModalVisible(false); | ||
}, | ||
[nonAuthCtx, ref] | ||
); | ||
|
||
useImportFromProdListener(onListenProjectsInfo); | ||
|
||
const showImportFromProd = window.location.hostname.includes("localhost"); | ||
// Don't even show this on prod, just for extra safety | ||
if (!showImportFromProd) { | ||
return null; | ||
} | ||
|
||
return ( | ||
<div> | ||
<h2>Import devflags and plasmic projects from prod</h2> | ||
<Modal | ||
open={modalVisible} | ||
footer={null} | ||
title={"Import plasmic projects from prod"} | ||
onCancel={() => setModalVisible(false)} | ||
width={800} | ||
> | ||
<iframe | ||
src="https://studio.plasmic.app/import-projects-from-prod" | ||
width={760} | ||
height={500} | ||
ref={ref} | ||
/> | ||
</Modal> | ||
<Button | ||
disabled={window.location.hostname.startsWith( | ||
"https://studio.plasmic.app" | ||
)} | ||
onClick={() => setModalVisible((v) => !v)} | ||
> | ||
Import | ||
</Button> | ||
<p>This will override your current devflags</p> | ||
</div> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
96 changes: 0 additions & 96 deletions
96
platform/wab/src/wab/client/components/pages/admin/ImportProjectsFromProd.tsx
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.