mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-23 21:33:19 +00:00
fix(native-share): read every trip's Collaboration when the share sheet opens and keep it settled
A Collaboration switch on a trip not open on this device now counts for a shared link, and a later reload of a trip's list no longer hides the trips or saves a refused link again.
This commit is contained in:
@@ -182,13 +182,37 @@ describe('NativeImportSheet', () => {
|
||||
expect(useSharedImportStore.getState().tripId).toBeNull();
|
||||
});
|
||||
|
||||
it('reads the Collab of a trip only while the app does not hold it already', async () => {
|
||||
it('reads every trip’s Collab again when it opens, so a switch this device missed counts', async () => {
|
||||
useAddonStore.setState({ addons: [], loaded: true });
|
||||
// The app still holds trip 2 as following the instance; its owner has since switched Collab on.
|
||||
useTripAddonsStore.getState().publish(2, []);
|
||||
collabOnlyFor2(true);
|
||||
render(<NativeImportSheet />);
|
||||
act(() => useSharedImportStore.getState().setFile(page(), link()));
|
||||
expect(await screen.findByText('What should TREK do with this link?')).toBeInTheDocument();
|
||||
expect(tripAddonsRepo.list).toHaveBeenCalledTimes(1);
|
||||
act(() => useSharedImportStore.getState().setFile(bareLink(), link()));
|
||||
expect(await screen.findByRole('button', { name: /Japan/ })).toBeInTheDocument();
|
||||
expect(tripAddonsRepo.list).toHaveBeenCalledWith(1);
|
||||
expect(tripAddonsRepo.list).toHaveBeenCalledWith(2);
|
||||
});
|
||||
|
||||
it('keeps the trips and does not save again when a list reloads after a refused link', async () => {
|
||||
vi.mocked(saveSharedLink).mockRejectedValue(new Error('offline'));
|
||||
render(<NativeImportSheet />);
|
||||
act(() => useSharedImportStore.getState().setFile(bareLink(), link(2)));
|
||||
expect(await screen.findByRole('button', { name: /Japan/ })).toBeInTheDocument();
|
||||
expect(saveSharedLink).toHaveBeenCalledTimes(1);
|
||||
|
||||
let answer: () => void = () => {};
|
||||
vi.spyOn(tripAddonsRepo, 'list').mockImplementation(
|
||||
() =>
|
||||
new Promise((resolve) => {
|
||||
answer = () => resolve([]);
|
||||
})
|
||||
);
|
||||
act(() => void useTripAddonsStore.getState().load(1));
|
||||
expect(screen.getByRole('button', { name: /Japan/ })).toBeInTheDocument();
|
||||
await act(async () => answer());
|
||||
expect(screen.getByRole('button', { name: /Japan/ })).toBeInTheDocument();
|
||||
expect(saveSharedLink).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it('stays closed with nothing shared', () => {
|
||||
|
||||
@@ -100,30 +100,39 @@ function useTripRows(open: boolean): { trips: TripRow[]; loaded: boolean } {
|
||||
return rows;
|
||||
}
|
||||
|
||||
const NOTHING_READ: ReadonlySet<number> = new Set();
|
||||
|
||||
/**
|
||||
* The trips a shared link may go to: those with Collaboration on, each by its
|
||||
* own list (the owner's choice, else the instance switch). The lists the app
|
||||
* does not hold yet are read once the sheet opens for a link, so a trip that
|
||||
* switched Collaboration on is offered while the instance has it off, and one
|
||||
* that switched it off is not. `settled` once every trip's list is read or
|
||||
* could not be.
|
||||
* own list (the owner's choice, else the instance switch). Every trip's list is
|
||||
* read again once the sheet opens for a link, since a change made on a trip that
|
||||
* is not open here never reaches this device, so a trip that switched
|
||||
* Collaboration on is offered while the instance has it off, and one that
|
||||
* switched it off is not. `settled` once each of those reads answered or failed;
|
||||
* a later reload, a sync say, keeps the list held meanwhile and settles nothing again.
|
||||
*/
|
||||
function useCollabTrips(trips: TripRow[], active: boolean): { collabTrips: TripRow[]; settled: boolean } {
|
||||
const instanceOn = useAddonStore((s) => s.isEnabled('collab'));
|
||||
const load = useTripAddonsStore((s) => s.load);
|
||||
const [read, setRead] = useState(NOTHING_READ);
|
||||
useEffect(() => {
|
||||
if (!active) return;
|
||||
const held = useTripAddonsStore.getState();
|
||||
let live = true;
|
||||
trips.forEach((trip) => {
|
||||
if (tripAddonsEntry(held, trip.id)?.status !== 'ready') void load(trip.id);
|
||||
void load(trip.id).then(() => {
|
||||
if (live) setRead((done) => new Set(done).add(trip.id));
|
||||
});
|
||||
});
|
||||
return () => {
|
||||
live = false;
|
||||
setRead(NOTHING_READ);
|
||||
};
|
||||
}, [active, trips, load]);
|
||||
const states = useTripAddonsStore(
|
||||
useShallow((state) =>
|
||||
trips.map((trip) => {
|
||||
const entry = tripAddonsEntry(state, trip.id);
|
||||
if (!entry || entry.status === 'loading') return 'waiting';
|
||||
return tripAddonEnabled(entry.addons, 'collab', instanceOn) ? 'on' : 'off';
|
||||
if (!read.has(trip.id)) return 'waiting';
|
||||
return tripAddonEnabled(tripAddonsEntry(state, trip.id)?.addons, 'collab', instanceOn) ? 'on' : 'off';
|
||||
})
|
||||
)
|
||||
);
|
||||
@@ -193,7 +202,7 @@ export default function NativeImportSheet(): React.ReactElement | null {
|
||||
const preset = link?.presetTripId ?? null;
|
||||
const applyPreset = useEffectEvent((id: number, use: LinkChoice) => {
|
||||
if (use === 'booking') chooseTrip(id);
|
||||
else saveLink(id);
|
||||
else if (failedFile !== file) saveLink(id);
|
||||
});
|
||||
useEffect(() => {
|
||||
if (preset != null && choice != null && file) applyPreset(preset, choice);
|
||||
|
||||
Reference in New Issue
Block a user