Refactor get/set preferences

pull/1149/head
Lim Chee Aun 2025-05-12 19:39:19 +08:00
rodzic b14b14f4bf
commit 0f3a556e9e
7 zmienionych plików z 261 dodań i 261 usunięć

Wyświetl plik

@ -27,7 +27,7 @@ import poweredByGiphyURL from '../assets/powered-by-giphy.svg';
import Menu2 from '../components/menu2'; import Menu2 from '../components/menu2';
import supportedLanguages from '../data/status-supported-languages'; import supportedLanguages from '../data/status-supported-languages';
import urlRegex from '../data/url-regex'; import urlRegex from '../data/url-regex';
import { api } from '../utils/api'; import { api, getPreferences } from '../utils/api';
import { langDetector } from '../utils/browser-translator'; import { langDetector } from '../utils/browser-translator';
import db from '../utils/db'; import db from '../utils/db';
import emojifyText from '../utils/emojify-text'; import emojifyText from '../utils/emojify-text';
@ -278,7 +278,7 @@ function Compose({
const [poll, setPoll] = useState(null); const [poll, setPoll] = useState(null);
const [scheduledAt, setScheduledAt] = useState(null); const [scheduledAt, setScheduledAt] = useState(null);
const prefs = store.account.get('preferences') || {}; const prefs = getPreferences();
const oninputTextarea = () => { const oninputTextarea = () => {
if (!textareaRef.current) return; if (!textareaRef.current) return;

Wyświetl plik

@ -5,6 +5,7 @@ import { memo } from 'preact/compat';
import { useContext, useMemo } from 'preact/hooks'; import { useContext, useMemo } from 'preact/hooks';
import { useSnapshot } from 'valtio'; import { useSnapshot } from 'valtio';
import { getPreferences } from '../utils/api';
import FilterContext from '../utils/filter-context'; import FilterContext from '../utils/filter-context';
import { isFiltered } from '../utils/filters'; import { isFiltered } from '../utils/filters';
import states, { statusKey } from '../utils/states'; import states, { statusKey } from '../utils/states';
@ -107,11 +108,9 @@ function MediaPost({
console.debug('RENDER Media post', id, status?.account.displayName); console.debug('RENDER Media post', id, status?.account.displayName);
const hasSpoiler = sensitive; const hasSpoiler = sensitive;
const readingExpandMedia = useMemo(() => { const prefs = getPreferences();
// default | show_all | hide_all const readingExpandMedia =
const prefs = store.account.get('preferences') || {}; prefs['reading:expand:media']?.toLowerCase() || 'default';
return prefs['reading:expand:media']?.toLowerCase() || 'default';
}, []);
const showSpoilerMedia = readingExpandMedia === 'show_all'; const showSpoilerMedia = readingExpandMedia === 'show_all';
const Parent = parent || 'div'; const Parent = parent || 'div';

Wyświetl plik

@ -40,7 +40,7 @@ import Menu2 from '../components/menu2';
import Modal from '../components/modal'; import Modal from '../components/modal';
import NameText from '../components/name-text'; import NameText from '../components/name-text';
import Poll from '../components/poll'; import Poll from '../components/poll';
import { api } from '../utils/api'; import { api, getPreferences } from '../utils/api';
import { langDetector } from '../utils/browser-translator'; import { langDetector } from '../utils/browser-translator';
import emojifyText from '../utils/emojify-text'; import emojifyText from '../utils/emojify-text';
import enhanceContent from '../utils/enhance-content'; import enhanceContent from '../utils/enhance-content';
@ -344,15 +344,6 @@ const getCurrentAccID = mem(
}, },
); );
const getPrefs = mem(
() => {
return store.account.get('preferences') || {};
},
{
maxAge: 60 * 1000, // 1 minute
},
);
function Status({ function Status({
statusID, statusID,
status, status,
@ -448,7 +439,7 @@ function Status({
inReplyToAccountId, inReplyToAccountId,
content, content,
mentions, mentions,
mediaAttachments, mediaAttachments = [],
reblog, reblog,
uri, uri,
url, url,
@ -554,16 +545,14 @@ function Status({
inReplyToAccountId === currentAccount || inReplyToAccountId === currentAccount ||
mentions?.find((mention) => mention.id === currentAccount); mentions?.find((mention) => mention.id === currentAccount);
const readingExpandSpoilers = useMemo(() => { const prefs = getPreferences();
const prefs = getPrefs(); const readingExpandSpoilers = !!prefs['reading:expand:spoilers'];
return !!prefs['reading:expand:spoilers'];
}, []);
const readingExpandMedia = useMemo(() => {
// default | show_all | hide_all // default | show_all | hide_all
// Ignore hide_all because it means hide *ALL* media including non-sensitive ones // Ignore hide_all because it means hide *ALL* media including non-sensitive ones
const prefs = getPrefs(); const readingExpandMedia =
return prefs['reading:expand:media']?.toLowerCase() || 'default'; prefs['reading:expand:media']?.toLowerCase() || 'default';
}, []);
// FOR TESTING: // FOR TESTING:
// const readingExpandSpoilers = true; // const readingExpandSpoilers = true;
// const readingExpandMedia = 'show_all'; // const readingExpandMedia = 'show_all';
@ -713,8 +702,9 @@ function Status({
const textWeight = useCallback( const textWeight = useCallback(
() => () =>
Math.max( Math.max(
Math.round((spoilerText.length + htmlContentLength(content)) / 140) || Math.round(
1, ((spoilerText?.length || 0) + htmlContentLength(content)) / 140,
) || 1,
1, 1,
), ),
[spoilerText, content], [spoilerText, content],

440
src/locales/en.po wygenerowano

Plik diff jest za duży Load Diff

Wyświetl plik

@ -15,7 +15,6 @@ import {
useRef, useRef,
useState, useState,
} from 'preact/hooks'; } from 'preact/hooks';
import punycode from 'punycode/';
import { useHotkeys } from 'react-hotkeys-hook'; import { useHotkeys } from 'react-hotkeys-hook';
import { useSearchParams } from 'react-router-dom'; import { useSearchParams } from 'react-router-dom';
import { uid } from 'uid/single'; import { uid } from 'uid/single';
@ -30,7 +29,7 @@ import Modal from '../components/modal';
import NameText from '../components/name-text'; import NameText from '../components/name-text';
import NavMenu from '../components/nav-menu'; import NavMenu from '../components/nav-menu';
import RelativeTime from '../components/relative-time'; import RelativeTime from '../components/relative-time';
import { api } from '../utils/api'; import { api, getPreferences } from '../utils/api';
import { oklab2rgb, rgb2oklab } from '../utils/color-utils'; import { oklab2rgb, rgb2oklab } from '../utils/color-utils';
import db from '../utils/db'; import db from '../utils/db';
import emojifyText from '../utils/emojify-text'; import emojifyText from '../utils/emojify-text';
@ -1855,10 +1854,8 @@ function PostPeek({ post, filterInfo }) {
const isThread = const isThread =
(inReplyToId && inReplyToAccountId === account.id) || !!_thread; (inReplyToId && inReplyToAccountId === account.id) || !!_thread;
const readingExpandSpoilers = useMemo(() => { const prefs = getPreferences();
const prefs = store.account.get('preferences') || {}; const readingExpandSpoilers = !!prefs['reading:expand:spoilers'];
return !!prefs['reading:expand:spoilers'];
}, []);
// const readingExpandSpoilers = true; // const readingExpandSpoilers = true;
const showMedia = const showMedia =
readingExpandSpoilers || readingExpandSpoilers ||

Wyświetl plik

@ -11,7 +11,7 @@ import LangSelector from '../components/lang-selector';
import Link from '../components/link'; import Link from '../components/link';
import RelativeTime from '../components/relative-time'; import RelativeTime from '../components/relative-time';
import languages from '../data/translang-languages'; import languages from '../data/translang-languages';
import { api } from '../utils/api'; import { api, getPreferences, setPreferences } from '../utils/api';
import getTranslateTargetLanguage from '../utils/get-translate-target-language'; import getTranslateTargetLanguage from '../utils/get-translate-target-language';
import localeCode2Text from '../utils/localeCode2Text'; import localeCode2Text from '../utils/localeCode2Text';
import prettyBytes from '../utils/pretty-bytes'; import prettyBytes from '../utils/pretty-bytes';
@ -55,7 +55,7 @@ function Settings({ onClose }) {
const systemTargetLanguageText = localeCode2Text(systemTargetLanguage); const systemTargetLanguageText = localeCode2Text(systemTargetLanguage);
const currentTextSize = store.local.get('textSize') || DEFAULT_TEXT_SIZE; const currentTextSize = store.local.get('textSize') || DEFAULT_TEXT_SIZE;
const [prefs, setPrefs] = useState(store.account.get('preferences') || {}); const [prefs, setPrefs] = useState(getPreferences());
const { masto, authenticated, instance } = api(); const { masto, authenticated, instance } = api();
// Get preferences every time Settings is opened // Get preferences every time Settings is opened
// NOTE: Disabled for now because I don't expect this to change often. Also for some reason, the /api/v1/preferences endpoint is cached for a while and return old prefs if refresh immediately after changing them. // NOTE: Disabled for now because I don't expect this to change often. Also for some reason, the /api/v1/preferences endpoint is cached for a while and return old prefs if refresh immediately after changing them.
@ -300,7 +300,7 @@ function Settings({ onClose }) {
...prefs, ...prefs,
'posting:default:visibility': value, 'posting:default:visibility': value,
}); });
store.account.set('preferences', { setPreferences({
...prefs, ...prefs,
'posting:default:visibility': value, 'posting:default:visibility': value,
}); });

Wyświetl plik

@ -1,6 +1,8 @@
import { compareVersions, satisfies, validate } from 'compare-versions'; import { compareVersions, satisfies, validate } from 'compare-versions';
import { createRestAPIClient, createStreamingAPIClient } from 'masto'; import { createRestAPIClient, createStreamingAPIClient } from 'masto';
import mem from '../utils/mem';
import store from './store'; import store from './store';
import { import {
getAccount, getAccount,
@ -179,8 +181,20 @@ export async function initAccount(client, instance, accessToken, vapidKey) {
}); });
} }
export const getPreferences = mem(
() => store.account.get('preferences') || {},
{
maxAge: 60 * 1000, // 1 minute
},
);
export function setPreferences(preferences) {
getPreferences.clear(); // clear memo cache
store.account.set('preferences', preferences);
}
export function hasPreferences() { export function hasPreferences() {
return !!store.account.get('preferences'); return !!getPreferences();
} }
// Get preferences // Get preferences
@ -190,7 +204,7 @@ export async function initPreferences(client) {
__BENCHMARK.start('fetch-preferences'); __BENCHMARK.start('fetch-preferences');
const preferences = await masto.v1.preferences.fetch(); const preferences = await masto.v1.preferences.fetch();
__BENCHMARK.end('fetch-preferences'); __BENCHMARK.end('fetch-preferences');
store.account.set('preferences', preferences); setPreferences(preferences);
} catch (e) { } catch (e) {
// silently fail // silently fail
console.error(e); console.error(e);