phanpy/src/pages/home.jsx

115 lines
3.1 KiB
React
Raw Normal View History

2023-02-18 12:48:24 +00:00
import { useEffect, useState } from 'preact/hooks';
import { useHotkeys } from 'react-hotkeys-hook';
import { useSnapshot } from 'valtio';
2022-12-10 09:14:48 +00:00
import Icon from '../components/icon';
import Link from '../components/link';
import db from '../utils/db';
2023-02-09 14:27:49 +00:00
import openCompose from '../utils/open-compose';
import states from '../utils/states';
import { getCurrentAccountNS } from '../utils/store-utils';
2022-12-10 09:14:48 +00:00
2023-02-09 14:27:49 +00:00
import Following from './following';
2023-02-09 14:27:49 +00:00
function Home() {
const snapStates = useSnapshot(states);
useEffect(() => {
(async () => {
const keys = await db.drafts.keys();
if (keys.length) {
const ns = getCurrentAccountNS();
const ownKeys = keys.filter((key) => key.startsWith(ns));
if (ownKeys.length) {
states.showDrafts = true;
}
}
})();
}, []);
2023-02-18 12:48:24 +00:00
const { shortcuts } = snapStates;
const { shortcutsColumnsMode } = snapStates.settings || {};
const [shortcutsComponents, setShortcutsComponents] = useState([]);
useEffect(() => {
if (shortcutsColumnsMode) {
const componentsPromises = shortcuts.map((shortcut) => {
const { type, ...params } = shortcut;
// Uppercase type
return import(`./${type}`).then((module) => {
const { default: Component } = module;
return <Component {...params} />;
});
});
Promise.all(componentsPromises)
.then((components) => {
setShortcutsComponents(components);
})
.catch((err) => {
console.error(err);
});
}
}, [shortcutsColumnsMode, shortcuts]);
useHotkeys(
['1', '2', '3', '4', '5', '6', '7', '8', '9'],
(e, handler) => {
try {
const index = parseInt(handler.keys[0], 10) - 1;
document.querySelectorAll('#columns > *')[index].focus();
} catch (e) {
console.error(e);
}
},
{
enabled: shortcutsColumnsMode,
},
);
2022-12-10 09:14:48 +00:00
return (
<>
2023-02-18 12:48:24 +00:00
{shortcutsColumnsMode ? (
<div id="columns">{shortcutsComponents}</div>
) : (
<Following
title="Home"
path="/"
id="home"
headerStart={false}
headerEnd={
<Link
to="/notifications"
class={`button plain ${
snapStates.notificationsShowNew ? 'has-badge' : ''
}`}
onClick={(e) => {
e.stopPropagation();
}}
>
<Icon icon="notification" size="l" alt="Notifications" />
</Link>
}
/>
)}
<button
2023-02-09 14:27:49 +00:00
// hidden={scrollDirection === 'end' && !nearReachStart}
type="button"
id="compose-button"
onClick={(e) => {
if (e.shiftKey) {
const newWin = openCompose();
if (!newWin) {
alert('Looks like your browser is blocking popups.');
states.showCompose = true;
}
} else {
states.showCompose = true;
}
}}
>
2023-02-16 09:51:54 +00:00
<Icon icon="quill" size="xl" alt="Compose" />
</button>
</>
2022-12-10 09:14:48 +00:00
);
2022-12-16 05:27:04 +00:00
}
2023-02-09 14:27:49 +00:00
export default Home;