Files
TREK/client/src/components/Collections/useOriginNotes.ts
T
Maurice 7008427022 refactor(places): drop the unused note chooser from the origin notes
The composer opens the first writable trip or list and its chips switch it,
so the old "+ Note" toggle and its chooser state are read by no view.
2026-11-30 12:34:47 +01:00

147 lines
4.8 KiB
TypeScript

import type { PlacesLibraryDetailResponse } from '@trek/shared';
import { useMemo, useState } from 'react';
import { placesLibraryRepo } from '../../repo/placesLibraryRepo';
import type { TranslationFn } from '../../types';
import { getApiErrorMessage } from '../../utils/apiError';
import { useToast } from '../shared/Toast';
import { originPairs } from './libraryPairs';
import { originName } from './originName';
/** One place's notes as one trip or one list keeps them, and whether the reader may change them. */
export interface OriginNote {
key: string;
kind: 'trip' | 'list';
/** The trip's title or the list's name. */
source: string;
text: string;
canEdit: boolean;
save: (notes: string | null) => Promise<void>;
}
/** Every origin's notes, the trips first, and the empty ones only where the reader could write some. */
export function notesOf(data: PlacesLibraryDetailResponse | null): OriginNote[] {
const { trips, lists } = originPairs(data);
const out: OriginNote[] = [];
for (const { origin, detail } of trips) {
out.push({
key: `trip:${origin.place_id}`,
kind: 'trip',
source: originName(origin),
text: detail.notes ?? '',
canEdit: origin.can_edit,
save: (notes) =>
placesLibraryRepo.saveNotes({ kind: 'trip', trip_id: origin.trip_id, place_id: origin.place_id }, notes),
});
}
for (const { origin, detail } of lists) {
out.push({
key: `list:${origin.collection_place_id}`,
kind: 'list',
source: originName(origin),
text: detail.notes ?? '',
canEdit: origin.can_edit,
save: (notes) =>
placesLibraryRepo.saveNotes({ kind: 'list', collection_place_id: origin.collection_place_id }, notes),
});
}
return out.filter((n) => n.text.trim() || n.canEdit);
}
/**
* The notes of a place of All places (PLC-17), for the desktop and the phone
* sheet: what each trip and each list keeps, apart from the place's description,
* each written where it lives and only where the reader may write. Saving reads
* the place again so the sheet shows what was stored.
*/
export function useOriginNotes({
data,
onSaved,
t,
}: {
data: PlacesLibraryDetailResponse | null;
onSaved: () => void;
t: TranslationFn;
}) {
const toast = useToast();
const all = useMemo(() => notesOf(data), [data]);
const [editing, setEditing] = useState<string | null>(null);
// What shows: the notes there are, and the one being written.
const notes = all.filter((n) => n.text.trim() || n.key === editing);
// Where a new note could go: the trips and lists the reader may write to that have none yet.
const targets = all.filter((n) => n.canEdit && !n.text.trim() && n.key !== editing);
const [draft, setDraft] = useState('');
const [busy, setBusy] = useState(false);
const [preview, setPreview] = useState(false);
/** A new note written for every trip and list that has none yet. */
const [everywhere, setEverywhere] = useState(false);
/** The note being written is a new one: it may still be moved to another trip or list. */
const isNew = !!editing && !all.find((n) => n.key === editing)?.text.trim();
/** Where a new note may go: the open one and every other empty one the reader may write. */
const composeTargets = isNew ? all.filter((n) => n.canEdit && !n.text.trim()) : [];
const edit = (note: OriginNote) => {
setEditing(note.key);
setDraft(note.text);
setPreview(false);
setEverywhere(false);
};
const cancel = () => {
setEditing(null);
setPreview(false);
setEverywhere(false);
};
/** The new note goes to another trip or list; what is written so far stays. */
const retarget = (key: string | 'all') => {
if (key === 'all') {
setEverywhere(true);
return;
}
if (!all.some((n) => n.key === key && n.canEdit && !n.text.trim())) return;
setEverywhere(false);
setEditing(key);
};
/** The composer: straight into writing, for the first place a note can go (the chips switch it). */
const compose = () => {
if (targets[0]) edit(targets[0]);
};
const save = async () => {
const note = all.find((n) => n.key === editing);
if (!note || busy) return;
setBusy(true);
try {
const text = draft.trim() ? draft : null;
// Everywhere: the same note in each trip and list that had none, one after another.
const into = everywhere && isNew ? composeTargets : [note];
for (const target of into) await target.save(text);
setEditing(null);
setPreview(false);
onSaved();
} catch (err) {
toast.error(getApiErrorMessage(err, t('common.error')));
} finally {
setBusy(false);
}
};
return {
notes,
targets,
compose,
editing,
isNew,
composeTargets,
everywhere,
retarget,
draft,
setDraft,
preview,
setPreview,
busy,
edit,
cancel,
save,
};
}