import { Chrome, Android, Apple, Windows } from "@styled-icons/boxicons-logos"; import { HelpCircle, Desktop } from "@styled-icons/boxicons-regular"; import { Safari, Firefoxbrowser, Microsoftedge, Linux, Macos, Opera, } from "@styled-icons/simple-icons"; 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 { dayjs } from "../../../context/Locale"; 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 ; case /opera/i.test(name): return ; case /desktop/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); const 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) => { const systemIcon = getSystemIcon(session); return (
-1 }> {deviceId === session.id && ( {" "} )}
{getIcon(session)} {systemIcon}
{deviceId !== session.id && ( )}
); })} {" "} switchPage("account")}>
); }