"use client"; import { useCallback, useMemo, useState } from "react"; import { Handle, Position, type Node, type NodeProps } from "@xyflow/react"; import { useTranslations } from "next-intl"; import { TrendingUp } from "lucide-react"; import type { Id } from "@/convex/_generated/dataModel"; import { useCanvasAdjustmentPresets, useSaveCanvasAdjustmentPreset, } from "@/components/canvas/canvas-presets-context"; import { useCanvasSync } from "@/components/canvas/canvas-sync-context"; import BaseNodeWrapper from "@/components/canvas/nodes/base-node-wrapper"; import AdjustmentPreview from "@/components/canvas/nodes/adjustment-preview"; import { useNodeLocalData } from "@/components/canvas/nodes/use-node-local-data"; import { ParameterSlider, type SliderConfig, type SliderValue, } from "@/src/components/tool-ui/parameter-slider"; import { cloneAdjustmentData, DEFAULT_CURVES_DATA, normalizeCurvesData, type CurvesData, } from "@/lib/image-pipeline/adjustment-types"; import { CURVE_PRESETS } from "@/lib/image-pipeline/presets"; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/components/ui/select"; import { toast } from "@/lib/toast"; type CurvesNodeData = CurvesData & { _status?: string; _statusMessage?: string; }; export type CurvesNodeType = Node; type PresetDoc = { _id: Id<"adjustmentPresets">; name: string; params: unknown; }; export default function CurvesNode({ id, data, selected, width }: NodeProps) { const tCommon = useTranslations("common"); const tNodes = useTranslations("nodes"); const tToasts = useTranslations("toasts"); const { queueNodeDataUpdate } = useCanvasSync(); const savePreset = useSaveCanvasAdjustmentPreset(); const userPresets = useCanvasAdjustmentPresets("curves") as PresetDoc[]; const [presetSelection, setPresetSelection] = useState("custom"); const normalizeData = useCallback( (value: unknown) => normalizeCurvesData({ ...cloneAdjustmentData(DEFAULT_CURVES_DATA), ...(value as Record), }), [], ); const { localData, applyLocalData, updateLocalData } = useNodeLocalData({ nodeId: id, data, normalize: normalizeData, saveDelayMs: 16, onSave: (next) => queueNodeDataUpdate({ nodeId: id as Id<"nodes">, data: next, }), debugLabel: "curves", }); const updateData = (updater: (draft: CurvesData) => CurvesData) => { setPresetSelection("custom"); updateLocalData(updater); }; const builtinOptions = useMemo(() => Object.entries(CURVE_PRESETS), []); const sliderConfigs = useMemo( () => [ { id: "black-point", label: tNodes("adjustments.curves.sliders.blackPoint"), min: 0, max: 255, value: DEFAULT_CURVES_DATA.levels.blackPoint, }, { id: "white-point", label: tNodes("adjustments.curves.sliders.whitePoint"), min: 0, max: 255, value: DEFAULT_CURVES_DATA.levels.whitePoint, }, { id: "gamma", label: tNodes("adjustments.curves.sliders.gamma"), min: 0.1, max: 3, step: 0.01, precision: 2, value: DEFAULT_CURVES_DATA.levels.gamma, }, ], [tNodes], ); const sliderValues = useMemo( () => [ { id: "black-point", value: localData.levels.blackPoint }, { id: "white-point", value: localData.levels.whitePoint }, { id: "gamma", value: localData.levels.gamma }, ], [localData.levels.blackPoint, localData.levels.gamma, localData.levels.whitePoint], ); const applyPresetValue = (value: string) => { if (value === "custom") { setPresetSelection("custom"); return; } if (value.startsWith("builtin:")) { const key = value.replace("builtin:", ""); const preset = CURVE_PRESETS[key]; if (!preset) return; setPresetSelection(value); applyLocalData(cloneAdjustmentData(preset)); return; } if (value.startsWith("user:")) { const presetId = value.replace("user:", "") as Id<"adjustmentPresets">; const preset = userPresets.find((entry) => entry._id === presetId); if (!preset) return; const next = normalizeCurvesData(preset.params); setPresetSelection(value); applyLocalData(next); } }; const handleSavePreset = async () => { const name = window.prompt(tNodes("adjustments.common.presetNamePrompt")); if (!name) return; await savePreset({ name, nodeType: "curves", params: localData, }); toast.success(tToasts("canvas.adjustmentPresetSaved")); }; return (
{tNodes("adjustments.curves.title")}
{ const valueById = new Map(values.map((entry) => [entry.id, entry.value])); updateData((current) => ({ ...current, levels: { ...current.levels, blackPoint: valueById.get("black-point") ?? current.levels.blackPoint, whitePoint: valueById.get("white-point") ?? current.levels.whitePoint, gamma: valueById.get("gamma") ?? current.levels.gamma, }, preset: null, })); }} />
); }