mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-12-03 17:41:51 +00:00
172 lines
5.9 KiB
TypeScript
172 lines
5.9 KiB
TypeScript
import type { AssistantChangeLabelKey, 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 { useAssistantView } from './assistantViewContext';
|
|
|
|
const AssistantPlaceProposal = lazy(() => import('./AssistantPlaceProposal'));
|
|
|
|
export function isVisitProposal(preview: AssistantChangePreview | null | undefined) {
|
|
return preview?.verbKey === 'addPlaceToDay' || preview?.verbKey === 'assignPlace';
|
|
}
|
|
|
|
export function isDayNoteProposal(preview: AssistantChangePreview | null | undefined) {
|
|
return preview?.verbKey === 'addDayNote' || preview?.verbKey === 'updateDayNote';
|
|
}
|
|
|
|
export function planField(preview: AssistantChangePreview, field: AssistantChangeLabelKey) {
|
|
return preview.rows.find((row) => row.labelKey === field)?.after;
|
|
}
|
|
|
|
export function dayNoteBody(preview: AssistantChangePreview) {
|
|
return planField(preview, 'content') ?? (preview.verbKey === 'addDayNote' ? planField(preview, 'time') : undefined);
|
|
}
|
|
|
|
export function proposalNote(preview: AssistantChangePreview, edits: AssistantPlanEdits | undefined) {
|
|
return isVisitProposal(preview)
|
|
? edits?.notes === undefined
|
|
? planField(preview, 'notes')
|
|
: edits.notes
|
|
: edits?.noteBody === undefined
|
|
? dayNoteBody(preview)
|
|
: edits.noteBody;
|
|
}
|
|
|
|
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>
|
|
);
|
|
}
|