-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(web): support dev plugins (#1059)
- Loading branch information
Showing
12 changed files
with
238 additions
and
5 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
import * as yaml from "js-yaml"; | ||
import { useCallback, useEffect } from "react"; | ||
|
||
import { fetchAndZipFiles } from "@reearth/beta/utils/file"; | ||
import { usePluginsFetcher } from "@reearth/services/api"; | ||
import { config } from "@reearth/services/config"; | ||
import { useDevPluginExtensionRenderKey, useDevPluginExtensions } from "@reearth/services/state"; | ||
|
||
type ReearthYML = { | ||
id: string; | ||
version: string; | ||
extensions?: { | ||
id: string; | ||
}[]; | ||
}; | ||
|
||
type Props = { | ||
sceneId?: string; | ||
}; | ||
|
||
export default ({ sceneId }: Props) => { | ||
const [devPluginExtensions, setDevPluginExtensions] = useDevPluginExtensions(); | ||
const [_, updateDevPluginExtensionRenderKey] = useDevPluginExtensionRenderKey(); | ||
const { useUploadPluginWithFile } = usePluginsFetcher(); | ||
|
||
const handleDevPluginExtensionsReload = useCallback(() => { | ||
updateDevPluginExtensionRenderKey(prev => prev + 1); | ||
}, [updateDevPluginExtensionRenderKey]); | ||
|
||
const handleInstallPluginFromFile = useCallback( | ||
async (file: File) => { | ||
if (!sceneId || !file) return; | ||
useUploadPluginWithFile(sceneId, file); | ||
}, | ||
[sceneId, useUploadPluginWithFile], | ||
); | ||
|
||
const handleDevPluginsInstall = useCallback(async () => { | ||
if (!sceneId) return; | ||
|
||
const { devPluginUrls } = config() ?? {}; | ||
if (!devPluginUrls || devPluginUrls.length === 0) return; | ||
|
||
devPluginUrls.forEach(async url => { | ||
const file: File | undefined = await getPluginZipFromUrl(url); | ||
if (!file) return; | ||
handleInstallPluginFromFile(file); | ||
}); | ||
}, [sceneId, handleInstallPluginFromFile]); | ||
|
||
useEffect(() => { | ||
const { devPluginUrls } = config() ?? {}; | ||
if (!devPluginUrls || devPluginUrls.length === 0) return; | ||
|
||
const fetchExtensions = async () => { | ||
const extensions = await Promise.all( | ||
devPluginUrls.map(async url => { | ||
const response = await fetch(`${url}/reearth.yml`); | ||
if (!response.ok) { | ||
throw new Error(`Failed to fetch the file: ${response.statusText}`); | ||
} | ||
const yamlText = await response.text(); | ||
const data = yaml.load(yamlText) as ReearthYML; | ||
return data.extensions?.map(e => ({ id: e.id, url: `${url}/${e.id}.js` })) ?? []; | ||
}), | ||
); | ||
setDevPluginExtensions(extensions.flatMap(e => e)); | ||
}; | ||
|
||
fetchExtensions(); | ||
}, [setDevPluginExtensions]); | ||
|
||
return { devPluginExtensions, handleDevPluginsInstall, handleDevPluginExtensionsReload }; | ||
}; | ||
|
||
async function getPluginZipFromUrl(url: string) { | ||
try { | ||
const response = await fetch(`${url}/reearth.yml`); | ||
if (!response.ok) { | ||
throw new Error(`Failed to fetch the file: ${response.statusText}`); | ||
} | ||
const yamlText = await response.text(); | ||
const data = yaml.load(yamlText) as ReearthYML; | ||
|
||
const extensionUrls = data?.extensions?.map(extensions => `${url}/${extensions.id}.js`); | ||
if (!extensionUrls) return; | ||
|
||
const file = await fetchAndZipFiles( | ||
[...extensionUrls, `${url}/reearth.yml`], | ||
`${data.id}-${data.version}.zip`, | ||
); | ||
|
||
return file; | ||
} catch (_err) { | ||
return undefined; | ||
} | ||
} |
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
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
5 changes: 5 additions & 0 deletions
5
web/src/beta/lib/reearth-ui/components/Icon/Icons/PluginInstall.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 5 additions & 0 deletions
5
web/src/beta/lib/reearth-ui/components/Icon/Icons/PluginUpdate.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import JSZip from "jszip"; | ||
|
||
export async function fetchFile(url: string) { | ||
const response = await fetch(url); | ||
if (!response.ok) { | ||
throw new Error(`Failed to fetch ${url}: ${response.statusText}`); | ||
} | ||
return response.text(); | ||
} | ||
|
||
export async function fetchAndZipFiles(urls: string[], zipFileName: string) { | ||
const zip = new JSZip(); | ||
|
||
for (const url of urls) { | ||
try { | ||
const content = await fetchFile(url); | ||
const fileName = url.split("/").pop(); | ||
|
||
if (!fileName) return; | ||
|
||
zip.file(fileName, content); | ||
} catch (error) { | ||
console.error(`Failed to fetch ${url}:`, error); | ||
} | ||
} | ||
|
||
let file; | ||
|
||
await zip | ||
.generateAsync({ type: "blob" }) | ||
.then(blob => { | ||
file = new File([blob], zipFileName); | ||
}) | ||
.catch(error => { | ||
console.error("Error generating ZIP file:", error); | ||
}); | ||
|
||
return file; | ||
} |
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
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,7 @@ | ||
import { atom, useAtom } from "jotai"; | ||
|
||
const devPluginExtensionRenderKey = atom<number>(0); | ||
export const useDevPluginExtensionRenderKey = () => useAtom(devPluginExtensionRenderKey); | ||
|
||
const devPluginExtensions = atom<{ id: string; url: string }[] | undefined>(undefined); | ||
export const useDevPluginExtensions = () => useAtom(devPluginExtensions); |
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