From 91aeed5fe6a5e0cf61e69200778c9d8db98fc05e Mon Sep 17 00:00:00 2001 From: Lim Chee Aun Date: Thu, 9 Feb 2023 23:59:57 +0800 Subject: [PATCH] Respect your preferences --- src/app.jsx | 10 +++++++++- src/components/compose.jsx | 18 +++++++++++++++--- src/utils/api.js | 12 ++++++++++++ 3 files changed, 36 insertions(+), 4 deletions(-) diff --git a/src/app.jsx b/src/app.jsx index 58c297b7..b4185f8f 100644 --- a/src/app.jsx +++ b/src/app.jsx @@ -42,7 +42,13 @@ import Public from './pages/public'; import Settings from './pages/settings'; import Status from './pages/status'; import Welcome from './pages/welcome'; -import { api, initAccount, initClient, initInstance } from './utils/api'; +import { + api, + initAccount, + initClient, + initInstance, + initPreferences, +} from './utils/api'; import { getAccessToken } from './utils/auth'; import states, { getStatus, saveStatus } from './utils/states'; import store from './utils/store'; @@ -92,6 +98,7 @@ function App() { initInstance(masto), initAccount(masto, instanceURL, accessToken), ]); + initPreferences(masto); setIsLoggedIn(true); setUIState('default'); @@ -101,6 +108,7 @@ function App() { if (account) { store.session.set('currentAccount', account.info.id); const { masto } = api({ account }); + initPreferences(masto); (async () => { await initInstance(masto); setIsLoggedIn(true); diff --git a/src/components/compose.jsx b/src/components/compose.jsx index 2055b9ad..8960784d 100644 --- a/src/components/compose.jsx +++ b/src/components/compose.jsx @@ -148,6 +148,8 @@ function Compose({ const [mediaAttachments, setMediaAttachments] = useState([]); const [poll, setPoll] = useState(null); + const prefs = store.account.get('preferences') || {}; + const customEmojis = useRef(); useEffect(() => { (async () => { @@ -194,7 +196,7 @@ function Compose({ } focusTextarea(); setVisibility(visibility); - setLanguage(language || DEFAULT_LANG); + setLanguage(language || prefs.postingDefaultLanguage || DEFAULT_LANG); setSensitive(sensitive); } if (draftStatus) { @@ -217,7 +219,7 @@ function Compose({ focusTextarea(); spoilerTextRef.current.value = spoilerText; setVisibility(visibility); - setLanguage(language || DEFAULT_LANG); + setLanguage(language || prefs.postingDefaultLanguage || DEFAULT_LANG); setSensitive(sensitive); setPoll(composablePoll); setMediaAttachments(mediaAttachments); @@ -243,7 +245,7 @@ function Compose({ focusTextarea(); spoilerTextRef.current.value = spoilerText; setVisibility(visibility); - setLanguage(language || DEFAULT_LANG); + setLanguage(language || presf.postingDefaultLanguage || DEFAULT_LANG); setSensitive(sensitive); setPoll(composablePoll); setMediaAttachments(mediaAttachments); @@ -256,6 +258,16 @@ function Compose({ })(); } else { focusTextarea(); + console.log('Apply prefs', prefs); + if (prefs.postingDefaultVisibility) { + setVisibility(prefs.postingDefaultVisibility); + } + if (prefs.postingDefaultLanguage) { + setLanguage(prefs.postingDefaultLanguage); + } + if (prefs.postingDefaultSensitive) { + setSensitive(prefs.postingDefaultSensitive); + } } }, [draftStatus, editStatus, replyToStatus]); diff --git a/src/utils/api.js b/src/utils/api.js index 7cca00fd..e98836f0 100644 --- a/src/utils/api.js +++ b/src/utils/api.js @@ -100,6 +100,18 @@ export async function initAccount(client, instance, accessToken) { }); } +// Get preferences +export async function initPreferences(client) { + try { + const masto = client; + const preferences = await masto.v1.preferences.fetch(); + store.account.set('preferences', preferences); + } catch (e) { + // silently fail + console.error(e); + } +} + // Get the masto instance // If accountID is provided, get the masto instance for that account export function api({ instance, accessToken, accountID, account } = {}) {