mirror of
https://github.com/PluralKit/PluralKit.git
synced 2026-02-13 09:10:14 +00:00
195 lines
No EOL
7.7 KiB
Svelte
195 lines
No EOL
7.7 KiB
Svelte
<script lang="ts">
|
|
import { Card, CardHeader, CardBody, Alert, Collapse, Row, Col, Spinner, Button, Tooltip } from 'sveltestrap';
|
|
import { Member, Group } from '../../api/types';
|
|
import { link } from 'svelte-navigator';
|
|
|
|
import FaLock from 'svelte-icons/fa/FaLock.svelte';
|
|
import FaUserCircle from 'svelte-icons/fa/FaUserCircle.svelte';
|
|
import FaUsers from 'svelte-icons/fa/FaUsers.svelte'
|
|
|
|
import MemberBody from '../member/Body.svelte';
|
|
import GroupBody from '../group/Body.svelte';
|
|
import CardsHeader from '../CardsHeader.svelte';
|
|
|
|
let settings = JSON.parse(localStorage.getItem("pk-settings"));
|
|
|
|
export let list: Member[]|Group[];
|
|
export let members: Member[] = [];
|
|
export let groups: Group[] = [];
|
|
|
|
export let isPublic: boolean;
|
|
export let itemType: string;
|
|
export let isMainDash: boolean;
|
|
export let itemsPerPage: number;
|
|
export let currentPage: number;
|
|
|
|
export let openByDefault = false;
|
|
|
|
$: indexStart = itemsPerPage * (currentPage - 1);
|
|
|
|
let cardIndexArray = [];
|
|
|
|
function getItemLink(item: Member | Group): string {
|
|
let url: string;
|
|
|
|
if (isMainDash) url = "/dash/";
|
|
else url = "/profile/";
|
|
|
|
if (itemType === "member") url += "m/";
|
|
else if (itemType === "group") url += "g/";
|
|
|
|
url += item.id;
|
|
|
|
return url;
|
|
}
|
|
|
|
function skipToNextItem(event, index: number) {
|
|
let el;
|
|
|
|
if (event.key === "ArrowDown") {
|
|
if (index + 1 < indexStart + itemsPerPage) el = cardIndexArray[index + 1];
|
|
else el = cardIndexArray[indexStart];
|
|
}
|
|
|
|
if (event.key === "ArrowUp") {
|
|
if (index - 1 >= indexStart) el = cardIndexArray[index - 1];
|
|
else el = cardIndexArray[indexStart + itemsPerPage - 1];
|
|
}
|
|
|
|
if (el) {
|
|
event.preventDefault();
|
|
el.focus();
|
|
}
|
|
}
|
|
|
|
let isOpenArray = [];
|
|
|
|
function toggleCard(index: number) {
|
|
if (isOpenArray[index] === true) {
|
|
isOpenArray[index] = false;
|
|
} else {
|
|
isOpenArray[index] = true;
|
|
}
|
|
}
|
|
|
|
function getShortLink(id: string) {
|
|
let url = "https://pk.mt"
|
|
|
|
if (itemType === "member") url += "/m/"
|
|
else if (itemType === "group") url += "/g/"
|
|
|
|
url += id;
|
|
|
|
return url;
|
|
}
|
|
|
|
let copiedArray = [];
|
|
|
|
async function copyShortLink(index: number, id: string, event?) {
|
|
if (event) {
|
|
if (event.key !== "Tab") event.preventDefault();
|
|
event.stopPropagation();
|
|
|
|
let ctrlDown = event.ctrlKey||event.metaKey; // mac support
|
|
if (!(ctrlDown && event.key === "c") && event.key !== "Enter") return;
|
|
}
|
|
try {
|
|
await navigator.clipboard.writeText(getShortLink(id));
|
|
|
|
copiedArray[index] = true;
|
|
await new Promise(resolve => setTimeout(resolve, 2000));
|
|
copiedArray[index] = false;
|
|
} catch (error) {
|
|
console.log(error);
|
|
}
|
|
}
|
|
</script>
|
|
|
|
{#if !openByDefault && (settings && settings.accessibility ? (!settings.accessibility.expandedcards && !settings.accessibility.pagelinks) : true)}
|
|
<div class="mb-3">
|
|
{#each list as item, index (item.id + index)}
|
|
<Card>
|
|
<h2 class="accordion-header">
|
|
<button class="w-100 accordion-button collapsed card-header" bind:this={cardIndexArray[indexStart + index]} on:click={() => toggleCard(indexStart + index)} on:keydown={(e) => skipToNextItem(e, indexStart + index)}>
|
|
<CardsHeader {item}>
|
|
<div slot="icon" style="cursor: pointer;" id={`copy-${item.id}-${index}`} on:click|stopPropagation={() => copyShortLink(indexStart + index, item.id)} on:keydown={(e) => copyShortLink(indexStart + index, item.id, e)} tabindex={0} >
|
|
{#if isPublic || item.privacy.visibility === "public"}
|
|
{#if itemType === "member"}
|
|
<FaUserCircle />
|
|
{:else if itemType === "group"}
|
|
<FaUsers />
|
|
{/if}
|
|
{:else}
|
|
<FaLock />
|
|
{/if}
|
|
</div>
|
|
</CardsHeader>
|
|
<Tooltip placement="top" target={`copy-${item.id}-${index}`}>{copiedArray[indexStart + index] ? "Copied!" : "Copy public link"}</Tooltip>
|
|
</button>
|
|
</h2>
|
|
<Collapse isOpen={isOpenArray[indexStart + index]}>
|
|
<CardBody>
|
|
{#if itemType === "member"}
|
|
<MemberBody on:deletion bind:isPublic bind:groups bind:member={item} />
|
|
{:else if itemType === "group"}
|
|
<GroupBody on:deletion {isPublic} {members} bind:group={item} />
|
|
{/if}
|
|
</CardBody>
|
|
</Collapse>
|
|
</Card>
|
|
{/each}
|
|
</div>
|
|
{:else if openByDefault || settings.accessibility.expandedcards}
|
|
{#each list as item, index (item.id + index)}
|
|
<Card class="mb-3">
|
|
<div class="accordion-button collapsed p-0" bind:this={cardIndexArray[indexStart + index]} on:keydown={(e) => skipToNextItem(e, indexStart + index)} tabindex={0}>
|
|
<CardHeader class="w-100">
|
|
<CardsHeader {item}>
|
|
<div slot="icon" style="cursor: pointer;" id={`copy-${item.id}-${index}`} on:click|stopPropagation={() => copyShortLink(indexStart + index, item.id)} on:keydown|stopPropagation={(e) => copyShortLink(indexStart + index, item.id, e)} tabindex={0} >
|
|
{#if isPublic || item.privacy.visibility === "public"}
|
|
{#if itemType === "member"}
|
|
<FaUserCircle />
|
|
{:else if itemType === "group"}
|
|
<FaUsers />
|
|
{/if}
|
|
{:else}
|
|
<FaLock />
|
|
{/if}
|
|
</div>
|
|
</CardsHeader>
|
|
<Tooltip placement="top" target={`copy-${item.id}-${index}`}>{copiedArray[indexStart + index] ? "Copied!" : "Copy public link"}</Tooltip>
|
|
</CardHeader>
|
|
</div>
|
|
<CardBody>
|
|
{#if itemType === "member"}
|
|
<MemberBody on:deletion bind:isPublic bind:groups bind:member={item} />
|
|
{:else if itemType === "group"}
|
|
<GroupBody on:deletion {isPublic} {members} bind:group={item} />
|
|
{/if}
|
|
</CardBody>
|
|
</Card>
|
|
{/each}
|
|
{:else}
|
|
<div class="my-3">
|
|
{#each list as item, index (item.id + index)}
|
|
<Card>
|
|
<a class="accordion-button collapsed" style="text-decoration: none;" href={getItemLink(item)} bind:this={cardIndexArray[indexStart + index]} on:keydown={(e) => skipToNextItem(e, indexStart + index)} use:link >
|
|
<CardsHeader {item}>
|
|
<div slot="icon" style="cursor: pointer;" id={`copy-${item.id}-${index}`} on:click|stopPropagation={() => copyShortLink(indexStart + index, item.id)} on:keydown|stopPropagation={(e) => copyShortLink(indexStart + index, item.id, e)} tabindex={0} >
|
|
{#if isPublic || item.privacy.visibility === "public"}
|
|
{#if itemType === "member"}
|
|
<FaUserCircle />
|
|
{:else if itemType === "group"}
|
|
<FaUsers />
|
|
{/if}
|
|
{:else}
|
|
<FaLock />
|
|
{/if}
|
|
</div>
|
|
</CardsHeader>
|
|
<Tooltip placement="top" target={`copy-${item.id}-${index}`}>{copiedArray[indexStart + index] ? "Copied!" : "Copy public link"}</Tooltip>
|
|
</a>
|
|
</Card>
|
|
{/each}
|
|
</div>
|
|
{/if} |