From 3ec67f9f477976c880dba238d22e7482405af4b4 Mon Sep 17 00:00:00 2001 From: lloydzhou Date: Tue, 3 Sep 2024 00:45:11 +0800 Subject: [PATCH] add load from url --- app/components/plugin.tsx | 50 +++++++++++++++++++++++++++++++++++++-- app/components/ui-lib.tsx | 2 +- app/locales/cn.ts | 1 + app/locales/en.ts | 1 + 4 files changed, 51 insertions(+), 3 deletions(-) diff --git a/app/components/plugin.tsx b/app/components/plugin.tsx index 5aa66ce9467..fcf671bff69 100644 --- a/app/components/plugin.tsx +++ b/app/components/plugin.tsx @@ -15,6 +15,7 @@ import DeleteIcon from "../icons/delete.svg"; import EyeIcon from "../icons/eye.svg"; import CopyIcon from "../icons/copy.svg"; import ConfirmIcon from "../icons/confirm.svg"; +import ReloadIcon from "../icons/reload.svg"; import { Plugin, usePluginStore, FunctionToolService } from "../store/plugin"; import { @@ -31,7 +32,7 @@ import { import { downloadAs } from "../utils"; import Locale from "../locales"; import { useNavigate } from "react-router-dom"; -import { useEffect, useState } from "react"; +import { useEffect, useState, useCallback } from "react"; import { Path } from "../constant"; import { nanoid } from "nanoid"; @@ -90,6 +91,37 @@ export function PluginPage() { } }, 100).bind(null, editingPlugin); + const [loadUrl, setLoadUrl] = useState(""); + const loadFromUrl = (loadUrl: string) => + fetch(loadUrl) + .catch((e) => { + const p = new URL(loadUrl); + return fetch(`/api/proxy/${p.pathname}?${p.search}`, { + headers: { + "X-Base-URL": p.origin, + }, + }); + }) + .then((res) => res.text()) + .then((content) => { + try { + return JSON.stringify(JSON.parse(content), null, " "); + } catch (e) { + return content; + } + }) + .then((content) => { + pluginStore.updatePlugin(editingPlugin.id, (plugin) => { + plugin.content = content; + const tool = FunctionToolService.add(plugin, true); + plugin.title = tool.api.definition.info.title; + plugin.version = tool.api.definition.info.version; + }); + }) + .catch((e) => { + showToast(Locale.Plugin.EditModal.Error); + }); + return (
@@ -262,8 +294,22 @@ export function PluginPage() { + +
+ setLoadUrl(e.currentTarget.value)} + > + } + text={Locale.Plugin.EditModal.Load} + bordered + onClick={() => loadFromUrl(loadUrl)} + /> +
+