Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix PWA router corruption when switching accounts in certain way #1698

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 36 additions & 14 deletions src/routes/common/ActorRedirect.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { Redirect, useLocation, useParams } from "react-router";
import { useLayoutEffect, useRef } from "react";
import { useLocation } from "react-router";

import { isInstalled } from "#/helpers/device";
import useIonViewIsVisible from "#/helpers/useIonViewIsVisible";
import { useOptimizedIonRouter } from "#/helpers/useOptimizedIonRouter";
import { useAppSelector } from "#/store";

export const usingActorRedirect = !isInstalled();
Expand All @@ -13,28 +15,48 @@ export default function ActorRedirect({ children }: React.PropsWithChildren) {
}

function ActorRedirectEnabled({ children }: React.PropsWithChildren) {
const { actor } = useParams<{ actor: string }>();
const connectedInstance = useAppSelector(
(state) => state.auth.connectedInstance,
);
const location = useLocation();
const ionViewIsVisible = useIonViewIsVisible();
const router = useOptimizedIonRouter();

if (!ionViewIsVisible) return children;
if (!connectedInstance || !actor) return children;
if (connectedInstance === actor) return children;
const fixedForPathname = useRef("");
aeharding marked this conversation as resolved.
Show resolved Hide resolved

const [first, second, _wrongActor, ...urlEnd] = location.pathname.split("/");
useLayoutEffect(() => {
if (fixedForPathname.current === location.pathname) {
return;
}
if (!ionViewIsVisible) return;

// no need to redirect if url doesn't have actor
if (!_wrongActor || !isPotentialActor(_wrongActor)) return children;
const [first, second, _wrongActor, ...urlEnd] =
location.pathname.split("/");

return (
<Redirect
to={[first, second, connectedInstance, ...urlEnd].join("/")}
push={false}
/>
);
// no need to redirect if url doesn't have actor
if (!_wrongActor || !isPotentialActor(_wrongActor)) return;

if (!connectedInstance || !_wrongActor) return;
if (connectedInstance === _wrongActor) return;

fixedForPathname.current = location.pathname;

requestAnimationFrame(() => {
router.push(
[first, second, connectedInstance, ...urlEnd].join("/"),
"root",
"replace",
);
});
}, [
children,
connectedInstance,
ionViewIsVisible,
location.pathname,
router,
]);

return children;
}

function isPotentialActor(host: string) {
Expand Down