Respect your preferences

This commit is contained in:
Lim Chee Aun 2023-02-09 23:59:57 +08:00
parent 2cd235a069
commit 91aeed5fe6
3 changed files with 36 additions and 4 deletions

View file

@ -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);

View file

@ -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]);

View file

@ -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 } = {}) {