web: handle notification deep links after auth
Some checks are pending
CI / test (push) Has started running
Some checks are pending
CI / test (push) Has started running
This commit is contained in:
@@ -11,6 +11,7 @@ import { applyAppearancePreferences, getAppPreferences } from "../utils/preferen
|
||||
|
||||
const PENDING_INVITE_TOKEN_KEY = "bm_pending_invite_token";
|
||||
const AUTH_NOTICE_KEY = "bm_auth_notice";
|
||||
const PENDING_NOTIFICATION_NAV_KEY = "bm_pending_notification_nav";
|
||||
|
||||
export function App() {
|
||||
const accessToken = useAuthStore((s) => s.accessToken);
|
||||
@@ -20,6 +21,7 @@ export function App() {
|
||||
const logout = useAuthStore((s) => s.logout);
|
||||
const loadChats = useChatStore((s) => s.loadChats);
|
||||
const setActiveChatId = useChatStore((s) => s.setActiveChatId);
|
||||
const setFocusedMessage = useChatStore((s) => s.setFocusedMessage);
|
||||
const showToast = useUiStore((s) => s.showToast);
|
||||
const [joiningInvite, setJoiningInvite] = useState(false);
|
||||
|
||||
@@ -67,6 +69,15 @@ export function App() {
|
||||
window.history.replaceState(null, "", "/");
|
||||
}, []);
|
||||
|
||||
useEffect(() => {
|
||||
const nav = extractNotificationNavigationFromLocation();
|
||||
if (!nav) {
|
||||
return;
|
||||
}
|
||||
window.localStorage.setItem(PENDING_NOTIFICATION_NAV_KEY, JSON.stringify(nav));
|
||||
window.history.replaceState(null, "", "/");
|
||||
}, []);
|
||||
|
||||
useEffect(() => {
|
||||
if (!accessToken || !me || joiningInvite) {
|
||||
return;
|
||||
@@ -91,6 +102,45 @@ export function App() {
|
||||
})();
|
||||
}, [accessToken, me, joiningInvite, loadChats, setActiveChatId, showToast]);
|
||||
|
||||
useEffect(() => {
|
||||
if (!accessToken || !me || joiningInvite) {
|
||||
return;
|
||||
}
|
||||
const raw = window.localStorage.getItem(PENDING_NOTIFICATION_NAV_KEY);
|
||||
if (!raw) {
|
||||
return;
|
||||
}
|
||||
let parsed: { chatId?: unknown; messageId?: unknown } | null = null;
|
||||
try {
|
||||
parsed = JSON.parse(raw) as { chatId?: unknown; messageId?: unknown };
|
||||
} catch {
|
||||
window.localStorage.removeItem(PENDING_NOTIFICATION_NAV_KEY);
|
||||
return;
|
||||
}
|
||||
const chatId = typeof parsed?.chatId === "number" && Number.isFinite(parsed.chatId) ? parsed.chatId : null;
|
||||
const messageId = typeof parsed?.messageId === "number" && Number.isFinite(parsed.messageId) ? parsed.messageId : null;
|
||||
if (!chatId) {
|
||||
window.localStorage.removeItem(PENDING_NOTIFICATION_NAV_KEY);
|
||||
return;
|
||||
}
|
||||
void (async () => {
|
||||
try {
|
||||
await loadChats();
|
||||
const hasChat = useChatStore.getState().chats.some((chat) => chat.id === chatId);
|
||||
if (!hasChat) {
|
||||
showToast("Chat from notification is unavailable");
|
||||
return;
|
||||
}
|
||||
setActiveChatId(chatId);
|
||||
if (messageId) {
|
||||
setFocusedMessage(chatId, messageId);
|
||||
}
|
||||
} finally {
|
||||
window.localStorage.removeItem(PENDING_NOTIFICATION_NAV_KEY);
|
||||
}
|
||||
})();
|
||||
}, [accessToken, joiningInvite, loadChats, me, setActiveChatId, setFocusedMessage, showToast]);
|
||||
|
||||
if (!accessToken || !me) {
|
||||
return <AuthPage />;
|
||||
}
|
||||
@@ -126,6 +176,30 @@ function extractEmailVerificationTokenFromLocation(): string | null {
|
||||
return url.searchParams.get("token")?.trim() || null;
|
||||
}
|
||||
|
||||
function extractNotificationNavigationFromLocation(): { chatId: number; messageId?: number } | null {
|
||||
if (typeof window === "undefined") {
|
||||
return null;
|
||||
}
|
||||
const url = new URL(window.location.href);
|
||||
const chatRaw = url.searchParams.get("chat");
|
||||
if (!chatRaw) {
|
||||
return null;
|
||||
}
|
||||
const chatId = Number(chatRaw);
|
||||
if (!Number.isFinite(chatId) || chatId <= 0) {
|
||||
return null;
|
||||
}
|
||||
const messageRaw = url.searchParams.get("message");
|
||||
if (!messageRaw) {
|
||||
return { chatId };
|
||||
}
|
||||
const messageId = Number(messageRaw);
|
||||
if (!Number.isFinite(messageId) || messageId <= 0) {
|
||||
return { chatId };
|
||||
}
|
||||
return { chatId, messageId };
|
||||
}
|
||||
|
||||
function inviteJoinErrorMessage(error: unknown): string {
|
||||
const status = getHttpStatusCode(error);
|
||||
if (status === 404) {
|
||||
|
||||
Reference in New Issue
Block a user