Merge pull request #1068 from Aesop7/main

Add Quicklaunch feature to jump to a url
pull/1069/head
shamoon 2 years ago committed by GitHub
commit 78e92dac22
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -286,7 +286,9 @@
"bookmark": "Bookmark", "bookmark": "Bookmark",
"service": "Service", "service": "Service",
"search": "Search", "search": "Search",
"custom": "Custom" "custom": "Custom",
"visit": "Visit",
"url": "URL"
}, },
"wmo": { "wmo": {
"0-day": "Sunny", "0-day": "Sunny",

@ -6,14 +6,16 @@ import ResolvedIcon from "./resolvedicon";
import { SettingsContext } from "utils/contexts/settings"; import { SettingsContext } from "utils/contexts/settings";
export default function QuickLaunch({servicesAndBookmarks, searchString, setSearchString, isOpen, close, searchDescriptions, searchProvider}) { export default function QuickLaunch({servicesAndBookmarks, searchString, setSearchString, isOpen, close, searchProvider}) {
const { t } = useTranslation(); const { t } = useTranslation();
const { settings } = useContext(SettingsContext); const { settings } = useContext(SettingsContext);
const { searchDescriptions, hideVisitURL } = settings?.quicklaunch ? settings.quicklaunch : { searchDescriptions: false, hideVisitURL: false };
const searchField = useRef(); const searchField = useRef();
const [results, setResults] = useState([]); const [results, setResults] = useState([]);
const [currentItemIndex, setCurrentItemIndex] = useState(null); const [currentItemIndex, setCurrentItemIndex] = useState(null);
const [url, setUrl] = useState(null);
function openCurrentItem(newWindow) { function openCurrentItem(newWindow) {
const result = results[currentItemIndex]; const result = results[currentItemIndex];
@ -29,7 +31,16 @@ export default function QuickLaunch({servicesAndBookmarks, searchString, setSear
}, [close, setSearchString, setCurrentItemIndex]); }, [close, setSearchString, setCurrentItemIndex]);
function handleSearchChange(event) { function handleSearchChange(event) {
setSearchString(event.target.value.toLowerCase()) const rawSearchString = event.target.value.toLowerCase();
try {
if (!/.+[.:].+/g.test(rawSearchString)) throw new Error(); // basic test for probably a url
let urlString = rawSearchString;
if (urlString.indexOf('http') !== 0) urlString = `https://${rawSearchString}`;
setUrl(new URL(urlString)); // basic validation
} catch (e) {
setUrl(null);
}
setSearchString(rawSearchString);
} }
function handleSearchKeyDown(event) { function handleSearchKeyDown(event) {
@ -76,6 +87,7 @@ export default function QuickLaunch({servicesAndBookmarks, searchString, setSear
if (searchDescriptions) { if (searchDescriptions) {
newResults = newResults.sort((a, b) => b.priority - a.priority); newResults = newResults.sort((a, b) => b.priority - a.priority);
} }
if (searchProvider) { if (searchProvider) {
newResults.push( newResults.push(
{ {
@ -86,13 +98,23 @@ export default function QuickLaunch({servicesAndBookmarks, searchString, setSear
) )
} }
if (!hideVisitURL && url) {
newResults.unshift(
{
href: url.toString(),
name: `${t("quicklaunch.visit")} URL`,
type: 'url',
}
)
}
setResults(newResults); setResults(newResults);
if (newResults.length) { if (newResults.length) {
setCurrentItemIndex(0); setCurrentItemIndex(0);
} }
} }
}, [searchString, servicesAndBookmarks, searchDescriptions, searchProvider, t]); }, [searchString, servicesAndBookmarks, searchDescriptions, hideVisitURL, searchProvider, url, t]);
const [hidden, setHidden] = useState(true); const [hidden, setHidden] = useState(true);

@ -265,7 +265,6 @@ function Home({ initialSettings }) {
setSearchString={setSearchString} setSearchString={setSearchString}
isOpen={searching} isOpen={searching}
close={setSearching} close={setSearching}
searchDescriptions={settings.quicklaunch?.searchDescriptions}
searchProvider={settings.quicklaunch?.hideInternetSearch ? null : searchProvider} searchProvider={settings.quicklaunch?.hideInternetSearch ? null : searchProvider}
/> />
{widgets && ( {widgets && (

Loading…
Cancel
Save