gtsocial-umbx

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

overview.js (4208B)


      1 /*
      2 	GoToSocial
      3 	Copyright (C) GoToSocial Authors admin@gotosocial.org
      4 	SPDX-License-Identifier: AGPL-3.0-or-later
      5 
      6 	This program is free software: you can redistribute it and/or modify
      7 	it under the terms of the GNU Affero General Public License as published by
      8 	the Free Software Foundation, either version 3 of the License, or
      9 	(at your option) any later version.
     10 
     11 	This program is distributed in the hope that it will be useful,
     12 	but WITHOUT ANY WARRANTY; without even the implied warranty of
     13 	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     14 	GNU Affero General Public License for more details.
     15 
     16 	You should have received a copy of the GNU Affero General Public License
     17 	along with this program.  If not, see <http://www.gnu.org/licenses/>.
     18 */
     19 
     20 "use strict";
     21 
     22 const React = require("react");
     23 const { Link } = require("wouter");
     24 const syncpipe = require("syncpipe");
     25 const { matchSorter } = require("match-sorter");
     26 
     27 const NewEmojiForm = require("./new-emoji");
     28 const { useTextInput } = require("../../../lib/form");
     29 
     30 const query = require("../../../lib/query");
     31 const { useEmojiByCategory } = require("../category-select");
     32 const { useBaseUrl } = require("../../../lib/navigation/util");
     33 
     34 const Loading = require("../../../components/loading");
     35 const { Error } = require("../../../components/error");
     36 const { TextInput } = require("../../../components/form/inputs");
     37 
     38 module.exports = function EmojiOverview({ }) {
     39 	const {
     40 		data: emoji = [],
     41 		isLoading,
     42 		isError,
     43 		error
     44 	} = query.useListEmojiQuery({ filter: "domain:local" });
     45 
     46 	let content = null;
     47 
     48 	if (isLoading) {
     49 		content = <Loading />;
     50 	} else if (isError) {
     51 		content = <Error error={error} />;
     52 	} else {
     53 		content = (
     54 			<>
     55 				<EmojiList emoji={emoji} />
     56 				<NewEmojiForm emoji={emoji} />
     57 			</>
     58 		);
     59 	}
     60 
     61 	return (
     62 		<>
     63 			<h1>Local Custom Emoji</h1>
     64 			<p>
     65 				To use custom emoji in your toots they have to be 'local' to the instance.
     66 				You can either upload them here directly, or copy from those already
     67 				present on other (known) instances through the <Link to={`./remote`}>Remote Emoji</Link> page.
     68 			</p>
     69 			{content}
     70 		</>
     71 	);
     72 };
     73 
     74 function EmojiList({ emoji }) {
     75 	const filterField = useTextInput("filter");
     76 	const filter = filterField.value;
     77 
     78 	const emojiByCategory = useEmojiByCategory(emoji);
     79 
     80 	/* Filter emoji based on shortcode match with user input, hiding empty categories */
     81 	const { filteredEmoji, hidden } = React.useMemo(() => {
     82 		let hidden = emoji.length;
     83 		const filteredEmoji = syncpipe(emojiByCategory, [
     84 			(_) => Object.entries(emojiByCategory),
     85 			(_) => _.map(([category, entries]) => {
     86 				let filteredEntries = matchSorter(entries, filter, { keys: ["shortcode"] });
     87 				if (filteredEntries.length == 0) {
     88 					return null;
     89 				} else {
     90 					hidden -= filteredEntries.length;
     91 					return [category, filteredEntries];
     92 				}
     93 			}),
     94 			(_) => _.filter((value) => value !== null)
     95 		]);
     96 
     97 		return { filteredEmoji, hidden };
     98 	}, [filter, emojiByCategory, emoji.length]);
     99 
    100 	return (
    101 		<div>
    102 			<h2>Overview</h2>
    103 			{emoji.length > 0
    104 				? <span>{emoji.length} custom emoji {hidden > 0 && `(${hidden} filtered)`}</span>
    105 				: <span>No custom emoji yet, you can add one below.</span>
    106 			}
    107 			<div className="list emoji-list">
    108 				<div className="header">
    109 					<TextInput
    110 						field={filterField}
    111 						name="emoji-shortcode"
    112 						placeholder="Search"
    113 					/>
    114 				</div>
    115 				<div className="entries scrolling">
    116 					{filteredEmoji.length > 0
    117 						? (
    118 							<div className="entries scrolling">
    119 								{filteredEmoji.map(([category, entries]) => {
    120 									return <EmojiCategory key={category} category={category} entries={entries} />;
    121 								})}
    122 							</div>
    123 						)
    124 						: <div className="entry">No local emoji matched your filter.</div>
    125 					}
    126 				</div>
    127 			</div>
    128 		</div>
    129 	);
    130 }
    131 
    132 function EmojiCategory({ category, entries }) {
    133 	const baseUrl = useBaseUrl();
    134 	return (
    135 		<div className="entry">
    136 			<b>{category}</b>
    137 			<div className="emoji-group">
    138 				{entries.map((e) => {
    139 					return (
    140 						<Link key={e.id} to={`${baseUrl}/${e.id}`}>
    141 							<a>
    142 								<img src={e.url} alt={e.shortcode} title={`:${e.shortcode}:`} />
    143 							</a>
    144 						</Link>
    145 					);
    146 				})}
    147 			</div>
    148 		</div>
    149 	);
    150 }