Skip to content

Commit

Permalink
frontend: fix back button with UseBackButton
Browse files Browse the repository at this point in the history
The Android back button stopped working after the back button was used
to close a dialog. The reason is that the backbutton pop handler did
not remove the handler, as `slice(index, 1)` does not remove the
element at `index`, it in fact keeps it. When properly removing the
handler, back button behavior works as expected again.
  • Loading branch information
benma committed Nov 7, 2024
1 parent 6077981 commit 52a7ee2
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions frontends/web/src/contexts/BackButtonContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ type TProviderProps = {
}

export const BackButtonProvider = ({ children }: TProviderProps) => {
const [handlers, sethandlers] = useState<THandler[]>([]);
const [handlers, setHandlers] = useState<THandler[]>([]);
const { guideShown, toggleGuide } = useContext(AppContext);

const callTopHandler = useCallback(() => {
Expand All @@ -61,17 +61,18 @@ export const BackButtonProvider = ({ children }: TProviderProps) => {
}, [handlers, guideShown, toggleGuide]);

const pushHandler = useCallback((handler: THandler) => {
sethandlers((prevStack) => [...prevStack, handler]);
setHandlers((prevStack) => [...prevStack, handler]);
}, []);

const popHandler = useCallback((handler: THandler) => {
sethandlers((prevStack) => {
setHandlers((prevStack) => {
const index = prevStack.indexOf(handler);
if (index === -1) {
// Should never happen.
return prevStack;
}
return prevStack.slice(index, 1);
const res = prevStack.slice(0, index).concat(prevStack.slice(index + 1));
return res;
});
}, []);

Expand Down

0 comments on commit 52a7ee2

Please sign in to comment.