mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-12-02 01:12:16 +00:00
e539860530
The Sonar quality gate rated new code D. Preserve focus and keyboard interaction through native controls and shared dialog behavior. Replace ambiguous regex scans with bounded parsing and make numeric coercion and initialization explicit.
183 lines
7.4 KiB
TypeScript
183 lines
7.4 KiB
TypeScript
import type { PlacesLibraryDetailResponse } from '@trek/shared';
|
|
import { Bookmark, Eye, Layers, Luggage, NotebookPen, Pencil, PenLine } from 'lucide-react';
|
|
import Markdown from 'react-markdown';
|
|
import remarkBreaks from 'remark-breaks';
|
|
import remarkGfm from 'remark-gfm';
|
|
import { useOriginNotes } from '../../../components/Collections/useOriginNotes';
|
|
import { focusRevealedEditor } from '../../../components/shared/dialogFocus';
|
|
import { markdownLinkComponents } from '../../../components/shared/markdownLink';
|
|
import type { TranslationFn } from '../../../types';
|
|
import { CancelPill, PrimaryPill } from './MCollSheetKit';
|
|
import MPlaceCard from './MPlaceCard';
|
|
|
|
type Notes = ReturnType<typeof useOriginNotes>;
|
|
|
|
const SourceIcon = ({ kind }: { kind: 'trip' | 'list' }) =>
|
|
kind === 'trip' ? <Luggage size={12} /> : <Bookmark size={12} />;
|
|
|
|
const CHIP = 'inline-flex h-[30px] items-center gap-[5px] rounded-full px-3 text-caption font-semibold';
|
|
const CHIP_ON = `${CHIP} border border-m-act bg-m-ic text-m-ink`;
|
|
const CHIP_OFF = `${CHIP} border border-m-rowbr bg-m-ic text-m-ink`;
|
|
|
|
function Rendered({ text }: { text: string }) {
|
|
return (
|
|
<div className="collab-note-md text-body leading-[1.55] text-m-ink [overflow-wrap:anywhere]">
|
|
<Markdown remarkPlugins={[remarkGfm, remarkBreaks]} components={markdownLinkComponents}>
|
|
{text}
|
|
</Markdown>
|
|
</div>
|
|
);
|
|
}
|
|
|
|
/** Writing a note on the phone: where it goes, write or preview, save. */
|
|
function MNoteEditor({ n, t }: { n: Notes; t: TranslationFn }) {
|
|
const current = n.composeTargets.find((x) => x.key === n.editing) ?? n.notes.find((x) => x.key === n.editing);
|
|
const multi = n.isNew && n.composeTargets.length > 1;
|
|
return (
|
|
<>
|
|
{multi && (
|
|
<div className="flex flex-wrap items-center gap-[6px] rounded-[14px] border border-m-rowbr bg-m-inner py-[6px] pe-[6px] ps-3">
|
|
<span className="me-1 text-caption font-semibold text-m-faint">{t('placeNotes.for')}</span>
|
|
<button
|
|
type="button"
|
|
aria-pressed={n.everywhere}
|
|
onClick={() => n.retarget('all')}
|
|
className={n.everywhere ? CHIP_ON : CHIP_OFF}
|
|
>
|
|
<Layers size={12} /> {t('placeMedia.everywhere')}
|
|
</button>
|
|
{n.composeTargets.map((x) => {
|
|
const on = !n.everywhere && x.key === n.editing;
|
|
return (
|
|
<button
|
|
key={x.key}
|
|
type="button"
|
|
aria-pressed={on}
|
|
onClick={() => n.retarget(x.key)}
|
|
className={on ? CHIP_ON : CHIP_OFF}
|
|
>
|
|
<SourceIcon kind={x.kind} /> {x.source}
|
|
</button>
|
|
);
|
|
})}
|
|
</div>
|
|
)}
|
|
<div className="flex flex-col gap-2 rounded-[14px] border border-m-rowbr bg-m-inner p-3">
|
|
{!multi && current && (
|
|
<div className="flex items-center gap-[6px] text-caption font-semibold text-m-faint">
|
|
<SourceIcon kind={current.kind} />
|
|
<span className="min-w-0 flex-1 truncate">{current.source}</span>
|
|
</div>
|
|
)}
|
|
<div className="flex gap-1 self-start rounded-full border border-m-rowbr bg-m-ic p-[3px]">
|
|
{[false, true].map((preview) => (
|
|
<button
|
|
key={String(preview)}
|
|
type="button"
|
|
aria-pressed={n.preview === preview}
|
|
onClick={() => n.setPreview(preview)}
|
|
className={
|
|
n.preview === preview
|
|
? 'inline-flex items-center gap-1 rounded-full bg-m-sheet px-3 py-[5px] text-caption font-bold text-m-ink'
|
|
: 'inline-flex items-center gap-1 rounded-full px-3 py-[5px] text-caption font-semibold text-m-muted'
|
|
}
|
|
>
|
|
{preview ? <Eye size={12} /> : <PenLine size={12} />}
|
|
{t(preview ? 'placeNotes.preview' : 'placeNotes.write')}
|
|
</button>
|
|
))}
|
|
</div>
|
|
{n.preview ? (
|
|
<div className="min-h-[120px] rounded-[12px] border border-m-rowbr bg-m-sheet px-3 py-[10px]">
|
|
{n.draft.trim() ? (
|
|
<Rendered text={n.draft} />
|
|
) : (
|
|
<span className="text-caption text-m-muted">{t('placeNotes.nothingYet')}</span>
|
|
)}
|
|
</div>
|
|
) : (
|
|
<textarea
|
|
ref={focusRevealedEditor}
|
|
rows={6}
|
|
value={n.draft}
|
|
onChange={(e) => n.setDraft(e.target.value)}
|
|
placeholder={t('placeNotes.placeholder')}
|
|
aria-label={current ? t('placeNotes.fieldLabel', { source: current.source }) : t('collections.notes')}
|
|
className="min-h-[120px] w-full resize-none rounded-[12px] border border-m-rowbr bg-m-sheet px-3 py-[10px] text-body leading-[1.55] text-m-ink outline-none placeholder:text-m-faint"
|
|
/>
|
|
)}
|
|
<span className="text-caption text-m-faint">{t('placeNotes.markdownHint')}</span>
|
|
<div className="flex gap-2">
|
|
<CancelPill onClick={n.cancel} className="flex-1">
|
|
{t('common.cancel')}
|
|
</CancelPill>
|
|
<PrimaryPill onClick={() => void n.save()} disabled={n.busy} className="flex-1">
|
|
{t('common.save')}
|
|
</PrimaryPill>
|
|
</div>
|
|
</div>
|
|
</>
|
|
);
|
|
}
|
|
|
|
/** The notes of a place on the phone (PLC-17), the desktop view's twin: rendered notes, a field to write one, the editor. */
|
|
export function MOriginNotesView({ state: n, t }: { state: Notes; t: TranslationFn }) {
|
|
if (n.notes.length === 0 && n.targets.length === 0 && !n.editing) return null;
|
|
return (
|
|
<MPlaceCard
|
|
icon={<NotebookPen size={14} />}
|
|
title={t('collections.notes')}
|
|
count={n.notes.filter((x) => x.text.trim()).length}
|
|
>
|
|
{n.notes.map((note) =>
|
|
n.editing === note.key && !n.isNew ? (
|
|
<MNoteEditor key={note.key} n={n} t={t} />
|
|
) : (
|
|
note.text.trim() && (
|
|
<article key={note.key} className="flex flex-col gap-1 rounded-[14px] border border-m-inbr bg-m-inner p-3">
|
|
<div className="flex items-center gap-[6px] text-caption font-semibold text-m-faint">
|
|
<SourceIcon kind={note.kind} />
|
|
<span className="min-w-0 flex-1 truncate">{note.source}</span>
|
|
{note.canEdit && (
|
|
<button
|
|
type="button"
|
|
onClick={() => n.edit(note)}
|
|
aria-label={t('placeNotes.edit')}
|
|
className="flex h-7 w-7 items-center justify-center rounded-full bg-m-ic text-m-ink"
|
|
>
|
|
<Pencil size={13} />
|
|
</button>
|
|
)}
|
|
</div>
|
|
<Rendered text={note.text} />
|
|
</article>
|
|
)
|
|
)
|
|
)}
|
|
{n.isNew && <MNoteEditor n={n} t={t} />}
|
|
{!n.editing && n.targets.length > 0 && (
|
|
<button
|
|
type="button"
|
|
onClick={n.compose}
|
|
className="flex w-full items-center gap-[10px] rounded-[14px] border border-m-rowbr bg-m-inner px-[14px] py-3 text-start text-body text-m-muted"
|
|
>
|
|
<PenLine size={15} /> {t('placeNotes.composer')}
|
|
</button>
|
|
)}
|
|
</MPlaceCard>
|
|
);
|
|
}
|
|
|
|
export default function MOriginNotesSection({
|
|
data,
|
|
onSaved,
|
|
t,
|
|
}: {
|
|
data: PlacesLibraryDetailResponse | null;
|
|
onSaved: () => void;
|
|
t: TranslationFn;
|
|
}) {
|
|
const n = useOriginNotes({ data, onSaved, t });
|
|
return <MOriginNotesView state={n} t={t} />;
|
|
}
|