Add CYD browser flasher with Next.js, i18n, and Web Serial install.
Replace the starter app with a public/local flasher that discovers sibling firmwares, supports en/zh, and documents HTTPS deploy. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
48
components/esp-web-install.tsx
Normal file
48
components/esp-web-install.tsx
Normal file
@@ -0,0 +1,48 @@
|
||||
"use client";
|
||||
|
||||
import { createElement, useEffect } from "react";
|
||||
|
||||
/**
|
||||
* Loads esp-web-tools once; hosts the install button via slot.
|
||||
*/
|
||||
export function EspWebInstall({
|
||||
manifest,
|
||||
label,
|
||||
disabled,
|
||||
}: {
|
||||
manifest: string | null;
|
||||
label: string;
|
||||
disabled?: boolean;
|
||||
}) {
|
||||
useEffect(() => {
|
||||
if (document.querySelector('script[data-esp-web-tools="1"]')) return;
|
||||
|
||||
const script = document.createElement("script");
|
||||
script.type = "module";
|
||||
script.src =
|
||||
"https://unpkg.com/esp-web-tools@10.1.0/dist/web/install-button.js?module";
|
||||
script.dataset.espWebTools = "1";
|
||||
document.head.appendChild(script);
|
||||
}, []);
|
||||
|
||||
const activate = createElement(
|
||||
"button",
|
||||
{
|
||||
slot: "activate",
|
||||
type: "button",
|
||||
className: "cta-btn",
|
||||
disabled: Boolean(disabled || !manifest),
|
||||
},
|
||||
label
|
||||
);
|
||||
|
||||
if (!manifest) {
|
||||
return activate;
|
||||
}
|
||||
|
||||
return createElement(
|
||||
"esp-web-install-button",
|
||||
{ manifest },
|
||||
activate
|
||||
);
|
||||
}
|
||||
162
components/flasher-panel.tsx
Normal file
162
components/flasher-panel.tsx
Normal file
@@ -0,0 +1,162 @@
|
||||
"use client";
|
||||
|
||||
import { useCallback, useEffect, useState } from "react";
|
||||
import { useTranslations } from "next-intl";
|
||||
import { RefreshCw } from "lucide-react";
|
||||
import type { FirmwareInfo } from "@/lib/firmware";
|
||||
import { formatBytes, cn } from "@/lib/utils";
|
||||
import { Button } from "@/components/ui/button";
|
||||
import { Badge } from "@/components/ui/badge";
|
||||
import { Card, CardHeader, CardTitle } from "@/components/ui/card";
|
||||
import { EspWebInstall } from "@/components/esp-web-install";
|
||||
|
||||
function supportsWebSerial() {
|
||||
return typeof navigator !== "undefined" && "serial" in navigator;
|
||||
}
|
||||
|
||||
export function FlasherPanel() {
|
||||
const t = useTranslations();
|
||||
const [firmwares, setFirmwares] = useState<FirmwareInfo[]>([]);
|
||||
const [selectedId, setSelectedId] = useState<string | null>(null);
|
||||
const [loading, setLoading] = useState(true);
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
const [serialOk, setSerialOk] = useState(true);
|
||||
|
||||
const load = useCallback(async () => {
|
||||
setLoading(true);
|
||||
setError(null);
|
||||
try {
|
||||
const res = await fetch("/api/firmware", { cache: "no-store" });
|
||||
if (!res.ok) throw new Error(`HTTP ${res.status}`);
|
||||
const data = (await res.json()) as { firmwares: FirmwareInfo[] };
|
||||
const list = data.firmwares ?? [];
|
||||
setFirmwares(list);
|
||||
setSelectedId((prev) => {
|
||||
if (prev && list.some((f) => f.id === prev)) return prev;
|
||||
return list[0]?.id ?? null;
|
||||
});
|
||||
} catch (err) {
|
||||
setError(err instanceof Error ? err.message : String(err));
|
||||
setFirmwares([]);
|
||||
} finally {
|
||||
setLoading(false);
|
||||
}
|
||||
}, []);
|
||||
|
||||
useEffect(() => {
|
||||
setSerialOk(supportsWebSerial());
|
||||
void load();
|
||||
}, [load]);
|
||||
|
||||
const selected = firmwares.find((f) => f.id === selectedId) ?? null;
|
||||
|
||||
return (
|
||||
<div className="flex flex-col gap-5">
|
||||
{!serialOk && (
|
||||
<Card className="border-[rgba(232,164,90,0.35)] text-[var(--warn)]">
|
||||
<p className="m-0 text-sm">{t("browser.unsupported")}</p>
|
||||
</Card>
|
||||
)}
|
||||
|
||||
<Card>
|
||||
<CardHeader>
|
||||
<CardTitle>{t("firmware.title")}</CardTitle>
|
||||
<Button
|
||||
type="button"
|
||||
variant="ghost"
|
||||
size="sm"
|
||||
onClick={() => void load()}
|
||||
disabled={loading}
|
||||
>
|
||||
<RefreshCw className={cn("size-3.5", loading && "animate-spin")} />
|
||||
{t("firmware.refresh")}
|
||||
</Button>
|
||||
</CardHeader>
|
||||
|
||||
<div className="flex flex-col gap-2" role="listbox" aria-label={t("firmware.title")}>
|
||||
{loading && !firmwares.length && (
|
||||
<p className="m-0 text-sm text-[var(--muted)]">{t("firmware.scanning")}</p>
|
||||
)}
|
||||
{error && (
|
||||
<p className="m-0 text-sm text-[var(--warn)]">
|
||||
{t("firmware.error", { message: error })}
|
||||
</p>
|
||||
)}
|
||||
{!loading && !error && !firmwares.length && (
|
||||
<p className="m-0 text-sm text-[var(--muted)]">{t("firmware.empty")}</p>
|
||||
)}
|
||||
{firmwares.map((fw) => {
|
||||
const active = fw.id === selectedId;
|
||||
const meta = [fw.board, fw.date, fw.git ? `git ${fw.git}` : null]
|
||||
.filter(Boolean)
|
||||
.join(" · ");
|
||||
return (
|
||||
<button
|
||||
key={fw.id}
|
||||
type="button"
|
||||
role="option"
|
||||
aria-selected={active}
|
||||
className={cn(
|
||||
"grid w-full grid-cols-[auto_1fr_auto] items-center gap-x-4 gap-y-1 rounded-[10px] border px-4 py-3.5 text-left transition-colors",
|
||||
active
|
||||
? "border-[var(--accent)] bg-[rgba(240,197,10,0.1)] shadow-[inset_0_0_0_1px_rgba(240,197,10,0.25)]"
|
||||
: "border-[var(--line)] bg-[rgba(0,0,0,0.2)] hover:border-[rgba(240,197,10,0.35)] hover:bg-[rgba(240,197,10,0.05)]"
|
||||
)}
|
||||
onClick={() => setSelectedId(fw.id)}
|
||||
>
|
||||
<span
|
||||
className={cn(
|
||||
"size-4 shrink-0 rounded-full border-2",
|
||||
active
|
||||
? "border-[var(--accent)] bg-[radial-gradient(circle_at_center,var(--accent)_40%,transparent_45%)]"
|
||||
: "border-[var(--muted)]"
|
||||
)}
|
||||
aria-hidden
|
||||
/>
|
||||
<span className="min-w-0">
|
||||
<span className="block font-semibold leading-snug">{fw.name}</span>
|
||||
<span className="mt-0.5 block text-xs text-[var(--muted)]">
|
||||
{fw.project} / {fw.variant}
|
||||
{meta ? ` · ${meta}` : ""}
|
||||
</span>
|
||||
</span>
|
||||
<Badge variant="muted" className="justify-self-end tabular-nums">
|
||||
{formatBytes(fw.size)}
|
||||
</Badge>
|
||||
</button>
|
||||
);
|
||||
})}
|
||||
</div>
|
||||
</Card>
|
||||
|
||||
<Card>
|
||||
<div className="mb-4 rounded-lg border border-dashed border-[var(--line)] bg-[rgba(0,0,0,0.25)] px-3.5 py-3 text-sm">
|
||||
{selected ? (
|
||||
<>
|
||||
{t("firmware.selectedLabel")}{" "}
|
||||
<strong className="font-semibold text-[var(--accent-hot)]">
|
||||
{selected.name}
|
||||
</strong>
|
||||
<span className="text-[var(--muted)]"> ({selected.id})</span>
|
||||
</>
|
||||
) : (
|
||||
<span className="text-[var(--muted)]">{t("firmware.noneSelected")}</span>
|
||||
)}
|
||||
</div>
|
||||
|
||||
<EspWebInstall
|
||||
manifest={selected?.manifestUrl ?? null}
|
||||
label={t("install.cta")}
|
||||
disabled={!serialOk || !selected}
|
||||
/>
|
||||
|
||||
<ol className="mt-5 list-decimal space-y-1.5 pl-5 text-sm text-[var(--muted)]">
|
||||
<li>{t("install.steps.1")}</li>
|
||||
<li>{t("install.steps.2")}</li>
|
||||
<li>{t("install.steps.3")}</li>
|
||||
<li>{t("install.steps.4")}</li>
|
||||
</ol>
|
||||
</Card>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
38
components/language-switcher.tsx
Normal file
38
components/language-switcher.tsx
Normal file
@@ -0,0 +1,38 @@
|
||||
"use client";
|
||||
|
||||
import { useLocale, useTranslations } from "next-intl";
|
||||
import { usePathname, useRouter } from "@/i18n/navigation";
|
||||
import { routing, type AppLocale } from "@/i18n/routing";
|
||||
import { cn } from "@/lib/utils";
|
||||
|
||||
export function LanguageSwitcher() {
|
||||
const t = useTranslations("lang");
|
||||
const locale = useLocale() as AppLocale;
|
||||
const router = useRouter();
|
||||
const pathname = usePathname();
|
||||
|
||||
return (
|
||||
<div
|
||||
className="inline-flex items-center gap-1 rounded-lg border border-[var(--line)] p-0.5"
|
||||
role="group"
|
||||
aria-label={t("label")}
|
||||
>
|
||||
{routing.locales.map((loc) => (
|
||||
<button
|
||||
key={loc}
|
||||
type="button"
|
||||
className={cn(
|
||||
"rounded-md px-2.5 py-1 text-xs font-medium transition-colors",
|
||||
loc === locale
|
||||
? "bg-[rgba(240,197,10,0.18)] text-[var(--accent-hot)]"
|
||||
: "text-[var(--muted)] hover:text-[var(--ink)]"
|
||||
)}
|
||||
aria-pressed={loc === locale}
|
||||
onClick={() => router.replace(pathname, { locale: loc })}
|
||||
>
|
||||
{t(loc)}
|
||||
</button>
|
||||
))}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
26
components/ui/badge.tsx
Normal file
26
components/ui/badge.tsx
Normal file
@@ -0,0 +1,26 @@
|
||||
import * as React from "react";
|
||||
import { cva, type VariantProps } from "class-variance-authority";
|
||||
import { cn } from "@/lib/utils";
|
||||
|
||||
const badgeVariants = cva(
|
||||
"inline-flex items-center rounded-md px-2 py-0.5 text-xs font-medium",
|
||||
{
|
||||
variants: {
|
||||
variant: {
|
||||
default: "bg-[rgba(240,197,10,0.12)] text-[var(--accent-hot)]",
|
||||
muted: "bg-[rgba(0,0,0,0.25)] text-[var(--muted)]",
|
||||
},
|
||||
},
|
||||
defaultVariants: {
|
||||
variant: "default",
|
||||
},
|
||||
}
|
||||
);
|
||||
|
||||
export function Badge({
|
||||
className,
|
||||
variant,
|
||||
...props
|
||||
}: React.HTMLAttributes<HTMLDivElement> & VariantProps<typeof badgeVariants>) {
|
||||
return <div className={cn(badgeVariants({ variant }), className)} {...props} />;
|
||||
}
|
||||
51
components/ui/button.tsx
Normal file
51
components/ui/button.tsx
Normal file
@@ -0,0 +1,51 @@
|
||||
import * as React from "react";
|
||||
import { Slot } from "@radix-ui/react-slot";
|
||||
import { cva, type VariantProps } from "class-variance-authority";
|
||||
import { cn } from "@/lib/utils";
|
||||
|
||||
const buttonVariants = cva(
|
||||
"inline-flex items-center justify-center gap-2 whitespace-nowrap rounded-full text-sm font-semibold transition-colors focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-[var(--accent)] focus-visible:ring-offset-2 focus-visible:ring-offset-[var(--bg)] disabled:pointer-events-none disabled:opacity-45",
|
||||
{
|
||||
variants: {
|
||||
variant: {
|
||||
default:
|
||||
"bg-[linear-gradient(180deg,var(--accent-hot),var(--accent))] text-[var(--accent-ink)] shadow-[0_8px_24px_rgba(240,197,10,0.25)] hover:brightness-105",
|
||||
ghost:
|
||||
"rounded-lg border border-[var(--line)] bg-transparent text-[var(--muted)] hover:border-[rgba(240,197,10,0.35)] hover:bg-[rgba(240,197,10,0.06)] hover:text-[var(--ink)]",
|
||||
outline:
|
||||
"rounded-lg border border-[var(--line)] bg-transparent text-[var(--ink)] hover:border-[rgba(240,197,10,0.35)]",
|
||||
},
|
||||
size: {
|
||||
default: "h-11 px-6",
|
||||
sm: "h-8 px-3 text-xs",
|
||||
lg: "h-12 px-8 text-base",
|
||||
},
|
||||
},
|
||||
defaultVariants: {
|
||||
variant: "default",
|
||||
size: "default",
|
||||
},
|
||||
}
|
||||
);
|
||||
|
||||
export interface ButtonProps
|
||||
extends React.ButtonHTMLAttributes<HTMLButtonElement>,
|
||||
VariantProps<typeof buttonVariants> {
|
||||
asChild?: boolean;
|
||||
}
|
||||
|
||||
const Button = React.forwardRef<HTMLButtonElement, ButtonProps>(
|
||||
({ className, variant, size, asChild = false, ...props }, ref) => {
|
||||
const Comp = asChild ? Slot : "button";
|
||||
return (
|
||||
<Comp
|
||||
className={cn(buttonVariants({ variant, size, className }))}
|
||||
ref={ref}
|
||||
{...props}
|
||||
/>
|
||||
);
|
||||
}
|
||||
);
|
||||
Button.displayName = "Button";
|
||||
|
||||
export { Button, buttonVariants };
|
||||
44
components/ui/card.tsx
Normal file
44
components/ui/card.tsx
Normal file
@@ -0,0 +1,44 @@
|
||||
import * as React from "react";
|
||||
import { cn } from "@/lib/utils";
|
||||
|
||||
export function Card({
|
||||
className,
|
||||
...props
|
||||
}: React.HTMLAttributes<HTMLDivElement>) {
|
||||
return (
|
||||
<div
|
||||
className={cn(
|
||||
"rounded-[14px] border border-[var(--line)] bg-[color-mix(in_srgb,var(--bg-elev)_88%,transparent)] p-5 backdrop-blur-sm",
|
||||
className
|
||||
)}
|
||||
{...props}
|
||||
/>
|
||||
);
|
||||
}
|
||||
|
||||
export function CardHeader({
|
||||
className,
|
||||
...props
|
||||
}: React.HTMLAttributes<HTMLDivElement>) {
|
||||
return (
|
||||
<div
|
||||
className={cn("mb-4 flex items-center justify-between gap-4", className)}
|
||||
{...props}
|
||||
/>
|
||||
);
|
||||
}
|
||||
|
||||
export function CardTitle({
|
||||
className,
|
||||
...props
|
||||
}: React.HTMLAttributes<HTMLHeadingElement>) {
|
||||
return (
|
||||
<h2
|
||||
className={cn(
|
||||
"font-[family-name:var(--font-display)] text-[1.05rem] font-bold tracking-tight",
|
||||
className
|
||||
)}
|
||||
{...props}
|
||||
/>
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user