2021-12-11 12:01:36 +01:00
|
|
|
<script lang="ts">
|
2021-12-12 10:31:18 +01:00
|
|
|
import { Container, Col, Row, TabContent, TabPane } from 'sveltestrap';
|
2021-12-11 12:01:36 +01:00
|
|
|
import { navigate, useLocation } from "svelte-navigator";
|
2022-11-26 13:23:59 +01:00
|
|
|
import { currentUser, loggedIn } from '../../stores';
|
2021-12-11 12:01:36 +01:00
|
|
|
|
2022-11-26 13:23:59 +01:00
|
|
|
import SystemMain from '../../components/system/Main.svelte';
|
2023-06-03 12:27:58 +02:00
|
|
|
import MemberList from '../../components/list/MemberList.svelte';
|
|
|
|
|
import GroupList from '../../components/list/GroupList.svelte';
|
2021-12-11 12:01:36 +01:00
|
|
|
|
2022-11-27 19:27:07 +01:00
|
|
|
import type { System, Member, Group } from '../../api/types';
|
2022-11-26 13:23:59 +01:00
|
|
|
import api from '../../api';
|
2022-11-27 19:27:07 +01:00
|
|
|
import { defaultListOptions, defaultPageOptions, type List as Lists, type ListOptions, type PageOptions } from '../../components/list/types';
|
2023-06-03 12:27:58 +02:00
|
|
|
import { setContext } from 'svelte';
|
|
|
|
|
import { writable } from 'svelte/store';
|
2022-02-01 15:24:45 -05:00
|
|
|
|
2021-12-11 15:57:47 +01:00
|
|
|
// get the state from the navigator so that we know which tab to start on
|
2021-12-11 12:01:36 +01:00
|
|
|
let location = useLocation();
|
2021-12-15 13:56:36 +01:00
|
|
|
let params = $location.search && new URLSearchParams($location.search);
|
2022-06-23 23:45:23 +02:00
|
|
|
|
2022-10-02 22:09:13 +02:00
|
|
|
let tabPane: string|number = params && params.get("tab") || "system";
|
|
|
|
|
let listView: string = params && params.get("view") || "list";
|
2022-06-23 23:45:23 +02:00
|
|
|
|
|
|
|
|
// change the URL when changing tabs
|
2022-10-02 22:09:13 +02:00
|
|
|
function navigateTo(tab: string|number, view: string) {
|
|
|
|
|
let url = "./dash";
|
|
|
|
|
if (tab || view) url += "?";
|
|
|
|
|
if (tab) url += `tab=${tab}`
|
|
|
|
|
if (tab && view) url += "&";
|
|
|
|
|
if (view) url += `view=${view}`
|
|
|
|
|
|
|
|
|
|
navigate(url);
|
2022-06-23 23:45:23 +02:00
|
|
|
tabPane = tab;
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-11 15:57:47 +01:00
|
|
|
// subscribe to the cached user in the store
|
2021-12-11 12:01:36 +01:00
|
|
|
let current;
|
|
|
|
|
currentUser.subscribe(value => {
|
|
|
|
|
current = value;
|
|
|
|
|
});
|
2021-12-11 15:57:47 +01:00
|
|
|
|
|
|
|
|
// if there is no cached user, get the user from localstorage
|
2022-02-01 15:24:45 -05:00
|
|
|
let user: System = current ?? JSON.parse(localStorage.getItem("pk-user"));
|
2021-12-11 15:57:47 +01:00
|
|
|
// since the user in localstorage can be outdated, fetch the user from the api again
|
2021-12-11 12:01:36 +01:00
|
|
|
if (!current) {
|
|
|
|
|
login(localStorage.getItem("pk-token"));
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-11 15:57:47 +01:00
|
|
|
// if there's no user, and there's no token, assume the login failed and send us back to the homepage.
|
2021-12-11 12:01:36 +01:00
|
|
|
if (!localStorage.getItem("pk-token") && !user) {
|
|
|
|
|
navigate("/");
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-11 15:56:47 +01:00
|
|
|
let settings = JSON.parse(localStorage.getItem("pk-settings"));
|
|
|
|
|
|
2021-12-11 15:57:47 +01:00
|
|
|
// just the login function
|
2021-12-11 12:01:36 +01:00
|
|
|
async function login(token: string) {
|
|
|
|
|
try {
|
|
|
|
|
if (!token) {
|
|
|
|
|
throw new Error("Token cannot be empty.")
|
|
|
|
|
}
|
2022-02-01 15:24:45 -05:00
|
|
|
const res: System = await api().systems("@me").get({ token });
|
2021-12-11 12:01:36 +01:00
|
|
|
localStorage.setItem("pk-token", token);
|
|
|
|
|
localStorage.setItem("pk-user", JSON.stringify(res));
|
|
|
|
|
loggedIn.update(() => true);
|
|
|
|
|
currentUser.update(() => res);
|
|
|
|
|
user = res;
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.log(error);
|
2022-06-02 23:12:38 -04:00
|
|
|
// localStorage.removeItem("pk-token");
|
|
|
|
|
// localStorage.removeItem("pk-user");
|
|
|
|
|
// currentUser.update(() => null);
|
2021-12-11 12:01:36 +01:00
|
|
|
navigate("/");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-03 12:27:58 +02:00
|
|
|
// context stores for each list
|
|
|
|
|
let memberStore = writable<Member[]>([]);
|
|
|
|
|
let groupStore = writable<Group[]>([]);
|
2022-11-27 19:27:07 +01:00
|
|
|
|
2023-06-03 12:27:58 +02:00
|
|
|
// state handling
|
|
|
|
|
let errs: Record<string, string> = {};
|
|
|
|
|
let loading: Record<string, boolean> = {};
|
|
|
|
|
|
|
|
|
|
setContext("members", memberStore);
|
|
|
|
|
setContext("groups", groupStore);
|
|
|
|
|
|
|
|
|
|
fetchLists()
|
|
|
|
|
|
|
|
|
|
// fetch both lists, and store them inside a context store
|
|
|
|
|
async function fetchLists() {
|
|
|
|
|
loading.members = true;
|
|
|
|
|
loading.groups = true;
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
const res = await api().systems("@me").members.get({ auth: true });
|
|
|
|
|
memberStore.set(res)
|
|
|
|
|
loading.members = false;
|
2022-11-27 19:27:07 +01:00
|
|
|
|
2023-06-03 12:27:58 +02:00
|
|
|
} catch (error) {
|
|
|
|
|
console.error(error);
|
|
|
|
|
errs.members = error.message;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
const res = await api().systems("@me").groups.get({ auth: true, query: { with_members: true } });
|
|
|
|
|
groupStore.set(res)
|
|
|
|
|
loading.groups = false;
|
2022-11-27 19:27:07 +01:00
|
|
|
|
2023-06-03 12:27:58 +02:00
|
|
|
} catch (error) {
|
|
|
|
|
console.error(error);
|
|
|
|
|
errs.groups = error.message;
|
|
|
|
|
}
|
2022-11-27 19:27:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let groupListOptions: ListOptions = JSON.parse(JSON.stringify(defaultListOptions));
|
|
|
|
|
let memberListOptions: ListOptions = JSON.parse(JSON.stringify(defaultListOptions));
|
|
|
|
|
|
|
|
|
|
let memberListPageOptions: PageOptions = {...defaultPageOptions,
|
|
|
|
|
view: listView,
|
|
|
|
|
isPublic: false,
|
|
|
|
|
type: 'member'
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let groupListPageOptions: PageOptions = {...defaultPageOptions,
|
|
|
|
|
view: listView,
|
|
|
|
|
isPublic: false,
|
|
|
|
|
type: 'group'
|
|
|
|
|
};
|
2021-12-18 20:10:02 +01:00
|
|
|
|
2021-12-11 12:01:36 +01:00
|
|
|
</script>
|
|
|
|
|
|
2021-12-11 15:56:47 +01:00
|
|
|
<!-- display the banner if there's a banner set, and if the current settings allow for it-->
|
2021-12-12 14:33:35 +01:00
|
|
|
{#if user && user.banner && ((settings && settings.appearance.banner_top) || !settings)}
|
2021-12-11 12:01:36 +01:00
|
|
|
<div class="banner" style="background-image: url({user.banner})" />
|
|
|
|
|
{/if}
|
2022-05-30 13:00:57 +02:00
|
|
|
{#if user}
|
2021-12-11 12:01:36 +01:00
|
|
|
<Container>
|
|
|
|
|
<Row>
|
2021-12-30 08:23:13 +01:00
|
|
|
<Col class="mx-auto" xs={12} lg={11} xl={10}>
|
2022-05-20 12:09:10 +02:00
|
|
|
<h2 class="visually-hidden">Viewing your own system</h2>
|
2022-11-28 17:19:26 +01:00
|
|
|
<TabContent class="mt-3" on:tab={(e) => navigateTo(e.detail, e.detail === 'members' ? memberListPageOptions.view : e.detail === 'groups' ? groupListPageOptions.view : 'list')}>
|
2021-12-11 12:01:36 +01:00
|
|
|
<TabPane tabId="system" tab="System" active={tabPane === "system"}>
|
2022-06-23 22:24:29 +02:00
|
|
|
<SystemMain bind:user={user} isPublic={false} />
|
2021-12-11 12:01:36 +01:00
|
|
|
</TabPane>
|
|
|
|
|
<TabPane tabId="members" tab="Members" active={tabPane === "members"}>
|
2023-06-03 12:27:58 +02:00
|
|
|
<MemberList on:viewChange={(e) => navigateTo("members", e.detail)} bind:listLoading={loading.members} pageOptions={memberListPageOptions} options={memberListOptions} />
|
2021-12-18 20:10:02 +01:00
|
|
|
</TabPane>
|
|
|
|
|
<TabPane tabId="groups" tab="Groups" active={tabPane === "groups"}>
|
2023-06-03 12:27:58 +02:00
|
|
|
<GroupList on:viewChange={(e) => navigateTo("groups", e.detail)} bind:listLoading={loading.groups} pageOptions={groupListPageOptions} options={groupListOptions} />
|
2022-11-27 19:27:07 +01:00
|
|
|
</TabPane>
|
2021-12-11 12:01:36 +01:00
|
|
|
</TabContent>
|
|
|
|
|
</Col>
|
|
|
|
|
</Row>
|
|
|
|
|
</Container>
|
2022-05-30 13:00:57 +02:00
|
|
|
{/if}
|
2021-12-11 12:01:36 +01:00
|
|
|
|
|
|
|
|
<svelte:head>
|
2022-05-16 23:22:51 -04:00
|
|
|
<title>PluralKit | dash</title>
|
2021-12-11 12:01:36 +01:00
|
|
|
</svelte:head>
|