mirror of
https://github.com/cheeaun/phanpy.git
synced 2025-02-17 15:51:51 +03:00
DRY get current Account
This commit is contained in:
parent
93431f9ebb
commit
a421406a11
4 changed files with 18 additions and 21 deletions
|
@ -13,6 +13,7 @@ import emojifyText from '../utils/emojify-text';
|
||||||
import openCompose from '../utils/open-compose';
|
import openCompose from '../utils/open-compose';
|
||||||
import states from '../utils/states';
|
import states from '../utils/states';
|
||||||
import store from '../utils/store';
|
import store from '../utils/store';
|
||||||
|
import { getCurrentAccount } from '../utils/store-utils';
|
||||||
import useDebouncedCallback from '../utils/useDebouncedCallback';
|
import useDebouncedCallback from '../utils/useDebouncedCallback';
|
||||||
import visibilityIconsMap from '../utils/visibility-icons-map';
|
import visibilityIconsMap from '../utils/visibility-icons-map';
|
||||||
|
|
||||||
|
@ -80,18 +81,13 @@ function Compose({
|
||||||
console.warn('RENDER COMPOSER');
|
console.warn('RENDER COMPOSER');
|
||||||
const [uiState, setUIState] = useState('default');
|
const [uiState, setUIState] = useState('default');
|
||||||
|
|
||||||
const accounts = store.local.getJSON('accounts');
|
const currentAccount = getCurrentAccount();
|
||||||
const currentAccount = store.session.get('currentAccount');
|
const currentAccountInfo = currentAccount.info;
|
||||||
const currentAccountInfo = accounts.find(
|
|
||||||
(a) => a.info.id === currentAccount,
|
|
||||||
).info;
|
|
||||||
|
|
||||||
const configuration = useMemo(() => {
|
const configuration = useMemo(() => {
|
||||||
try {
|
try {
|
||||||
const instances = store.local.getJSON('instances');
|
const instances = store.local.getJSON('instances');
|
||||||
const currentInstance = accounts
|
const currentInstance = currentAccount.instanceURL.toLowerCase();
|
||||||
.find((a) => a.info.id === currentAccount)
|
|
||||||
.instanceURL.toLowerCase();
|
|
||||||
const config = instances[currentInstance].configuration;
|
const config = instances[currentInstance].configuration;
|
||||||
console.log(config);
|
console.log(config);
|
||||||
return config;
|
return config;
|
||||||
|
@ -269,7 +265,7 @@ function Compose({
|
||||||
}
|
}
|
||||||
|
|
||||||
// check if status contains only "@acct", if replying
|
// check if status contains only "@acct", if replying
|
||||||
const isSelf = replyToStatus?.account.id === currentAccount;
|
const isSelf = replyToStatus?.account.id === currentAccountInfo.id;
|
||||||
const hasOnlyAcct =
|
const hasOnlyAcct =
|
||||||
replyToStatus && value.trim() === `@${replyToStatus.account.acct}`;
|
replyToStatus && value.trim() === `@${replyToStatus.account.acct}`;
|
||||||
// TODO: check for mentions, or maybe just generic "@username<space>", including multiple mentions like "@username1<space>@username2<space>"
|
// TODO: check for mentions, or maybe just generic "@username<space>", including multiple mentions like "@username1<space>@username2<space>"
|
||||||
|
|
|
@ -7,7 +7,7 @@ import { render } from 'preact';
|
||||||
import { useEffect, useState } from 'preact/hooks';
|
import { useEffect, useState } from 'preact/hooks';
|
||||||
|
|
||||||
import Compose from './components/compose';
|
import Compose from './components/compose';
|
||||||
import store from './utils/store';
|
import { getCurrentAccount } from './utils/store-utils';
|
||||||
import useTitle from './utils/useTitle';
|
import useTitle from './utils/useTitle';
|
||||||
|
|
||||||
if (window.opener) {
|
if (window.opener) {
|
||||||
|
@ -18,12 +18,7 @@ if (window.opener) {
|
||||||
if (window.masto) return;
|
if (window.masto) return;
|
||||||
console.warn('window.masto not found. Trying to log in...');
|
console.warn('window.masto not found. Trying to log in...');
|
||||||
try {
|
try {
|
||||||
const accounts = store.local.getJSON('accounts') || [];
|
const { instanceURL, accessToken } = getCurrentAccount();
|
||||||
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;
|
|
||||||
window.masto = await login({
|
window.masto = await login({
|
||||||
url: `https://${instanceURL}`,
|
url: `https://${instanceURL}`,
|
||||||
accessToken,
|
accessToken,
|
||||||
|
|
|
@ -16,6 +16,7 @@ import htmlContentLength from '../utils/html-content-length';
|
||||||
import shortenNumber from '../utils/shorten-number';
|
import shortenNumber from '../utils/shorten-number';
|
||||||
import states, { saveStatus } from '../utils/states';
|
import states, { saveStatus } from '../utils/states';
|
||||||
import store from '../utils/store';
|
import store from '../utils/store';
|
||||||
|
import { getCurrentAccount } from '../utils/store-utils';
|
||||||
import useDebouncedCallback from '../utils/useDebouncedCallback';
|
import useDebouncedCallback from '../utils/useDebouncedCallback';
|
||||||
import useScroll from '../utils/useScroll';
|
import useScroll from '../utils/useScroll';
|
||||||
import useTitle from '../utils/useTitle';
|
import useTitle from '../utils/useTitle';
|
||||||
|
@ -206,11 +207,7 @@ function StatusPage({ id }) {
|
||||||
// Delete the cache for the context
|
// Delete the cache for the context
|
||||||
(async () => {
|
(async () => {
|
||||||
try {
|
try {
|
||||||
const accounts = store.local.getJSON('accounts') || [];
|
const { instanceURL } = getCurrentAccount();
|
||||||
const currentAccount = store.session.get('currentAccount');
|
|
||||||
const account =
|
|
||||||
accounts.find((a) => a.info.id === currentAccount) || accounts[0];
|
|
||||||
const instanceURL = account.instanceURL;
|
|
||||||
const contextURL = `https://${instanceURL}/api/v1/statuses/${id}/context`;
|
const contextURL = `https://${instanceURL}/api/v1/statuses/${id}/context`;
|
||||||
console.log('Clear cache', contextURL);
|
console.log('Clear cache', contextURL);
|
||||||
const apiCache = await caches.open('api');
|
const apiCache = await caches.open('api');
|
||||||
|
|
9
src/utils/store-utils.js
Normal file
9
src/utils/store-utils.js
Normal 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;
|
||||||
|
}
|
Loading…
Add table
Reference in a new issue