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) {}
let hash = (location.hash || '').replace(/^#/, '').trim();
if (hash === '') hash = '/';
const isActive = hash === props.to;
const { to, ...restProps } = props;
const isActive = hash === to;
return (
<a
href={`#${props.to}`}
{...props}
href={`#${to}`}
{...restProps}
class={`${props.class || ''} ${isActive ? 'is-active' : ''}`}
onClick={(e) => {
if (routerLocation) states.prevLocation = routerLocation;

View file

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

View file

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

View file

@ -1,9 +1,15 @@
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;
export default function useTitle(title) {
export default function useTitle(title, path) {
const snapStates = useSnapshot(states);
useEffect(() => {
if (path && !matchPath(path, snapStates.currentLocation)) return;
document.title = title ? `${title} / ${CLIENT_NAME}` : CLIENT_NAME;
}, [title]);
}, [title, snapStates.currentLocation]);
}