fix(web): stop invite auto-join retry spam on failures
Some checks failed
CI / test (push) Failing after 2m18s
Some checks failed
CI / test (push) Failing after 2m18s
This commit is contained in:
@@ -6,6 +6,7 @@ import { AuthPage } from "../pages/AuthPage";
|
||||
import { ChatsPage } from "../pages/ChatsPage";
|
||||
import { useAuthStore } from "../store/authStore";
|
||||
import { useChatStore } from "../store/chatStore";
|
||||
import { useUiStore } from "../store/uiStore";
|
||||
import { applyAppearancePreferences, getAppPreferences } from "../utils/preferences";
|
||||
|
||||
const PENDING_INVITE_TOKEN_KEY = "bm_pending_invite_token";
|
||||
@@ -19,6 +20,7 @@ export function App() {
|
||||
const logout = useAuthStore((s) => s.logout);
|
||||
const loadChats = useChatStore((s) => s.loadChats);
|
||||
const setActiveChatId = useChatStore((s) => s.setActiveChatId);
|
||||
const showToast = useUiStore((s) => s.showToast);
|
||||
const [joiningInvite, setJoiningInvite] = useState(false);
|
||||
|
||||
useEffect(() => {
|
||||
@@ -79,12 +81,15 @@ export function App() {
|
||||
const chat = await joinByInvite(token);
|
||||
await loadChats();
|
||||
setActiveChatId(chat.id);
|
||||
window.localStorage.removeItem(PENDING_INVITE_TOKEN_KEY);
|
||||
showToast("Joined by invite");
|
||||
} catch (error) {
|
||||
showToast(inviteJoinErrorMessage(error));
|
||||
} finally {
|
||||
window.localStorage.removeItem(PENDING_INVITE_TOKEN_KEY);
|
||||
setJoiningInvite(false);
|
||||
}
|
||||
})();
|
||||
}, [accessToken, me, joiningInvite, loadChats, setActiveChatId]);
|
||||
}, [accessToken, me, joiningInvite, loadChats, setActiveChatId, showToast]);
|
||||
|
||||
if (!accessToken || !me) {
|
||||
return <AuthPage />;
|
||||
@@ -120,3 +125,28 @@ function extractEmailVerificationTokenFromLocation(): string | null {
|
||||
}
|
||||
return url.searchParams.get("token")?.trim() || null;
|
||||
}
|
||||
|
||||
function inviteJoinErrorMessage(error: unknown): string {
|
||||
const status = getHttpStatusCode(error);
|
||||
if (status === 404) {
|
||||
return "Invite link not found or expired.";
|
||||
}
|
||||
if (status === 403) {
|
||||
return "You are not allowed to join by this invite.";
|
||||
}
|
||||
if (status === 409) {
|
||||
return "You are already a member of this chat.";
|
||||
}
|
||||
return "Failed to join by invite.";
|
||||
}
|
||||
|
||||
function getHttpStatusCode(error: unknown): number | null {
|
||||
if (!error || typeof error !== "object") {
|
||||
return null;
|
||||
}
|
||||
const maybeResponse = (error as { response?: { status?: unknown } }).response;
|
||||
if (!maybeResponse || typeof maybeResponse.status !== "number") {
|
||||
return null;
|
||||
}
|
||||
return maybeResponse.status;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user