mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-22 03:50:30 +00:00
feat(places): colour tags, tabs for the texts and opening hours on the phone
Tags on the place form are pills in their own colour, with a palette under the dot that recolours a tag for the whole trip (PUT /trips/:id/tags/:id was there already, the client now reaches it through repo and store) and a custom colour field of its own instead of the system picker. A new tag is made in a colour picked the same way. Description, notes and the note for one day's visit sit in one card under a switch, each with a line on where the text shows up; the day tab is named after its day and is only there with one visit in context. Nothing about the three fields changes underneath. Opening hours get a switch for open around the clock next to closed. Google writes a place open all week as one period on Sunday that never closes; it read as Sunday open and every other day closed, and now opens every day. The phone form follows the desktop: category as one field with a list, tags in one field, the texts switch, and phone, e-mail and opening hours, which it did not have. The hours and the text tabs are shared hooks (usePlaceHours, usePlaceTextTabs). Files and costs on the phone sheets are drawn as fields like the rest, and the paste hint, a keyboard shortcut a phone has no use for, is gone with its key.
This commit is contained in:
@@ -0,0 +1,86 @@
|
||||
import type { PlaceClientOrigin, PlaceOpeningHours } from '@trek/shared';
|
||||
import { useEffect, useRef, useState } from 'react';
|
||||
|
||||
import { ALL_DAY, cleanWeek, isAllDay, readWeek, writeWeek } from './placeHours';
|
||||
|
||||
interface PlaceHoursOptions {
|
||||
/** The stored hours text (JSON), empty when the place has none of its own. */
|
||||
openingHours: string;
|
||||
/** `origin` is set when the hours were taken over from the details lookup rather than typed. */
|
||||
onChange: (value: string, origin?: PlaceClientOrigin) => void;
|
||||
/**
|
||||
* The hours the place details looked up, as a week. A place without hours of
|
||||
* its own takes them over on its own; one that has some can take them over
|
||||
* on request (takeOver).
|
||||
*/
|
||||
suggestedHours?: PlaceOpeningHours | null;
|
||||
}
|
||||
|
||||
/**
|
||||
* The opening hours of the place form (#2472): the one logic path behind the
|
||||
* desktop dialog and the phone sheet, which draw their own rows over it. It
|
||||
* keeps the editor's own draft, so a half-typed time ("09:3") stays on screen
|
||||
* while only finished times reach the place, folds the week away until someone
|
||||
* wants it, and switches a day closed or open around the clock.
|
||||
*/
|
||||
export function usePlaceHours({ openingHours, onChange, suggestedHours }: PlaceHoursOptions) {
|
||||
const [week, setDraft] = useState(() => readWeek(openingHours));
|
||||
const written = useRef(openingHours);
|
||||
const [editing, setEditing] = useState(() => openingHours !== '');
|
||||
|
||||
// A value from outside (a picked search result, a reopened place) replaces the draft.
|
||||
useEffect(() => {
|
||||
if (openingHours === written.current) return;
|
||||
written.current = openingHours;
|
||||
setDraft(readWeek(openingHours));
|
||||
if (openingHours) setEditing(true);
|
||||
}, [openingHours]);
|
||||
|
||||
const setWeek = (next: PlaceOpeningHours, origin?: PlaceClientOrigin) => {
|
||||
setDraft(next);
|
||||
const text = writeWeek(cleanWeek(next));
|
||||
written.current = text;
|
||||
onChange(text, origin);
|
||||
};
|
||||
const setDay = (i: number, patch: Partial<PlaceOpeningHours[number]>) =>
|
||||
setWeek(week.map((day, d) => (d === i ? { ...day, ...patch } : day)));
|
||||
const toggleClosed = (i: number) =>
|
||||
setDay(i, week[i].closed ? { closed: false } : { closed: true, open: undefined, close: undefined });
|
||||
const toggleAllDay = (i: number) =>
|
||||
setDay(i, isAllDay(week[i]) ? { open: undefined, close: undefined } : { closed: false, ...ALL_DAY });
|
||||
const copyFirstToAll = () => setWeek(week.map(() => ({ ...week[0] })));
|
||||
const clearHours = () => {
|
||||
setWeek(readWeek(''));
|
||||
setEditing(false);
|
||||
};
|
||||
|
||||
// Looked-up hours are taken over by a place that has none yet, once per lookup.
|
||||
const adopted = useRef<PlaceOpeningHours | null>(null);
|
||||
useEffect(() => {
|
||||
if (!suggestedHours || adopted.current === suggestedHours) return;
|
||||
adopted.current = suggestedHours;
|
||||
if (written.current) return;
|
||||
setWeek(suggestedHours, 'enrichment');
|
||||
setEditing(true);
|
||||
// setWeek only reads refs and setters; the lookup is the trigger.
|
||||
}, [suggestedHours]);
|
||||
const canTakeOver = !!suggestedHours && writeWeek(suggestedHours) !== writeWeek(cleanWeek(week));
|
||||
const takeOver = () => {
|
||||
if (suggestedHours) setWeek(suggestedHours, 'enrichment');
|
||||
};
|
||||
|
||||
return {
|
||||
week,
|
||||
editing,
|
||||
setEditing,
|
||||
setDay,
|
||||
toggleClosed,
|
||||
toggleAllDay,
|
||||
copyFirstToAll,
|
||||
clearHours,
|
||||
canTakeOver,
|
||||
takeOver,
|
||||
};
|
||||
}
|
||||
|
||||
export type PlaceHoursField = ReturnType<typeof usePlaceHours>;
|
||||
Reference in New Issue
Block a user