Fix title not working when not logged in

Welcome page's useTitle overridden other page's useTitle
pull/74/head
Lim Chee Aun 2023-02-25 10:03:34 +08:00
rodzic be83ca7358
commit 9e867c7af5
2 zmienionych plików z 6 dodań i 5 usunięć

Wyświetl plik

@ -6,7 +6,7 @@ import states from '../utils/states';
import useTitle from '../utils/useTitle';
function Welcome() {
useTitle();
useTitle(null, ['/', '/welcome']);
return (
<main id="welcome">
<h1>

Wyświetl plik

@ -9,10 +9,11 @@ const { VITE_CLIENT_NAME: CLIENT_NAME } = import.meta.env;
export default function useTitle(title, path) {
const snapStates = useSnapshot(states);
const { currentLocation } = snapStates;
let paths = [];
const hasPaths = Array.isArray(path);
let paths = hasPaths ? path : [];
// Workaround for matchPath not working for optional path segments
// https://github.com/remix-run/react-router/discussions/9862
if (/:?\w+\?/.test(path)) {
if (!hasPaths && /:?\w+\?/.test(path)) {
paths.push(path.replace(/(:\w+)\?/g, '$1'));
paths.push(path.replace(/\/?:\w+\?/g, ''));
}
@ -24,7 +25,7 @@ export default function useTitle(title, path) {
}
console.debug({ paths, matched, currentLocation });
useEffect(() => {
if (path && !matched) return;
if (!matched) return;
document.title = title ? `${title} / ${CLIENT_NAME}` : CLIENT_NAME;
}, [title, snapStates.currentLocation]);
}, [title, matched]);
}