import { Plus } from "@styled-icons/boxicons-regular"; import { Cog } from "@styled-icons/boxicons-solid"; import { observer } from "mobx-react-lite"; import { Link, useHistory, useLocation, useParams } from "react-router-dom"; import { RelationshipStatus } from "revolt-api/types/Users"; import styled, { css } from "styled-components"; import { attachContextMenu } from "preact-context-menu"; import { Text } from "preact-i18n"; import ConditionalLink from "../../../lib/ConditionalLink"; import PaintCounter from "../../../lib/PaintCounter"; import { isTouchscreenDevice } from "../../../lib/isTouchscreenDevice"; import { connectState } from "../../../redux/connector"; import { LastOpened } from "../../../redux/reducers/last_opened"; import { Unreads } from "../../../redux/reducers/unreads"; import { useIntermediate } from "../../../context/intermediate/Intermediate"; import { useClient } from "../../../context/revoltjs/RevoltClient"; import ServerIcon from "../../common/ServerIcon"; import Tooltip from "../../common/Tooltip"; import UserHover from "../../common/user/UserHover"; import UserIcon from "../../common/user/UserIcon"; import IconButton from "../../ui/IconButton"; import LineDivider from "../../ui/LineDivider"; import { mapChannelWithUnread } from "./common"; import { Children } from "../../../types/Preact"; function Icon({ children, unread, count, size, }: { children: Children; unread?: "mention" | "unread"; count: number | 0; size: number; }) { return ( {children} {unread === "unread" && ( )} {unread === "mention" && ( <> {count < 10 ? count : "9+"} > )} ); } const ServersBase = styled.div` width: 56px; height: 100%; padding-left: 2px; display: flex; flex-direction: column; ${isTouchscreenDevice && css` padding-bottom: 50px; `} `; const ServerList = styled.div` flex-grow: 1; display: flex; overflow-y: scroll; padding-bottom: 20px; flex-direction: column; scrollbar-width: none; > :first-child > svg { margin: 6px 0 6px 4px; } &::-webkit-scrollbar { width: 0px; } `; const ServerEntry = styled.div<{ active: boolean; home?: boolean }>` height: 58px; display: flex; align-items: center; :focus { outline: 3px solid blue; } > div { height: 42px; padding-inline-start: 6px; display: grid; place-items: center; border-start-start-radius: 50%; border-end-start-radius: 50%; &:active { transform: translateY(1px); } ${(props) => props.active && css` &:active { transform: none; } `} } > span { width: 0; display: relative; ${(props) => !props.active && css` display: none; `} svg { margin-top: 5px; pointer-events: none; } } ${(props) => (!props.active || props.home) && css` cursor: pointer; `} `; const SettingsButton = styled.div` width: 50px; height: 56px; display: grid; place-items: center; `; function Swoosh() { return ( ); } interface Props { unreads: Unreads; lastOpened: LastOpened; } export const ServerListSidebar = observer(({ unreads, lastOpened }: Props) => { const client = useClient(); const { server: server_id } = useParams<{ server?: string }>(); const server = server_id ? client.servers.get(server_id) : undefined; const activeServers = [...client.servers.values()]; const channels = [...client.channels.values()].map((x) => mapChannelWithUnread(x, unreads), ); const unreadChannels = channels .filter((x) => x.unread) .map((x) => x.channel?._id); const servers = activeServers.map((server) => { let alertCount = 0; for (const id of server.channel_ids) { const channel = channels.find((x) => x.channel?._id === id); if (channel?.alertCount) { alertCount += channel.alertCount; } } return { server, unread: (typeof server.channel_ids.find((x) => unreadChannels.includes(x), ) !== "undefined" ? alertCount > 0 ? "mention" : "unread" : undefined) as "mention" | "unread" | undefined, alertCount, }; }); const history = useHistory(); const path = useLocation().pathname; const { openScreen } = useIntermediate(); let homeUnread: "mention" | "unread" | undefined; let alertCount = 0; for (const x of channels) { if (x.channel?.channel_type === "Group" && x.unread) { homeUnread = "unread"; alertCount += x.alertCount ?? 0; } if ( x.channel?.channel_type === "DirectMessage" && x.channel.active && x.unread ) { alertCount++; } } alertCount += [...client.users.values()].filter( (x) => x.relationship === RelationshipStatus.Incoming, ).length; if (alertCount > 0) homeUnread = "mention"; const homeActive = typeof server === "undefined" && !path.startsWith("/invite"); return ( homeActive && history.push("/settings") }> {servers.map((entry) => { const active = entry.server._id === server?._id; const id = lastOpened[entry.server._id]; return ( ); })} openScreen({ id: "special_input", type: "create_server", }) }> {!isTouchscreenDevice && ( } placement="right"> )} ); }); export default connectState(ServerListSidebar, (state) => { return { unreads: state.unreads, lastOpened: state.lastOpened, }; });