107 lines
3.1 KiB
TypeScript
107 lines
3.1 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;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Ensures no Service Worker is used (temporary).
|
|
* Unregisters all service workers and verifies none remain.
|
|
*/
|
|
export function useServiceWorker() {
|
|
useEffect(() => {
|
|
if (!("serviceWorker" in navigator)) return;
|
|
|
|
navigator.serviceWorker.getRegistrations()
|
|
.then((registrations) => {
|
|
if (registrations.length === 0) {
|
|
console.log("[SW] No service workers registered (OK).");
|
|
return [] as ServiceWorkerRegistration[];
|
|
}
|
|
const unregisterPromises = registrations.map((reg) =>
|
|
reg.unregister().then(() => {
|
|
console.log("[SW] Unregistered:", reg.scope);
|
|
}).catch((err) => {
|
|
console.warn("[SW] Unregister failed for", reg.scope, err);
|
|
})
|
|
);
|
|
return Promise.all(unregisterPromises).then(() =>
|
|
navigator.serviceWorker.getRegistrations()
|
|
);
|
|
})
|
|
.then((after) => {
|
|
if (after && after.length > 0) {
|
|
console.warn("[SW] Still registered after unregister:", after.length);
|
|
} else {
|
|
console.log("[SW] Verified: no service workers registered.");
|
|
}
|
|
})
|
|
.catch((err) => {
|
|
console.warn("[SW] Check failed:", err);
|
|
});
|
|
}, []);
|
|
}
|
|
|
|
/**
|
|
* 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);
|
|
}
|
|
}, []);
|
|
}
|