|
| 1 | +"use client"; |
| 2 | + |
| 3 | +import { useState, useMemo } from "react"; |
| 4 | +import { SchemeUriBlock } from "./SchemeUriBlock"; |
| 5 | + |
| 6 | +type SchemePath = "create" | "extension"; |
| 7 | + |
| 8 | +interface CreateParams { |
| 9 | + req: { |
| 10 | + url: string; |
| 11 | + }; |
| 12 | +} |
| 13 | + |
| 14 | +interface ExtensionParams { |
| 15 | + url: string; |
| 16 | + devMode: boolean; |
| 17 | +} |
| 18 | + |
| 19 | +export function SchemeUriBuilder() { |
| 20 | + const [path, setPath] = useState<SchemePath>("create"); |
| 21 | + const [createUrl, setCreateUrl] = useState("https://example.com/file.zip"); |
| 22 | + const [extUrl, setExtUrl] = useState( |
| 23 | + "https://github.com/user/gopeed-extension-example", |
| 24 | + ); |
| 25 | + const [extDevMode, setExtDevMode] = useState(false); |
| 26 | + |
| 27 | + const generatedUri = useMemo(() => { |
| 28 | + try { |
| 29 | + if (path === "create") { |
| 30 | + const params: CreateParams = { req: { url: createUrl } }; |
| 31 | + const encoded = btoa(JSON.stringify(params)); |
| 32 | + return `gopeed:///create?params=${encoded}`; |
| 33 | + } else { |
| 34 | + const params: ExtensionParams = { url: extUrl, devMode: extDevMode }; |
| 35 | + const encoded = btoa(JSON.stringify(params)); |
| 36 | + return `gopeed:///extension?params=${encoded}`; |
| 37 | + } |
| 38 | + } catch { |
| 39 | + return ""; |
| 40 | + } |
| 41 | + }, [path, createUrl, extUrl, extDevMode]); |
| 42 | + |
| 43 | + const paramJson = useMemo(() => { |
| 44 | + if (path === "create") { |
| 45 | + return JSON.stringify({ req: { url: createUrl } }, null, 2); |
| 46 | + } |
| 47 | + return JSON.stringify({ url: extUrl, devMode: extDevMode }, null, 2); |
| 48 | + }, [path, createUrl, extUrl, extDevMode]); |
| 49 | + |
| 50 | + return ( |
| 51 | + <div className="not-prose my-6 rounded-xl border border-fd-border bg-fd-card overflow-hidden"> |
| 52 | + {/* Path selector */} |
| 53 | + <div className="flex border-b border-fd-border"> |
| 54 | + {(["create", "extension"] as const).map((p) => ( |
| 55 | + <button |
| 56 | + key={p} |
| 57 | + onClick={() => setPath(p)} |
| 58 | + className={`flex-1 px-4 py-3 text-sm font-medium transition-colors cursor-pointer ${ |
| 59 | + path === p |
| 60 | + ? "bg-primary-500/10 text-primary-600 dark:text-primary-400 border-b-2 border-primary-500" |
| 61 | + : "text-fd-muted-foreground hover:text-fd-foreground" |
| 62 | + }`} |
| 63 | + > |
| 64 | + /{p} |
| 65 | + </button> |
| 66 | + ))} |
| 67 | + </div> |
| 68 | + |
| 69 | + {/* Form */} |
| 70 | + <div className="p-4 space-y-3"> |
| 71 | + {path === "create" ? ( |
| 72 | + <div> |
| 73 | + <label className="block text-sm font-medium text-fd-foreground mb-1"> |
| 74 | + 下载链接 |
| 75 | + </label> |
| 76 | + <input |
| 77 | + type="text" |
| 78 | + value={createUrl} |
| 79 | + onChange={(e) => setCreateUrl(e.target.value)} |
| 80 | + className="w-full px-3 py-2 rounded-lg border border-fd-border bg-fd-secondary text-sm text-fd-foreground focus:outline-none focus:ring-2 focus:ring-primary-500/30 focus:border-primary-500" |
| 81 | + placeholder="https://example.com/file.zip" |
| 82 | + /> |
| 83 | + </div> |
| 84 | + ) : ( |
| 85 | + <> |
| 86 | + <div> |
| 87 | + <label className="block text-sm font-medium text-fd-foreground mb-1"> |
| 88 | + 扩展地址 |
| 89 | + </label> |
| 90 | + <input |
| 91 | + type="text" |
| 92 | + value={extUrl} |
| 93 | + onChange={(e) => setExtUrl(e.target.value)} |
| 94 | + className="w-full px-3 py-2 rounded-lg border border-fd-border bg-fd-secondary text-sm text-fd-foreground focus:outline-none focus:ring-2 focus:ring-primary-500/30 focus:border-primary-500" |
| 95 | + placeholder="https://github.com/user/gopeed-extension-example" |
| 96 | + /> |
| 97 | + </div> |
| 98 | + <div className="flex items-center gap-2"> |
| 99 | + <input |
| 100 | + type="checkbox" |
| 101 | + id="devMode" |
| 102 | + checked={extDevMode} |
| 103 | + onChange={(e) => setExtDevMode(e.target.checked)} |
| 104 | + className="rounded border-fd-border text-primary-500 focus:ring-primary-500/30" |
| 105 | + /> |
| 106 | + <label htmlFor="devMode" className="text-sm text-fd-foreground"> |
| 107 | + 开发模式 |
| 108 | + </label> |
| 109 | + </div> |
| 110 | + </> |
| 111 | + )} |
| 112 | + </div> |
| 113 | + |
| 114 | + {/* Params preview */} |
| 115 | + <div className="px-4 pb-3"> |
| 116 | + <label className="block text-xs font-medium text-fd-muted-foreground mb-1"> |
| 117 | + params 解码内容 |
| 118 | + </label> |
| 119 | + <pre className="text-xs bg-fd-secondary rounded-lg p-3 overflow-x-auto text-fd-muted-foreground"> |
| 120 | + {paramJson} |
| 121 | + </pre> |
| 122 | + </div> |
| 123 | + |
| 124 | + {/* Generated URI */} |
| 125 | + <div className="px-4 pb-4"> |
| 126 | + <label className="block text-xs font-medium text-fd-muted-foreground mb-1"> |
| 127 | + 生成的 URI |
| 128 | + </label> |
| 129 | + <div className="[&_figure]:my-0"> |
| 130 | + <SchemeUriBlock uri={generatedUri} /> |
| 131 | + </div> |
| 132 | + </div> |
| 133 | + </div> |
| 134 | + ); |
| 135 | +} |
0 commit comments