feat(extension): expose silent / downloadNow knobs in options
The useUrlInvoke hook at apps/ui/src/hooks/use-url-invoke.ts reads both `silent` and `downloadNow` query params; they were previously hard-coded on the extension side. Surface them as user toggles so Schema-mode users can opt into "open the download form for review" and both modes can opt into "start downloading immediately instead of just queuing". - ExtensionSettings gains `downloadNow` (default false) and `schemaSilent` (default true). - importViaHttp threads `startDownload` into the POST body. - buildTaskDeeplink writes `silent=1` / `downloadNow=1` into the deeplink URL based on the flags (falsy => omit the key, matching the truthy-check in useUrlInvoke). - New headless Switch component (no @radix-ui/react-switch dep). - New ImportBehaviourCard in options, with a patch-style save hook so each toggle persists immediately. - use-options.ts now merges on top of the current persisted settings so the server-config card doesn't wipe the new fields. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -68,6 +68,7 @@ async function probeHttp(config: HttpConfig): Promise<ServerStatus> {
|
||||
async function importViaHttp(
|
||||
config: HttpConfig,
|
||||
sources: DetectedSource[],
|
||||
opts: { startDownload: boolean },
|
||||
): Promise<ImportResult> {
|
||||
if (!config.serverUrl) {
|
||||
return { ok: false, count: 0, error: "MediaGo server not configured" };
|
||||
@@ -81,7 +82,7 @@ async function importViaHttp(
|
||||
),
|
||||
body: JSON.stringify({
|
||||
tasks: sourcesToTasks(sources),
|
||||
startDownload: false,
|
||||
startDownload: opts.startDownload,
|
||||
}),
|
||||
});
|
||||
if (!res.ok) {
|
||||
@@ -121,10 +122,16 @@ async function importViaHttp(
|
||||
* Because the route binds a *single* task into query params, batch
|
||||
* imports are serialised (one deeplink at a time) in `importViaSchema`.
|
||||
*/
|
||||
function buildTaskDeeplink(source: DetectedSource): string {
|
||||
interface SchemaFlags {
|
||||
silent: boolean;
|
||||
downloadNow: boolean;
|
||||
}
|
||||
|
||||
function buildTaskDeeplink(source: DetectedSource, flags: SchemaFlags): string {
|
||||
const params = new URLSearchParams();
|
||||
params.set("n", "1");
|
||||
params.set("silent", "1");
|
||||
params.set("n", "1"); // required trigger for useUrlInvoke
|
||||
if (flags.silent) params.set("silent", "1");
|
||||
if (flags.downloadNow) params.set("downloadNow", "1");
|
||||
params.set("url", source.url);
|
||||
if (source.name) params.set("name", source.name);
|
||||
params.set("type", source.type);
|
||||
@@ -161,6 +168,7 @@ async function openDeeplink(url: string): Promise<void> {
|
||||
|
||||
async function importViaSchema(
|
||||
sources: DetectedSource[],
|
||||
flags: SchemaFlags,
|
||||
): Promise<ImportResult> {
|
||||
// `chrome.tabs.update` navigates THE single active tab — there's
|
||||
// no way to chain more than one scheme invocation without racing
|
||||
@@ -176,7 +184,7 @@ async function importViaSchema(
|
||||
};
|
||||
}
|
||||
try {
|
||||
await openDeeplink(buildTaskDeeplink(sources[0]));
|
||||
await openDeeplink(buildTaskDeeplink(sources[0], flags));
|
||||
return { ok: true, count: 1 };
|
||||
} catch (err) {
|
||||
return {
|
||||
@@ -252,9 +260,14 @@ export async function importSources(
|
||||
|
||||
switch (settings.mode) {
|
||||
case "desktop-schema":
|
||||
return importViaSchema(sources);
|
||||
return importViaSchema(sources, {
|
||||
silent: settings.schemaSilent,
|
||||
downloadNow: settings.downloadNow,
|
||||
});
|
||||
case "desktop-http":
|
||||
return importViaHttp({ serverUrl: DESKTOP_HTTP_BASE }, sources);
|
||||
return importViaHttp({ serverUrl: DESKTOP_HTTP_BASE }, sources, {
|
||||
startDownload: settings.downloadNow,
|
||||
});
|
||||
case "docker-http":
|
||||
if (!settings.serverUrl) {
|
||||
return {
|
||||
@@ -266,6 +279,7 @@ export async function importSources(
|
||||
return importViaHttp(
|
||||
{ serverUrl: settings.serverUrl, apiKey: settings.apiKey || undefined },
|
||||
sources,
|
||||
{ startDownload: settings.downloadNow },
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,44 @@
|
||||
import * as React from "react";
|
||||
|
||||
import { cn } from "@/lib/utils";
|
||||
|
||||
/**
|
||||
* Headless Switch. Visually matches shadcn's new-york Switch without
|
||||
* pulling in `@radix-ui/react-switch` (one extra 4KB dep for a single
|
||||
* button-shaped control isn't worth it in an extension popup).
|
||||
*/
|
||||
export interface SwitchProps extends Omit<
|
||||
React.ButtonHTMLAttributes<HTMLButtonElement>,
|
||||
"onChange"
|
||||
> {
|
||||
checked: boolean;
|
||||
onCheckedChange: (checked: boolean) => void;
|
||||
}
|
||||
|
||||
export const Switch = React.forwardRef<HTMLButtonElement, SwitchProps>(
|
||||
({ className, checked, onCheckedChange, disabled, ...props }, ref) => (
|
||||
<button
|
||||
type="button"
|
||||
role="switch"
|
||||
aria-checked={checked}
|
||||
data-state={checked ? "checked" : "unchecked"}
|
||||
disabled={disabled}
|
||||
onClick={() => onCheckedChange(!checked)}
|
||||
ref={ref}
|
||||
className={cn(
|
||||
"peer inline-flex h-5 w-9 shrink-0 cursor-pointer items-center rounded-full border-2 border-transparent shadow-sm transition-colors focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 disabled:cursor-not-allowed disabled:opacity-50",
|
||||
checked ? "bg-primary" : "bg-input",
|
||||
className,
|
||||
)}
|
||||
{...props}
|
||||
>
|
||||
<span
|
||||
className={cn(
|
||||
"pointer-events-none block h-4 w-4 rounded-full bg-background shadow-lg ring-0 transition-transform",
|
||||
checked ? "translate-x-4" : "translate-x-0",
|
||||
)}
|
||||
/>
|
||||
</button>
|
||||
),
|
||||
);
|
||||
Switch.displayName = "Switch";
|
||||
@@ -1,3 +1,4 @@
|
||||
import { ImportBehaviourCard } from "./components/ImportBehaviourCard";
|
||||
import { RuleListCard } from "./components/RuleListCard";
|
||||
import { ServerCard } from "./components/ServerCard";
|
||||
|
||||
@@ -15,6 +16,7 @@ export function App() {
|
||||
<h1 className="text-lg font-semibold">MediaGo 扩展设置</h1>
|
||||
</header>
|
||||
<ServerCard />
|
||||
<ImportBehaviourCard />
|
||||
<RuleListCard />
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -0,0 +1,98 @@
|
||||
import { toast } from "sonner";
|
||||
|
||||
import {
|
||||
Card,
|
||||
CardContent,
|
||||
CardDescription,
|
||||
CardHeader,
|
||||
CardTitle,
|
||||
} from "@/components/ui/card";
|
||||
import { Label } from "@/components/ui/label";
|
||||
import { Switch } from "@/components/ui/switch";
|
||||
import type { ExtensionSettings } from "@/shared/types";
|
||||
|
||||
import { useImportBehaviour } from "../use-import-behaviour";
|
||||
|
||||
export function ImportBehaviourCard() {
|
||||
const { settings, patch } = useImportBehaviour();
|
||||
const disabled = settings === null;
|
||||
const downloadNow = settings?.downloadNow ?? false;
|
||||
const schemaSilent = settings?.schemaSilent ?? true;
|
||||
const schemaOnly = settings?.mode === "desktop-schema";
|
||||
|
||||
const apply = async (update: Partial<ExtensionSettings>) => {
|
||||
const ok = await patch(update);
|
||||
if (ok) toast.success("已保存");
|
||||
else toast.error("保存失败");
|
||||
};
|
||||
|
||||
return (
|
||||
<Card>
|
||||
<CardHeader>
|
||||
<CardTitle>导入行为</CardTitle>
|
||||
<CardDescription>
|
||||
这些选项通过 deeplink 查询串(<code>silent</code> /{" "}
|
||||
<code>downloadNow</code>)或 HTTP body(
|
||||
<code>startDownload</code>)告诉 MediaGo 收到任务后怎么处理。
|
||||
</CardDescription>
|
||||
</CardHeader>
|
||||
<CardContent className="space-y-4">
|
||||
<ToggleRow
|
||||
label="立即开始下载"
|
||||
description="开:任务进队列并立刻开跑。关:仅加入下载列表,等用户手动触发。对 Schema 和 HTTP 两种模式都生效。"
|
||||
checked={downloadNow}
|
||||
disabled={disabled}
|
||||
onCheckedChange={(v) => apply({ downloadNow: v })}
|
||||
/>
|
||||
|
||||
<ToggleRow
|
||||
label="静默导入(Schema 模式)"
|
||||
description={
|
||||
schemaOnly
|
||||
? "开:deeplink 携带 silent=1,MediaGo 收到即创建任务。关:MediaGo 会弹出下载表单让用户核对名字 / 类型 / 保存路径再提交。"
|
||||
: "仅 Schema 模式生效 —— HTTP 模式没有桌面弹窗概念,总是静默。"
|
||||
}
|
||||
checked={schemaSilent}
|
||||
disabled={disabled || !schemaOnly}
|
||||
onCheckedChange={(v) => apply({ schemaSilent: v })}
|
||||
/>
|
||||
</CardContent>
|
||||
</Card>
|
||||
);
|
||||
}
|
||||
|
||||
interface ToggleRowProps {
|
||||
label: string;
|
||||
description: string;
|
||||
checked: boolean;
|
||||
disabled?: boolean;
|
||||
onCheckedChange: (checked: boolean) => void;
|
||||
}
|
||||
|
||||
function ToggleRow({
|
||||
label,
|
||||
description,
|
||||
checked,
|
||||
disabled,
|
||||
onCheckedChange,
|
||||
}: ToggleRowProps) {
|
||||
const id = `toggle-${label}`;
|
||||
return (
|
||||
<div className="flex items-start justify-between gap-4 rounded-md border p-3">
|
||||
<div className="flex-1 space-y-1">
|
||||
<Label htmlFor={id} className="text-sm">
|
||||
{label}
|
||||
</Label>
|
||||
<p className="text-xs leading-relaxed text-muted-foreground">
|
||||
{description}
|
||||
</p>
|
||||
</div>
|
||||
<Switch
|
||||
id={id}
|
||||
checked={checked}
|
||||
onCheckedChange={onCheckedChange}
|
||||
disabled={disabled}
|
||||
/>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
import { useCallback, useEffect, useState } from "react";
|
||||
|
||||
import type {
|
||||
ExtensionMessage,
|
||||
ExtensionResponse,
|
||||
ExtensionSettings,
|
||||
} from "@/shared/types";
|
||||
|
||||
async function sendMessage<T extends ExtensionResponse>(
|
||||
msg: ExtensionMessage,
|
||||
): Promise<T> {
|
||||
return (await chrome.runtime.sendMessage(msg)) as T;
|
||||
}
|
||||
|
||||
/**
|
||||
* Read settings + expose a patch-style save helper. The import-
|
||||
* behaviour card toggles each knob independently, so we avoid the
|
||||
* "edit form + click Save" dance used by the server card and just
|
||||
* persist every change immediately.
|
||||
*/
|
||||
export function useImportBehaviour() {
|
||||
const [settings, setSettings] = useState<ExtensionSettings | null>(null);
|
||||
|
||||
useEffect(() => {
|
||||
void (async () => {
|
||||
const res = await sendMessage<ExtensionResponse>({
|
||||
type: "GET_SETTINGS",
|
||||
});
|
||||
if (res.type === "SETTINGS") setSettings(res.settings);
|
||||
})();
|
||||
}, []);
|
||||
|
||||
const patch = useCallback(
|
||||
async (update: Partial<ExtensionSettings>): Promise<boolean> => {
|
||||
if (!settings) return false;
|
||||
const next = { ...settings, ...update };
|
||||
const res = await sendMessage<ExtensionResponse>({
|
||||
type: "SAVE_SETTINGS",
|
||||
settings: next,
|
||||
});
|
||||
if (res.type === "OK") {
|
||||
setSettings(next);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
},
|
||||
[settings],
|
||||
);
|
||||
|
||||
return { settings, patch };
|
||||
}
|
||||
@@ -72,7 +72,24 @@ export function useOptions() {
|
||||
}
|
||||
setSaving(true);
|
||||
try {
|
||||
// Re-fetch the current persisted settings so we merge on top
|
||||
// instead of wiping fields this card doesn't own (downloadNow,
|
||||
// schemaSilent — managed by ImportBehaviourCard).
|
||||
const current = await sendMessage<ExtensionResponse>({
|
||||
type: "GET_SETTINGS",
|
||||
});
|
||||
const base: ExtensionSettings =
|
||||
current.type === "SETTINGS"
|
||||
? current.settings
|
||||
: {
|
||||
mode: "desktop-http",
|
||||
serverUrl: "",
|
||||
apiKey: "",
|
||||
downloadNow: false,
|
||||
schemaSilent: true,
|
||||
};
|
||||
const settings: ExtensionSettings = {
|
||||
...base,
|
||||
mode,
|
||||
serverUrl: mode === "docker-http" ? normalizedUrl() : "",
|
||||
apiKey: mode === "docker-http" ? apiKey.trim() : "",
|
||||
|
||||
@@ -15,11 +15,19 @@ export const DESKTOP_HTTP_BASE = "http://127.0.0.1:9900";
|
||||
export const MEDIAGO_SCHEME: string =
|
||||
import.meta.env.APP_NAME || "mediago-community";
|
||||
|
||||
/** Default mode on first install: local Desktop via HTTP (silent, no prompt). */
|
||||
/**
|
||||
* Default mode on first install: local Desktop via HTTP (silent, no
|
||||
* prompt). Import-behaviour knobs default to the least-surprising
|
||||
* choices: don't auto-start (user gets to review the queue first) and
|
||||
* don't re-prompt on schema (one-click is the entire point of the
|
||||
* extension).
|
||||
*/
|
||||
export const DEFAULT_SETTINGS: ExtensionSettings = {
|
||||
mode: "desktop-http",
|
||||
serverUrl: "",
|
||||
apiKey: "",
|
||||
downloadNow: false,
|
||||
schemaSilent: true,
|
||||
};
|
||||
|
||||
/** localStorage keys in `chrome.storage.local`. */
|
||||
|
||||
@@ -57,6 +57,25 @@ export interface ExtensionSettings {
|
||||
serverUrl: string;
|
||||
/** For `docker-http` only, when the server runs with `--enable-auth`. */
|
||||
apiKey: string;
|
||||
/**
|
||||
* Whether to start downloading immediately after the task lands on
|
||||
* MediaGo, or just append it to the list for the user to start
|
||||
* manually. Applies to **both** modes:
|
||||
* - HTTP: sent as `startDownload` in the POST body.
|
||||
* - Schema: encoded as `downloadNow=1` in the deeplink URL.
|
||||
*/
|
||||
downloadNow: boolean;
|
||||
/**
|
||||
* Schema-only knob. When `true` (default), the deeplink carries
|
||||
* `silent=1` so MediaGo adds the task without prompting. When
|
||||
* `false`, MediaGo Desktop opens its download form dialog prefilled
|
||||
* with the sniffed values so the user can tweak name / folder /
|
||||
* type before committing.
|
||||
*
|
||||
* Has no effect in HTTP modes — HTTP imports are always silent
|
||||
* because there's no interactive UI in that path.
|
||||
*/
|
||||
schemaSilent: boolean;
|
||||
}
|
||||
|
||||
/** Reachability probe result for the configured MediaGo server. */
|
||||
|
||||
Reference in New Issue
Block a user