fix: replace channel enable buttons with real Switch toggles
Add shared EnableSwitch, confirm before disable, busy state, and card dimming on shorts/yt-long/tiktok/x. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
parent
f2b3242e7d
commit
d60d3e5ac0
@ -17,6 +17,7 @@ import {
|
||||
} from "lucide-react";
|
||||
|
||||
import { DouyinLiveBadge } from "@/components/DouyinLiveBadge";
|
||||
import { EnableSwitch } from "@/components/EnableSwitch";
|
||||
import { Shell } from "@/components/Shell";
|
||||
import { useConfirm } from "@/components/ConfirmDialog";
|
||||
import { useToast } from "@/components/Toast";
|
||||
@ -150,6 +151,7 @@ export default function ShortsPage() {
|
||||
const [form, setForm] = useState({ ...emptyForm });
|
||||
const [editId, setEditId] = useState("");
|
||||
const [busy, setBusy] = useState(false);
|
||||
const [togglingId, setTogglingId] = useState("");
|
||||
const [msg, setMsg] = useState("");
|
||||
const [error, setError] = useState("");
|
||||
const [jobFilter, setJobFilter] = useState("");
|
||||
@ -549,6 +551,16 @@ export default function ShortsPage() {
|
||||
|
||||
async function toggleChannelEnabled(ch: Channel) {
|
||||
const turningOn = ch.enabled === false;
|
||||
if (!turningOn) {
|
||||
const ok = await confirm({
|
||||
title: `停用「${ch.name}」?`,
|
||||
message: "关闭后将立即清除该频道全部任务与成片,并停止取样/录制/上传。可随时再开。",
|
||||
confirmLabel: "停用并清任务",
|
||||
destructive: true,
|
||||
});
|
||||
if (!ok) return;
|
||||
}
|
||||
setTogglingId(ch.id);
|
||||
try {
|
||||
const r = await fetchJson(`/api/shorts/channels/${ch.id}`, {
|
||||
method: "PATCH",
|
||||
@ -557,7 +569,7 @@ export default function ShortsPage() {
|
||||
const cleared = r.cleared as { jobs?: number; clips?: number } | undefined;
|
||||
if (!turningOn && cleared) {
|
||||
toast.push(
|
||||
`已停用「${ch.name}」· 清除任务 ${cleared.jobs ?? 0} · 成片 ${cleared.clips ?? 0}`,
|
||||
`已停用 · 清除任务 ${cleared.jobs ?? 0} · 成片 ${cleared.clips ?? 0}`,
|
||||
"ok",
|
||||
);
|
||||
} else {
|
||||
@ -566,6 +578,8 @@ export default function ShortsPage() {
|
||||
await load();
|
||||
} catch (e) {
|
||||
toast.push((e as Error).message, "err");
|
||||
} finally {
|
||||
setTogglingId("");
|
||||
}
|
||||
}
|
||||
|
||||
@ -1203,8 +1217,14 @@ export default function ShortsPage() {
|
||||
return (
|
||||
<div
|
||||
key={ch.id}
|
||||
className={`rounded-xl border bg-gradient-to-br from-white/[0.04] to-transparent p-3 ${
|
||||
warn ? "border-amber-500/35" : ch.max_met ? "border-teal-500/30" : "border-white/10"
|
||||
className={`rounded-xl border bg-gradient-to-br from-white/[0.04] to-transparent p-3 transition ${
|
||||
full?.enabled === false
|
||||
? "border-white/10 opacity-55"
|
||||
: warn
|
||||
? "border-amber-500/35"
|
||||
: ch.max_met
|
||||
? "border-teal-500/30"
|
||||
: "border-white/10"
|
||||
}`}
|
||||
>
|
||||
<div className="flex items-start justify-between gap-2">
|
||||
@ -1218,18 +1238,13 @@ export default function ShortsPage() {
|
||||
/>
|
||||
</div>
|
||||
{full ? (
|
||||
<button
|
||||
type="button"
|
||||
title={full.enabled === false ? "启用此频道" : "停用并清除任务"}
|
||||
onClick={() => void toggleChannelEnabled(full)}
|
||||
className={`shrink-0 rounded-lg px-2 py-1 text-[10px] ${
|
||||
full.enabled === false
|
||||
? "bg-white/5 text-slate-400"
|
||||
: "bg-teal-500/15 text-teal-200"
|
||||
}`}
|
||||
>
|
||||
{full.enabled === false ? "已停" : "启用中"}
|
||||
</button>
|
||||
<EnableSwitch
|
||||
checked={full.enabled !== false}
|
||||
busy={togglingId === full.id}
|
||||
accent="teal"
|
||||
label={full.enabled === false ? "关" : "开"}
|
||||
onChange={() => void toggleChannelEnabled(full)}
|
||||
/>
|
||||
) : null}
|
||||
</div>
|
||||
<div className="relative mt-3 h-1.5 overflow-hidden rounded-full bg-white/10">
|
||||
|
||||
@ -16,6 +16,7 @@ import {
|
||||
import { Shell } from "@/components/Shell";
|
||||
import { useConfirm } from "@/components/ConfirmDialog";
|
||||
import { useToast } from "@/components/Toast";
|
||||
import { EnableSwitch } from "@/components/EnableSwitch";
|
||||
import { PageLoader } from "@/components/ui";
|
||||
import {
|
||||
TikTokUpcomingUploadRow,
|
||||
@ -130,6 +131,7 @@ export default function TikTokPage() {
|
||||
const rightPanelRef = useRef<HTMLElement>(null);
|
||||
|
||||
const [loading, setLoading] = useState(true);
|
||||
const [togglingId, setTogglingId] = useState("");
|
||||
const [status, setStatus] = useState<any>(null);
|
||||
const [channels, setChannels] = useState<Channel[]>([]);
|
||||
const [socksProfiles, setSocksProfiles] = useState<SocksProfile[]>([]);
|
||||
@ -421,6 +423,16 @@ export default function TikTokPage() {
|
||||
|
||||
const toggleChannelEnabled = async (ch: Channel) => {
|
||||
const turningOn = ch.enabled === false;
|
||||
if (!turningOn) {
|
||||
const ok = await confirm({
|
||||
title: `停用「${ch.name}」?`,
|
||||
message: "关闭后将立即清除该账号全部任务与成片,并停止取样/录制/上传。可随时再开。",
|
||||
confirmLabel: "停用并清任务",
|
||||
destructive: true,
|
||||
});
|
||||
if (!ok) return;
|
||||
}
|
||||
setTogglingId(ch.id);
|
||||
try {
|
||||
const r = await fetchJson(`/api/tiktok/channels/${ch.id}`, {
|
||||
method: "PATCH",
|
||||
@ -429,7 +441,7 @@ export default function TikTokPage() {
|
||||
const cleared = r.cleared as { jobs?: number; clips?: number } | undefined;
|
||||
if (!turningOn && cleared) {
|
||||
toast.push(
|
||||
`已停用「${ch.name}」· 清除任务 ${cleared.jobs ?? 0} · 成片 ${cleared.clips ?? 0}`,
|
||||
`已停用 · 清除任务 ${cleared.jobs ?? 0} · 成片 ${cleared.clips ?? 0}`,
|
||||
"ok",
|
||||
);
|
||||
} else {
|
||||
@ -438,6 +450,8 @@ export default function TikTokPage() {
|
||||
void load();
|
||||
} catch (e: any) {
|
||||
toast.push(e?.message || "切换失败", "err");
|
||||
} finally {
|
||||
setTogglingId("");
|
||||
}
|
||||
};
|
||||
|
||||
@ -1156,9 +1170,20 @@ export default function TikTokPage() {
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<span className={`shrink-0 text-[10px] ${cookieAgeClass(full)}`}>
|
||||
{full?.authorized ? "Cookie 就绪" : "未配置"}
|
||||
</span>
|
||||
<div className="flex shrink-0 flex-col items-end gap-1.5">
|
||||
{full ? (
|
||||
<EnableSwitch
|
||||
checked={full.enabled !== false}
|
||||
busy={togglingId === full.id}
|
||||
accent="pink"
|
||||
label={full.enabled === false ? "关" : "开"}
|
||||
onChange={() => void toggleChannelEnabled(full)}
|
||||
/>
|
||||
) : null}
|
||||
<span className={`text-[10px] ${cookieAgeClass(full)}`}>
|
||||
{full?.authorized ? "Cookie 就绪" : "未配置"}
|
||||
</span>
|
||||
</div>
|
||||
</div>
|
||||
{(full?.follower_count || full?.video_count || full?.heart_count) ? (
|
||||
<div className="mt-2 flex flex-wrap gap-2 text-[10px] text-slate-400">
|
||||
@ -1220,15 +1245,6 @@ export default function TikTokPage() {
|
||||
刷新资料
|
||||
</button>
|
||||
) : null}
|
||||
{full && (
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => void toggleChannelEnabled(full)}
|
||||
className="rounded-lg bg-white/5 px-2 py-1 text-[11px]"
|
||||
>
|
||||
{full.enabled === false ? "启用" : "停用"}
|
||||
</button>
|
||||
)}
|
||||
{full && (
|
||||
<button
|
||||
type="button"
|
||||
|
||||
@ -9,7 +9,9 @@ import {
|
||||
} from "lucide-react";
|
||||
|
||||
import { Shell } from "@/components/Shell";
|
||||
import { useConfirm } from "@/components/ConfirmDialog";
|
||||
import { useToast } from "@/components/Toast";
|
||||
import { EnableSwitch } from "@/components/EnableSwitch";
|
||||
import { fetchJson, fmtTs } from "@/lib/api";
|
||||
import {
|
||||
kindLabel,
|
||||
@ -62,8 +64,10 @@ const NAV: { id: TabId; emoji: string; label: string }[] = [
|
||||
|
||||
export default function XPage() {
|
||||
const toast = useToast();
|
||||
const confirm = useConfirm();
|
||||
const [tab, setTab] = useState<TabId>("workbench");
|
||||
const [loading, setLoading] = useState(true);
|
||||
const [togglingId, setTogglingId] = useState("");
|
||||
const [agents, setAgents] = useState<X2Agent[]>([]);
|
||||
const [accounts, setAccounts] = useState<X2Account[]>([]);
|
||||
const [jobs, setJobs] = useState<XJob[]>([]);
|
||||
@ -133,6 +137,16 @@ export default function XPage() {
|
||||
const toggleAccountEnabled = useCallback(
|
||||
async (acc: X2Account) => {
|
||||
const turningOn = acc.enabled === false;
|
||||
if (!turningOn) {
|
||||
const ok = await confirm({
|
||||
title: `停用 @${acc.username || "—"}?`,
|
||||
message: "关闭后将立即清除该账号切片任务并取消插件队列,禁止继续发帖/切片。可随时再开。",
|
||||
confirmLabel: "停用并清任务",
|
||||
destructive: true,
|
||||
});
|
||||
if (!ok) return;
|
||||
}
|
||||
setTogglingId(acc.id);
|
||||
try {
|
||||
const r = await fetchJson(`/api/x2/accounts/${encodeURIComponent(acc.id)}`, {
|
||||
method: "PATCH",
|
||||
@ -142,7 +156,7 @@ export default function XPage() {
|
||||
if (!turningOn && cleared) {
|
||||
const x2n = cleared.x2_cancelled?.count ?? 0;
|
||||
toast.push(
|
||||
`已停用 @${acc.username || "—"} · 切片 ${cleared.jobs ?? 0} · 插件任务 ${x2n}`,
|
||||
`已停用 · 切片 ${cleared.jobs ?? 0} · 插件任务 ${x2n}`,
|
||||
"ok",
|
||||
);
|
||||
} else {
|
||||
@ -151,9 +165,11 @@ export default function XPage() {
|
||||
await load();
|
||||
} catch (e: unknown) {
|
||||
toast.push((e as Error)?.message || "切换失败", "err");
|
||||
} finally {
|
||||
setTogglingId("");
|
||||
}
|
||||
},
|
||||
[load, toast],
|
||||
[confirm, load, toast],
|
||||
);
|
||||
|
||||
useEffect(() => {
|
||||
@ -421,13 +437,18 @@ export default function XPage() {
|
||||
|
||||
{accounts.length > 0 ? (
|
||||
<div className="mb-4">
|
||||
<div className="mb-2 text-[11px] uppercase tracking-[0.14em] text-slate-500">账号开关</div>
|
||||
<div className="mb-2 flex items-center justify-between gap-2">
|
||||
<div className="text-[11px] uppercase tracking-[0.14em] text-slate-500">账号开关</div>
|
||||
<div className="text-[10px] text-slate-500">关闭会清切片任务</div>
|
||||
</div>
|
||||
<div className="grid gap-2 sm:grid-cols-2 xl:grid-cols-3">
|
||||
{accounts.map((a) => (
|
||||
<div
|
||||
key={a.id}
|
||||
className={`flex items-center justify-between gap-2 rounded-xl border px-3 py-2.5 ${
|
||||
a.enabled === false ? "border-white/10 bg-white/[0.02]" : "border-orange-400/25 bg-orange-500/5"
|
||||
className={`flex items-center justify-between gap-3 rounded-xl border px-3 py-2.5 transition ${
|
||||
a.enabled === false
|
||||
? "border-white/10 bg-white/[0.02] opacity-60"
|
||||
: "border-orange-400/25 bg-orange-500/5"
|
||||
}`}
|
||||
>
|
||||
<div className="min-w-0">
|
||||
@ -436,18 +457,14 @@ export default function XPage() {
|
||||
<div className="truncate text-[11px] text-slate-500">{a.display_name}</div>
|
||||
) : null}
|
||||
</div>
|
||||
<button
|
||||
type="button"
|
||||
title={a.enabled === false ? "启用此账号" : "停用并清除切片任务"}
|
||||
onClick={() => void toggleAccountEnabled(a)}
|
||||
className={`shrink-0 rounded-lg px-2.5 py-1 text-[11px] ${
|
||||
a.enabled === false
|
||||
? "bg-white/5 text-slate-400"
|
||||
: "bg-orange-500/20 text-orange-100"
|
||||
}`}
|
||||
>
|
||||
{a.enabled === false ? "已停" : "启用中"}
|
||||
</button>
|
||||
<EnableSwitch
|
||||
checked={a.enabled !== false}
|
||||
busy={togglingId === a.id}
|
||||
accent="orange"
|
||||
size="md"
|
||||
label={a.enabled === false ? "关" : "开"}
|
||||
onChange={() => void toggleAccountEnabled(a)}
|
||||
/>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
|
||||
@ -17,6 +17,7 @@ import {
|
||||
import { Shell } from "@/components/Shell";
|
||||
import { useConfirm } from "@/components/ConfirmDialog";
|
||||
import { useToast } from "@/components/Toast";
|
||||
import { EnableSwitch } from "@/components/EnableSwitch";
|
||||
import { OauthClientBadge, normalizeOauthProfileId } from "@/components/OauthClientBadge";
|
||||
import { YtChannelProfileSnippet, type YtProfileSnippet } from "@/components/YtChannelProfileSnippet";
|
||||
import { buildChannelMeta } from "@/lib/ytChannelMeta";
|
||||
@ -190,6 +191,7 @@ export default function YtLongPage() {
|
||||
const [form, setForm] = useState({ ...emptyForm });
|
||||
const [editId, setEditId] = useState("");
|
||||
const [busy, setBusy] = useState(false);
|
||||
const [togglingId, setTogglingId] = useState("");
|
||||
const [msg, setMsg] = useState("");
|
||||
const [error, setError] = useState("");
|
||||
const [jobFilter, setJobFilter] = useState("");
|
||||
@ -455,6 +457,16 @@ export default function YtLongPage() {
|
||||
|
||||
async function toggleChannelEnabled(ch: Channel) {
|
||||
const turningOn = ch.enabled === false;
|
||||
if (!turningOn) {
|
||||
const ok = await confirm({
|
||||
title: `停用「${ch.name}」?`,
|
||||
message: "关闭后将立即清除该频道全部长视频任务,并停止取样/录制/上传。可随时再开。",
|
||||
confirmLabel: "停用并清任务",
|
||||
destructive: true,
|
||||
});
|
||||
if (!ok) return;
|
||||
}
|
||||
setTogglingId(ch.id);
|
||||
try {
|
||||
const r = await fetchJson(`/api/yt-long/channels/${ch.id}`, {
|
||||
method: "PATCH",
|
||||
@ -462,13 +474,15 @@ export default function YtLongPage() {
|
||||
});
|
||||
const cleared = r.cleared as { jobs?: number } | undefined;
|
||||
if (!turningOn && cleared) {
|
||||
toast.push(`已停用「${ch.name}」· 清除任务 ${cleared.jobs ?? 0}`, "ok");
|
||||
toast.push(`已停用 · 清除任务 ${cleared.jobs ?? 0}`, "ok");
|
||||
} else {
|
||||
toast.push(turningOn ? `已启用「${ch.name}」` : `已停用「${ch.name}」`, "ok");
|
||||
}
|
||||
await load();
|
||||
} catch (e) {
|
||||
toast.push((e as Error).message, "err");
|
||||
} finally {
|
||||
setTogglingId("");
|
||||
}
|
||||
}
|
||||
|
||||
@ -675,10 +689,12 @@ export default function YtLongPage() {
|
||||
return (
|
||||
<div
|
||||
key={ch.id}
|
||||
className={`rounded-xl border bg-gradient-to-br from-white/[0.04] to-transparent p-3 ${
|
||||
gap <= 0 && ch.enabled !== false
|
||||
? "border-teal-500/30"
|
||||
: "border-white/10"
|
||||
className={`rounded-xl border bg-gradient-to-br from-white/[0.04] to-transparent p-3 transition ${
|
||||
full?.enabled === false
|
||||
? "border-white/10 opacity-55"
|
||||
: gap <= 0 && ch.enabled !== false
|
||||
? "border-teal-500/30"
|
||||
: "border-white/10"
|
||||
}`}
|
||||
>
|
||||
<div className="flex items-start justify-between gap-2">
|
||||
@ -692,18 +708,13 @@ export default function YtLongPage() {
|
||||
/>
|
||||
</div>
|
||||
{full ? (
|
||||
<button
|
||||
type="button"
|
||||
title={full.enabled === false ? "启用此频道" : "停用并清除任务"}
|
||||
onClick={() => void toggleChannelEnabled(full)}
|
||||
className={`shrink-0 rounded-lg px-2 py-1 text-[10px] ${
|
||||
full.enabled === false
|
||||
? "bg-white/5 text-slate-400"
|
||||
: "bg-teal-500/15 text-teal-200"
|
||||
}`}
|
||||
>
|
||||
{full.enabled === false ? "已停" : "启用中"}
|
||||
</button>
|
||||
<EnableSwitch
|
||||
checked={full.enabled !== false}
|
||||
busy={togglingId === full.id}
|
||||
accent="amber"
|
||||
label={full.enabled === false ? "关" : "开"}
|
||||
onChange={() => void toggleChannelEnabled(full)}
|
||||
/>
|
||||
) : null}
|
||||
</div>
|
||||
<div className="relative mt-3 h-1.5 overflow-hidden rounded-full bg-white/10">
|
||||
|
||||
90
frontend/src/components/EnableSwitch.tsx
Normal file
90
frontend/src/components/EnableSwitch.tsx
Normal file
@ -0,0 +1,90 @@
|
||||
"use client";
|
||||
|
||||
type Accent = "teal" | "pink" | "orange" | "amber";
|
||||
|
||||
const TRACK_ON: Record<Accent, string> = {
|
||||
teal: "bg-teal-400 shadow-[0_0_12px_rgba(45,212,191,0.35)]",
|
||||
pink: "bg-pink-400 shadow-[0_0_12px_rgba(244,114,182,0.35)]",
|
||||
orange: "bg-orange-400 shadow-[0_0_12px_rgba(251,146,60,0.35)]",
|
||||
amber: "bg-amber-400 shadow-[0_0_12px_rgba(251,191,36,0.35)]",
|
||||
};
|
||||
|
||||
type Props = {
|
||||
checked: boolean;
|
||||
onChange: (next: boolean) => void;
|
||||
disabled?: boolean;
|
||||
busy?: boolean;
|
||||
label?: string;
|
||||
accent?: Accent;
|
||||
size?: "sm" | "md";
|
||||
className?: string;
|
||||
};
|
||||
|
||||
/** Compact iOS-style enable switch for channel / account cards. */
|
||||
export function EnableSwitch({
|
||||
checked,
|
||||
onChange,
|
||||
disabled = false,
|
||||
busy = false,
|
||||
label,
|
||||
accent = "teal",
|
||||
size = "sm",
|
||||
className = "",
|
||||
}: Props) {
|
||||
const locked = disabled || busy;
|
||||
const trackW = size === "md" ? "h-6 w-11" : "h-5 w-9";
|
||||
const knob = size === "md" ? "h-5 w-5" : "h-4 w-4";
|
||||
const travel = size === "md" ? "translate-x-5" : "translate-x-4";
|
||||
|
||||
return (
|
||||
<div
|
||||
className={`inline-flex shrink-0 select-none items-center gap-2 ${className}`}
|
||||
onClick={(e) => e.stopPropagation()}
|
||||
>
|
||||
{label ? (
|
||||
<span
|
||||
className={`text-[10px] font-medium tracking-wide ${
|
||||
checked ? "text-slate-200" : "text-slate-500"
|
||||
}`}
|
||||
>
|
||||
{label}
|
||||
</span>
|
||||
) : null}
|
||||
<button
|
||||
type="button"
|
||||
role="switch"
|
||||
aria-checked={checked}
|
||||
aria-busy={busy || undefined}
|
||||
aria-label={checked ? "关闭" : "开启"}
|
||||
title={checked ? "关闭将清除任务并停止取样/上传" : "开启后恢复自动取样与上传"}
|
||||
disabled={locked}
|
||||
onClick={(e) => {
|
||||
e.preventDefault();
|
||||
e.stopPropagation();
|
||||
if (locked) return;
|
||||
onChange(!checked);
|
||||
}}
|
||||
className={[
|
||||
"relative inline-flex shrink-0 items-center rounded-full transition-colors duration-200 ease-out",
|
||||
"focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-white/30 focus-visible:ring-offset-2 focus-visible:ring-offset-[#0b1220]",
|
||||
trackW,
|
||||
checked ? TRACK_ON[accent] : "bg-white/15",
|
||||
locked ? "cursor-wait opacity-70" : "cursor-pointer",
|
||||
].join(" ")}
|
||||
>
|
||||
<span
|
||||
className={[
|
||||
"pointer-events-none inline-block transform rounded-full bg-white shadow-md transition duration-200 ease-out",
|
||||
knob,
|
||||
checked ? travel : "translate-x-0.5",
|
||||
].join(" ")}
|
||||
/>
|
||||
{busy ? (
|
||||
<span className="pointer-events-none absolute inset-0 flex items-center justify-center">
|
||||
<span className="h-2.5 w-2.5 animate-spin rounded-full border border-white/40 border-t-white" />
|
||||
</span>
|
||||
) : null}
|
||||
</button>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user