1
0
Fork 0

feat: bulk delete invites

user-stream
Lea 2023-08-10 23:05:53 +02:00 committed by insert
parent 977986736b
commit 91ba9b94c8
2 changed files with 62 additions and 8 deletions

View File

@ -1,10 +1,13 @@
"use client"
import { InviteCard } from "@/components/cards/InviteCard";
import { AlertDialog, AlertDialogAction, AlertDialogCancel, AlertDialogContent, AlertDialogDescription, AlertDialogFooter, AlertDialogHeader, AlertDialogTitle, AlertDialogTrigger } from "@/components/ui/alert-dialog";
import { Button } from "@/components/ui/button";
import { Command, CommandItem } from "@/components/ui/command";
import { Input } from "@/components/ui/input";
import { Popover, PopoverTrigger, PopoverContent } from "@/components/ui/popover";
import { toast } from "@/components/ui/use-toast";
import { bulkDeleteInvites } from "@/lib/actions";
import { ChannelInvite } from "@/lib/db";
import { ChevronsUpDown } from "lucide-react";
import { useMemo, useState } from "react";
@ -19,17 +22,19 @@ export default function ServerInviteList({ server, invites, channels, users }: {
const [selectVanityOnly, setSelectVanityOnly] = useState(false);
const [selectChannel, setSelectChannel] = useState(false);
const [selectUser, setSelectUser] = useState(false);
const [vanityOnly, setVanityOnly] = useState(false);
const [vanityFilter, setVanityFilter] = useState<boolean | null>(null);
const [channelFilter, setChannelFilter] = useState("");
const [userFilter, setUserFilter] = useState("");
const [deletedInvites, setDeletedInvites] = useState<string[]>([]);
const filteredInvites = useMemo(() => {
return invites
?.filter(invite => vanityOnly ? invite.vanity : true)
?.filter(invite => !deletedInvites.includes(invite._id))
?.filter(invite => vanityFilter === true ? invite.vanity : vanityFilter === false ? !invite.vanity : true)
?.filter(invite => channelFilter ? invite.channel == channelFilter : true)
?.filter(invite => userFilter ? invite.creator == userFilter : true)
?.reverse();
}, [vanityOnly, channelFilter, userFilter, invites]);
}, [vanityFilter, channelFilter, userFilter, invites, deletedInvites]);
return (
<div>
@ -42,21 +47,22 @@ export default function ServerInviteList({ server, invites, channels, users }: {
aria-expanded={selectVanityOnly}
className="flex-1 justify-between"
>
{vanityOnly ? "Vanity" : "All"}
{vanityFilter === true ? "Vanity" : vanityFilter === false ? "Not vanity" : "All"}
<ChevronsUpDown className="ml-2 h-4 w-4 shrink-0 opacity-50" />
</Button>
</PopoverTrigger>
<PopoverContent className="w-[200px] p-0">
<Command>
{[
{ value: false, label: "All" },
{ value: null, label: "All" },
{ value: true, label: "Vanity" },
{ value: false, label: "Not vanity" },
].map((option) => (
<CommandItem
key={String(option.value)}
onSelect={async () => {
setSelectVanityOnly(false);
setVanityOnly(option.value);
setVanityFilter(option.value);
}}
>{option.label}</CommandItem>
))}
@ -73,7 +79,7 @@ export default function ServerInviteList({ server, invites, channels, users }: {
className="flex-1 justify-between"
>
{channelFilter
? (channels?.find(c => c._id == channelFilter) as any)?.name
? '#' + (channels?.find(c => c._id == channelFilter) as any)?.name
: "Select channel"}
<ChevronsUpDown className="ml-2 h-4 w-4 shrink-0 opacity-50" />
</Button>
@ -131,6 +137,42 @@ export default function ServerInviteList({ server, invites, channels, users }: {
</Command>
</PopoverContent>
</Popover>
<AlertDialog>
<AlertDialogTrigger asChild>
<Button disabled={!filteredInvites.length}>Bulk delete</Button>
</AlertDialogTrigger>
<AlertDialogContent>
<AlertDialogHeader>
<AlertDialogTitle>
Bulk delete invites
</AlertDialogTitle>
</AlertDialogHeader>
<AlertDialogDescription>
This will delete all invites that match your filter options.
<br />
<b>{filteredInvites.length}</b> invite{filteredInvites.length == 1 ? '' : 's'} will be deleted.
</AlertDialogDescription>
<AlertDialogFooter>
<AlertDialogAction
onClick={async () => {
try {
await bulkDeleteInvites(filteredInvites.map(i => i._id));
setDeletedInvites([...deletedInvites, ...filteredInvites.map(i => i._id)]);
toast({ title: "Selected invites have been deleted" });
} catch(e) {
toast({
title: "Failed to delete invite",
description: String(e),
variant: "destructive",
});
}
}}
>Bulk delete</AlertDialogAction>
<AlertDialogCancel>Cancel</AlertDialogCancel>
</AlertDialogFooter>
</AlertDialogContent>
</AlertDialog>
</div>
{filteredInvites.map(invite => (<InviteCard

View File

@ -591,6 +591,8 @@ export async function updateServerDiscoverability(
export async function deleteInvite(invite: string) {
await checkPermission("channels/update/invites", invite);
if (!invite) throw new Error("invite is empty");
await mongo()
.db("revolt")
.collection<ChannelInvite>("channel_invites")
@ -600,7 +602,8 @@ export async function deleteInvite(invite: string) {
export async function editInvite(invite: string, newInvite: string) {
await checkPermission("channels/update/invites", { invite, newInvite });
if (!newInvite) throw new Error("invite is empty");
if (!invite) throw new Error("invite is empty");
if (!newInvite) throw new Error("new invite is empty");
const { value } = await mongo()
.db("revolt")
@ -615,6 +618,15 @@ export async function editInvite(invite: string, newInvite: string) {
.insertOne({ ...value, _id: newInvite });
}
export async function bulkDeleteInvites(invites: string[]) {
await checkPermission("channels/update/invites", invites);
await mongo()
.db("revolt")
.collection<ChannelInvite>("channel_invites")
.deleteMany({ _id: { $in: invites } });
}
export async function updateBotDiscoverability(botId: string, state: boolean) {
await checkPermission("bots/update/discoverability", botId, { state });
await mongo()