diff --git a/src/components/form.jsx b/src/components/form.jsx index 185d7a8a..38ccb3f4 100644 --- a/src/components/form.jsx +++ b/src/components/form.jsx @@ -1,5 +1,7 @@ import { useEffect, useRef, useState } from "react"; import { LoadingSpinner } from "./loadingSpinner"; +const searchParams = new URLSearchParams(window.location.search); +const prefillVar = searchParams.get("prefill_var"); const Form = () => { const [data, setData] = useState(null); @@ -59,12 +61,33 @@ const Form = () => { } }; + const copyToClipboard = (e) => { + e.preventDefault(); + const link = `${window.location.origin}/?prefill_var=${userInput}`; + navigator.clipboard + .writeText(link) + .then(() => { + console.log("Link copied to clipboard"); + }) + .catch((err) => { + console.error("Could not copy text: ", err); + }); + }; + useEffect(() => { if (!isUserInputBlank) { previousUserInput.current = userInput; } }, [userInput]); + useEffect(() => { + if (prefillVar) { + setUserInput(prefillVar); + const mockEvent = { preventDefault: () => {} }; + fetchData(mockEvent); + } + }, [isUserInputBlank]); + return (
@@ -126,6 +149,9 @@ const Form = () => {

{data.definition}

+ copyToClipboard(e)} + className="text-blue-600 hover:text-blue-800 visited:text-purple-600">

{data.alternatives}