add auto-fix shortcut url formatting

This commit is contained in:
SethBurkart123
2023-11-26 21:06:10 +11:00
parent 366013ba31
commit eb4d834c98
+13 -1
View File
@@ -4,6 +4,18 @@ import { useSettingsContext } from "../SettingsContext";
import { motion, AnimatePresence } from "framer-motion"; import { motion, AnimatePresence } from "framer-motion";
import { CustomShortcut } from "../types/AppProps"; import { CustomShortcut } from "../types/AppProps";
function formatUrl (inputUrl: string) {
// Regular expression to check if the URL starts with http://, https://, or ftp://
const protocolRegex = /^(http:\/\/|https:\/\/|ftp:\/\/)/;
// Check if the URL starts with one of the protocols
if (protocolRegex.test(inputUrl)) {
return inputUrl; // The URL is fine as is
} else {
return `https://${inputUrl}`; // Prepend https:// to the URL
}
}
export default function Shortcuts() { export default function Shortcuts() {
const { settingsState, setSettingsState } = useSettingsContext(); const { settingsState, setSettingsState } = useSettingsContext();
@@ -30,7 +42,7 @@ export default function Shortcuts() {
const addNewCustomShortcut = (): void => { const addNewCustomShortcut = (): void => {
if (isValidTitle(newTitle) && isValidURL(newURL)) { if (isValidTitle(newTitle) && isValidURL(newURL)) {
const newShortcut: CustomShortcut = { name: newTitle.trim(), url: newURL.trim(), icon: newTitle[0] }; const newShortcut: CustomShortcut = { name: newTitle.trim(), url: formatUrl(newURL).trim(), icon: newTitle[0] };
const updatedCustomShortcuts = [...settingsState.customshortcuts, newShortcut]; const updatedCustomShortcuts = [...settingsState.customshortcuts, newShortcut];
setSettingsState({ ...settingsState, customshortcuts: updatedCustomShortcuts }); setSettingsState({ ...settingsState, customshortcuts: updatedCustomShortcuts });
setNewTitle(""); setNewTitle("");