Fix document.title not working properly

This commit is contained in:
Lim Chee Aun 2023-01-29 15:19:26 +08:00
parent 80cc387c1c
commit ae37d58826
4 changed files with 15 additions and 5 deletions

View file

@ -17,11 +17,12 @@ const Link = (props) => {
} catch (e) {} } catch (e) {}
let hash = (location.hash || '').replace(/^#/, '').trim(); let hash = (location.hash || '').replace(/^#/, '').trim();
if (hash === '') hash = '/'; if (hash === '') hash = '/';
const isActive = hash === props.to; const { to, ...restProps } = props;
const isActive = hash === to;
return ( return (
<a <a
href={`#${props.to}`} href={`#${to}`}
{...props} {...restProps}
class={`${props.class || ''} ${isActive ? 'is-active' : ''}`} class={`${props.class || ''} ${isActive ? 'is-active' : ''}`}
onClick={(e) => { onClick={(e) => {
if (routerLocation) states.prevLocation = routerLocation; if (routerLocation) states.prevLocation = routerLocation;

View file

@ -12,10 +12,12 @@ import states, { saveStatus } from '../utils/states';
import { getCurrentAccountNS } from '../utils/store-utils'; import { getCurrentAccountNS } from '../utils/store-utils';
import useDebouncedCallback from '../utils/useDebouncedCallback'; import useDebouncedCallback from '../utils/useDebouncedCallback';
import useScroll from '../utils/useScroll'; import useScroll from '../utils/useScroll';
import useTitle from '../utils/useTitle';
const LIMIT = 20; const LIMIT = 20;
function Home({ hidden }) { function Home({ hidden }) {
useTitle('Home', '/');
const snapStates = useSnapshot(states); const snapStates = useSnapshot(states);
const isHomeLocation = snapStates.currentLocation === '/'; const isHomeLocation = snapStates.currentLocation === '/';
const [uiState, setUIState] = useState('default'); const [uiState, setUIState] = useState('default');

View file

@ -308,6 +308,7 @@ function StatusPage() {
heroDisplayName && heroContentText heroDisplayName && heroContentText
? `${heroDisplayName}: "${heroContentText}"` ? `${heroDisplayName}: "${heroContentText}"`
: 'Status', : 'Status',
'/s/:id',
); );
const closeLink = useMemo(() => { const closeLink = useMemo(() => {

View file

@ -1,9 +1,15 @@
import { useEffect } from 'preact/hooks'; import { useEffect } from 'preact/hooks';
import { matchPath } from 'react-router-dom';
import { useSnapshot } from 'valtio';
import states from './states';
const { VITE_CLIENT_NAME: CLIENT_NAME } = import.meta.env; const { VITE_CLIENT_NAME: CLIENT_NAME } = import.meta.env;
export default function useTitle(title) { export default function useTitle(title, path) {
const snapStates = useSnapshot(states);
useEffect(() => { useEffect(() => {
if (path && !matchPath(path, snapStates.currentLocation)) return;
document.title = title ? `${title} / ${CLIENT_NAME}` : CLIENT_NAME; document.title = title ? `${title} / ${CLIENT_NAME}` : CLIENT_NAME;
}, [title]); }, [title, snapStates.currentLocation]);
} }