mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-27 15:17:30 +00:00
fix(tours): open a tour's packing list on Packing, and ask before letting go of it while Packing is off
"Open packing list" switched to the Lists tab but left it on whichever sub-tab was open last, so someone who had been in the to-dos landed there. It now picks the packing side first, and a Lists tab already on screen follows the choice; storage that is blocked no longer throws either. With the Packing addon off, "Remove link" let go of the link on one click, and the link cannot be made again until Packing is back on. It now opens the tour's packing dialog with only the dimmed link and the Remove link and Cancel buttons, so nothing changes without a second, deliberate click.
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
import { act, renderHook } from '@testing-library/react';
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import { describe, expect, it, vi } from 'vitest';
|
||||
|
||||
import { useListsSubTab } from './useListsSubTab';
|
||||
import { rememberListsSubTab, useListsSubTab } from './useListsSubTab';
|
||||
|
||||
// FE-HOOK-LISTSTAB-001 to FE-HOOK-LISTSTAB-006
|
||||
|
||||
@@ -47,3 +47,40 @@ describe('useListsSubTab', () => {
|
||||
expect(renderHook(() => useListsSubTab(3, { onlyKnownTabs: true })).result.current[0]).toBe('todo');
|
||||
});
|
||||
});
|
||||
|
||||
// FE-HOOK-LISTSTAB-007 to FE-HOOK-LISTSTAB-009: picking the sub-tab from outside the Lists tab.
|
||||
describe('rememberListsSubTab', () => {
|
||||
it('FE-HOOK-LISTSTAB-007: the Lists tab opens on the sub-tab picked before it was on screen', () => {
|
||||
sessionStorage.setItem('trip-lists-subtab-7', 'todo');
|
||||
rememberListsSubTab(7, 'packing');
|
||||
expect(renderHook(() => useListsSubTab(7)).result.current[0]).toBe('packing');
|
||||
});
|
||||
|
||||
it('FE-HOOK-LISTSTAB-008: a Lists tab already on screen follows, one for another trip does not', () => {
|
||||
sessionStorage.setItem('trip-lists-subtab-7', 'todo');
|
||||
sessionStorage.setItem('trip-lists-subtab-8', 'todo');
|
||||
const same = renderHook(() => useListsSubTab(7));
|
||||
const other = renderHook(() => useListsSubTab(8));
|
||||
act(() => rememberListsSubTab(7, 'packing'));
|
||||
expect(same.result.current[0]).toBe('packing');
|
||||
expect(other.result.current[0]).toBe('todo');
|
||||
same.unmount();
|
||||
act(() => rememberListsSubTab(7, 'todo'));
|
||||
expect(sessionStorage.getItem('trip-lists-subtab-7')).toBe('todo');
|
||||
});
|
||||
|
||||
it('FE-HOOK-LISTSTAB-009: blocked storage still switches the tab on screen and does not throw', () => {
|
||||
const { result } = renderHook(() => useListsSubTab(7));
|
||||
const setItem = vi.spyOn(Storage.prototype, 'setItem').mockImplementation(() => {
|
||||
throw new Error('blocked');
|
||||
});
|
||||
const getItem = vi.spyOn(Storage.prototype, 'getItem').mockImplementation(() => {
|
||||
throw new Error('blocked');
|
||||
});
|
||||
act(() => rememberListsSubTab(7, 'todo'));
|
||||
expect(result.current[0]).toBe('todo');
|
||||
expect(renderHook(() => useListsSubTab(9)).result.current[0]).toBe('packing');
|
||||
setItem.mockRestore();
|
||||
getItem.mockRestore();
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,10 +1,39 @@
|
||||
import { useState } from 'react';
|
||||
import { useEffect, useState } from 'react';
|
||||
|
||||
/** The two lists the Lists tab switches between. */
|
||||
export type ListsSubTab = 'packing' | 'todo';
|
||||
|
||||
const storageKey = (tripId: number) => `trip-lists-subtab-${tripId}`;
|
||||
|
||||
/** Lists tabs that are on screen, told when another surface picks their sub-tab. */
|
||||
const followers = new Set<(tripId: number, tab: ListsSubTab) => void>();
|
||||
|
||||
function readStored(tripId: number): string | null {
|
||||
try {
|
||||
return sessionStorage.getItem(storageKey(tripId));
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
function store(tripId: number, tab: ListsSubTab) {
|
||||
try {
|
||||
sessionStorage.setItem(storageKey(tripId), tab);
|
||||
} catch {
|
||||
// Storage can be blocked; the choice then lasts as long as the tab is on screen.
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Picks the Lists tab's sub-tab from outside it, e.g. "Open packing list" on a
|
||||
* tour. The Lists tab opens with it next time, and one already on screen
|
||||
* switches to it right away.
|
||||
*/
|
||||
export function rememberListsSubTab(tripId: number, tab: ListsSubTab) {
|
||||
store(tripId, tab);
|
||||
followers.forEach((follow) => follow(tripId, tab));
|
||||
}
|
||||
|
||||
/**
|
||||
* The Lists tab's sub-tab, remembered per trip for the browser session. Both the
|
||||
* desktop container and the phone shell read and write the same key. The phone
|
||||
@@ -13,13 +42,22 @@ const storageKey = (tripId: number) => `trip-lists-subtab-${tripId}`;
|
||||
*/
|
||||
export function useListsSubTab(tripId: number, { onlyKnownTabs = false }: { onlyKnownTabs?: boolean } = {}) {
|
||||
const [subTab, setSubTabState] = useState<ListsSubTab>(() => {
|
||||
const saved = sessionStorage.getItem(storageKey(tripId));
|
||||
const saved = readStored(tripId);
|
||||
if (onlyKnownTabs) return saved === 'todo' ? 'todo' : 'packing';
|
||||
return (saved as ListsSubTab) || 'packing';
|
||||
});
|
||||
useEffect(() => {
|
||||
const follow = (forTrip: number, tab: ListsSubTab) => {
|
||||
if (forTrip === tripId) setSubTabState(tab);
|
||||
};
|
||||
followers.add(follow);
|
||||
return () => {
|
||||
followers.delete(follow);
|
||||
};
|
||||
}, [tripId]);
|
||||
const setSubTab = (tab: ListsSubTab) => {
|
||||
setSubTabState(tab);
|
||||
sessionStorage.setItem(storageKey(tripId), tab);
|
||||
store(tripId, tab);
|
||||
};
|
||||
return [subTab, setSubTab] as const;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user