diff --git a/src/app.jsx b/src/app.jsx index 99d5165d..8a7ec4d0 100644 --- a/src/app.jsx +++ b/src/app.jsx @@ -2,7 +2,6 @@ import './app.css'; import 'toastify-js/src/toastify.css'; import debounce from 'just-debounce-it'; -import { createClient } from 'masto'; import { useEffect, useLayoutEffect, @@ -36,9 +35,11 @@ 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 { getAccessToken } from './utils/auth'; import states, { saveStatus } from './utils/states'; import store from './utils/store'; +import { getCurrentAccount } from './utils/store-utils'; window.__STATES__ = states; @@ -54,13 +55,12 @@ function App() { document.documentElement.classList.add(`is-${theme}`); document .querySelector('meta[name="color-scheme"]') - .setAttribute('content', theme); + .setAttribute('content', theme === 'auto' ? 'dark light' : theme); } }, []); useEffect(() => { const instanceURL = store.local.get('instanceURL'); - const accounts = store.local.getJSON('accounts') || []; const code = (window.location.search.match(/code=([^&]+)/) || [])[1]; if (code) { @@ -73,58 +73,31 @@ function App() { (async () => { setUIState('loading'); - const tokenJSON = await getAccessToken({ + const { access_token: accessToken } = await getAccessToken({ instanceURL, client_id: clientID, client_secret: clientSecret, code, }); - const { access_token: accessToken } = tokenJSON; - store.session.set('accessToken', accessToken); - initMasto({ - url: `https://${instanceURL}`, - accessToken, - }); - - const mastoAccount = await masto.v1.accounts.verifyCredentials(); - - // console.log({ tokenJSON, mastoAccount }); - - let account = accounts.find((a) => a.info.id === mastoAccount.id); - if (account) { - account.info = mastoAccount; - account.instanceURL = instanceURL.toLowerCase(); - account.accessToken = accessToken; - } else { - account = { - info: mastoAccount, - instanceURL, - accessToken, - }; - accounts.push(account); - } - - store.local.setJSON('accounts', accounts); - store.session.set('currentAccount', account.info.id); + const masto = initClient({ instance: instanceURL, accessToken }); + await Promise.allSettled([ + initInstance(masto), + initAccount(masto, instanceURL, accessToken), + ]); setIsLoggedIn(true); setUIState('default'); })(); - } else if (accounts.length) { - 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; - store.session.set('currentAccount', account.info.id); - if (accessToken) setIsLoggedIn(true); - - initMasto({ - url: `https://${instanceURL}`, - accessToken, - }); } else { + const account = getCurrentAccount(); + if (account) { + store.session.set('currentAccount', account.info.id); + const { masto } = api({ account }); + initInstance(masto); + setIsLoggedIn(true); + } + setUIState('default'); } }, []); @@ -181,9 +154,11 @@ function App() { const nonRootLocation = useMemo(() => { const { pathname } = location; - return !/^\/(login|welcome|p)/.test(pathname); + return !/^\/(login|welcome)/.test(pathname); }, [location]); + console.log('nonRootLocation', nonRootLocation, 'location', location); + return ( <> @@ -210,13 +185,17 @@ function App() { {isLoggedIn && } />} {isLoggedIn && } />} {isLoggedIn && } />} - {isLoggedIn && } />} - {isLoggedIn && } />} + {isLoggedIn && ( + } /> + )} + {isLoggedIn && ( + } /> + )} } /> {/* } /> */} - } /> + } />