2022-10-18 23:10:44 -07:00
|
|
|
import { useTranslation } from "react-i18next";
|
2022-10-19 23:07:01 -07:00
|
|
|
import { useEffect, useState, useRef, useCallback, useContext } from "react";
|
2022-10-18 23:10:44 -07:00
|
|
|
import classNames from "classnames";
|
2022-10-19 00:54:35 -07:00
|
|
|
|
2022-11-04 17:38:33 -04:00
|
|
|
import ResolvedIcon from "./resolvedicon";
|
2022-10-18 23:10:44 -07:00
|
|
|
|
2022-10-19 23:07:01 -07:00
|
|
|
import { SettingsContext } from "utils/contexts/settings";
|
|
|
|
|
2022-10-20 11:12:47 -07:00
|
|
|
export default function QuickLaunch({servicesAndBookmarks, searchString, setSearchString, isOpen, close, searchDescriptions}) {
|
2022-10-19 00:54:35 -07:00
|
|
|
const { t } = useTranslation();
|
2022-10-19 23:07:01 -07:00
|
|
|
const { settings } = useContext(SettingsContext);
|
2022-10-18 23:10:44 -07:00
|
|
|
|
|
|
|
const searchField = useRef();
|
|
|
|
|
|
|
|
const [results, setResults] = useState([]);
|
|
|
|
const [currentItemIndex, setCurrentItemIndex] = useState(null);
|
|
|
|
|
2022-10-19 23:07:01 -07:00
|
|
|
function openCurrentItem(newWindow) {
|
2022-10-19 14:23:51 -07:00
|
|
|
const result = results[currentItemIndex];
|
2022-10-20 11:35:21 -07:00
|
|
|
window.open(result.href, newWindow ? "_blank" : result.target ?? settings.target ?? "_blank");
|
2022-10-19 14:23:51 -07:00
|
|
|
}
|
|
|
|
|
2022-10-19 20:16:29 -07:00
|
|
|
const closeAndReset = useCallback(() => {
|
2022-10-19 09:04:34 -07:00
|
|
|
close(false);
|
2022-10-19 20:16:29 -07:00
|
|
|
setTimeout(() => {
|
|
|
|
setSearchString("");
|
2022-10-19 23:07:01 -07:00
|
|
|
setCurrentItemIndex(null);
|
2022-10-19 20:16:29 -07:00
|
|
|
}, 200); // delay a little for animations
|
2022-10-19 23:07:01 -07:00
|
|
|
}, [close, setSearchString, setCurrentItemIndex]);
|
2022-10-19 09:04:34 -07:00
|
|
|
|
2022-10-18 23:10:44 -07:00
|
|
|
function handleSearchChange(event) {
|
|
|
|
setSearchString(event.target.value.toLowerCase())
|
|
|
|
}
|
|
|
|
|
|
|
|
function handleSearchKeyDown(event) {
|
2022-10-21 22:02:36 -07:00
|
|
|
if (!isOpen) return;
|
|
|
|
|
2022-10-18 23:10:44 -07:00
|
|
|
if (event.key === "Escape") {
|
2022-10-19 20:16:29 -07:00
|
|
|
closeAndReset();
|
2022-10-21 22:02:36 -07:00
|
|
|
event.preventDefault();
|
2022-10-18 23:10:44 -07:00
|
|
|
} else if (event.key === "Enter" && results.length) {
|
2022-10-19 20:16:29 -07:00
|
|
|
closeAndReset();
|
2022-10-19 23:07:01 -07:00
|
|
|
openCurrentItem(event.metaKey);
|
2022-10-19 00:54:35 -07:00
|
|
|
} else if (event.key === "ArrowDown" && results[currentItemIndex + 1]) {
|
2022-10-18 23:10:44 -07:00
|
|
|
setCurrentItemIndex(currentItemIndex + 1);
|
|
|
|
event.preventDefault();
|
2022-10-19 00:54:35 -07:00
|
|
|
} else if (event.key === "ArrowUp" && currentItemIndex > 0) {
|
2022-10-18 23:10:44 -07:00
|
|
|
setCurrentItemIndex(currentItemIndex - 1);
|
|
|
|
event.preventDefault();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-19 13:55:18 -07:00
|
|
|
function handleItemHover(event) {
|
2022-10-19 17:10:01 -07:00
|
|
|
setCurrentItemIndex(parseInt(event.target?.dataset?.index, 10));
|
2022-10-19 13:55:18 -07:00
|
|
|
}
|
|
|
|
|
2022-10-19 23:07:01 -07:00
|
|
|
function handleItemClick(event) {
|
2022-10-19 20:16:29 -07:00
|
|
|
closeAndReset();
|
2022-10-19 23:07:01 -07:00
|
|
|
openCurrentItem(event.metaKey);
|
2022-10-19 09:04:34 -07:00
|
|
|
}
|
|
|
|
|
2022-10-18 23:10:44 -07:00
|
|
|
useEffect(() => {
|
|
|
|
if (searchString.length === 0) setResults([]);
|
|
|
|
else {
|
2022-10-20 11:12:47 -07:00
|
|
|
let newResults = servicesAndBookmarks.filter(r => {
|
|
|
|
const nameMatch = r.name.toLowerCase().includes(searchString);
|
|
|
|
let descriptionMatch;
|
|
|
|
if (searchDescriptions) {
|
|
|
|
descriptionMatch = r.description?.toLowerCase().includes(searchString)
|
|
|
|
r.priority = nameMatch ? 2 * (+nameMatch) : +descriptionMatch; // eslint-disable-line no-param-reassign
|
|
|
|
}
|
|
|
|
return nameMatch || descriptionMatch;
|
|
|
|
});
|
|
|
|
|
|
|
|
if (searchDescriptions) {
|
|
|
|
newResults = newResults.sort((a, b) => b.priority - a.priority);
|
|
|
|
}
|
|
|
|
|
2022-10-18 23:10:44 -07:00
|
|
|
setResults(newResults);
|
2022-10-20 11:12:47 -07:00
|
|
|
|
2022-10-18 23:10:44 -07:00
|
|
|
if (newResults.length) {
|
|
|
|
setCurrentItemIndex(0);
|
|
|
|
}
|
|
|
|
}
|
2022-10-20 11:12:47 -07:00
|
|
|
}, [searchString, servicesAndBookmarks, searchDescriptions]);
|
2022-10-18 23:10:44 -07:00
|
|
|
|
|
|
|
|
|
|
|
const [hidden, setHidden] = useState(true);
|
|
|
|
useEffect(() => {
|
2022-10-19 20:16:29 -07:00
|
|
|
function handleBackdropClick(event) {
|
|
|
|
if (event.target?.tagName === "DIV") closeAndReset();
|
|
|
|
}
|
|
|
|
|
2022-10-18 23:10:44 -07:00
|
|
|
if (isOpen) {
|
|
|
|
searchField.current.focus();
|
2022-10-19 20:16:29 -07:00
|
|
|
document.body.addEventListener('click', handleBackdropClick);
|
2022-10-18 23:10:44 -07:00
|
|
|
setHidden(false);
|
|
|
|
} else {
|
2022-10-19 20:16:29 -07:00
|
|
|
document.body.removeEventListener('click', handleBackdropClick);
|
2022-10-21 22:02:36 -07:00
|
|
|
searchField.current.blur();
|
2022-10-18 23:10:44 -07:00
|
|
|
setTimeout(() => {
|
|
|
|
setHidden(true);
|
|
|
|
}, 300); // disable on close
|
|
|
|
}
|
2022-10-19 20:16:29 -07:00
|
|
|
|
|
|
|
}, [isOpen, closeAndReset]);
|
2022-10-18 23:10:44 -07:00
|
|
|
|
2022-10-20 11:12:47 -07:00
|
|
|
function highlightText(text) {
|
|
|
|
const parts = text.split(new RegExp(`(${searchString})`, 'gi'));
|
2022-11-30 20:52:30 -08:00
|
|
|
// eslint-disable-next-line react/no-array-index-key
|
|
|
|
return <span>{parts.map((part, i) => part.toLowerCase() === searchString.toLowerCase() ? <span key={`${searchString}_${i}`} className="bg-theme-300/10">{part}</span> : part)}</span>;
|
2022-10-20 11:12:47 -07:00
|
|
|
}
|
|
|
|
|
2022-10-18 23:10:44 -07:00
|
|
|
return (
|
|
|
|
<div className={classNames(
|
2022-11-29 13:26:35 -08:00
|
|
|
"relative z-20 ease-in-out duration-300 transition-opacity",
|
2022-10-18 23:10:44 -07:00
|
|
|
hidden && !isOpen && "hidden",
|
|
|
|
!hidden && isOpen && "opacity-100",
|
|
|
|
!isOpen && "opacity-0",
|
|
|
|
)} role="dialog" aria-modal="true">
|
2022-10-19 00:54:35 -07:00
|
|
|
<div className="fixed inset-0 bg-gray-500 bg-opacity-50" />
|
2022-11-29 13:26:35 -08:00
|
|
|
<div className="fixed inset-0 z-20 overflow-y-auto">
|
2022-10-18 23:10:44 -07:00
|
|
|
<div className="flex min-h-full min-w-full items-start justify-center text-center">
|
2022-10-19 20:16:29 -07:00
|
|
|
<dialog className="mt-[10%] min-w-[80%] max-w-[90%] md:min-w-[40%] rounded-md p-0 block font-medium text-theme-700 dark:text-theme-200 dark:hover:text-theme-300 shadow-md shadow-theme-900/10 dark:shadow-theme-900/20 bg-theme-50 dark:bg-theme-800">
|
2022-10-19 00:54:35 -07:00
|
|
|
<input placeholder="Search" className={classNames(
|
|
|
|
results.length > 0 && "rounded-t-md",
|
|
|
|
results.length === 0 && "rounded-md",
|
|
|
|
"w-full p-4 m-0 border-0 border-b border-slate-700 focus:border-slate-700 focus:outline-0 focus:ring-0 text-sm md:text-xl text-theme-700 dark:text-theme-200 bg-theme-60 dark:bg-theme-800"
|
2022-10-19 20:16:29 -07:00
|
|
|
)} type="text" autoCorrect="false" ref={searchField} value={searchString} onChange={handleSearchChange} onKeyDown={handleSearchKeyDown} />
|
2022-10-19 14:23:51 -07:00
|
|
|
{results.length > 0 && <ul className="max-h-[60vh] overflow-y-auto m-2">
|
2022-10-19 00:54:35 -07:00
|
|
|
{results.map((r, i) => (
|
2022-10-19 09:04:34 -07:00
|
|
|
<li key={r.name}>
|
2022-10-19 17:10:01 -07:00
|
|
|
<button type="button" data-index={i} onMouseEnter={handleItemHover} className={classNames(
|
2022-10-19 14:23:51 -07:00
|
|
|
"flex flex-row w-full items-center justify-between rounded-md text-sm md:text-xl py-2 px-4 cursor-pointer text-theme-700 dark:text-theme-200",
|
2022-10-19 09:04:34 -07:00
|
|
|
i === currentItemIndex && "bg-theme-300/50 dark:bg-theme-700/50",
|
2022-10-19 14:23:51 -07:00
|
|
|
)} onClick={handleItemClick}>
|
|
|
|
<div className="flex flex-row items-center mr-4 pointer-events-none">
|
2022-10-19 09:04:34 -07:00
|
|
|
<div className="w-5 text-xs mr-4">
|
2022-11-04 17:38:33 -04:00
|
|
|
{r.icon && <ResolvedIcon icon={r.icon} />}
|
2022-10-19 09:04:34 -07:00
|
|
|
{r.abbr && r.abbr}
|
|
|
|
</div>
|
2022-10-19 17:10:01 -07:00
|
|
|
<div className="flex flex-col md:flex-row text-left items-baseline mr-4 pointer-events-none">
|
|
|
|
<span className="mr-4">{r.name}</span>
|
2022-10-20 11:12:47 -07:00
|
|
|
{r.description &&
|
|
|
|
<span className="text-xs text-theme-600 text-light">
|
|
|
|
{searchDescriptions && r.priority < 2 ? highlightText(r.description) : r.description}
|
|
|
|
</span>
|
|
|
|
}
|
2022-10-19 17:10:01 -07:00
|
|
|
</div>
|
2022-10-19 00:54:35 -07:00
|
|
|
</div>
|
2022-11-19 22:10:39 -08:00
|
|
|
<div className="text-xs text-theme-600 font-bold pointer-events-none">{r.type === 'service' ? t("quicklaunch.service") : t("quicklaunch.bookmark")}</div>
|
2022-10-19 14:23:51 -07:00
|
|
|
</button>
|
2022-10-19 00:54:35 -07:00
|
|
|
</li>
|
|
|
|
))}
|
|
|
|
</ul>}
|
2022-10-19 20:16:29 -07:00
|
|
|
</dialog>
|
2022-10-18 23:10:44 -07:00
|
|
|
</div>
|
|
|
|
</div>
|
2022-10-19 00:54:35 -07:00
|
|
|
</div>
|
2022-10-18 23:10:44 -07:00
|
|
|
);
|
|
|
|
}
|