"use client";
import { useRef, useState, type ReactNode } from "react";
import { useTranslations } from "next-intl";
import {
AlignCenter,
Bookmark,
Captions,
Clapperboard,
Crop,
Highlighter,
Info,
Palette,
Play,
Plus,
RotateCcw,
Trash2,
Volume2,
} from "lucide-react";
import { toast } from "sonner";
import { Alert, AlertDescription } from "@/components/ui/alert";
import { Button } from "@/components/ui/button";
import { Input } from "@/components/ui/input";
import { Label } from "@/components/ui/label";
import {
Select,
SelectContent,
SelectItem,
SelectTrigger,
SelectValue,
} from "@/components/ui/select";
import { Switch } from "@/components/ui/switch";
import { Tabs, TabsContent, TabsList, TabsTrigger } from "@/components/ui/tabs";
import { Textarea } from "@/components/ui/textarea";
import { ToggleGroup, ToggleGroupItem } from "@/components/ui/toggle-group";
import {
DEFAULT_EFFECTS,
captionAt,
captionKey,
captionTransform,
clamp,
replaceWords,
setCaptionVisualSize,
textTransform,
uid,
type EditDocument,
type CaptionStyle,
type CaptionTransform,
type EditorState,
} from "@/lib/editor/document";
import { cn } from "@/lib/utils";
import { editorMessage, Field, Panel, Range, Section } from "./studio-ui";
import { BRollPanel } from "./broll-panel";
export type InspectorTab = "captions" | "framing" | "broll" | "audio" | "effects";
const TABS = [
["captions", Captions],
["framing", Crop],
["broll", Clapperboard],
["audio", Volume2],
["effects", Palette],
] as const;
function ColorField({
label,
inputLabel,
value,
onChange,
}: {
label: string;
inputLabel: string;
value: string;
onChange: (value: string) => void;
}) {
return (
onChange(e.target.value)}
className="size-6 shrink-0 cursor-pointer rounded border-0 ring-1 ring-border bg-transparent p-0 outline-none [&::-moz-color-swatch]:rounded [&::-moz-color-swatch]:border-0 [&::-webkit-color-swatch]:rounded [&::-webkit-color-swatch]:border-0 [&::-webkit-color-swatch-wrapper]:p-0"
/>
{value}
);
}
function SwitchRow({
id,
label,
checked,
onChange,
}: {
id: string;
label: ReactNode;
checked: boolean;
onChange: (checked: boolean) => void;
}) {
return (
);
}
interface Props {
doc: EditDocument;
asset: EditorState;
tab: InspectorTab;
setTab: (tab: InspectorTab) => void;
time: number;
editTime: number;
seek: (value: number) => void;
update: (change: (doc: EditDocument) => EditDocument, group?: string) => void;
applyAll: (style: CaptionStyle) => Promise;
endpoint: string;
brollId: string | null;
selectBroll: (id: string | null) => void;
}
export function Inspector({
doc,
asset,
tab,
setTab,
time,
editTime,
seek,
update,
applyAll,
endpoint,
brollId,
selectBroll,
}: Props) {
const t = useTranslations("editor");
const activeCaption = captionAt(doc, editTime);
const captionId = activeCaption ? captionKey(doc, activeCaption) : undefined;
const placement: CaptionTransform = activeCaption ? captionTransform(doc, activeCaption) : textTransform(doc, "captions");
const captionSize = (placement.size ?? doc.captions.size) * placement.scale;
const scriptWords = useRef(doc.words);
const [script, setScript] = useState(null),
[applying, setApplying] = useState(false);
const caption = (change: Partial, group = "caption-style") =>
update((d) => ({ ...d, captions: { ...d.captions, ...change } }), group);
return (
setTab(value as InspectorTab)}
className="gap-0 lg:min-h-0 lg:flex-1"
>
{TABS.map(([id, Icon]) => (
{t(`tabs.${id}`)}
))}
caption({ enabled })}
/>
}
>
{(
[
["clean", false, "#FFFFFF"],
["highlight", true, "#FACC15"],
["pop", true, "#A3E635"],
] as const
).map(([name, background, accent]) => {
const active =
doc.captions.background === background &&
doc.captions.accent.toUpperCase() === accent;
return (
);
})}
update(current => setCaptionVisualSize(current, size, captionId), `caption-size:${captionId ?? "all"}`)}
/>
update(current => {
const transform = { ...(captionId ? current.captionTransforms?.[captionId] : undefined) ?? textTransform(current, "captions"), y: y / 100 - current.captions.y };
return captionId ? { ...current, captionTransforms: { ...current.captionTransforms, [captionId]: transform } } : { ...current, textTransforms: { ...current.textTransforms, captions: transform } };
}, `caption-position:${captionId ?? "all"}`)}
/>
caption({ color })}
/>
caption({ accent })}
/>
caption({ background })}
/>
caption({ karaoke })}
/>
caption({ uppercase })}
/>
{t("captions.transcript")}{" "}
· {t("captions.wordCount", { count: doc.words.length })}
>
}
description={t("captions.transcriptDescription")}
action={
}
>
{script !== null && (
)}
{doc.words.map((word, i) => {
const current = time >= word.start && time < word.end;
return (
-
update(
(d) => ({
...d,
words: d.words.map((w) =>
w.id === word.id
? { ...w, text: e.target.value }
: w,
),
}),
`word-${word.id}`,
)
}
/>
update(
(d) => ({
...d,
words: d.words.map((w) =>
w.id === word.id
? {
...w,
start: clamp(
Number(e.target.value),
0,
w.end - 0.01,
),
}
: w,
),
}),
`timing-${word.id}`,
)
}
/>
→
update(
(d) => ({
...d,
words: d.words.map((w) =>
w.id === word.id
? {
...w,
end: clamp(
Number(e.target.value),
w.start + 0.01,
asset.duration,
),
}
: w,
),
}),
`timing-${word.id}`,
)
}
/>
);
})}
{asset.landscapeRecommended && doc.framing.aspect !== "original" &&
{t("framing.landscapeHint")}
}
aspect &&
update((d) => ({
...d,
framing: {
...d.framing,
aspect: aspect as EditDocument["framing"]["aspect"],
},
}))
}
>
{(
[
["vertical", "9:16"],
["square", "1:1"],
["original", t("framing.sourceRatio")],
] as const
).map(([value, ratio]) => (
{ratio}
{t(`framing.aspects.${value}`)}
))}
fit &&
update((d) => ({
...d,
framing: {
...d.framing,
fit: fit as EditDocument["framing"]["fit"],
},
}))
}
>
{asset.hasLayout && {t("framing.autoLayout")}}
{t("framing.fill")}
{t("framing.contain")}
update(
(d) => ({ ...d, framing: { ...d.framing, zoom } }),
"zoom",
)
}
/>
update(
(d) => ({ ...d, framing: { ...d.framing, x: x / 100 } }),
"position",
)
}
/>
update(
(d) => ({ ...d, framing: { ...d.framing, y: y / 100 } }),
"position",
)
}
/>
{!asset.hasAudio && (
{t("audioPanel.noTrack")}
)}
update((d) => ({ ...d, volume: volume / 100 }), "volume")
}
/>
update((d) => ({ ...d, muted }))}
/>
{doc.volume > 1 && (
{t("audioPanel.boostWarning")}
)}
update((d) => ({ ...d, effects: { ...DEFAULT_EFFECTS } }))
}
>
{t("effects.reset")}
}
>
{(
[
["brightness", 40, 180, 1],
["contrast", 40, 180, 1],
["saturation", 0, 220, 1],
["blur", 0, 8, 0.1],
["hue", -180, 180, 1],
] as const
).map(([key, min, max, step]) => (
update(
(d) => ({
...d,
effects: { ...d.effects, [key]: value },
}),
key,
)
}
/>
))}
);
}