"use client"; import { useEffect, useState } from "react"; import { apiFetch, ApiError } from "@/lib/api"; import { useItem, useList } from "@/lib/useResource"; import { useToast } from "@/components/toast"; import { Button, Card, Field, Input, PageHeader, Select, Spinner, Switch, } from "@/components/ui"; import { toFa } from "@/lib/utils"; interface Scene { id: number; name: string; } interface SceneSettings { active_scene_id?: number | string | null; scene_volume?: number | string | null; background_play_seconds?: number | string | null; video_enabled?: boolean; } export default function SceneSettingsPage() { const { data, loading } = useItem("/scene-settings"); const { data: scenes } = useList("/scenes"); const toast = useToast(); const [activeSceneId, setActiveSceneId] = useState(""); const [sceneVolume, setSceneVolume] = useState(""); const [backgroundPlaySeconds, setBackgroundPlaySeconds] = useState(""); const [videoEnabled, setVideoEnabled] = useState(false); const [saving, setSaving] = useState(false); useEffect(() => { if (!data) return; setActiveSceneId( data.active_scene_id != null ? String(data.active_scene_id) : "", ); setSceneVolume(data.scene_volume != null ? String(data.scene_volume) : ""); setBackgroundPlaySeconds( data.background_play_seconds != null ? String(data.background_play_seconds) : "", ); setVideoEnabled(!!data.video_enabled); }, [data]); async function save(e: React.FormEvent) { e.preventDefault(); setSaving(true); try { await apiFetch("/scene-settings", { method: "PUT", body: { active_scene_id: activeSceneId ? Number(activeSceneId) : null, scene_volume: sceneVolume ? Number(sceneVolume) : null, background_play_seconds: backgroundPlaySeconds ? Number(backgroundPlaySeconds) : null, video_enabled: videoEnabled, }, }); toast.success("تنظیمات صحنه ذخیره شد."); } catch (err) { toast.error(err instanceof ApiError ? err.message : "خطا در ذخیره‌سازی"); } finally { setSaving(false); } } return (
{loading ? (
) : (
setSceneVolume(e.target.value)} placeholder="0" dir="ltr" /> setBackgroundPlaySeconds(e.target.value)} placeholder="0" dir="ltr" />
)}
); }