"use client"; import { useSafeIntl } from "@kopexa/i18n"; import { EditIcon } from "@kopexa/icons"; import { Button, Card, Heading } from "@kopexa/sight"; import type { CardVariantProps } from "@kopexa/theme"; import { Editor, type JSONContent } from "@kopexa/tiptap"; import { useState } from "react"; import { messages } from "./messages"; // ============================================ // Types // ============================================ export interface EditorCardProps { /** The content as TipTap JSONContent */ value?: JSONContent; /** Callback when the content changes */ onChange?: (content: JSONContent | undefined) => void; /** Title for the card header */ title: string; /** Placeholder text for the editor */ placeholder?: string; /** Text to show when there's no content in view mode */ emptyText?: string; /** Make the component read-only */ readOnly?: boolean; /** Card variant */ cardVariant?: "default" | "accent"; /** Props forwarded to the underlying `Card.Root` (e.g. `spacing`, `shadow`, `radius`). `cardVariant` still takes precedence for `variant`. */ cardProps?: CardVariantProps; /** Size of the edit icon button. */ editButtonSize?: "sm" | "md" | "lg"; } export function EditorCard({ value, onChange, title, placeholder, emptyText, readOnly = false, cardVariant = "accent", cardProps, editButtonSize = "sm", }: EditorCardProps) { const intl = useSafeIntl(); const [isEditing, setIsEditing] = useState(false); const [draftContent, setDraftContent] = useState( value, ); // i18n labels const t = { edit: intl.formatMessage(messages.edit), cancel: intl.formatMessage(messages.cancel), save: intl.formatMessage(messages.save), placeholder: placeholder ?? intl.formatMessage(messages.placeholder), empty: emptyText ?? intl.formatMessage(messages.empty), }; const handleStartEdit = () => { setDraftContent(value); setIsEditing(true); }; const handleSave = () => { onChange?.(draftContent); setIsEditing(false); }; const handleCancel = () => { setDraftContent(value); setIsEditing(false); }; return ( {title} {!readOnly && (isEditing ? (
) : ( ))}
{isEditing ? ( ) : value ? ( ) : (

{t.empty}

)}
); }