69 lines
1.9 KiB
TypeScript
69 lines
1.9 KiB
TypeScript
import { useEffect } from "preact/hooks";
|
|
|
|
// Simple safe storage functions to avoid errorHandling.ts dependency
|
|
function safeLocalStorage() {
|
|
try {
|
|
if (typeof localStorage === "undefined") {
|
|
return null;
|
|
}
|
|
// Test if localStorage is accessible
|
|
const testKey = "__test__";
|
|
localStorage.setItem(testKey, "test");
|
|
localStorage.removeItem(testKey);
|
|
return localStorage;
|
|
} catch {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
function safeSessionStorage() {
|
|
try {
|
|
if (typeof sessionStorage === "undefined") {
|
|
return null;
|
|
}
|
|
// Test if sessionStorage is accessible
|
|
const testKey = "__test__";
|
|
sessionStorage.setItem(testKey, "test");
|
|
sessionStorage.removeItem(testKey);
|
|
return sessionStorage;
|
|
} catch {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Custom hook to manage logout button state
|
|
* Handles post-logout redirect cleanup
|
|
* Includes error handling for localStorage access issues
|
|
*/
|
|
export function useLogoutCleanup() {
|
|
useEffect(() => {
|
|
try {
|
|
// Use safe localStorage access
|
|
const storage = safeLocalStorage();
|
|
if (!storage) {
|
|
console.warn("localStorage not available");
|
|
return;
|
|
}
|
|
|
|
const lastClickedButton = storage.getItem("lastClickedButton");
|
|
if (lastClickedButton === "logout") {
|
|
storage.removeItem("lastClickedButton");
|
|
|
|
// Add try-catch for URL manipulation and redirect
|
|
try {
|
|
const url = new URL(globalThis.location.href);
|
|
globalThis.location.href = url.origin + "?popstate=true";
|
|
} catch (urlError) {
|
|
console.error("Error during logout redirect:", urlError);
|
|
// Fallback to simple redirect
|
|
globalThis.location.href = "/";
|
|
}
|
|
}
|
|
} catch (error) {
|
|
// Handle localStorage access errors (quota exceeded, privacy mode, etc.)
|
|
console.error("Error during logout cleanup:", error);
|
|
}
|
|
}, []);
|
|
}
|