From a130743d4c0a7d2caf2e2a6ddf2ae4d2339ebb37 Mon Sep 17 00:00:00 2001 From: Lim Chee Aun Date: Mon, 6 Feb 2023 00:17:19 +0800 Subject: [PATCH] Breaking: refactor all masto API calls Everything need to be instance-aware! --- src/app.jsx | 130 ++++++---------------- src/components/account.jsx | 19 +++- src/components/compose.jsx | 9 +- src/components/drafts.jsx | 2 + src/components/media-modal.jsx | 5 +- src/components/name-text.jsx | 15 ++- src/components/status.jsx | 137 ++++++++++++++++------- src/components/timeline.jsx | 19 +++- src/compose.jsx | 20 ---- src/pages/account-statuses.jsx | 9 +- src/pages/bookmarks.jsx | 2 + src/pages/favourites.jsx | 2 + src/pages/following.jsx | 2 + src/pages/hashtags.jsx | 14 ++- src/pages/home.jsx | 2 + src/pages/lists.jsx | 2 + src/pages/notifications.jsx | 2 + src/pages/public.jsx | 69 ++++-------- src/pages/settings.jsx | 7 +- src/pages/status.jsx | 44 ++++++-- src/utils/api.js | 176 ++++++++++++++++++++++++++++++ src/utils/handle-content-links.js | 16 ++- src/utils/open-compose.js | 6 +- src/utils/states.js | 2 + src/utils/store-utils.js | 23 +++- 25 files changed, 481 insertions(+), 253 deletions(-) create mode 100644 src/utils/api.js 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 && ( + } /> + )} } /> {/* } /> */} - } /> + } />