2023-02-11 03:37:42 +03:00
|
|
|
import './app.css';
|
|
|
|
|
2023-01-20 20:04:27 +03:00
|
|
|
import {
|
|
|
|
useEffect,
|
|
|
|
useLayoutEffect,
|
|
|
|
useMemo,
|
|
|
|
useRef,
|
|
|
|
useState,
|
|
|
|
} from 'preact/hooks';
|
2023-09-12 06:27:54 +03:00
|
|
|
import { matchPath, Route, Routes, useLocation } from 'react-router-dom';
|
2023-04-25 15:41:08 +03:00
|
|
|
import 'swiped-events';
|
2022-12-10 12:14:48 +03:00
|
|
|
import { useSnapshot } from 'valtio';
|
|
|
|
|
2023-09-02 13:19:09 +03:00
|
|
|
import BackgroundService from './components/background-service';
|
2023-09-05 13:49:16 +03:00
|
|
|
import ComposeButton from './components/compose-button';
|
|
|
|
import { ICONS } from './components/icon';
|
2023-09-06 17:54:05 +03:00
|
|
|
import KeyboardShortcutsHelp from './components/keyboard-shortcuts-help';
|
2022-12-10 12:14:48 +03:00
|
|
|
import Loader from './components/loader';
|
2023-09-12 06:27:54 +03:00
|
|
|
import Modals from './components/modals';
|
2023-09-02 13:19:09 +03:00
|
|
|
import NotificationService from './components/notification-service';
|
2023-09-04 09:49:39 +03:00
|
|
|
import SearchCommand from './components/search-command';
|
2023-02-16 12:51:54 +03:00
|
|
|
import Shortcuts from './components/shortcuts';
|
2023-01-28 13:52:18 +03:00
|
|
|
import NotFound from './pages/404';
|
2023-01-29 18:37:13 +03:00
|
|
|
import AccountStatuses from './pages/account-statuses';
|
2023-01-20 19:23:59 +03:00
|
|
|
import Bookmarks from './pages/bookmarks';
|
2023-01-28 13:52:18 +03:00
|
|
|
import Favourites from './pages/favourites';
|
2023-02-11 11:48:47 +03:00
|
|
|
import FollowedHashtags from './pages/followed-hashtags';
|
2023-02-03 16:08:08 +03:00
|
|
|
import Following from './pages/following';
|
2023-02-18 15:48:24 +03:00
|
|
|
import Hashtag from './pages/hashtag';
|
2022-12-10 12:14:48 +03:00
|
|
|
import Home from './pages/home';
|
2023-09-02 13:19:09 +03:00
|
|
|
import HttpRoute from './pages/http-route';
|
2023-02-10 19:05:18 +03:00
|
|
|
import List from './pages/list';
|
2023-01-28 13:52:18 +03:00
|
|
|
import Lists from './pages/lists';
|
2022-12-10 12:14:48 +03:00
|
|
|
import Login from './pages/login';
|
2023-04-06 14:32:26 +03:00
|
|
|
import Mentions from './pages/mentions';
|
2022-12-10 12:14:48 +03:00
|
|
|
import Notifications from './pages/notifications';
|
2023-01-28 13:52:18 +03:00
|
|
|
import Public from './pages/public';
|
2023-02-10 17:10:13 +03:00
|
|
|
import Search from './pages/search';
|
2023-09-02 13:19:09 +03:00
|
|
|
import StatusRoute from './pages/status-route';
|
2023-04-05 20:14:38 +03:00
|
|
|
import Trending from './pages/trending';
|
2022-12-10 12:14:48 +03:00
|
|
|
import Welcome from './pages/welcome';
|
2023-02-09 18:59:57 +03:00
|
|
|
import {
|
|
|
|
api,
|
|
|
|
initAccount,
|
|
|
|
initClient,
|
|
|
|
initInstance,
|
|
|
|
initPreferences,
|
|
|
|
} from './utils/api';
|
2022-12-10 12:14:48 +03:00
|
|
|
import { getAccessToken } from './utils/auth';
|
2023-09-12 06:27:54 +03:00
|
|
|
import focusDeck from './utils/focus-deck';
|
2023-09-02 13:19:09 +03:00
|
|
|
import states, { initStates } from './utils/states';
|
2022-12-10 12:14:48 +03:00
|
|
|
import store from './utils/store';
|
2023-09-02 13:19:09 +03:00
|
|
|
import { getCurrentAccount } from './utils/store-utils';
|
2023-09-01 10:40:00 +03:00
|
|
|
import './utils/toast-alert';
|
2022-12-10 12:14:48 +03:00
|
|
|
|
2022-12-13 15:42:09 +03:00
|
|
|
window.__STATES__ = states;
|
2022-12-10 12:14:48 +03:00
|
|
|
|
2023-08-13 12:15:49 +03:00
|
|
|
// Preload icons
|
|
|
|
// There's probably a better way to do this
|
|
|
|
// Related: https://github.com/vitejs/vite/issues/10600
|
|
|
|
setTimeout(() => {
|
|
|
|
for (const icon in ICONS) {
|
|
|
|
if (Array.isArray(ICONS[icon])) {
|
|
|
|
ICONS[icon][0]?.();
|
|
|
|
} else {
|
|
|
|
ICONS[icon]?.();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}, 5000);
|
|
|
|
|
2022-12-31 20:46:08 +03:00
|
|
|
function App() {
|
2022-12-10 12:14:48 +03:00
|
|
|
const snapStates = useSnapshot(states);
|
|
|
|
const [isLoggedIn, setIsLoggedIn] = useState(false);
|
2022-12-21 15:34:24 +03:00
|
|
|
const [uiState, setUIState] = useState('loading');
|
2022-12-10 12:14:48 +03:00
|
|
|
|
|
|
|
useLayoutEffect(() => {
|
|
|
|
const theme = store.local.get('theme');
|
|
|
|
if (theme) {
|
|
|
|
document.documentElement.classList.add(`is-${theme}`);
|
|
|
|
document
|
|
|
|
.querySelector('meta[name="color-scheme"]')
|
2023-02-05 19:17:19 +03:00
|
|
|
.setAttribute('content', theme === 'auto' ? 'dark light' : theme);
|
2022-12-10 12:14:48 +03:00
|
|
|
}
|
2023-03-08 12:17:23 +03:00
|
|
|
const textSize = store.local.get('textSize');
|
|
|
|
if (textSize) {
|
|
|
|
document.documentElement.style.setProperty(
|
|
|
|
'--text-size',
|
|
|
|
`${textSize}px`,
|
|
|
|
);
|
|
|
|
}
|
2022-12-10 12:14:48 +03:00
|
|
|
}, []);
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
const instanceURL = store.local.get('instanceURL');
|
2023-05-20 05:08:41 +03:00
|
|
|
const code = decodeURIComponent(
|
|
|
|
(window.location.search.match(/code=([^&]+)/) || [, ''])[1],
|
|
|
|
);
|
2022-12-10 12:14:48 +03:00
|
|
|
|
|
|
|
if (code) {
|
|
|
|
console.log({ code });
|
|
|
|
// Clear the code from the URL
|
2023-08-30 12:46:22 +03:00
|
|
|
window.history.replaceState({}, document.title, location.pathname || '/');
|
2022-12-10 12:14:48 +03:00
|
|
|
|
|
|
|
const clientID = store.session.get('clientID');
|
|
|
|
const clientSecret = store.session.get('clientSecret');
|
2023-09-01 10:40:00 +03:00
|
|
|
const vapidKey = store.session.get('vapidKey');
|
2022-12-10 12:14:48 +03:00
|
|
|
|
|
|
|
(async () => {
|
|
|
|
setUIState('loading');
|
2023-02-05 19:17:19 +03:00
|
|
|
const { access_token: accessToken } = await getAccessToken({
|
2022-12-10 12:14:48 +03:00
|
|
|
instanceURL,
|
|
|
|
client_id: clientID,
|
|
|
|
client_secret: clientSecret,
|
|
|
|
code,
|
|
|
|
});
|
|
|
|
|
2023-02-05 19:17:19 +03:00
|
|
|
const masto = initClient({ instance: instanceURL, accessToken });
|
|
|
|
await Promise.allSettled([
|
2023-05-09 13:48:19 +03:00
|
|
|
initInstance(masto, instanceURL),
|
2023-09-01 10:40:00 +03:00
|
|
|
initAccount(masto, instanceURL, accessToken, vapidKey),
|
2023-02-05 19:17:19 +03:00
|
|
|
]);
|
2023-08-30 12:42:33 +03:00
|
|
|
initStates();
|
2023-02-09 18:59:57 +03:00
|
|
|
initPreferences(masto);
|
2022-12-10 12:14:48 +03:00
|
|
|
|
|
|
|
setIsLoggedIn(true);
|
|
|
|
setUIState('default');
|
|
|
|
})();
|
2022-12-21 15:34:24 +03:00
|
|
|
} else {
|
2023-10-11 14:07:36 +03:00
|
|
|
window.__IGNORE_GET_ACCOUNT_ERROR__ = true;
|
2023-02-05 19:17:19 +03:00
|
|
|
const account = getCurrentAccount();
|
|
|
|
if (account) {
|
|
|
|
store.session.set('currentAccount', account.info.id);
|
2023-05-09 13:48:19 +03:00
|
|
|
const { masto, instance } = api({ account });
|
2023-02-12 12:38:50 +03:00
|
|
|
console.log('masto', masto);
|
2023-10-11 14:07:36 +03:00
|
|
|
initStates();
|
2023-02-09 18:59:57 +03:00
|
|
|
initPreferences(masto);
|
2023-02-10 06:35:06 +03:00
|
|
|
setUIState('loading');
|
2023-02-07 19:31:46 +03:00
|
|
|
(async () => {
|
2023-02-10 06:35:06 +03:00
|
|
|
try {
|
2023-05-09 13:48:19 +03:00
|
|
|
await initInstance(masto, instance);
|
2023-02-10 06:35:06 +03:00
|
|
|
} catch (e) {
|
|
|
|
} finally {
|
|
|
|
setIsLoggedIn(true);
|
|
|
|
setUIState('default');
|
|
|
|
}
|
2023-02-07 19:31:46 +03:00
|
|
|
})();
|
2023-02-10 07:29:07 +03:00
|
|
|
} else {
|
|
|
|
setUIState('default');
|
2023-02-05 19:17:19 +03:00
|
|
|
}
|
2022-12-10 12:14:48 +03:00
|
|
|
}
|
|
|
|
}, []);
|
|
|
|
|
2023-01-20 19:23:59 +03:00
|
|
|
let location = useLocation();
|
2023-01-27 15:54:18 +03:00
|
|
|
states.currentLocation = location.pathname;
|
|
|
|
|
2023-09-07 13:44:12 +03:00
|
|
|
useEffect(focusDeck, [location, isLoggedIn]);
|
2022-12-10 12:14:48 +03:00
|
|
|
|
2023-09-12 06:27:54 +03:00
|
|
|
const prevLocation = snapStates.prevLocation;
|
2023-01-21 05:08:55 +03:00
|
|
|
const backgroundLocation = useRef(prevLocation || null);
|
2023-09-10 10:30:04 +03:00
|
|
|
const isModalPage = useMemo(() => {
|
|
|
|
return (
|
|
|
|
matchPath('/:instance/s/:id', location.pathname) ||
|
|
|
|
matchPath('/s/:id', location.pathname)
|
|
|
|
);
|
|
|
|
}, [location.pathname, matchPath]);
|
2023-01-21 05:08:55 +03:00
|
|
|
if (isModalPage) {
|
|
|
|
if (!backgroundLocation.current) backgroundLocation.current = prevLocation;
|
|
|
|
} else {
|
|
|
|
backgroundLocation.current = null;
|
|
|
|
}
|
|
|
|
console.debug({
|
|
|
|
backgroundLocation: backgroundLocation.current,
|
|
|
|
location,
|
|
|
|
});
|
2023-01-20 19:23:59 +03:00
|
|
|
|
2023-04-17 13:56:09 +03:00
|
|
|
if (/\/https?:/.test(location.pathname)) {
|
|
|
|
return <HttpRoute />;
|
|
|
|
}
|
|
|
|
|
2023-01-20 19:23:59 +03:00
|
|
|
const nonRootLocation = useMemo(() => {
|
|
|
|
const { pathname } = location;
|
2023-02-05 19:17:19 +03:00
|
|
|
return !/^\/(login|welcome)/.test(pathname);
|
2023-01-20 19:23:59 +03:00
|
|
|
}, [location]);
|
|
|
|
|
2023-04-18 12:46:40 +03:00
|
|
|
// Change #app dataset based on snapStates.settings.shortcutsViewMode
|
2023-04-02 10:18:08 +03:00
|
|
|
useEffect(() => {
|
|
|
|
const $app = document.getElementById('app');
|
|
|
|
if ($app) {
|
2023-09-15 16:12:04 +03:00
|
|
|
$app.dataset.shortcutsViewMode = snapStates.shortcuts?.length
|
|
|
|
? snapStates.settings.shortcutsViewMode
|
|
|
|
: '';
|
2023-04-02 10:18:08 +03:00
|
|
|
}
|
2023-09-15 16:12:04 +03:00
|
|
|
}, [snapStates.shortcuts, snapStates.settings.shortcutsViewMode]);
|
2023-04-02 10:18:08 +03:00
|
|
|
|
2023-04-23 07:08:41 +03:00
|
|
|
// Add/Remove cloak class to body
|
|
|
|
useEffect(() => {
|
|
|
|
const $body = document.body;
|
|
|
|
$body.classList.toggle('cloak', snapStates.settings.cloakMode);
|
|
|
|
}, [snapStates.settings.cloakMode]);
|
|
|
|
|
2022-12-10 12:14:48 +03:00
|
|
|
return (
|
|
|
|
<>
|
2023-01-20 19:23:59 +03:00
|
|
|
<Routes location={nonRootLocation || location}>
|
|
|
|
<Route
|
|
|
|
path="/"
|
|
|
|
element={
|
|
|
|
isLoggedIn ? (
|
|
|
|
<Home />
|
|
|
|
) : uiState === 'loading' ? (
|
2023-08-19 14:21:51 +03:00
|
|
|
<Loader id="loader-root" />
|
2023-01-20 19:23:59 +03:00
|
|
|
) : (
|
|
|
|
<Welcome />
|
|
|
|
)
|
2022-12-10 12:14:48 +03:00
|
|
|
}
|
2023-01-20 19:23:59 +03:00
|
|
|
/>
|
|
|
|
<Route path="/login" element={<Login />} />
|
|
|
|
<Route path="/welcome" element={<Welcome />} />
|
|
|
|
</Routes>
|
2023-01-20 20:04:27 +03:00
|
|
|
<Routes location={backgroundLocation.current || location}>
|
2023-01-20 19:23:59 +03:00
|
|
|
{isLoggedIn && (
|
|
|
|
<Route path="/notifications" element={<Notifications />} />
|
|
|
|
)}
|
2023-04-06 14:32:26 +03:00
|
|
|
{isLoggedIn && <Route path="/mentions" element={<Mentions />} />}
|
2023-02-18 16:37:34 +03:00
|
|
|
{isLoggedIn && <Route path="/following" element={<Following />} />}
|
2023-01-28 13:52:18 +03:00
|
|
|
{isLoggedIn && <Route path="/b" element={<Bookmarks />} />}
|
|
|
|
{isLoggedIn && <Route path="/f" element={<Favourites />} />}
|
2023-02-10 19:05:18 +03:00
|
|
|
{isLoggedIn && (
|
|
|
|
<Route path="/l">
|
|
|
|
<Route index element={<Lists />} />
|
|
|
|
<Route path=":id" element={<List />} />
|
|
|
|
</Route>
|
|
|
|
)}
|
2023-02-11 11:48:47 +03:00
|
|
|
{isLoggedIn && <Route path="/ft" element={<FollowedHashtags />} />}
|
2023-02-18 15:48:24 +03:00
|
|
|
<Route path="/:instance?/t/:hashtag" element={<Hashtag />} />
|
2023-02-06 14:54:18 +03:00
|
|
|
<Route path="/:instance?/a/:id" element={<AccountStatuses />} />
|
2023-02-06 15:17:07 +03:00
|
|
|
<Route path="/:instance?/p">
|
|
|
|
<Route index element={<Public />} />
|
|
|
|
<Route path="l" element={<Public local />} />
|
|
|
|
</Route>
|
2023-04-05 20:14:38 +03:00
|
|
|
<Route path="/:instance?/trending" element={<Trending />} />
|
2023-02-10 17:10:13 +03:00
|
|
|
<Route path="/:instance?/search" element={<Search />} />
|
2023-01-28 13:52:18 +03:00
|
|
|
{/* <Route path="/:anything" element={<NotFound />} /> */}
|
2023-01-20 19:23:59 +03:00
|
|
|
</Routes>
|
2023-04-18 14:47:21 +03:00
|
|
|
{uiState === 'default' && (
|
|
|
|
<Routes>
|
|
|
|
<Route path="/:instance?/s/:id" element={<StatusRoute />} />
|
|
|
|
</Routes>
|
|
|
|
)}
|
2023-09-05 13:49:16 +03:00
|
|
|
{isLoggedIn && <ComposeButton />}
|
2023-05-08 07:53:27 +03:00
|
|
|
{isLoggedIn &&
|
|
|
|
!snapStates.settings.shortcutsColumnsMode &&
|
|
|
|
snapStates.settings.shortcutsViewMode !== 'multi-column' && (
|
|
|
|
<Shortcuts />
|
2023-05-07 17:19:19 +03:00
|
|
|
)}
|
2023-09-12 06:27:54 +03:00
|
|
|
<Modals />
|
2023-09-01 10:40:00 +03:00
|
|
|
<NotificationService />
|
2023-03-27 10:05:50 +03:00
|
|
|
<BackgroundService isLoggedIn={isLoggedIn} />
|
2023-09-04 09:49:39 +03:00
|
|
|
<SearchCommand onClose={focusDeck} />
|
2023-09-06 17:54:05 +03:00
|
|
|
<KeyboardShortcutsHelp />
|
2022-12-10 12:14:48 +03:00
|
|
|
</>
|
|
|
|
);
|
|
|
|
}
|
2022-12-31 20:46:08 +03:00
|
|
|
|
|
|
|
export { App };
|