DRY get current Account

This commit is contained in:
Lim Chee Aun 2023-01-11 13:28:42 +08:00
parent 93431f9ebb
commit a421406a11
4 changed files with 18 additions and 21 deletions

View file

@ -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<space>", including multiple mentions like "@username1<space>@username2<space>"

View file

@ -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,

View file

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

9
src/utils/store-utils.js Normal file
View file

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