Skip to content

Instantly share code, notes, and snippets.

@schalkventer
Created October 30, 2025 09:23
Show Gist options
  • Select an option

  • Save schalkventer/ff4a59e9a835eeb808b4c12af9996fa4 to your computer and use it in GitHub Desktop.

Select an option

Save schalkventer/ff4a59e9a835eeb808b4c12af9996fa4 to your computer and use it in GitHub Desktop.
import { useEffect, useState } from "react";
import { useAccount } from "../../new-data/account";
import { useLocation } from "react-router-dom";
const PHASES = [
"LOGIN",
"CREATE",
"INVITED",
"CONNECTION_REQUIRED",
"CONNECTION_MINI",
"CONNECTION_FULL",
"PASSWORD_TRIGGER",
"PASSWORD_CHANGE",
"PASSWORD_SENT",
"VERIFY_EMAIL_TRIGGER",
"VERIFY_EMAIL_CONFIRM",
] as const;
export type Phase = (typeof PHASES)[number];
const usePhaseWithEffect = (): [null | Phase, (phase: Phase) => void] => {
const account = useAccount();
const { company } = account.session.extract() || {};
const [phase, setPhase] = useState<Phase | null>(null);
const location = useLocation();
useEffect(() => {
if (phase !== null) return;
if (location.pathname.includes("reset-password/confirm")) {
return setPhase("PASSWORD_CHANGE");
}
if (!company) return setPhase("CONNECTION_REQUIRED");
return setPhase("LOGIN");
}, [company, location, phase]);
return [
phase,
(newPhase: Phase) => {
if (phase === newPhase) return;
setPhase(newPhase);
},
];
};
const usePhaseNoEffect = (): [Phase, (phase: Phase) => void] => {
const account = useAccount();
const { company } = account.session.extract() || {};
const [value, setValue] = useState<Phase | null>(null);
const location = useLocation();
const withFallback = (value: Phase | null): Phase => {
if (value !== null) return value;
if (location.pathname.includes("reset-password/confirm")) {
return "PASSWORD_CHANGE";
}
if (!company) return "CONNECTION_REQUIRED";
return "LOGIN";
};
const phase = withFallback(value);
return [
phase,
(newPhase: Phase) => {
if (phase === newPhase) return;
setValue(newPhase);
},
];
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment