2022-11-25 15:49:48 +01:00
|
|
|
/*
|
2023-01-18 14:45:14 +01:00
|
|
|
GoToSocial
|
2023-03-12 18:49:06 +01:00
|
|
|
Copyright (C) GoToSocial Authors admin@gotosocial.org
|
|
|
|
SPDX-License-Identifier: AGPL-3.0-or-later
|
2022-11-25 15:49:48 +01:00
|
|
|
|
2023-01-18 14:45:14 +01:00
|
|
|
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.
|
2022-11-25 15:49:48 +01:00
|
|
|
|
2023-01-18 14:45:14 +01:00
|
|
|
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.
|
2022-11-25 15:49:48 +01:00
|
|
|
|
2023-01-18 14:45:14 +01:00
|
|
|
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/>.
|
2022-11-25 15:49:48 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
const React = require("react");
|
|
|
|
const splitFilterN = require("split-filter-n");
|
|
|
|
const syncpipe = require('syncpipe');
|
|
|
|
const { matchSorter } = require("match-sorter");
|
|
|
|
|
|
|
|
const query = require("../../lib/query");
|
|
|
|
|
|
|
|
const ComboBox = require("../../components/combo-box");
|
|
|
|
|
|
|
|
function useEmojiByCategory(emoji) {
|
|
|
|
// split all emoji over an object keyed by the category names (or Unsorted)
|
|
|
|
return React.useMemo(() => splitFilterN(
|
|
|
|
emoji,
|
|
|
|
[],
|
|
|
|
(entry) => entry.category ?? "Unsorted"
|
|
|
|
), [emoji]);
|
|
|
|
}
|
|
|
|
|
2023-01-18 14:45:14 +01:00
|
|
|
function CategorySelect({ field, children }) {
|
|
|
|
const { value, setIsNew } = field;
|
|
|
|
|
2022-11-25 15:49:48 +01:00
|
|
|
const {
|
|
|
|
data: emoji = [],
|
|
|
|
isLoading,
|
|
|
|
isSuccess,
|
|
|
|
error
|
2023-01-18 16:41:00 +01:00
|
|
|
} = query.useListEmojiQuery({ filter: "domain:local" });
|
2022-11-25 15:49:48 +01:00
|
|
|
|
|
|
|
const emojiByCategory = useEmojiByCategory(emoji);
|
|
|
|
|
|
|
|
const categories = React.useMemo(() => new Set(Object.keys(emojiByCategory)), [emojiByCategory]);
|
|
|
|
|
|
|
|
// data used by the ComboBox element to select an emoji category
|
|
|
|
const categoryItems = React.useMemo(() => {
|
|
|
|
return syncpipe(emojiByCategory, [
|
|
|
|
(_) => Object.keys(_), // just emoji category names
|
2023-01-18 14:45:14 +01:00
|
|
|
(_) => matchSorter(_, value, { threshold: matchSorter.rankings.NO_MATCH }), // sorted by complex algorithm
|
2022-11-25 15:49:48 +01:00
|
|
|
(_) => _.map((categoryName) => [ // map to input value, and selectable element with icon
|
|
|
|
categoryName,
|
|
|
|
<>
|
|
|
|
<img src={emojiByCategory[categoryName][0].static_url} aria-hidden="true"></img>
|
|
|
|
{categoryName}
|
|
|
|
</>
|
|
|
|
])
|
|
|
|
]);
|
|
|
|
}, [emojiByCategory, value]);
|
|
|
|
|
|
|
|
React.useEffect(() => {
|
|
|
|
if (value != undefined && isSuccess && value.trim().length > 0) {
|
|
|
|
setIsNew(!categories.has(value.trim()));
|
|
|
|
}
|
2023-01-18 14:45:14 +01:00
|
|
|
}, [categories, value, isSuccess, setIsNew]);
|
2022-11-25 15:49:48 +01:00
|
|
|
|
|
|
|
if (error) { // fall back to plain text input, but this would almost certainly have caused a bigger error message elsewhere
|
|
|
|
return (
|
|
|
|
<>
|
2023-01-18 14:45:14 +01:00
|
|
|
<input type="text" placeholder="e.g., reactions" onChange={(e) => { field.value = e.target.value; }} />;
|
2022-11-25 15:49:48 +01:00
|
|
|
</>
|
|
|
|
);
|
|
|
|
} else if (isLoading) {
|
2023-01-18 14:45:14 +01:00
|
|
|
return <input type="text" value="Loading categories..." disabled={true} />;
|
2022-11-25 15:49:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<ComboBox
|
2023-01-18 14:45:14 +01:00
|
|
|
field={field}
|
2022-11-25 15:49:48 +01:00
|
|
|
items={categoryItems}
|
|
|
|
label="Category"
|
2023-01-18 14:45:14 +01:00
|
|
|
placeholder="e.g., reactions"
|
2022-11-25 15:49:48 +01:00
|
|
|
children={children}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
useEmojiByCategory,
|
|
|
|
CategorySelect
|
|
|
|
};
|