fluxify / components /builder /ConfigurationPanel.tsx
phungtuan's picture
test tests tests
602df14
Raw History Blame Contribute Delete
2.59 kB
"use client";
import { Suspense, useCallback } from "react";
import { ComponentType } from "@prisma/client";
import { Spinner } from "@heroui/spinner";
import { ComponentConfig } from "@/types/shop";
import { COMPONENT_REGISTRY } from "@/lib/registry/component-registry";
interface ConfigurationPanelProps {
componentType: ComponentType | null;
config: ComponentConfig | null;
onUpdateConfig: (newConfig: ComponentConfig) => void;
}
/**
* Configuration panel for editing shop component properties
* Uses the component registry for dynamic config panel loading
*/
export default function ConfigurationPanel({
componentType,
config,
onUpdateConfig,
}: ConfigurationPanelProps) {
const updateField = useCallback(
(
field: string | number | symbol | Partial<ComponentConfig>,
value?: unknown,
) => {
let newConfig: ComponentConfig;
if (typeof field === "object" && field !== null) {
// Handle partial update
newConfig = { ...config, ...field } as ComponentConfig;
} else {
// Handle single field update
newConfig = { ...config, [field as string]: value } as ComponentConfig;
}
onUpdateConfig(newConfig);
},
[config, onUpdateConfig],
);
if (!componentType || !config) {
return (
<div className="w-80 bg-content1 border-l border-divider p-4">
<h3 className="text-lg font-bold mb-4">cấu hình</h3>
<p className="text-sm text-default-500">
chọn một thành phần để cấu hình
</p>
</div>
);
}
// @ts-ignore - Validated by runtime check or we accept undefined for excluded types
const meta =
COMPONENT_REGISTRY[componentType as keyof typeof COMPONENT_REGISTRY];
if (!meta) {
return (
<div className="w-80 bg-content1 border-l border-divider p-4">
<h3 className="text-lg font-bold mb-4">cấu hình</h3>
<p className="text-sm text-default-500">
Loại thành phần không xác định: {componentType}
</p>
</div>
);
}
const { ConfigPanel } = meta;
return (
<div className="w-80 bg-content1 border-l border-divider p-4 overflow-y-auto">
<h3 className="text-lg font-bold mb-4">cấu hình</h3>
<div className="space-y-4">
<Suspense
fallback={
<div className="flex items-center justify-center py-8">
<Spinner size="sm" />
</div>
}
>
<ConfigPanel config={config} onUpdate={updateField} />
</Suspense>
</div>
</div>
);
}