import { HelpCircle } from "@styled-icons/boxicons-regular"; import { Android, Firefoxbrowser, Googlechrome, Ios, Linux, Macos, Microsoftedge, Safari, Windows, } from "@styled-icons/simple-icons"; import dayjs from "dayjs"; import relativeTime from "dayjs/plugin/relativeTime"; import { useHistory } from "react-router-dom"; import { decodeTime } from "ulid"; import styles from "./Panes.module.scss"; import { Text } from "preact-i18n"; import { useContext, useEffect, useState } from "preact/hooks"; import { AppContext } from "../../../context/revoltjs/RevoltClient"; import Button from "../../../components/ui/Button"; import Preloader from "../../../components/ui/Preloader"; import Tip from "../../../components/ui/Tip"; dayjs.extend(relativeTime); interface Session { id: string; friendly_name: string; } export function Sessions() { const client = useContext(AppContext); const deviceId = client.session?.id; const [sessions, setSessions] = useState(undefined); const [attemptingDelete, setDelete] = useState([]); const history = useHistory(); function switchPage(to: string) { history.replace(`/settings/${to}`); } useEffect(() => { client.req("GET", "/auth/sessions").then((data) => { data.sort( (a, b) => (b.id === deviceId ? 1 : 0) - (a.id === deviceId ? 1 : 0), ); setSessions(data); }); }, []); if (typeof sessions === "undefined") { return (
); } function getIcon(session: Session) { const name = session.friendly_name; switch (true) { case /firefox/i.test(name): return ; case /chrome/i.test(name): return ; case /safari/i.test(name): return ; case /edge/i.test(name): return ; default: return ; } } function getSystemIcon(session: Session) { const name = session.friendly_name; switch (true) { case /linux/i.test(name): return ; case /android/i.test(name): return ; case /mac.*os/i.test(name): return ; case /ios/i.test(name): return ; case /windows/i.test(name): return ; default: return null; } } const mapped = sessions.map((session) => { return { ...session, timestamp: decodeTime(session.id), }; }); mapped.sort((a, b) => b.timestamp - a.timestamp); let id = mapped.findIndex((x) => x.id === deviceId); const render = [ mapped[id], ...mapped.slice(0, id), ...mapped.slice(id + 1, mapped.length), ]; return (

{render.map((session) => (
-1}> {deviceId === session.id && ( {" "} )}
{getIcon(session)}
{getSystemIcon(session)}
{session.friendly_name}
{deviceId !== session.id && ( )}
))} {" "} switchPage("account")}>
); }