freezmo/lib/use-back-close.ts
Martin Dvorak 8557b04621 Navigation restructure: five-tab bottom bar, no phone drawer
Drawer removed (phones only; desktop rail untouched)
- MobileSidebar, sidebarOpen/useSidebarOpen, MenuIcon and the drawer's
  back-gesture layer are gone; the shared nav content now serves only
  the desktop rail (dead mobile branches deleted). No drawer-specific
  CSS existed; the three drawer strings (header.openMenu,
  sidebar.closeMenu, common.menu) and library.importAria are removed.
- The header drops the hamburger: logo + name on the left, the single
  theme chip on the right.

Five-tab bottom bar
- Library / Progress / New deck (prominent centre +) / Exam / Settings,
  in that order; Progress and Exam reuse the existing /stats and /exam
  screens, Settings keeps opening the dialog. Sliding pill, icon bounce,
  press feedback, re-tap-to-top and safe-area padding kept; every item
  is >=44px (72px wide at 360px).
- New short tab.* keys (EN + CS) so all five labels stay on one line at
  360px — 'Zkouškový režim' became 'Zkouška'. No ellipsis anywhere.
- The bar is mounted once in AppHeader so library, progress, exam and
  share get it; the wizards keep their own bottom action bar and the
  tab bar steps aside there, exactly as before.

Re-homed drawer destinations
- Smart Review card loses the Exam link (it is a tab now); Review due
  and Mistakes stay.
- New deck screen (and /homework) get a Study deck / Make my homework
  segmented switch; the library's homework empty-state button stays.
- The phone import icon becomes a ... button opening a sheet with
  Import JSON and New folder (back-gesture and Escape close it); folder
  chips still appear only once at least one folder exists, and folder
  creation stays in the New deck options step.

Verified: mobile suite 134 checks (no hamburger/drawer, tab order and
44px targets, label matrix at 360/390 x EN/CS x dark/light x reduced
motion, per-tab routing, deep links, Back from every tab, the ... sheet
with folder creation) plus regression 74/74, wizard 83/83, declutter
116/116, animation 8/8, native 35/35, i18n 25/25, phase1 38/38, phase2
43/43, phase3 20/20, freeze-loading 6/6. Key parity 740/740. Lint +
build clean. SW freezmo-v28.
2026-10-10 16:53:55 +00:00

109 lines
3.2 KiB
TypeScript

"use client";
import { useCallback, useEffect, useRef } from "react";
/**
* Lets the system Back gesture/button close the topmost layer first.
*
* While `open` is true a same-URL history entry is pushed; a popstate (the
* Android back gesture, the browser back button, an iOS edge swipe in
* standalone) closes the layer instead of leaving the page. Layers form a
* stack, so only the topmost one reacts. Closing a layer through its own UI
* pops the pushed entry again — unless the layer closed because of a
* navigation (a link inside the layer), in which case the entry is left for
* the permanent listener to skip on the next Back press.
*/
type State = { freezmoLayer?: boolean };
const stack: symbol[] = [];
export function useBackClose(open: boolean, onClose: () => void) {
const closeRef = useRef(onClose);
closeRef.current = onClose;
const openRef = useRef(open);
openRef.current = open;
const idRef = useRef<symbol>(Symbol("layer"));
const pushedRef = useRef(false);
const closingRef = useRef(false);
const skipRef = useRef(false);
const poppedRef = useRef(false);
const urlRef = useRef("");
/** Call before closing for a navigation: the entry is left for the skipper. */
const skipRewind = useCallback(() => {
skipRef.current = true;
}, []);
// Push the entry when the layer opens; rewind it when it closes on its own.
useEffect(() => {
const id = idRef.current;
if (open) {
if (pushedRef.current) return;
pushedRef.current = true;
urlRef.current = window.location.href;
stack.push(id);
try {
window.history.pushState({ freezmoLayer: true }, "");
} catch {
pushedRef.current = false;
stack.splice(stack.indexOf(id), 1);
}
return;
}
if (!pushedRef.current) return;
pushedRef.current = false;
const index = stack.indexOf(id);
if (index >= 0) stack.splice(index, 1);
if (skipRef.current) {
// Closed because of a navigation: leave the entry alone, the permanent
// listener skips it on the next Back press.
skipRef.current = false;
return;
}
if (poppedRef.current) {
// The Back press already consumed the entry — nothing to rewind.
poppedRef.current = false;
return;
}
if (window.location.href === urlRef.current) {
closingRef.current = true;
try {
window.history.back();
} catch {
/* ignore */
}
}
}, [open]);
// One listener per mounted component: close this layer if it is the top of
// the stack; otherwise skip stale entries left behind by navigations.
useEffect(() => {
const id = idRef.current;
const onPop = () => {
if (closingRef.current) {
closingRef.current = false;
return;
}
if (openRef.current) {
if (stack[stack.length - 1] !== id) return;
poppedRef.current = true;
closeRef.current();
return;
}
const state = window.history.state as State | null;
if (state?.freezmoLayer) {
try {
window.history.back();
} catch {
/* ignore */
}
}
};
window.addEventListener("popstate", onPop);
return () => window.removeEventListener("popstate", onPop);
}, []);
return { skipRewind };
}