2022-12-10 12:14:48 +03:00
|
|
|
import './app.css';
|
2022-12-26 09:02:05 +03:00
|
|
|
import 'toastify-js/src/toastify.css';
|
2022-12-10 12:14:48 +03:00
|
|
|
|
2023-01-01 07:01:54 +03:00
|
|
|
import debounce from 'just-debounce-it';
|
2023-01-28 13:52:18 +03:00
|
|
|
import { createClient } from 'masto';
|
2023-01-20 20:04:27 +03:00
|
|
|
import {
|
|
|
|
useEffect,
|
|
|
|
useLayoutEffect,
|
|
|
|
useMemo,
|
|
|
|
useRef,
|
|
|
|
useState,
|
|
|
|
} from 'preact/hooks';
|
2023-01-20 19:23:59 +03:00
|
|
|
import { Route, Routes, useLocation, useNavigate } from 'react-router-dom';
|
2022-12-26 09:02:05 +03:00
|
|
|
import Toastify from 'toastify-js';
|
2022-12-10 12:14:48 +03:00
|
|
|
import { useSnapshot } from 'valtio';
|
|
|
|
|
|
|
|
import Account from './components/account';
|
|
|
|
import Compose from './components/compose';
|
2023-01-13 10:30:09 +03:00
|
|
|
import Drafts from './components/drafts';
|
2023-01-20 19:23:59 +03:00
|
|
|
import Icon from './components/icon';
|
|
|
|
import Link from './components/link';
|
2022-12-10 12:14:48 +03:00
|
|
|
import Loader from './components/loader';
|
2023-01-29 10:23:53 +03:00
|
|
|
import MediaModal from './components/media-modal';
|
2022-12-10 12:14:48 +03:00
|
|
|
import Modal from './components/modal';
|
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';
|
|
|
|
import Hashtags from './pages/hashtags';
|
2022-12-10 12:14:48 +03:00
|
|
|
import Home from './pages/home';
|
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';
|
|
|
|
import Notifications from './pages/notifications';
|
2023-01-28 13:52:18 +03:00
|
|
|
import Public from './pages/public';
|
2022-12-10 12:14:48 +03:00
|
|
|
import Settings from './pages/settings';
|
|
|
|
import Status from './pages/status';
|
|
|
|
import Welcome from './pages/welcome';
|
|
|
|
import { getAccessToken } from './utils/auth';
|
2023-01-09 14:11:34 +03:00
|
|
|
import states, { saveStatus } from './utils/states';
|
2022-12-10 12:14:48 +03:00
|
|
|
import store from './utils/store';
|
|
|
|
|
2022-12-13 15:42:09 +03:00
|
|
|
window.__STATES__ = states;
|
2022-12-10 12:14:48 +03:00
|
|
|
|
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');
|
2023-01-20 19:23:59 +03:00
|
|
|
const navigate = useNavigate();
|
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"]')
|
|
|
|
.setAttribute('content', theme);
|
|
|
|
}
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
const instanceURL = store.local.get('instanceURL');
|
|
|
|
const accounts = store.local.getJSON('accounts') || [];
|
|
|
|
const code = (window.location.search.match(/code=([^&]+)/) || [])[1];
|
|
|
|
|
|
|
|
if (code) {
|
|
|
|
console.log({ code });
|
|
|
|
// Clear the code from the URL
|
|
|
|
window.history.replaceState({}, document.title, '/');
|
|
|
|
|
|
|
|
const clientID = store.session.get('clientID');
|
|
|
|
const clientSecret = store.session.get('clientSecret');
|
|
|
|
|
|
|
|
(async () => {
|
|
|
|
setUIState('loading');
|
|
|
|
const tokenJSON = await getAccessToken({
|
|
|
|
instanceURL,
|
|
|
|
client_id: clientID,
|
|
|
|
client_secret: clientSecret,
|
|
|
|
code,
|
|
|
|
});
|
|
|
|
const { access_token: accessToken } = tokenJSON;
|
|
|
|
store.session.set('accessToken', accessToken);
|
|
|
|
|
2023-01-28 13:52:18 +03:00
|
|
|
initMasto({
|
2022-12-10 12:14:48 +03:00
|
|
|
url: `https://${instanceURL}`,
|
|
|
|
accessToken,
|
|
|
|
});
|
|
|
|
|
2022-12-25 18:28:55 +03:00
|
|
|
const mastoAccount = await masto.v1.accounts.verifyCredentials();
|
2022-12-10 12:14:48 +03:00
|
|
|
|
2022-12-27 04:05:54 +03:00
|
|
|
// console.log({ tokenJSON, mastoAccount });
|
2022-12-10 12:14:48 +03:00
|
|
|
|
|
|
|
let account = accounts.find((a) => a.info.id === mastoAccount.id);
|
|
|
|
if (account) {
|
|
|
|
account.info = mastoAccount;
|
2023-01-01 15:59:55 +03:00
|
|
|
account.instanceURL = instanceURL.toLowerCase();
|
2022-12-10 12:14:48 +03:00
|
|
|
account.accessToken = accessToken;
|
|
|
|
} else {
|
|
|
|
account = {
|
|
|
|
info: mastoAccount,
|
|
|
|
instanceURL,
|
|
|
|
accessToken,
|
|
|
|
};
|
|
|
|
accounts.push(account);
|
|
|
|
}
|
|
|
|
|
|
|
|
store.local.setJSON('accounts', accounts);
|
|
|
|
store.session.set('currentAccount', account.info.id);
|
|
|
|
|
|
|
|
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);
|
2023-01-28 13:52:18 +03:00
|
|
|
if (accessToken) setIsLoggedIn(true);
|
2022-12-10 12:14:48 +03:00
|
|
|
|
2023-01-28 13:52:18 +03:00
|
|
|
initMasto({
|
|
|
|
url: `https://${instanceURL}`,
|
|
|
|
accessToken,
|
|
|
|
});
|
2022-12-21 15:34:24 +03:00
|
|
|
} else {
|
|
|
|
setUIState('default');
|
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-01-20 19:23:59 +03:00
|
|
|
const locationDeckMap = {
|
|
|
|
'/': 'home-page',
|
|
|
|
'/notifications': 'notifications-page',
|
|
|
|
};
|
2022-12-30 15:37:57 +03:00
|
|
|
const focusDeck = () => {
|
|
|
|
let timer = setTimeout(() => {
|
2023-01-20 19:23:59 +03:00
|
|
|
const page = document.getElementById(locationDeckMap[location.pathname]);
|
|
|
|
console.debug('FOCUS', location.pathname, page);
|
2022-12-30 15:37:57 +03:00
|
|
|
if (page) {
|
|
|
|
page.focus();
|
|
|
|
}
|
|
|
|
}, 100);
|
|
|
|
return () => clearTimeout(timer);
|
|
|
|
};
|
2023-01-20 19:23:59 +03:00
|
|
|
useEffect(focusDeck, [location]);
|
2022-12-30 15:37:57 +03:00
|
|
|
useEffect(() => {
|
|
|
|
if (
|
|
|
|
!snapStates.showCompose &&
|
|
|
|
!snapStates.showSettings &&
|
|
|
|
!snapStates.showAccount
|
|
|
|
) {
|
|
|
|
focusDeck();
|
|
|
|
}
|
|
|
|
}, [snapStates.showCompose, snapStates.showSettings, snapStates.showAccount]);
|
2022-12-10 12:14:48 +03:00
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
// HACK: prevent this from running again due to HMR
|
|
|
|
if (states.init) return;
|
|
|
|
if (isLoggedIn) {
|
2023-01-28 13:52:18 +03:00
|
|
|
requestAnimationFrame(startVisibility);
|
2022-12-10 12:14:48 +03:00
|
|
|
states.init = true;
|
|
|
|
}
|
|
|
|
}, [isLoggedIn]);
|
|
|
|
|
2023-01-21 05:08:55 +03:00
|
|
|
const { prevLocation } = snapStates;
|
|
|
|
const backgroundLocation = useRef(prevLocation || null);
|
|
|
|
const isModalPage = /^\/s\//i.test(location.pathname);
|
|
|
|
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
|
|
|
|
|
|
|
const nonRootLocation = useMemo(() => {
|
|
|
|
const { pathname } = location;
|
2023-01-28 13:52:18 +03:00
|
|
|
return !/^\/(login|welcome|p)/.test(pathname);
|
2023-01-20 19:23:59 +03:00
|
|
|
}, [location]);
|
|
|
|
|
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' ? (
|
|
|
|
<Loader />
|
|
|
|
) : (
|
|
|
|
<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-01-28 13:52:18 +03:00
|
|
|
{isLoggedIn && <Route path="/b" element={<Bookmarks />} />}
|
|
|
|
{isLoggedIn && <Route path="/f" element={<Favourites />} />}
|
|
|
|
{isLoggedIn && <Route path="/l/:id" element={<Lists />} />}
|
|
|
|
{isLoggedIn && <Route path="/t/:hashtag" element={<Hashtags />} />}
|
2023-01-29 18:37:13 +03:00
|
|
|
{isLoggedIn && <Route path="/a/:id" element={<AccountStatuses />} />}
|
2023-01-28 13:52:18 +03:00
|
|
|
<Route path="/p/l?/:instance" element={<Public />} />
|
|
|
|
{/* <Route path="/:anything" element={<NotFound />} /> */}
|
2023-01-20 19:23:59 +03:00
|
|
|
</Routes>
|
|
|
|
<Routes>
|
|
|
|
{isLoggedIn && <Route path="/s/:id" element={<Status />} />}
|
|
|
|
</Routes>
|
|
|
|
<nav id="tab-bar" hidden>
|
|
|
|
<li>
|
|
|
|
<Link to="/">
|
|
|
|
<Icon icon="home" alt="Home" size="xl" />
|
|
|
|
</Link>
|
|
|
|
</li>
|
|
|
|
<li>
|
|
|
|
<Link to="/notifications">
|
|
|
|
<Icon icon="notification" alt="Notifications" size="xl" />
|
|
|
|
</Link>
|
|
|
|
</li>
|
|
|
|
<li>
|
|
|
|
<Link to="/bookmarks">
|
|
|
|
<Icon icon="bookmark" alt="Bookmarks" size="xl" />
|
|
|
|
</Link>
|
|
|
|
</li>
|
|
|
|
</nav>
|
2022-12-10 12:14:48 +03:00
|
|
|
{!!snapStates.showCompose && (
|
|
|
|
<Modal>
|
|
|
|
<Compose
|
|
|
|
replyToStatus={
|
|
|
|
typeof snapStates.showCompose !== 'boolean'
|
|
|
|
? snapStates.showCompose.replyToStatus
|
2023-01-02 07:03:06 +03:00
|
|
|
: window.__COMPOSE__?.replyToStatus || null
|
|
|
|
}
|
|
|
|
editStatus={
|
|
|
|
states.showCompose?.editStatus ||
|
|
|
|
window.__COMPOSE__?.editStatus ||
|
|
|
|
null
|
|
|
|
}
|
|
|
|
draftStatus={
|
|
|
|
states.showCompose?.draftStatus ||
|
|
|
|
window.__COMPOSE__?.draftStatus ||
|
|
|
|
null
|
2022-12-10 12:14:48 +03:00
|
|
|
}
|
2022-12-13 16:54:16 +03:00
|
|
|
onClose={(results) => {
|
|
|
|
const { newStatus } = results || {};
|
2022-12-10 12:14:48 +03:00
|
|
|
states.showCompose = false;
|
2023-01-02 07:03:06 +03:00
|
|
|
window.__COMPOSE__ = null;
|
2022-12-13 16:54:16 +03:00
|
|
|
if (newStatus) {
|
2022-12-10 12:14:48 +03:00
|
|
|
states.reloadStatusPage++;
|
2022-12-28 13:05:22 +03:00
|
|
|
setTimeout(() => {
|
|
|
|
const toast = Toastify({
|
|
|
|
text: 'Status posted. Check it out.',
|
|
|
|
duration: 10_000, // 10 seconds
|
|
|
|
gravity: 'bottom',
|
|
|
|
position: 'center',
|
|
|
|
// destination: `/#/s/${newStatus.id}`,
|
|
|
|
onClick: () => {
|
|
|
|
toast.hideToast();
|
2023-01-20 19:23:59 +03:00
|
|
|
states.prevLocation = location;
|
|
|
|
navigate(`/s/${newStatus.id}`);
|
2022-12-28 13:05:22 +03:00
|
|
|
},
|
|
|
|
});
|
|
|
|
toast.showToast();
|
|
|
|
}, 1000);
|
2022-12-10 12:14:48 +03:00
|
|
|
}
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
</Modal>
|
|
|
|
)}
|
|
|
|
{!!snapStates.showSettings && (
|
|
|
|
<Modal
|
|
|
|
onClick={(e) => {
|
|
|
|
if (e.target === e.currentTarget) {
|
|
|
|
states.showSettings = false;
|
|
|
|
}
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
<Settings
|
|
|
|
onClose={() => {
|
|
|
|
states.showSettings = false;
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
</Modal>
|
|
|
|
)}
|
|
|
|
{!!snapStates.showAccount && (
|
|
|
|
<Modal
|
|
|
|
class="light"
|
|
|
|
onClick={(e) => {
|
|
|
|
if (e.target === e.currentTarget) {
|
|
|
|
states.showAccount = false;
|
|
|
|
}
|
|
|
|
}}
|
|
|
|
>
|
2023-01-29 18:37:13 +03:00
|
|
|
<Account
|
|
|
|
account={snapStates.showAccount}
|
|
|
|
onClose={() => {
|
|
|
|
states.showAccount = false;
|
|
|
|
}}
|
|
|
|
/>
|
2022-12-10 12:14:48 +03:00
|
|
|
</Modal>
|
|
|
|
)}
|
2023-01-13 10:30:09 +03:00
|
|
|
{!!snapStates.showDrafts && (
|
|
|
|
<Modal
|
|
|
|
onClick={(e) => {
|
|
|
|
if (e.target === e.currentTarget) {
|
|
|
|
states.showDrafts = false;
|
|
|
|
}
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
<Drafts />
|
|
|
|
</Modal>
|
|
|
|
)}
|
2023-01-29 10:23:53 +03:00
|
|
|
{!!snapStates.showMediaModal && (
|
|
|
|
<Modal
|
|
|
|
onClick={(e) => {
|
|
|
|
if (
|
|
|
|
e.target === e.currentTarget ||
|
|
|
|
e.target.classList.contains('media')
|
|
|
|
) {
|
|
|
|
states.showMediaModal = false;
|
|
|
|
}
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
<MediaModal
|
|
|
|
mediaAttachments={snapStates.showMediaModal.mediaAttachments}
|
|
|
|
index={snapStates.showMediaModal.index}
|
|
|
|
statusID={snapStates.showMediaModal.statusID}
|
|
|
|
onClose={() => {
|
|
|
|
states.showMediaModal = false;
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
</Modal>
|
|
|
|
)}
|
2022-12-10 12:14:48 +03:00
|
|
|
</>
|
|
|
|
);
|
|
|
|
}
|
2022-12-31 20:46:08 +03:00
|
|
|
|
2023-01-28 13:52:18 +03:00
|
|
|
function initMasto(params) {
|
|
|
|
const clientParams = {
|
|
|
|
url: params.url || 'https://mastodon.social',
|
|
|
|
accessToken: params.accessToken || null,
|
|
|
|
disableVersionCheck: true,
|
|
|
|
timeout: 30_000,
|
|
|
|
};
|
|
|
|
window.masto = createClient(clientParams);
|
|
|
|
|
|
|
|
(async () => {
|
|
|
|
// Request v2, fallback to v1 if fail
|
|
|
|
let info;
|
|
|
|
try {
|
|
|
|
info = await masto.v2.instance.fetch();
|
|
|
|
} catch (e) {}
|
|
|
|
if (!info) {
|
|
|
|
try {
|
|
|
|
info = await masto.v1.instances.fetch();
|
|
|
|
} catch (e) {}
|
|
|
|
}
|
|
|
|
if (!info) return;
|
|
|
|
console.log(info);
|
|
|
|
const {
|
|
|
|
// v1
|
|
|
|
uri,
|
|
|
|
urls: { streamingApi } = {},
|
|
|
|
// v2
|
|
|
|
domain,
|
|
|
|
configuration: { urls: { streaming } = {} } = {},
|
|
|
|
} = info;
|
|
|
|
if (uri || domain) {
|
|
|
|
const instances = store.local.getJSON('instances') || {};
|
|
|
|
instances[(domain || uri).toLowerCase()] = info;
|
|
|
|
store.local.setJSON('instances', instances);
|
|
|
|
}
|
|
|
|
if (streamingApi || streaming) {
|
|
|
|
window.masto = createClient({
|
|
|
|
...clientParams,
|
|
|
|
streamingApiUrl: streaming || streamingApi,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
})();
|
|
|
|
}
|
|
|
|
|
2023-01-27 06:47:30 +03:00
|
|
|
let ws;
|
2022-12-31 20:46:08 +03:00
|
|
|
async function startStream() {
|
2023-01-27 06:47:30 +03:00
|
|
|
if (
|
|
|
|
ws &&
|
|
|
|
(ws.readyState === WebSocket.CONNECTING || ws.readyState === WebSocket.OPEN)
|
|
|
|
) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-12-31 20:46:08 +03:00
|
|
|
const stream = await masto.v1.stream.streamUser();
|
|
|
|
console.log('STREAM START', { stream });
|
2023-01-27 06:47:30 +03:00
|
|
|
ws = stream.ws;
|
|
|
|
|
2023-01-01 07:01:54 +03:00
|
|
|
const handleNewStatus = debounce((status) => {
|
2022-12-31 20:46:08 +03:00
|
|
|
console.log('UPDATE', status);
|
|
|
|
|
|
|
|
const inHomeNew = states.homeNew.find((s) => s.id === status.id);
|
2023-01-27 06:47:30 +03:00
|
|
|
const inHome = status.id === states.homeLast?.id;
|
2022-12-31 20:46:08 +03:00
|
|
|
if (!inHomeNew && !inHome) {
|
2023-01-27 06:47:30 +03:00
|
|
|
if (states.settings.boostsCarousel && status.reblog) {
|
|
|
|
// do nothing
|
|
|
|
} else {
|
|
|
|
states.homeNew.unshift({
|
|
|
|
id: status.id,
|
|
|
|
reblog: status.reblog?.id,
|
|
|
|
reply: !!status.inReplyToAccountId,
|
|
|
|
});
|
|
|
|
console.log('homeNew 1', [...states.homeNew]);
|
|
|
|
}
|
2022-12-31 20:46:08 +03:00
|
|
|
}
|
|
|
|
|
2023-01-09 14:11:34 +03:00
|
|
|
saveStatus(status);
|
2023-01-01 07:01:54 +03:00
|
|
|
}, 5000);
|
|
|
|
stream.on('update', handleNewStatus);
|
2022-12-31 20:46:08 +03:00
|
|
|
stream.on('status.update', (status) => {
|
|
|
|
console.log('STATUS.UPDATE', status);
|
2023-01-09 14:11:34 +03:00
|
|
|
saveStatus(status);
|
2022-12-31 20:46:08 +03:00
|
|
|
});
|
|
|
|
stream.on('delete', (statusID) => {
|
|
|
|
console.log('DELETE', statusID);
|
2023-01-07 15:26:23 +03:00
|
|
|
// delete states.statuses[statusID];
|
|
|
|
const s = states.statuses[statusID];
|
2022-12-31 20:46:08 +03:00
|
|
|
if (s) s._deleted = true;
|
|
|
|
});
|
|
|
|
stream.on('notification', (notification) => {
|
|
|
|
console.log('NOTIFICATION', notification);
|
|
|
|
|
|
|
|
const inNotificationsNew = states.notificationsNew.find(
|
|
|
|
(n) => n.id === notification.id,
|
|
|
|
);
|
2023-01-27 06:47:30 +03:00
|
|
|
const inNotifications = notification.id === states.notificationLast?.id;
|
2022-12-31 20:46:08 +03:00
|
|
|
if (!inNotificationsNew && !inNotifications) {
|
|
|
|
states.notificationsNew.unshift(notification);
|
|
|
|
}
|
|
|
|
|
2023-01-09 14:11:34 +03:00
|
|
|
saveStatus(notification.status, { override: false });
|
2022-12-31 20:46:08 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
stream.ws.onclose = () => {
|
|
|
|
console.log('STREAM CLOSED!');
|
2023-01-27 06:47:30 +03:00
|
|
|
if (document.visibilityState !== 'hidden') {
|
2022-12-31 20:46:08 +03:00
|
|
|
startStream();
|
2023-01-27 06:47:30 +03:00
|
|
|
}
|
2022-12-31 20:46:08 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
return {
|
|
|
|
stream,
|
|
|
|
stopStream: () => {
|
|
|
|
stream.ws.close();
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2023-01-28 13:52:18 +03:00
|
|
|
let lastHidden;
|
2022-12-31 20:46:08 +03:00
|
|
|
function startVisibility() {
|
2023-01-27 06:47:30 +03:00
|
|
|
const handleVisible = (visible) => {
|
|
|
|
if (!visible) {
|
2022-12-31 20:46:08 +03:00
|
|
|
const timestamp = Date.now();
|
2023-01-28 13:52:18 +03:00
|
|
|
lastHidden = timestamp;
|
2022-12-31 20:46:08 +03:00
|
|
|
} else {
|
|
|
|
const timestamp = Date.now();
|
|
|
|
const diff = timestamp - lastHidden;
|
|
|
|
const diffMins = Math.round(diff / 1000 / 60);
|
2023-01-28 13:52:18 +03:00
|
|
|
console.log(`visible: ${visible}`, { lastHidden, diffMins });
|
|
|
|
if (!lastHidden || diffMins > 1) {
|
2023-01-27 06:47:30 +03:00
|
|
|
(async () => {
|
|
|
|
try {
|
2023-01-27 09:36:47 +03:00
|
|
|
const firstStatusID = states.homeLast?.id;
|
|
|
|
const firstNotificationID = states.notificationsLast?.id;
|
2023-01-27 06:47:30 +03:00
|
|
|
const fetchHome = masto.v1.timelines.listHome({
|
2023-01-27 09:36:47 +03:00
|
|
|
limit: 5,
|
2023-01-27 06:47:30 +03:00
|
|
|
...(firstStatusID && { sinceId: firstStatusID }),
|
|
|
|
});
|
|
|
|
const fetchNotifications = masto.v1.notifications.list({
|
|
|
|
limit: 1,
|
|
|
|
...(firstNotificationID && { sinceId: firstNotificationID }),
|
|
|
|
});
|
|
|
|
|
|
|
|
const newStatuses = await fetchHome;
|
2023-01-27 09:36:47 +03:00
|
|
|
const hasOneAndReblog =
|
|
|
|
newStatuses.length === 1 && newStatuses?.[0]?.reblog;
|
2023-01-27 10:17:56 +03:00
|
|
|
if (newStatuses.length) {
|
2023-01-27 09:36:47 +03:00
|
|
|
if (states.settings.boostsCarousel && hasOneAndReblog) {
|
2023-01-27 06:47:30 +03:00
|
|
|
// do nothing
|
|
|
|
} else {
|
2022-12-31 20:46:08 +03:00
|
|
|
states.homeNew = newStatuses.map((status) => {
|
2023-01-09 14:11:34 +03:00
|
|
|
saveStatus(status);
|
2022-12-31 20:46:08 +03:00
|
|
|
return {
|
|
|
|
id: status.id,
|
|
|
|
reblog: status.reblog?.id,
|
|
|
|
reply: !!status.inReplyToAccountId,
|
|
|
|
};
|
|
|
|
});
|
2023-01-27 06:47:30 +03:00
|
|
|
console.log('homeNew 2', [...states.homeNew]);
|
2022-12-31 20:46:08 +03:00
|
|
|
}
|
2023-01-27 06:47:30 +03:00
|
|
|
}
|
2022-12-31 20:46:08 +03:00
|
|
|
|
2023-01-27 06:47:30 +03:00
|
|
|
const newNotifications = await fetchNotifications;
|
|
|
|
if (newNotifications.length) {
|
|
|
|
const notification = newNotifications[0];
|
|
|
|
const inNotificationsNew = states.notificationsNew.find(
|
|
|
|
(n) => n.id === notification.id,
|
|
|
|
);
|
|
|
|
const inNotifications =
|
|
|
|
notification.id === states.notificationLast?.id;
|
|
|
|
if (!inNotificationsNew && !inNotifications) {
|
|
|
|
states.notificationsNew.unshift(notification);
|
2022-12-31 20:46:08 +03:00
|
|
|
}
|
2023-01-27 06:47:30 +03:00
|
|
|
|
|
|
|
saveStatus(notification.status, { override: false });
|
2022-12-31 20:46:08 +03:00
|
|
|
}
|
2023-01-27 06:47:30 +03:00
|
|
|
} catch (e) {
|
|
|
|
// Silently fail
|
|
|
|
console.error(e);
|
|
|
|
} finally {
|
|
|
|
startStream();
|
|
|
|
}
|
|
|
|
})();
|
2022-12-31 20:46:08 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2023-01-27 06:47:30 +03:00
|
|
|
|
|
|
|
const handleVisibilityChange = () => {
|
|
|
|
const hidden = document.visibilityState === 'hidden';
|
|
|
|
handleVisible(!hidden);
|
|
|
|
console.log('VISIBILITY: ' + (hidden ? 'hidden' : 'visible'));
|
|
|
|
};
|
2022-12-31 20:46:08 +03:00
|
|
|
document.addEventListener('visibilitychange', handleVisibilityChange);
|
2023-01-28 13:52:18 +03:00
|
|
|
requestAnimationFrame(handleVisibilityChange);
|
2022-12-31 20:46:08 +03:00
|
|
|
return {
|
|
|
|
stop: () => {
|
|
|
|
document.removeEventListener('visibilitychange', handleVisibilityChange);
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export { App };
|