import Dropdown from "./react/Dropdown"; import "./note_icon.css"; import { t } from "i18next"; import { useNoteContext } from "./react/hooks"; import { useCallback, useEffect, useRef, useState } from "preact/hooks"; import server from "../services/server"; import type { Category, Icon } from "./icon_list"; import FormTextBox from "./react/FormTextBox"; import FormSelect from "./react/FormSelect"; interface IconToCountCache { iconClassToCountMap: Record; } interface IconData { iconToCount: Record; categories: Category[]; icons: Icon[]; } let fullIconData: { categories: Category[]; icons: Icon[]; }; let iconToCountCache!: Promise | null; export default function NoteIcon() { const { note } = useNoteContext(); const [ icon, setIcon ] = useState("bx bx-empty"); const refreshIcon = useCallback(() => { if (note) { setIcon(note.getIcon()); } }, [ note ]); useEffect(refreshIcon, [ note ]); return ( ) } function NoteIconList() { const searchBoxRef = useRef(null); const [ search, setSearch ] = useState(); const [ categoryId, setCategoryId ] = useState("0"); const [ iconData, setIconData ] = useState(); useEffect(() => { async function loadIcons() { if (!fullIconData) { fullIconData = (await import("./icon_list.js")).default; } // Filter by text and/or category. let icons: Icon[] = fullIconData.icons; const processedSearch = search?.trim()?.toLowerCase(); if (processedSearch || categoryId) { icons = icons.filter((icon) => { if (categoryId !== "0" && String(icon.category_id) !== categoryId) { return false; } if (processedSearch) { if (!icon.name.includes(processedSearch) && !icon.term?.find((t) => t.includes(processedSearch))) { return false; } } return true; }); } // Sort by count. const iconToCount = await getIconToCountMap(); if (iconToCount) { icons.sort((a, b) => { const countA = iconToCount[a.className ?? ""] || 0; const countB = iconToCount[b.className ?? ""] || 0; return countB - countA; }); } setIconData({ iconToCount, icons, categories: fullIconData.categories }) } loadIcons(); }, [ search, categoryId ]); // Focus on search by default. useEffect(() => { searchBoxRef?.current?.focus(); }, []); return ( <>
{t("note_icon.category")} {t("note_icon.search")}
{(iconData?.icons ?? []).map(({className, name}) => ( ))}
); } async function getIconToCountMap() { if (!iconToCountCache) { iconToCountCache = server.get("other/icon-usage"); setTimeout(() => (iconToCountCache = null), 20000); // invalidate cache after 20 seconds } return (await iconToCountCache).iconClassToCountMap; }