-
Notifications
You must be signed in to change notification settings - Fork 465
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: Extract ImportProjectsFromProd
GitOrigin-RevId: 67ff7c3919f3f237d0e766642e704168f90adee8
- Loading branch information
1 parent
adffd5d
commit bfc5625
Showing
2 changed files
with
98 additions
and
92 deletions.
There are no files selected for viewing
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: 96 additions & 0 deletions
96
platform/wab/src/wab/client/components/pages/admin/ImportProjectsFromProd.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,96 @@ | ||
import { useNonAuthCtx } from "@/wab/client/app-ctx"; | ||
import { Modal } from "@/wab/client/components/widgets/Modal"; | ||
import { spawn } from "@/wab/shared/common"; | ||
import { Button } from "antd"; | ||
import React, { useState } from "react"; | ||
|
||
export function ImportProjectsFromProd() { | ||
const nonAuthCtx = useNonAuthCtx(); | ||
const [projectsInfo, setProjectsInfo] = React.useState< | ||
{ projectId: string; bundle: string; name: string }[] | undefined | ||
>(undefined); | ||
const [modalVisible, setModalVisible] = useState(false); | ||
const ref = React.createRef<HTMLIFrameElement>(); | ||
React.useEffect(() => { | ||
const listener = (event: MessageEvent) => { | ||
try { | ||
const data = JSON.parse(event.data); | ||
if (data.source === "import-project-from-prod") { | ||
setProjectsInfo(data.projectsInfo); | ||
spawn( | ||
nonAuthCtx.api.setDevFlagOverrides( | ||
JSON.stringify(data.devflags, null, 2) | ||
) | ||
); | ||
} | ||
} catch {} | ||
}; | ||
window.addEventListener("message", listener); | ||
return () => window.removeEventListener("message", listener); | ||
}, []); | ||
|
||
const updateProjects = async () => { | ||
if (projectsInfo) { | ||
console.log("## Deleting existing projects..."); | ||
for (const bundle of projectsInfo) { | ||
await nonAuthCtx.api.deleteProjectAndRevisions(bundle.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, | ||
}); | ||
} | ||
|
||
ref.current!.contentWindow?.postMessage( | ||
JSON.stringify({ | ||
source: "import-project-from-prod", | ||
done: true, | ||
}) | ||
); | ||
|
||
setModalVisible(false); | ||
} | ||
}; | ||
|
||
React.useEffect(() => { | ||
spawn(updateProjects()); | ||
}, [projectsInfo]); | ||
|
||
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> | ||
); | ||
} |