mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-30 16:33:16 +00:00
feat(places): notes, visits, tours, history, tags and location in the place sheet, saved lists on the trip map
Both the desktop sheet and the phone sheet of All places show the place's location as a badge, its notes per list, its visits from trips (hidden without any), linked tours and bookings, its change history and the reader's own tags with a tag filter. A trip map can show the places of linked lists and add one to the trip. All places reloads after a list is added or removed.
This commit is contained in:
@@ -0,0 +1,105 @@
|
||||
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);
|
||||
const [choosing, setChoosing] = useState(false);
|
||||
// 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 edit = (note: OriginNote) => {
|
||||
setChoosing(false);
|
||||
setEditing(note.key);
|
||||
setDraft(note.text);
|
||||
};
|
||||
const cancel = () => setEditing(null);
|
||||
/** "+ Note": straight into the one place it can go, or ask where. */
|
||||
const add = () => {
|
||||
if (targets.length === 1) edit(targets[0]);
|
||||
else setChoosing((c) => !c);
|
||||
};
|
||||
|
||||
const save = async () => {
|
||||
const note = all.find((n) => n.key === editing);
|
||||
if (!note || busy) return;
|
||||
setBusy(true);
|
||||
try {
|
||||
await note.save(draft.trim() ? draft : null);
|
||||
setEditing(null);
|
||||
onSaved();
|
||||
} catch (err) {
|
||||
toast.error(getApiErrorMessage(err, t('common.error')));
|
||||
} finally {
|
||||
setBusy(false);
|
||||
}
|
||||
};
|
||||
|
||||
return { notes, targets, choosing, add, editing, draft, setDraft, busy, edit, cancel, save };
|
||||
}
|
||||
Reference in New Issue
Block a user