1
0
Fork 0

feat: bulk delete invites

dufisgsd
Lea 2023-08-10 23:05:53 +02:00
parent 24f4357775
commit eba78e8579
Signed by: lea
GPG Key ID: 1BAFFE8347019C42
2 changed files with 62 additions and 8 deletions

View File

@ -1,10 +1,13 @@
"use client" "use client"
import { InviteCard } from "@/components/cards/InviteCard"; 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 { Button } from "@/components/ui/button";
import { Command, CommandItem } from "@/components/ui/command"; import { Command, CommandItem } from "@/components/ui/command";
import { Input } from "@/components/ui/input"; import { Input } from "@/components/ui/input";
import { Popover, PopoverTrigger, PopoverContent } from "@/components/ui/popover"; 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 { ChannelInvite } from "@/lib/db";
import { ChevronsUpDown } from "lucide-react"; import { ChevronsUpDown } from "lucide-react";
import { useMemo, useState } from "react"; import { useMemo, useState } from "react";
@ -19,17 +22,19 @@ export default function ServerInviteList({ server, invites, channels, users }: {
const [selectVanityOnly, setSelectVanityOnly] = useState(false); const [selectVanityOnly, setSelectVanityOnly] = useState(false);
const [selectChannel, setSelectChannel] = useState(false); const [selectChannel, setSelectChannel] = useState(false);
const [selectUser, setSelectUser] = useState(false); const [selectUser, setSelectUser] = useState(false);
const [vanityOnly, setVanityOnly] = useState(false); const [vanityFilter, setVanityFilter] = useState<boolean | null>(null);
const [channelFilter, setChannelFilter] = useState(""); const [channelFilter, setChannelFilter] = useState("");
const [userFilter, setUserFilter] = useState(""); const [userFilter, setUserFilter] = useState("");
const [deletedInvites, setDeletedInvites] = useState<string[]>([]);
const filteredInvites = useMemo(() => { const filteredInvites = useMemo(() => {
return invites 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 => channelFilter ? invite.channel == channelFilter : true)
?.filter(invite => userFilter ? invite.creator == userFilter : true) ?.filter(invite => userFilter ? invite.creator == userFilter : true)
?.reverse(); ?.reverse();
}, [vanityOnly, channelFilter, userFilter, invites]); }, [vanityFilter, channelFilter, userFilter, invites, deletedInvites]);
return ( return (
<div> <div>
@ -42,21 +47,22 @@ export default function ServerInviteList({ server, invites, channels, users }: {
aria-expanded={selectVanityOnly} aria-expanded={selectVanityOnly}
className="flex-1 justify-between" 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" /> <ChevronsUpDown className="ml-2 h-4 w-4 shrink-0 opacity-50" />
</Button> </Button>
</PopoverTrigger> </PopoverTrigger>
<PopoverContent className="w-[200px] p-0"> <PopoverContent className="w-[200px] p-0">
<Command> <Command>
{[ {[
{ value: false, label: "All" }, { value: null, label: "All" },
{ value: true, label: "Vanity" }, { value: true, label: "Vanity" },
{ value: false, label: "Not vanity" },
].map((option) => ( ].map((option) => (
<CommandItem <CommandItem
key={String(option.value)} key={String(option.value)}
onSelect={async () => { onSelect={async () => {
setSelectVanityOnly(false); setSelectVanityOnly(false);
setVanityOnly(option.value); setVanityFilter(option.value);
}} }}
>{option.label}</CommandItem> >{option.label}</CommandItem>
))} ))}
@ -73,7 +79,7 @@ export default function ServerInviteList({ server, invites, channels, users }: {
className="flex-1 justify-between" className="flex-1 justify-between"
> >
{channelFilter {channelFilter
? (channels?.find(c => c._id == channelFilter) as any)?.name ? '#' + (channels?.find(c => c._id == channelFilter) as any)?.name
: "Select channel"} : "Select channel"}
<ChevronsUpDown className="ml-2 h-4 w-4 shrink-0 opacity-50" /> <ChevronsUpDown className="ml-2 h-4 w-4 shrink-0 opacity-50" />
</Button> </Button>
@ -131,6 +137,42 @@ export default function ServerInviteList({ server, invites, channels, users }: {
</Command> </Command>
</PopoverContent> </PopoverContent>
</Popover> </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> </div>
{filteredInvites.map(invite => (<InviteCard {filteredInvites.map(invite => (<InviteCard

View File

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