feat(styles): remember style selection per sfw or nsfw category, instead of single board

This commit is contained in:
Tom (plebeius.eth)
2024-06-30 21:50:50 +02:00
parent ed393d0c72
commit 06e18289a4
4 changed files with 73 additions and 54 deletions
+20 -26
View File
@@ -172,6 +172,24 @@ export const TimeFilter = ({ isInAllView, isInCatalogView, isInSubscriptionsView
); );
}; };
const AdButton = () => {
return (
<div className={styles.adButton}>
[
<Link
to='/boards'
onClick={(e) => {
e.preventDefault();
alert('work in progress');
}}
>
Plebbit Leaderboard
</Link>
]
</div>
);
};
export const MobileBoardButtons = () => { export const MobileBoardButtons = () => {
const params = useParams(); const params = useParams();
const location = useLocation(); const location = useLocation();
@@ -186,19 +204,7 @@ export const MobileBoardButtons = () => {
return ( return (
<div className={`${styles.mobileBoardButtons} ${!isInCatalogView ? styles.addMargin : ''}`}> <div className={`${styles.mobileBoardButtons} ${!isInCatalogView ? styles.addMargin : ''}`}>
<div className={styles.adButton}> <AdButton />
[
<Link
to='/boards'
onClick={(e) => {
e.preventDefault();
alert('work in progress');
}}
>
Vote for Boards
</Link>
]
</div>
<hr /> <hr />
{isInPostView || isInPendingPostPage ? ( {isInPostView || isInPendingPostPage ? (
<> <>
@@ -248,19 +254,7 @@ export const DesktopBoardButtons = () => {
return ( return (
<> <>
<hr /> <hr />
<div className={styles.adButton}> <AdButton />
[
<Link
to='/boards'
onClick={(e) => {
e.preventDefault();
alert('work in progress');
}}
>
Vote for Boards
</Link>
]
</div>
<hr /> <hr />
<div className={styles.desktopBoardButtons}> <div className={styles.desktopBoardButtons}>
{isInPostView || isInPendingPostPage ? ( {isInPostView || isInPendingPostPage ? (
+6 -9
View File
@@ -15,21 +15,18 @@ const useInitialTheme = () => {
const isInAllView = isAllView(location.pathname, params); const isInAllView = isAllView(location.pathname, params);
const isInSubscriptionsView = isSubscriptionsView(location.pathname, params); const isInSubscriptionsView = isSubscriptionsView(location.pathname, params);
if (isInAllView || isInSubscriptionsView) { if (isInAllView) {
const userTheme = getTheme(isInAllView ? 'all' : 'subscriptions'); return getTheme('all') || 'yotsuba-b';
return userTheme || 'yotsuba-b'; } else if (isInSubscriptionsView) {
return getTheme('subscriptions') || 'yotsuba-b';
} else if (isInHomeView || isInNotFoundView) { } else if (isInHomeView || isInNotFoundView) {
return 'yotsuba'; return 'yotsuba';
} else if (subplebbitAddress) { } else if (subplebbitAddress) {
const userTheme = getTheme(subplebbitAddress);
if (userTheme) {
return userTheme;
}
const subplebbit = subplebbits.find((s) => s.address === subplebbitAddress); const subplebbit = subplebbits.find((s) => s.address === subplebbitAddress);
if (subplebbit && subplebbit.tags && subplebbit.tags.some((tag) => nsfwTags.includes(tag))) { if (subplebbit && subplebbit.tags && subplebbit.tags.some((tag) => nsfwTags.includes(tag))) {
return 'yotsuba'; return getTheme('nsfw') || 'yotsuba';
} }
return 'yotsuba-b'; return getTheme('sfw') || 'yotsuba-b';
} }
return 'yotsuba'; return 'yotsuba';
}; };
+21 -8
View File
@@ -2,7 +2,9 @@ import { useState, useEffect } from 'react';
import { useLocation, useParams } from 'react-router-dom'; import { useLocation, useParams } from 'react-router-dom';
import { isAllView, isSubscriptionsView } from '../lib/utils/view-utils'; import { isAllView, isSubscriptionsView } from '../lib/utils/view-utils';
import useThemeStore from '../stores/use-theme-store'; import useThemeStore from '../stores/use-theme-store';
import useDefaultSubplebbits from './use-default-subplebbits';
import useInitialTheme from './use-initial-theme'; import useInitialTheme from './use-initial-theme';
import { nsfwTags } from '../views/home/home';
const themeClasses = ['yotsuba', 'yotsuba-b', 'futaba', 'burichan', 'tomorrow', 'photon']; const themeClasses = ['yotsuba', 'yotsuba-b', 'futaba', 'burichan', 'tomorrow', 'photon'];
@@ -15,10 +17,11 @@ const updateThemeClass = (newTheme: string) => {
const useTheme = (): [string, (theme: string) => void] => { const useTheme = (): [string, (theme: string) => void] => {
const location = useLocation(); const location = useLocation();
const params = useParams(); const params = useParams<{ subplebbitAddress: string }>();
const setThemeStore = useThemeStore((state) => state.setTheme); const setThemeStore = useThemeStore((state) => state.setTheme);
const getTheme = useThemeStore((state) => state.getTheme); const getTheme = useThemeStore((state) => state.getTheme);
const loadThemes = useThemeStore((state) => state.loadThemes); const loadThemes = useThemeStore((state) => state.loadThemes);
const subplebbits = useDefaultSubplebbits();
const initialTheme = useInitialTheme(); const initialTheme = useInitialTheme();
const [theme, setLocalTheme] = useState<string>(() => initialTheme); const [theme, setLocalTheme] = useState<string>(() => initialTheme);
@@ -41,30 +44,40 @@ const useTheme = (): [string, (theme: string) => void] => {
const isInSubscriptionsView = isSubscriptionsView(location.pathname, params); const isInSubscriptionsView = isSubscriptionsView(location.pathname, params);
let storedTheme = null; let storedTheme = null;
if (subplebbitAddress) { if (isInAllView) {
storedTheme = getTheme(subplebbitAddress);
} else if (isInAllView) {
storedTheme = getTheme('all'); storedTheme = getTheme('all');
} else if (isInSubscriptionsView) { } else if (isInSubscriptionsView) {
storedTheme = getTheme('subscriptions'); storedTheme = getTheme('subscriptions');
} else if (subplebbitAddress) {
const subplebbit = subplebbits.find((s) => s.address === subplebbitAddress);
if (subplebbit && subplebbit.tags && subplebbit.tags.some((tag) => nsfwTags.includes(tag))) {
storedTheme = getTheme('nsfw');
} else {
storedTheme = getTheme('sfw');
}
} }
const themeToSet = storedTheme || initialTheme; const themeToSet = storedTheme || initialTheme;
setLocalTheme(themeToSet); setLocalTheme(themeToSet);
updateThemeClass(themeToSet); updateThemeClass(themeToSet);
}, [initialTheme, location.pathname, params, getTheme, themesLoaded]); }, [initialTheme, location.pathname, params, getTheme, themesLoaded, subplebbits]);
const setSubplebbitTheme = async (newTheme: string) => { const setSubplebbitTheme = async (newTheme: string) => {
const subplebbitAddress = params?.subplebbitAddress; const subplebbitAddress = params?.subplebbitAddress;
const isInAllView = isAllView(location.pathname, params); const isInAllView = isAllView(location.pathname, params);
const isInSubscriptionsView = isSubscriptionsView(location.pathname, params); const isInSubscriptionsView = isSubscriptionsView(location.pathname, params);
if (subplebbitAddress) { if (isInAllView) {
await setThemeStore(subplebbitAddress, newTheme);
} else if (isInAllView) {
await setThemeStore('all', newTheme); await setThemeStore('all', newTheme);
} else if (isInSubscriptionsView) { } else if (isInSubscriptionsView) {
await setThemeStore('subscriptions', newTheme); await setThemeStore('subscriptions', newTheme);
} else if (subplebbitAddress) {
const subplebbit = subplebbits.find((s) => s.address === subplebbitAddress);
if (subplebbit && subplebbit.tags && subplebbit.tags.some((tag) => nsfwTags.includes(tag))) {
await setThemeStore('nsfw', newTheme);
} else {
await setThemeStore('sfw', newTheme);
}
} }
setLocalTheme(newTheme); setLocalTheme(newTheme);
+26 -11
View File
@@ -2,9 +2,14 @@ import { create, StoreApi } from 'zustand';
import localForageLru from '@plebbit/plebbit-react-hooks/dist/lib/localforage-lru/index.js'; import localForageLru from '@plebbit/plebbit-react-hooks/dist/lib/localforage-lru/index.js';
interface ThemeState { interface ThemeState {
themes: Record<string, string>; themes: {
setTheme: (subplebbitAddress: string, theme: string) => void; nsfw: string;
getTheme: (subplebbitAddress: string) => string | null; sfw: string;
all: string;
subscriptions: string;
};
setTheme: (category: keyof ThemeState['themes'], theme: string) => void;
getTheme: (category: keyof ThemeState['themes']) => string | null;
loadThemes: () => Promise<void>; loadThemes: () => Promise<void>;
} }
@@ -14,20 +19,30 @@ const themeStore = localForageLru.createInstance({
}); });
const useThemeStore = create<ThemeState>((set: StoreApi<ThemeState>['setState'], get: StoreApi<ThemeState>['getState']) => ({ const useThemeStore = create<ThemeState>((set: StoreApi<ThemeState>['setState'], get: StoreApi<ThemeState>['getState']) => ({
themes: {}, themes: {
setTheme: async (subplebbitAddress: string, theme: string) => { nsfw: 'yotsuba',
sfw: 'yotsuba-b',
all: 'yotsuba-b',
subscriptions: 'yotsuba-b',
},
setTheme: async (category, theme) => {
const currentThemes = get().themes; const currentThemes = get().themes;
const updatedThemes = { ...currentThemes, [subplebbitAddress]: theme }; const updatedThemes = { ...currentThemes, [category]: theme };
await themeStore.setItem(subplebbitAddress, theme); await themeStore.setItem(category, theme);
set({ themes: updatedThemes }); set({ themes: updatedThemes });
}, },
getTheme: (subplebbitAddress: string) => { getTheme: (category) => {
const currentThemes = get().themes; const currentThemes = get().themes;
return currentThemes[subplebbitAddress] || null; return currentThemes[category] || null;
}, },
loadThemes: async () => { loadThemes: async () => {
const entries: [string, string][] = await themeStore.entries(); const entries: [keyof ThemeState['themes'], string][] = await themeStore.entries();
const themes: Record<string, string> = {}; const themes: Record<keyof ThemeState['themes'], string> = {
nsfw: 'yotsuba',
sfw: 'yotsuba-b',
all: 'yotsuba-b',
subscriptions: 'yotsuba-b',
};
entries.forEach(([key, value]) => { entries.forEach(([key, value]) => {
themes[key] = value; themes[key] = value;
}); });