Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(web): import builtin plugin from external url #625

Merged
merged 4 commits into from
Aug 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ function processUnsafeBuiltinBlocks(plugin?: UnsafeBuiltinPlugin[]) {

const unsafeBlocks: UnsafeBuiltinBlocks<Component> | undefined = plugin
.map(p =>
p.widgets.map(w => {
p.blocks?.map(w => {
return {
widgetId: `${p.id}/${w.extensionId}`,
...w,
Expand All @@ -23,11 +23,11 @@ function processUnsafeBuiltinBlocks(plugin?: UnsafeBuiltinPlugin[]) {
)
.reduce((a, b) => {
const newObject: { [key: string]: Component } = {};
b.forEach(w => {
b?.forEach(w => {
newObject[w.widgetId] = w.component;
});
return merge(a, newObject);
}, {});

return unsafeBlocks;
return unsafeBlocks || [];
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ function processUnsafeBuiltinWidgets(plugin?: UnsafeBuiltinPlugin[]) {

const unsafeWidgets: UnsafeBuiltinWidgets<Component> | undefined = plugin
.map(p =>
p.widgets.map(w => {
p.widgets?.map(w => {
return {
widgetId: `${p.id}/${w.extensionId}`,
...w,
Expand All @@ -25,11 +25,11 @@ function processUnsafeBuiltinWidgets(plugin?: UnsafeBuiltinPlugin[]) {
)
.reduce((a, b) => {
const newObject: { [key: string]: Component } = {};
b.forEach(w => {
b?.forEach(w => {
newObject[w.widgetId] = w.component;
});
return merge(a, newObject);
}, {});

return unsafeWidgets;
return unsafeWidgets || [];
}
4 changes: 0 additions & 4 deletions web/src/beta/lib/unsafeBuiltinPlugins/.gitignore

This file was deleted.

34 changes: 0 additions & 34 deletions web/src/beta/lib/unsafeBuiltinPlugins/README.md

This file was deleted.

5 changes: 0 additions & 5 deletions web/src/beta/lib/unsafeBuiltinPlugins/index.ts

This file was deleted.

7 changes: 5 additions & 2 deletions web/src/services/config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ export type Config = {
documentationUrl?: string;
marketplaceUrl?: string;
extensionUrls?: string[];
unsafePluginUrls?: string[];
extensions?: Extensions;
unsafeBuiltinPlugins?: UnsafeBuiltinPlugin[];
};
Expand All @@ -60,7 +61,7 @@ declare global {
export default async function loadConfig() {
if (window.REEARTH_CONFIG) return;
window.REEARTH_CONFIG = defaultConfig;
const config = {
const config: Config = {
...defaultConfig,
...(await (await fetch("/reearth_config.json")).json()),
};
Expand All @@ -80,7 +81,9 @@ export default async function loadConfig() {
config.extensions = extensions;
}

config.unsafeBuiltinPlugins = await loadUnsafeBuiltinPlugins();
if (config.unsafePluginUrls) {
config.unsafeBuiltinPlugins = await loadUnsafeBuiltinPlugins(config.unsafePluginUrls);
}

window.REEARTH_CONFIG = config;
}
Expand Down
33 changes: 19 additions & 14 deletions web/src/services/config/unsafeBuiltinPlugin.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { FC } from "react";

export type UnsafeBuiltinPlugin = {
id: string;
name: string;
widgets: UnsafeBuiltinWidget[];
blocks: UnsafeBuiltinBlock[];
widgets?: UnsafeBuiltinWidget[];
blocks?: UnsafeBuiltinBlock[];
};

type UnsafeBuiltinWidget = UnsafeBuiltinPluginExtension<"widget">;
Expand All @@ -13,19 +15,22 @@ type UnsafeBuiltinPluginExtension<T extends "widget" | "block"> = {
type: T;
extensionId: string;
name: string;
component: React.FC;
component: FC;
};

export type UnsafeBuiltinWidgets<T = unknown> = Record<string, T>;

export async function loadUnsafeBuiltinPlugins() {
try {
const unsafeBuiltinPlugins = (
await import(/* @vite-ignore */ "src/beta/lib/unsafeBuiltinPlugins")
).default as UnsafeBuiltinPlugin[];
return unsafeBuiltinPlugins;
} catch (e) {
console.error("unsafe builtin plugin load failed", e);
}
return undefined;
}
export const loadUnsafeBuiltinPlugins = async (urls: string[]) => {
return (
await Promise.all(
urls.map(async url => {
try {
const plugin: UnsafeBuiltinPlugin = (await import(/* @vite-ignore */ url)).default;
return plugin;
} catch (e) {
throw new Error(`Specified unsafe built-in module could not find: ${url} ${e}`);
}
}),
)
).filter(Boolean);
};
Loading