Plugin

SortFriendRequests

Sorts friend requests by date of receipt

Friends Organisation
index.tsx
Download

Source

src/plugins/sortFriendRequests/index.tsx
1import "./styles.css";
2
3import { definePluginSettings } from "@api/Settings";
4import ErrorBoundary from "@components/ErrorBoundary";
5import { TooltipContainer } from "@components/TooltipContainer";
6import { Devs } from "@utils/constants";
7import { classNameFactory } from "@utils/css";
8import definePlugin, { OptionType } from "@utils/types";
9import { User } from "@vencord/discord-types";
10import { DateUtils, RelationshipStore, Text } from "@webpack/common";
11import { PropsWithChildren } from "react";
12
13const formatter = new Intl.DateTimeFormat(undefined, {
14 month: "numeric",
15 day: "numeric",
16 year: "numeric",
17});
18
19const cl = classNameFactory("vc-sortFriendRequests-");
20
21function getSince(user: User) {
22 return new Date(RelationshipStore.getSince(user.id));
23}
24
25const settings = definePluginSettings({
26 showDates: {
27 type: OptionType.BOOLEAN,
28 description: "Show dates on friend requests",
29 default: false,
30 restartNeeded: true
31 }
32});
33
34export default definePlugin({
35 name: "SortFriendRequests",
36 authors: [Devs.Megu],
37 description: "Sorts friend requests by date of receipt",
38 tags: ["Friends", "Organisation"],
39 settings,
40
41 patches: [{
42 find: "getRelationshipCounts(){",
43 replacement: {
44 match: /\}\)\.sortBy\((.+?)\)\.value\(\)/,
45 replace: "}).sortBy(row => $self.wrapSort(($1), row)).value()"
46 }
47 }, {
48 find: "#{intl::FRIEND_REQUEST_CANCEL}",
49 replacement: {
50 predicate: () => settings.store.showDates,
51 match: /(?<=children:\[)\(0,.{0,100}user:\i,hovered:\i.+?(?=,\(0)(?<=user:(\i).+?)/,
52 replace: (children, user) => `$self.WrapperDateComponent({user:${user},children:${children}})`
53 }
54 }],
55
56 wrapSort(comparator: Function, row: any) {
57 return row.type === 3 || row.type === 4
58 ? -getSince(row.user)
59 : comparator(row);
60 },
61
62 WrapperDateComponent: ErrorBoundary.wrap(({ user, children }: PropsWithChildren<{ user: User; }>) => {
63 const since = getSince(user);
64
65 return <div className={cl("wrapper")}>
66 {children}
67 {!isNaN(since.getTime()) && (
68 <TooltipContainer text={DateUtils.dateFormat(since, "LLLL")} tooltipClassName={cl("tooltip")}>
69 <Text variant="text-xs/normal" className={cl("date")}>{formatter.format(since)}</Text>
70 </TooltipContainer>
71 )}
72 </div>;
73 }, { noop: true })
74});
75