/**
* The quick modal of a Build row — the ONE shell every section (automation,
* action, scheduler, script, composed device, template) opens on a row click,
* modelled on the device quick-detail: a header with the name and the tags
* edited in place, an enable switch where the kind has one, and the button to
* the full editor; the section's own body sits below as children, with the Exporters and
* Last runs panels as lazy tabs where the kind has them.
*
* The shell only collects the two header values and reports them; the save is
* the section's own (`quick-edit-inputs.ts`), so a rename never drops a step, a
* point, a file or a placement.
*/
import {
Button,
Dialog,
DialogBody,
DialogContent,
DialogHeader,
DialogTitle,
ErrorBox,
Input,
Switch,
TagInput,
Tabs,
TabsContent,
TabsList,
TabsTrigger,
} from '@camstack/ui-library'
import { type ReactNode, useEffect, useRef, useState } from 'react'
import { useTranslation } from 'react-i18next'
import type { QuickEditValues } from './QuickEditDialog'
export interface BuildQuickModalProps {
/** Identifies the item, so switching items resets the header fields. */
readonly subjectKey: string
readonly name: string
readonly tags: readonly string[]
/** Read-only items (an addon's action, a built-in template) show name and tags without inputs. */
readonly readOnly?: boolean
/** The kind's switch; undefined = the kind has none (an action, a template). */
readonly enabled?: boolean
readonly enabledBusy?: boolean
readonly onEnabledChange?: (enabled: boolean) => void
readonly saving: boolean
readonly error: string | null
readonly onSave: (next: QuickEditValues) => void
/** The label of the button to the full editor: "Open automation". */
readonly openLabel: string
readonly onOpen: () => void
readonly onClose: () => void
/**
* The Exporters tab (Alexa / HomeKit / HA): the caller's panel, mounted only
* while that tab is open, so its queries do not fire at modal open. Absent
* when the kind is not exportable (no tab).
*/
readonly exporters?: ReactNode
/** The Last runs tab, mounted only while open for the same reason; absent when the kind keeps no run ledger. */
readonly lastRuns?: ReactNode
readonly children: ReactNode
}
type QuickTabId = 'settings' | 'exporters' | 'runs'
function asQuickTab(value: string): QuickTabId {
return value === 'exporters' || value === 'runs' ? value : 'settings'
}
export function BuildQuickModal(props: BuildQuickModalProps): ReactNode {
return (
)
}
function QuickModalContent({
name,
tags,
readOnly = false,
enabled,
enabledBusy = false,
onEnabledChange,
saving,
error,
onSave,
openLabel,
onOpen,
exporters,
lastRuns,
children,
}: BuildQuickModalProps): ReactNode {
const { t } = useTranslation()
const [tab, setTab] = useState('settings')
const [draft, setDraft] = useState(name)
/** What the stored item holds, or what was last submitted: every save is built
* from it, so a second edit landing before the first answer carries the first. */
const saved = useRef({ name, tags })
const tagsKey = tags.join('\u0000')
// biome-ignore lint/correctness/useExhaustiveDependencies: tagsKey stands for tags, whose identity changes on every parent render
useEffect(() => {
saved.current = { name, tags }
}, [name, tagsKey])
const submit = (next: QuickEditValues): void => {
const last = saved.current
const same =
next.name === last.name &&
next.tags.length === last.tags.length &&
next.tags.every((tag, i) => tag === last.tags[i])
if (same) return
saved.current = next
onSave(next)
}
/** A name left blank is not saved: the field falls back to the stored one. */
const commitName = (): void => {
const next = draft.trim()
if (next === '') {
setDraft(saved.current.name)
return
}
submit({ name: next, tags: saved.current.tags })
}
const commitTags = (next: readonly string[]): void => {
const shown = draft.trim()
submit({ name: shown === '' ? saved.current.name : shown, tags: next })
}
return (
<>
{name}