2024-06-17 12:17:36 +02:00
|
|
|
import { useEffect, useState } from 'react';
|
|
|
|
|
import { useLocation, useParams } from 'react-router-dom';
|
2024-05-31 20:14:32 +02:00
|
|
|
import useThemeStore from '../stores/use-theme-store';
|
2024-06-17 12:17:36 +02:00
|
|
|
import useDefaultSubplebbits from './use-default-subplebbits';
|
|
|
|
|
import { isHomeView } from '../lib/utils/view-utils';
|
|
|
|
|
import { nsfwTags } from '../views/home/home';
|
2024-02-28 21:18:37 +01:00
|
|
|
|
|
|
|
|
const useTheme = (): [string, (theme: string) => void] => {
|
2024-06-17 12:17:36 +02:00
|
|
|
const location = useLocation();
|
|
|
|
|
const isInHomeView = isHomeView(location.pathname);
|
|
|
|
|
const { subplebbitAddress } = useParams<{ subplebbitAddress: string }>();
|
|
|
|
|
const getTheme = useThemeStore((state) => state.getTheme);
|
|
|
|
|
const setTheme = useThemeStore((state) => state.setTheme);
|
|
|
|
|
const subplebbits = useDefaultSubplebbits();
|
|
|
|
|
const [theme, setLocalTheme] = useState('yotsuba-b');
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
let initialTheme = 'yotsuba-b';
|
|
|
|
|
|
|
|
|
|
if (isInHomeView) {
|
|
|
|
|
initialTheme = 'yotsuba';
|
|
|
|
|
} else if (subplebbitAddress) {
|
|
|
|
|
initialTheme = getTheme(subplebbitAddress);
|
|
|
|
|
const subplebbit = subplebbits.find((s) => s.address === subplebbitAddress);
|
|
|
|
|
|
|
|
|
|
if (subplebbit && subplebbit.tags && subplebbit.tags.some((tag) => nsfwTags.includes(tag)) && initialTheme === 'yotsuba-b') {
|
|
|
|
|
initialTheme = 'yotsuba';
|
|
|
|
|
setTheme(subplebbitAddress, 'yotsuba');
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
setLocalTheme(initialTheme);
|
|
|
|
|
document.body.classList.remove('yotsuba', 'yotsuba-b', 'futaba', 'burichan', 'tomorrow', 'photon');
|
|
|
|
|
document.body.classList.add(initialTheme);
|
|
|
|
|
}, [subplebbitAddress, subplebbits, getTheme, setTheme, isInHomeView, location.pathname]);
|
|
|
|
|
|
|
|
|
|
const setSubplebbitTheme = (theme: string) => {
|
|
|
|
|
if (subplebbitAddress) {
|
|
|
|
|
setTheme(subplebbitAddress, theme);
|
|
|
|
|
setLocalTheme(theme);
|
|
|
|
|
document.body.classList.remove('yotsuba', 'yotsuba-b', 'futaba', 'burichan', 'tomorrow', 'photon');
|
|
|
|
|
document.body.classList.add(theme);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return [theme, setSubplebbitTheme];
|
2024-02-28 21:18:37 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export default useTheme;
|