summaryrefslogtreecommitdiff
path: root/web/source/settings/views/admin/emoji/category-select.tsx
blob: fabcad9b65b9d044b0faa8573af8845bcda97a2e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/*
	GoToSocial
	Copyright (C) GoToSocial Authors admin@gotosocial.org
	SPDX-License-Identifier: AGPL-3.0-or-later

	This program is free software: you can redistribute it and/or modify
	it under the terms of the GNU Affero General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU Affero General Public License for more details.

	You should have received a copy of the GNU Affero General Public License
	along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

import React, { useMemo, useEffect, PropsWithChildren, ReactElement } from "react";
import { matchSorter } from "match-sorter";
import ComboBox from "../../../components/combo-box";
import { useListEmojiQuery } from "../../../lib/query/admin/custom-emoji";
import { CustomEmoji } from "../../../lib/types/custom-emoji";
import { ComboboxFormInputHook } from "../../../lib/form/types";
import Loading from "../../../components/loading";
import { Error } from "../../../components/error";

/**
 * Sort all emoji into a map keyed by
 * the category names (or "Unsorted").
 */
export function useEmojiByCategory(emojis: CustomEmoji[]) {	
	return useMemo(() => {
		const byCategory = new Map<string, CustomEmoji[]>();
		
		emojis.forEach((emoji) => {
			const key = emoji.category ?? "Unsorted";
			const value = byCategory.get(key) ?? [];
			value.push(emoji);
			byCategory.set(key, value);
		});

		return byCategory;
	}, [emojis]);
}

interface CategorySelectProps {
	field: ComboboxFormInputHook;
}

/**
 * 
 * Renders a cute lil searchable "category select" dropdown.
 */
export function CategorySelect({ field, children }: PropsWithChildren<CategorySelectProps>) {
	// Get all local emojis.
	const {
		data: emoji = [],
		isLoading,
		isSuccess,
		isError,
		error,
	} = useListEmojiQuery({ filter: "domain:local" });

	const emojiByCategory = useEmojiByCategory(emoji);	
	const categories = useMemo(() => new Set(emojiByCategory.keys()), [emojiByCategory]);
	const { value, setIsNew } = field;

	// Data used by the ComboBox element
	// to select an emoji category.
	const categoryItems = useMemo(() => {
		const categoriesArr = Array.from(categories); 

		// Sorted by complex algorithm.
		const categoryNames = matchSorter(
			categoriesArr,
			value ?? "",
			{ threshold: matchSorter.rankings.NO_MATCH },
		);

		// Map each category to the static image
		// of the first emoji it contains.
		const categoryItems: [string, ReactElement][] = [];
		categoryNames.forEach((categoryName) => {
			let src: string | undefined;
			const items = emojiByCategory.get(categoryName);
			if (items && items.length > 0) {
				src = items[0].static_url;
			}

			categoryItems.push([
				categoryName,
				<>
					<img
						src={src}
						aria-hidden="true"
					/>
					{categoryName}
				</>
			]);
		});

		return categoryItems;
	}, [emojiByCategory, categories, value]);

	// New category if something has been entered
	// and we don't have it in categories yet.
	useEffect(() => {
		if (value !== undefined) {
			const trimmed = value.trim();
			if (trimmed.length > 0) {
				setIsNew(!categories.has(trimmed));
			}
		}
	}, [categories, value, isSuccess, setIsNew]);

	if (isLoading) {
		return <Loading />;
	} else if (isError) {
		return <Error error={error} />;
	} else {
		return (
			<ComboBox
				field={field}
				items={categoryItems}
				label="Category"
				placeholder="e.g., reactions"
				autoCapitalize="none"
				spellCheck="false"
			>
				{children}
			</ComboBox>
		);
	}
}