Files
TREK/client/src/components/Assistant/AssistantPlanFields.tsx
T
Maurice 7196ad30da fix(ci): repair trip assistant checks
Fix the mobile dashboard import and align the assistant UI with client layer and lint rules. Document the OpenRouter setting, share the reservation status schema, and update coverage for external POI identifiers and the empty costs view.
2026-12-03 12:06:46 +01:00

147 lines
4.9 KiB
TypeScript

import type { AssistantChangePreview, AssistantPlanEdits } from '@trek/shared';
import { Check, Pencil } from 'lucide-react';
import { lazy, Suspense, useEffect, useId, useState } from 'react';
import { useTranslation } from '../../i18n';
import { useAssistantStore } from '../../store/assistantStore';
import { EditorField, PANEL, TEXTAREA } from '../shared/dialogParts';
import {
DialogButton,
DialogFooter,
DialogHeader,
DialogShell,
DialogTile,
FooterSpacer,
NEUTRAL_TINT,
} from '../shared/DialogShell';
import { Tooltip } from '../shared/Tooltip';
import { isVisitProposal, planField, proposalNote } from './assistantPlanFieldsModel';
import { useAssistantView } from './assistantViewContext';
const AssistantPlaceProposal = lazy(() => import('./AssistantPlaceProposal'));
interface ProposalEditProps {
preview: AssistantChangePreview;
args?: unknown;
edits: AssistantPlanEdits | undefined;
disabled: boolean;
onEdit: (patch: AssistantPlanEdits) => void;
}
export default function AssistantPlanFields({ preview, args, edits, disabled, onEdit }: ProposalEditProps) {
const { t } = useTranslation();
const [open, setOpen] = useState(false);
return (
<>
<Tooltip label={t('common.edit')} disabled={disabled}>
<button
type="button"
aria-label={t('common.edit')}
aria-haspopup="dialog"
disabled={disabled}
onClick={() => setOpen(true)}
className="relative grid h-8 w-8 shrink-0 place-items-center self-start rounded-lg text-content-muted transition-colors focus-visible:ring-2 focus-visible:ring-accent enabled:hover:bg-accent enabled:hover:text-accent-text disabled:opacity-50 [@media(pointer:coarse)]:before:absolute [@media(pointer:coarse)]:before:-inset-1.5"
>
<Pencil size={14} aria-hidden="true" />
</button>
</Tooltip>
{open &&
(isVisitProposal(preview) ? (
<Suspense fallback={null}>
<AssistantPlaceProposal
preview={preview}
args={args}
edits={edits}
disabled={disabled}
onEdit={onEdit}
onClose={() => setOpen(false)}
/>
</Suspense>
) : (
<DayNoteEditDialog
preview={preview}
edits={edits}
disabled={disabled}
onEdit={onEdit}
onClose={() => setOpen(false)}
/>
))}
</>
);
}
function DayNoteEditDialog({ preview, edits, disabled, onEdit, onClose }: ProposalEditProps & { onClose: () => void }) {
const { t } = useTranslation();
const { tripId } = useAssistantView();
const { toolCallId } = preview;
const previewPlanEdit = useAssistantStore((s) => s.previewPlanEdit);
const id = useId();
const [draft, setDraft] = useState<AssistantPlanEdits>(() => edits ?? {});
const change = (patch: AssistantPlanEdits) => setDraft((current) => ({ ...current, ...patch }));
const note = proposalNote(preview, draft);
const noteTitle = draft.noteTitle ?? planField(preview, 'title') ?? preview.subject;
useEffect(() => previewPlanEdit(tripId, toolCallId, draft), [draft, previewPlanEdit, toolCallId, tripId]);
useEffect(() => () => previewPlanEdit(tripId, toolCallId, null), [previewPlanEdit, toolCallId, tripId]);
const noteLabel = t('assistant.change.label.content');
const save = () => {
if (Object.keys(draft).length > 0) onEdit(draft);
onClose();
};
return (
<DialogShell
labelledBy={id}
width="narrow"
onClose={onClose}
header={
<DialogHeader
labelId={id}
tint={NEUTRAL_TINT}
tile={
<DialogTile>
<Pencil size={19} aria-hidden="true" />
</DialogTile>
}
eyebrow={t('common.edit')}
titleInput={{
label: t('assistant.change.label.title'),
value: noteTitle,
required: true,
maxLength: 500,
readOnly: disabled,
onChange: (noteTitle) => change({ noteTitle }),
}}
onClose={onClose}
/>
}
footer={
<DialogFooter>
<FooterSpacer />
<DialogButton onClick={onClose}>{t('common.cancel')}</DialogButton>
<DialogButton
variant="primary"
icon={<Check size={15} aria-hidden="true" />}
onClick={save}
disabled={disabled || !noteTitle.trim()}
>
{t('common.save')}
</DialogButton>
</DialogFooter>
}
>
<fieldset disabled={disabled} className={`${PANEL} m-0 min-w-0`}>
<EditorField label={noteLabel} htmlFor={`${id}-note`}>
<textarea
id={`${id}-note`}
className={TEXTAREA}
rows={4}
maxLength={2000}
value={note ?? ''}
onChange={(e) => change({ noteBody: e.target.value || null })}
/>
</EditorField>
</fieldset>
</DialogShell>
);
}