mirror of
https://github.com/Moepchi/webspeak3.git
synced 2026-12-01 00:42:49 +08:00
Add client-only Design Store MVP
Opt-in gateway endpoints (STORE_ENABLED=1, off by default like every other
optional feature here) to browse and publish community themes, reusing the
existing {name, baseTheme, css} export/import shape so no new packaging
format is needed. CSS is sanitized against @import and remote url() before
being accepted, since it's rendered on other users' pages. Client side adds
a "Store" button to the design picker with a browse/install and a publish
view built on the same DesignPanel patterns as import/export.
No website-side store yet - deliberately client-only for the MVP.
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Hu2cuPLqpuKVfd9wWFFakU
This commit is contained in:
1 parent
7b52c0baa4
commit
bd33b962fe
6 files changed
+483
-2
No files matched your search
+193
@@ -97,6 +97,17 @@ const FEEDBACK_URL = import.meta.env.DEV
|
||||
? `${import.meta.env.VITE_GATEWAY_URL.replace(/\/$/, "").replace(/^ws(s?):\/\//, "http$1://")}/api/feedback`
|
||||
: `${window.location.protocol}//${window.location.host}/api/feedback`;
|
||||
|
||||
// Same derivation again, for the design store (see gateway/src/index.ts's
|
||||
// /api/store/themes handler). Unlike FEEDBACK_URL/IS_OWN_HOSTED_INSTANCE this
|
||||
// isn't limited to the maintainer's own instance - any self-hosted gateway
|
||||
// can turn STORE_ENABLED on, so the button always shows and the store panel
|
||||
// itself reports "unavailable" if the gateway 404s.
|
||||
const STORE_URL = import.meta.env.DEV
|
||||
? "http://localhost:8080/api/store/themes"
|
||||
: import.meta.env.VITE_GATEWAY_URL
|
||||
? `${import.meta.env.VITE_GATEWAY_URL.replace(/\/$/, "").replace(/^ws(s?):\/\//, "http$1://")}/api/store/themes`
|
||||
: `${window.location.protocol}//${window.location.host}/api/store/themes`;
|
||||
|
||||
// Ko-fi page of the project (.github/FUNDING.yml). Unlike the feedback button
|
||||
// below this is not limited to the maintainer's own instance: it ships in
|
||||
// every build, the Docker image included. Self-hosters who would rather not
|
||||
@@ -291,6 +302,18 @@ type CustomTheme = {
|
||||
css: string;
|
||||
};
|
||||
|
||||
/** Metadata for one theme listed by GET /api/store/themes - deliberately
|
||||
* without `css`, so browsing the store doesn't pull every submission's full
|
||||
* stylesheet up front. The full package (matching CustomTheme's export
|
||||
* shape) is fetched separately per-id on install, see STORE_URL above. */
|
||||
type StoreThemeSummary = {
|
||||
id: string;
|
||||
name: string;
|
||||
baseTheme: DesignTheme;
|
||||
author: string;
|
||||
createdAt: string;
|
||||
};
|
||||
|
||||
function isDesignTheme(value: string): value is DesignTheme {
|
||||
return value === "standard" || value === "nova" || value === "greenteaspeak" || value === "pulse";
|
||||
}
|
||||
@@ -5193,6 +5216,7 @@ function DesignPanel({
|
||||
}) {
|
||||
const t = useT();
|
||||
const [editing, setEditing] = useState<CustomThemeDraft | null>(null);
|
||||
const [storeOpen, setStoreOpen] = useState(false);
|
||||
const importInputRef = useRef<HTMLInputElement | null>(null);
|
||||
|
||||
const builtins: { id: DesignTheme; name: string; desc: string }[] = [
|
||||
@@ -5250,6 +5274,20 @@ function DesignPanel({
|
||||
}
|
||||
};
|
||||
|
||||
if (storeOpen) {
|
||||
return (
|
||||
<DesignStorePanel
|
||||
customThemes={customThemes}
|
||||
onInstall={(theme) => {
|
||||
const id = `${Date.now().toString(36)}-${Math.random().toString(36).slice(2, 8)}`;
|
||||
onSaveCustomTheme({ id, name: theme.name, baseTheme: theme.baseTheme, css: theme.css });
|
||||
onSelectionChange(`custom:${id}`);
|
||||
}}
|
||||
onClose={() => setStoreOpen(false)}
|
||||
/>
|
||||
);
|
||||
}
|
||||
|
||||
if (editing) {
|
||||
return (
|
||||
<>
|
||||
@@ -5363,6 +5401,161 @@ function DesignPanel({
|
||||
style={{ display: "none" }}
|
||||
onChange={handleImportSelected}
|
||||
/>
|
||||
<button onClick={() => setStoreOpen(true)}>{t("design.store.open")}</button>
|
||||
</div>
|
||||
</>
|
||||
);
|
||||
}
|
||||
|
||||
/** Browse community-submitted designs (GET STORE_URL) and publish one of the
|
||||
* viewer's own custom themes (POST STORE_URL) - see gateway/src/index.ts's
|
||||
* /api/store/themes handler. Reports "unavailable" rather than erroring when
|
||||
* the gateway has STORE_ENABLED off (the default), same as a 404. */
|
||||
function DesignStorePanel({
|
||||
customThemes,
|
||||
onInstall,
|
||||
onClose,
|
||||
}: {
|
||||
customThemes: CustomTheme[];
|
||||
onInstall: (theme: { name: string; baseTheme: DesignTheme; css: string }) => void;
|
||||
onClose: () => void;
|
||||
}) {
|
||||
const t = useT();
|
||||
const [themes, setThemes] = useState<StoreThemeSummary[] | null>(null);
|
||||
const [unavailable, setUnavailable] = useState(false);
|
||||
const [installedIds, setInstalledIds] = useState<Set<string>>(new Set());
|
||||
const [publishTarget, setPublishTarget] = useState("");
|
||||
const [publishAuthor, setPublishAuthor] = useState("");
|
||||
const [publishing, setPublishing] = useState(false);
|
||||
const [publishResult, setPublishResult] = useState<"ok" | "error" | null>(null);
|
||||
|
||||
const refreshThemes = () =>
|
||||
fetch(STORE_URL)
|
||||
.then((res) => {
|
||||
if (!res.ok) throw new Error(String(res.status));
|
||||
return res.json() as Promise<StoreThemeSummary[]>;
|
||||
})
|
||||
.then(setThemes)
|
||||
.catch(() => setUnavailable(true));
|
||||
|
||||
useEffect(() => {
|
||||
refreshThemes();
|
||||
// Only ever needs to run once per panel open - refreshThemes is re-created
|
||||
// each render but doesn't depend on any prop/state that would need it here.
|
||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||
}, []);
|
||||
|
||||
const handleInstall = async (id: string) => {
|
||||
try {
|
||||
const res = await fetch(`${STORE_URL}/${id}`);
|
||||
if (!res.ok) throw new Error(String(res.status));
|
||||
const parsed = await res.json();
|
||||
if (
|
||||
!parsed ||
|
||||
typeof parsed.name !== "string" ||
|
||||
!isDesignTheme(parsed.baseTheme) ||
|
||||
typeof parsed.css !== "string"
|
||||
) {
|
||||
throw new Error("invalid");
|
||||
}
|
||||
onInstall({ name: parsed.name, baseTheme: parsed.baseTheme, css: parsed.css });
|
||||
setInstalledIds((prev) => new Set(prev).add(id));
|
||||
} catch {
|
||||
window.alert(t("design.custom.importError"));
|
||||
}
|
||||
};
|
||||
|
||||
const handlePublish = async () => {
|
||||
const theme = customThemes.find((c) => c.id === publishTarget);
|
||||
if (!theme) return;
|
||||
setPublishing(true);
|
||||
setPublishResult(null);
|
||||
try {
|
||||
const res = await fetch(STORE_URL, {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({
|
||||
name: theme.name,
|
||||
baseTheme: theme.baseTheme,
|
||||
css: theme.css,
|
||||
author: publishAuthor.trim() || undefined,
|
||||
}),
|
||||
});
|
||||
if (!res.ok) throw new Error(String(res.status));
|
||||
setPublishResult("ok");
|
||||
setPublishAuthor("");
|
||||
await refreshThemes();
|
||||
} catch {
|
||||
setPublishResult("error");
|
||||
} finally {
|
||||
setPublishing(false);
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
<>
|
||||
<h3>{t("design.store.title")}</h3>
|
||||
{unavailable ? (
|
||||
<p className="ts-options-subtitle">{t("design.store.unavailable")}</p>
|
||||
) : themes === null ? (
|
||||
<p className="ts-options-subtitle">{t("design.store.loading")}</p>
|
||||
) : (
|
||||
<>
|
||||
<div className="ts-design-theme-grid">
|
||||
{themes.length === 0 && <p className="ts-options-subtitle">{t("design.store.empty")}</p>}
|
||||
{themes.map((theme) => (
|
||||
<div key={theme.id} className="ts-design-theme-card ts-design-theme-card-custom">
|
||||
<div className="ts-design-theme-card-select">
|
||||
<span className="ts-design-theme-swatch ts-design-theme-swatch-custom" />
|
||||
<span className="ts-design-theme-card-name">{theme.name}</span>
|
||||
<span className="ts-design-theme-card-desc">{t("design.store.by", { author: theme.author })}</span>
|
||||
</div>
|
||||
<div className="ts-design-theme-card-actions">
|
||||
<button onClick={() => handleInstall(theme.id)} disabled={installedIds.has(theme.id)}>
|
||||
{installedIds.has(theme.id) ? t("design.store.installed") : t("design.store.install")}
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
|
||||
<h3>{t("design.store.publishTitle")}</h3>
|
||||
{customThemes.length === 0 ? (
|
||||
<p className="ts-options-subtitle">{t("design.store.publish.none")}</p>
|
||||
) : (
|
||||
<>
|
||||
<label className="ts-options-field">
|
||||
{t("design.store.publish.select")}
|
||||
<select value={publishTarget} onChange={(e) => setPublishTarget(e.target.value)}>
|
||||
<option value="" />
|
||||
{customThemes.map((theme) => (
|
||||
<option key={theme.id} value={theme.id}>
|
||||
{theme.name}
|
||||
</option>
|
||||
))}
|
||||
</select>
|
||||
</label>
|
||||
<label className="ts-options-field">
|
||||
{t("design.store.publish.author")}
|
||||
<input
|
||||
value={publishAuthor}
|
||||
onChange={(e) => setPublishAuthor(e.target.value)}
|
||||
placeholder={t("design.store.publish.authorPlaceholder")}
|
||||
/>
|
||||
</label>
|
||||
<div className="ts-dialog-buttons-right">
|
||||
<button onClick={handlePublish} disabled={!publishTarget || publishing}>
|
||||
{t("design.store.publish.submit")}
|
||||
</button>
|
||||
</div>
|
||||
{publishResult === "ok" && <p className="ts-options-subtitle">{t("design.store.publish.success")}</p>}
|
||||
{publishResult === "error" && <p className="ts-options-subtitle">{t("design.store.publish.error")}</p>}
|
||||
</>
|
||||
)}
|
||||
</>
|
||||
)}
|
||||
<div className="ts-dialog-buttons-right">
|
||||
<button onClick={onClose}>{t("design.store.back")}</button>
|
||||
</div>
|
||||
</>
|
||||
);
|
||||
|
||||
@@ -289,6 +289,23 @@
|
||||
"design.custom.save": "Speichern",
|
||||
"design.custom.copyOf": "Kopie von {name}",
|
||||
"design.custom.importError": "Diese Datei konnte nicht als WebSpeak3-Design gelesen werden.",
|
||||
"design.store.open": "Store",
|
||||
"design.store.title": "Design-Store",
|
||||
"design.store.back": "Zurück",
|
||||
"design.store.loading": "Lädt…",
|
||||
"design.store.unavailable": "Der Design-Store ist auf diesem Server nicht verfügbar.",
|
||||
"design.store.empty": "Noch keine Designs im Store.",
|
||||
"design.store.install": "Installieren",
|
||||
"design.store.installed": "Installiert",
|
||||
"design.store.by": "von {author}",
|
||||
"design.store.publishTitle": "Ein Design veröffentlichen",
|
||||
"design.store.publish.select": "Eigenes Design auswählen",
|
||||
"design.store.publish.none": "Du hast noch kein eigenes Design zum Veröffentlichen.",
|
||||
"design.store.publish.author": "Dein Name (optional)",
|
||||
"design.store.publish.authorPlaceholder": "Anonym",
|
||||
"design.store.publish.submit": "Im Store veröffentlichen",
|
||||
"design.store.publish.success": "Design wurde veröffentlicht!",
|
||||
"design.store.publish.error": "Veröffentlichen fehlgeschlagen.",
|
||||
"options.section.erweiterungen": "Erweiterungen",
|
||||
"options.section.hotkeys": "Hotkeys",
|
||||
"options.section.whispern": "Whispern",
|
||||
|
||||
@@ -289,6 +289,23 @@
|
||||
"design.custom.save": "Save",
|
||||
"design.custom.copyOf": "Copy of {name}",
|
||||
"design.custom.importError": "This file couldn't be read as a WebSpeak3 design.",
|
||||
"design.store.open": "Store",
|
||||
"design.store.title": "Design store",
|
||||
"design.store.back": "Back",
|
||||
"design.store.loading": "Loading…",
|
||||
"design.store.unavailable": "The design store isn't available on this server.",
|
||||
"design.store.empty": "No designs in the store yet.",
|
||||
"design.store.install": "Install",
|
||||
"design.store.installed": "Installed",
|
||||
"design.store.by": "by {author}",
|
||||
"design.store.publishTitle": "Publish a design",
|
||||
"design.store.publish.select": "Choose one of your own designs",
|
||||
"design.store.publish.none": "You don't have a custom design to publish yet.",
|
||||
"design.store.publish.author": "Your name (optional)",
|
||||
"design.store.publish.authorPlaceholder": "Anonymous",
|
||||
"design.store.publish.submit": "Publish to the store",
|
||||
"design.store.publish.success": "Design published!",
|
||||
"design.store.publish.error": "Publishing failed.",
|
||||
"options.section.erweiterungen": "Addons",
|
||||
"options.section.hotkeys": "Hotkeys",
|
||||
"options.section.whispern": "Whisper",
|
||||
|
||||
@@ -289,6 +289,23 @@
|
||||
"design.custom.save": "ذخیره",
|
||||
"design.custom.copyOf": "کپی از {name}",
|
||||
"design.custom.importError": "این فایل بهعنوان طراحی WebSpeak3 خوانده نشد.",
|
||||
"design.store.open": "فروشگاه",
|
||||
"design.store.title": "فروشگاه طراحیها",
|
||||
"design.store.back": "بازگشت",
|
||||
"design.store.loading": "در حال بارگذاری…",
|
||||
"design.store.unavailable": "فروشگاه طراحی روی این سرور در دسترس نیست.",
|
||||
"design.store.empty": "هنوز طراحی در فروشگاه نیست.",
|
||||
"design.store.install": "نصب",
|
||||
"design.store.installed": "نصب شد",
|
||||
"design.store.by": "از {author}",
|
||||
"design.store.publishTitle": "انتشار یک طراحی",
|
||||
"design.store.publish.select": "یکی از طراحیهای خودت را انتخاب کن",
|
||||
"design.store.publish.none": "هنوز طراحی سفارشی برای انتشار نداری.",
|
||||
"design.store.publish.author": "نام تو (اختیاری)",
|
||||
"design.store.publish.authorPlaceholder": "ناشناس",
|
||||
"design.store.publish.submit": "انتشار در فروشگاه",
|
||||
"design.store.publish.success": "طراحی منتشر شد!",
|
||||
"design.store.publish.error": "انتشار ناموفق بود.",
|
||||
"options.section.erweiterungen": "افزونهها",
|
||||
"options.section.hotkeys": "کلیدهای میانبر",
|
||||
"options.section.whispern": "نجوا",
|
||||
|
||||
@@ -571,6 +571,23 @@
|
||||
"design.custom.save": "保存",
|
||||
"design.custom.copyOf": "{name} 的副本",
|
||||
"design.custom.importError": "该文件无法作为 WebSpeak3 设计读取。",
|
||||
"design.store.open": "商店",
|
||||
"design.store.title": "设计商店",
|
||||
"design.store.back": "返回",
|
||||
"design.store.loading": "加载中…",
|
||||
"design.store.unavailable": "此服务器未启用设计商店。",
|
||||
"design.store.empty": "商店里还没有设计。",
|
||||
"design.store.install": "安装",
|
||||
"design.store.installed": "已安装",
|
||||
"design.store.by": "作者:{author}",
|
||||
"design.store.publishTitle": "发布设计",
|
||||
"design.store.publish.select": "选择你自己的设计",
|
||||
"design.store.publish.none": "你还没有可发布的自定义设计。",
|
||||
"design.store.publish.author": "你的名字(可选)",
|
||||
"design.store.publish.authorPlaceholder": "匿名",
|
||||
"design.store.publish.submit": "发布到商店",
|
||||
"design.store.publish.success": "设计已发布!",
|
||||
"design.store.publish.error": "发布失败。",
|
||||
"recording.autoGainControl": "自动增益控制",
|
||||
"tree.streaming": "正在直播",
|
||||
"publish.alpha": "内测",
|
||||
|
||||
Reference in new issue
Block a user