From a421406a11dc2df23f0fb750198e22e74854f82a Mon Sep 17 00:00:00 2001 From: Lim Chee Aun Date: Wed, 11 Jan 2023 13:28:42 +0800 Subject: [PATCH] DRY get current Account --- src/components/compose.jsx | 14 +++++--------- src/compose.jsx | 9 ++------- src/pages/status.jsx | 7 ++----- src/utils/store-utils.js | 9 +++++++++ 4 files changed, 18 insertions(+), 21 deletions(-) create mode 100644 src/utils/store-utils.js diff --git a/src/components/compose.jsx b/src/components/compose.jsx index aa7dfeea..71e5b3ac 100644 --- a/src/components/compose.jsx +++ b/src/components/compose.jsx @@ -13,6 +13,7 @@ import emojifyText from '../utils/emojify-text'; import openCompose from '../utils/open-compose'; import states from '../utils/states'; import store from '../utils/store'; +import { getCurrentAccount } from '../utils/store-utils'; import useDebouncedCallback from '../utils/useDebouncedCallback'; import visibilityIconsMap from '../utils/visibility-icons-map'; @@ -80,18 +81,13 @@ function Compose({ console.warn('RENDER COMPOSER'); const [uiState, setUIState] = useState('default'); - const accounts = store.local.getJSON('accounts'); - const currentAccount = store.session.get('currentAccount'); - const currentAccountInfo = accounts.find( - (a) => a.info.id === currentAccount, - ).info; + const currentAccount = getCurrentAccount(); + const currentAccountInfo = currentAccount.info; const configuration = useMemo(() => { try { const instances = store.local.getJSON('instances'); - const currentInstance = accounts - .find((a) => a.info.id === currentAccount) - .instanceURL.toLowerCase(); + const currentInstance = currentAccount.instanceURL.toLowerCase(); const config = instances[currentInstance].configuration; console.log(config); return config; @@ -269,7 +265,7 @@ function Compose({ } // check if status contains only "@acct", if replying - const isSelf = replyToStatus?.account.id === currentAccount; + const isSelf = replyToStatus?.account.id === currentAccountInfo.id; const hasOnlyAcct = replyToStatus && value.trim() === `@${replyToStatus.account.acct}`; // TODO: check for mentions, or maybe just generic "@username", including multiple mentions like "@username1@username2" diff --git a/src/compose.jsx b/src/compose.jsx index 757502eb..5b77f738 100644 --- a/src/compose.jsx +++ b/src/compose.jsx @@ -7,7 +7,7 @@ import { render } from 'preact'; import { useEffect, useState } from 'preact/hooks'; import Compose from './components/compose'; -import store from './utils/store'; +import { getCurrentAccount } from './utils/store-utils'; import useTitle from './utils/useTitle'; if (window.opener) { @@ -18,12 +18,7 @@ if (window.opener) { if (window.masto) return; console.warn('window.masto not found. Trying to log in...'); try { - const accounts = store.local.getJSON('accounts') || []; - const currentAccount = store.session.get('currentAccount'); - const account = - accounts.find((a) => a.info.id === currentAccount) || accounts[0]; - const instanceURL = account.instanceURL; - const accessToken = account.accessToken; + const { instanceURL, accessToken } = getCurrentAccount(); window.masto = await login({ url: `https://${instanceURL}`, accessToken, diff --git a/src/pages/status.jsx b/src/pages/status.jsx index ba689ad1..f251d819 100644 --- a/src/pages/status.jsx +++ b/src/pages/status.jsx @@ -16,6 +16,7 @@ import htmlContentLength from '../utils/html-content-length'; import shortenNumber from '../utils/shorten-number'; import states, { saveStatus } from '../utils/states'; import store from '../utils/store'; +import { getCurrentAccount } from '../utils/store-utils'; import useDebouncedCallback from '../utils/useDebouncedCallback'; import useScroll from '../utils/useScroll'; import useTitle from '../utils/useTitle'; @@ -206,11 +207,7 @@ function StatusPage({ id }) { // Delete the cache for the context (async () => { try { - const accounts = store.local.getJSON('accounts') || []; - const currentAccount = store.session.get('currentAccount'); - const account = - accounts.find((a) => a.info.id === currentAccount) || accounts[0]; - const instanceURL = account.instanceURL; + const { instanceURL } = getCurrentAccount(); const contextURL = `https://${instanceURL}/api/v1/statuses/${id}/context`; console.log('Clear cache', contextURL); const apiCache = await caches.open('api'); diff --git a/src/utils/store-utils.js b/src/utils/store-utils.js new file mode 100644 index 00000000..6e307f06 --- /dev/null +++ b/src/utils/store-utils.js @@ -0,0 +1,9 @@ +import store from './store'; + +export function getCurrentAccount() { + const accounts = store.local.getJSON('accounts') || []; + const currentAccount = store.session.get('currentAccount'); + const account = + accounts.find((a) => a.info.id === currentAccount) || accounts[0]; + return account; +}