mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-26 22:57:32 +00:00
9956c58dd5
- The trip's addon list is read through tripAddonsRepo into tripAddonsStore (per account and trip, kept in Dexie version 16 for offline), refreshed when the connection returns and live on tripAddons:changed. - The planner's tabs, dock, rail and deep links, the cost, tour and list readers, the PDF export, the share link options, the widgets and the watch follow the trip instead of the instance switch. - Trip settings gets an Addons area for the owner, on the desktop dialog and the phone sheet, on one useTripAddonRows hook: each trip addon with its switch, marked "This trip only" while it differs from the instance. Switching back to the instance's value drops the trip's choice. Switching needs a connection.
87 lines
4.1 KiB
TypeScript
87 lines
4.1 KiB
TypeScript
import type { CollectionTourItem } from '@trek/shared';
|
|
import { useEffect, useState } from 'react';
|
|
import { useShallow } from 'zustand/react/shallow';
|
|
import { useTranslation } from '../../../i18n';
|
|
import { OnlineOnlyError } from '../../../repo/onlineOnly';
|
|
import { useAddonStore } from '../../../store/addonStore';
|
|
import { useAuthStore } from '../../../store/authStore';
|
|
import { useCollectionStore } from '../../../store/collectionStore';
|
|
import { useCollectionTourStore, watchCollectionTours } from '../../../store/collectionTourStore';
|
|
import { canRemoveFromCollection, collectionRoleOf } from '../../../utils/collectionAccess';
|
|
import { useToast } from '../../shared/Toast';
|
|
|
|
/**
|
|
* LIB-03: the tours a collection keeps, for the collection's page. Read when
|
|
* the collection opens and kept current while its members keep or remove one;
|
|
* nothing at all while the Tours addon is off. Tours counts as on while the
|
|
* instance has it on or any of the user's trips switched it on for itself;
|
|
* only the server knows the second, so with the instance off the tours are
|
|
* read all the same and its refusal hides them. Removing asks first and says
|
|
* how it went.
|
|
*/
|
|
export function useCollectionTours(collectionId: number) {
|
|
const { t } = useTranslation();
|
|
const toast = useToast();
|
|
const instanceOn = useAddonStore((state) => state.isEnabled('tours'));
|
|
const { tours, status, shownFor } = useCollectionTourStore(
|
|
useShallow((state) => ({ tours: state.tours, status: state.status, shownFor: state.collectionId }))
|
|
);
|
|
const [removing, setRemoving] = useState<CollectionTourItem | null>(null);
|
|
|
|
// Read again when the instance switch flips, since the answer may change with it.
|
|
useEffect(() => {
|
|
void useCollectionTourStore.getState().load(collectionId);
|
|
return watchCollectionTours();
|
|
}, [collectionId, instanceOn]);
|
|
|
|
const confirmRemove = async () => {
|
|
const tour = removing;
|
|
setRemoving(null);
|
|
if (!tour) return;
|
|
try {
|
|
await useCollectionTourStore.getState().remove(collectionId, tour.id);
|
|
toast.success(t('tours.collection.removed'));
|
|
} catch (err: unknown) {
|
|
toast.error(t(err instanceof OnlineOnlyError ? 'tours.collection.offline' : 'tours.collection.removeFailed'));
|
|
}
|
|
};
|
|
|
|
const current = shownFor === collectionId;
|
|
// With the instance on nothing changes; with it off only tours the server handed over show,
|
|
// so an install without a trip of its own choice shows nothing, as it always did.
|
|
const enabled = instanceOn ? !(current && status === 'off') : current && status === 'ready';
|
|
return {
|
|
enabled,
|
|
tours: enabled && current ? tours : [],
|
|
status: enabled && current ? status : 'idle',
|
|
retry: () => void useCollectionTourStore.getState().load(collectionId),
|
|
removing,
|
|
askRemove: setRemoving,
|
|
cancelRemove: () => setRemoving(null),
|
|
confirmRemove,
|
|
};
|
|
}
|
|
|
|
export type CollectionTours = ReturnType<typeof useCollectionTours>;
|
|
|
|
/** The kept tours whose name holds the list's search, all of them without one. */
|
|
export function toursMatching(tours: readonly CollectionTourItem[], search: string): CollectionTourItem[] {
|
|
const query = search.trim().toLocaleLowerCase();
|
|
return query ? tours.filter((tour) => tour.name.toLocaleLowerCase().includes(query)) : [...tours];
|
|
}
|
|
|
|
/**
|
|
* The list open on the collections page and whether this user may remove its
|
|
* tours (its owner or an admin of it), read from the list itself; no list on
|
|
* "All saved" or before one opens. Desktop and phone both ask here.
|
|
*/
|
|
export function useActiveCollectionTourList(): { collectionId: number | null; canRemove: boolean } {
|
|
const { activeId, collections, members } = useCollectionStore(
|
|
useShallow((state) => ({ activeId: state.activeId, collections: state.collections, members: state.members }))
|
|
);
|
|
const userId = useAuthStore((state) => state.user?.id);
|
|
if (typeof activeId !== 'number') return { collectionId: null, canRemove: false };
|
|
const isOwner = collections.find((collection) => collection.id === activeId)?.is_owner ?? false;
|
|
return { collectionId: activeId, canRemove: canRemoveFromCollection(collectionRoleOf(isOwner, members, userId)) };
|
|
}
|