2021-07-05 06:23:23 -04:00
|
|
|
import {
|
2021-07-05 06:25:20 -04:00
|
|
|
Home,
|
|
|
|
UserDetail,
|
|
|
|
Wrench,
|
|
|
|
Notepad,
|
2021-07-05 06:23:23 -04:00
|
|
|
} from "@styled-icons/boxicons-solid";
|
|
|
|
import { Link, Redirect, useLocation, useParams } from "react-router-dom";
|
|
|
|
import { Channels } from "revolt.js/dist/api/objects";
|
|
|
|
import { Users as UsersNS } from "revolt.js/dist/api/objects";
|
|
|
|
|
2021-07-04 10:53:06 -04:00
|
|
|
import { Text } from "preact-i18n";
|
2021-06-24 11:22:45 -04:00
|
|
|
import { useContext, useEffect } from "preact/hooks";
|
2021-06-19 10:29:04 -04:00
|
|
|
|
2021-07-05 06:23:23 -04:00
|
|
|
import ConditionalLink from "../../../lib/ConditionalLink";
|
2021-06-20 15:30:42 -04:00
|
|
|
import PaintCounter from "../../../lib/PaintCounter";
|
2021-07-05 06:23:23 -04:00
|
|
|
import { isTouchscreenDevice } from "../../../lib/isTouchscreenDevice";
|
|
|
|
|
|
|
|
import { dispatch } from "../../../redux";
|
2021-06-20 15:30:42 -04:00
|
|
|
import { connectState } from "../../../redux/connector";
|
2021-06-19 10:29:04 -04:00
|
|
|
import { Unreads } from "../../../redux/reducers/unreads";
|
2021-07-05 06:23:23 -04:00
|
|
|
|
2021-06-19 15:24:11 -04:00
|
|
|
import { useIntermediate } from "../../../context/intermediate/Intermediate";
|
2021-07-05 06:23:23 -04:00
|
|
|
import { AppContext } from "../../../context/revoltjs/RevoltClient";
|
|
|
|
import {
|
2021-07-05 06:25:20 -04:00
|
|
|
useDMs,
|
|
|
|
useForceUpdate,
|
|
|
|
useUsers,
|
2021-07-05 06:23:23 -04:00
|
|
|
} from "../../../context/revoltjs/hooks";
|
2021-06-19 10:29:04 -04:00
|
|
|
|
2021-07-05 06:23:23 -04:00
|
|
|
import Category from "../../ui/Category";
|
2021-06-29 17:21:26 -04:00
|
|
|
import placeholderSVG from "../items/placeholder.svg";
|
2021-07-05 06:23:23 -04:00
|
|
|
import { mapChannelWithUnread, useUnreads } from "./common";
|
|
|
|
|
|
|
|
import { GenericSidebarBase, GenericSidebarList } from "../SidebarBase";
|
|
|
|
import ButtonItem, { ChannelButton } from "../items/ButtonItem";
|
|
|
|
import ConnectionStatus from "../items/ConnectionStatus";
|
2021-06-29 17:21:26 -04:00
|
|
|
|
2021-07-05 05:59:48 -04:00
|
|
|
type Props = {
|
2021-07-05 06:25:20 -04:00
|
|
|
unreads: Unreads;
|
2021-07-05 06:23:23 -04:00
|
|
|
};
|
2021-06-19 10:29:04 -04:00
|
|
|
|
|
|
|
function HomeSidebar(props: Props) {
|
2021-07-05 06:25:20 -04:00
|
|
|
const { pathname } = useLocation();
|
|
|
|
const client = useContext(AppContext);
|
|
|
|
const { channel } = useParams<{ channel: string }>();
|
|
|
|
const { openScreen } = useIntermediate();
|
|
|
|
|
|
|
|
const ctx = useForceUpdate();
|
|
|
|
const channels = useDMs(ctx);
|
|
|
|
|
|
|
|
const obj = channels.find((x) => x?._id === channel);
|
|
|
|
if (channel && !obj) return <Redirect to="/" />;
|
|
|
|
if (obj) useUnreads({ ...props, channel: obj });
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (!channel) return;
|
|
|
|
|
|
|
|
dispatch({
|
|
|
|
type: "LAST_OPENED_SET",
|
|
|
|
parent: "home",
|
|
|
|
child: channel,
|
|
|
|
});
|
|
|
|
}, [channel]);
|
|
|
|
|
|
|
|
const channelsArr = channels
|
|
|
|
.filter((x) => x.channel_type !== "SavedMessages")
|
|
|
|
.map((x) => mapChannelWithUnread(x, props.unreads));
|
|
|
|
|
2021-07-25 07:50:34 -04:00
|
|
|
const users = useUsers(undefined, ctx);
|
2021-07-05 06:25:20 -04:00
|
|
|
channelsArr.sort((b, a) => a.timestamp.localeCompare(b.timestamp));
|
|
|
|
|
|
|
|
return (
|
|
|
|
<GenericSidebarBase padding>
|
|
|
|
<ConnectionStatus />
|
|
|
|
<GenericSidebarList>
|
2021-07-10 06:21:05 -04:00
|
|
|
<ConditionalLink active={pathname === "/"} to="/">
|
|
|
|
<ButtonItem active={pathname === "/"}>
|
|
|
|
<Home size={20} />
|
|
|
|
<span>
|
|
|
|
<Text id="app.navigation.tabs.home" />
|
|
|
|
</span>
|
|
|
|
</ButtonItem>
|
|
|
|
</ConditionalLink>
|
2021-07-05 06:25:20 -04:00
|
|
|
{!isTouchscreenDevice && (
|
|
|
|
<>
|
|
|
|
<ConditionalLink
|
|
|
|
active={pathname === "/friends"}
|
|
|
|
to="/friends">
|
|
|
|
<ButtonItem
|
|
|
|
active={pathname === "/friends"}
|
|
|
|
alert={
|
|
|
|
typeof users.find(
|
|
|
|
(user) =>
|
|
|
|
user?.relationship ===
|
|
|
|
UsersNS.Relationship.Incoming,
|
|
|
|
) !== "undefined"
|
|
|
|
? "unread"
|
|
|
|
: undefined
|
|
|
|
}>
|
|
|
|
<UserDetail size={20} />
|
|
|
|
<span>
|
|
|
|
<Text id="app.navigation.tabs.friends" />
|
|
|
|
</span>
|
|
|
|
</ButtonItem>
|
|
|
|
</ConditionalLink>
|
|
|
|
</>
|
|
|
|
)}
|
|
|
|
<ConditionalLink
|
|
|
|
active={obj?.channel_type === "SavedMessages"}
|
|
|
|
to="/open/saved">
|
|
|
|
<ButtonItem active={obj?.channel_type === "SavedMessages"}>
|
|
|
|
<Notepad size={20} />
|
|
|
|
<span>
|
|
|
|
<Text id="app.navigation.tabs.saved" />
|
|
|
|
</span>
|
|
|
|
</ButtonItem>
|
|
|
|
</ConditionalLink>
|
|
|
|
{import.meta.env.DEV && (
|
|
|
|
<Link to="/dev">
|
|
|
|
<ButtonItem active={pathname === "/dev"}>
|
|
|
|
<Wrench size={20} />
|
|
|
|
<span>
|
|
|
|
<Text id="app.navigation.tabs.dev" />
|
|
|
|
</span>
|
|
|
|
</ButtonItem>
|
|
|
|
</Link>
|
|
|
|
)}
|
|
|
|
<Category
|
|
|
|
text={<Text id="app.main.categories.conversations" />}
|
|
|
|
action={() =>
|
|
|
|
openScreen({
|
|
|
|
id: "special_input",
|
|
|
|
type: "create_group",
|
|
|
|
})
|
|
|
|
}
|
|
|
|
/>
|
2021-07-24 12:01:50 -04:00
|
|
|
{channelsArr.length === 0 && (
|
|
|
|
<img src={placeholderSVG} loading="eager" />
|
|
|
|
)}
|
2021-07-05 06:25:20 -04:00
|
|
|
{channelsArr.map((x) => {
|
|
|
|
let user;
|
|
|
|
if (x.channel_type === "DirectMessage") {
|
|
|
|
if (!x.active) return null;
|
|
|
|
|
2021-07-10 10:57:29 -04:00
|
|
|
const recipient = client.channels.getRecipient(x._id);
|
2021-07-05 06:25:20 -04:00
|
|
|
user = users.find((x) => x?._id === recipient);
|
|
|
|
|
|
|
|
if (!user) {
|
|
|
|
console.warn(
|
|
|
|
`Skipped DM ${x._id} because user was missing.`,
|
|
|
|
);
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<ConditionalLink
|
|
|
|
active={x._id === channel}
|
|
|
|
to={`/channel/${x._id}`}>
|
|
|
|
<ChannelButton
|
|
|
|
user={user}
|
|
|
|
channel={x}
|
|
|
|
alert={x.unread}
|
|
|
|
alertCount={x.alertCount}
|
|
|
|
active={x._id === channel}
|
|
|
|
/>
|
|
|
|
</ConditionalLink>
|
|
|
|
);
|
|
|
|
})}
|
|
|
|
<PaintCounter />
|
|
|
|
</GenericSidebarList>
|
|
|
|
</GenericSidebarBase>
|
|
|
|
);
|
2021-07-05 06:23:23 -04:00
|
|
|
}
|
2021-06-19 10:29:04 -04:00
|
|
|
|
|
|
|
export default connectState(
|
2021-07-05 06:25:20 -04:00
|
|
|
HomeSidebar,
|
|
|
|
(state) => {
|
|
|
|
return {
|
|
|
|
unreads: state.unreads,
|
|
|
|
};
|
|
|
|
},
|
|
|
|
true,
|
2021-06-19 10:29:04 -04:00
|
|
|
);
|