diff --git a/src/frontend/src/components/ui/rename-label.tsx b/src/frontend/src/components/ui/rename-label.tsx new file mode 100644 index 000000000..9f9a69137 --- /dev/null +++ b/src/frontend/src/components/ui/rename-label.tsx @@ -0,0 +1,84 @@ +import { useState, useEffect, useRef } from "react"; +import { cn } from "../../utils"; + +export default function RenameLabel(props) { + const [internalState, setInternalState] = useState(false); + const [isRename, setIsRename] = props.rename + ? [props.rename, props.setRename] + : [internalState, setInternalState]; + + const [myValue, setMyValue] = useState(props.value); + useEffect(() => { + if (isRename) { + document.addEventListener("keydown", (e) => { + if (e.key === "Escape") { + setIsRename(false); + props.setValue(""); + } + }); + if (inputRef.current) { + setTimeout(() => { + inputRef.current.focus(); + }, 100); + } + } + resizeInput(); + }, [isRename]); + + const inputRef = useRef(null); + + const resizeInput = () => { + const input = inputRef.current; + if (input) { + const span = document.createElement("span"); + span.style.position = "absolute"; + span.style.visibility = "hidden"; + span.style.whiteSpace = "pre"; + span.style.font = window.getComputedStyle(input).font; + span.textContent = input.value; + + document.body.appendChild(span); + const textWidth = span.getBoundingClientRect().width; + document.body.removeChild(span); + + input.style.width = `${textWidth + 16}px`; + } + }; + return ( +
+ {isRename ? ( + { + setIsRename(false); + if (props.value !== "") { + props.setValue(myValue); + } + }} + value={myValue} + onChange={(e) => { + setMyValue(e.target.value); + }} + /> + ) : ( +
+ { + setIsRename(true); + setMyValue(props.value); + }} + > + {props.value} + +
+ )} +
+ ); +} diff --git a/src/frontend/src/pages/MainPage/index.tsx b/src/frontend/src/pages/MainPage/index.tsx index 0cb9f7cb7..b341275de 100644 --- a/src/frontend/src/pages/MainPage/index.tsx +++ b/src/frontend/src/pages/MainPage/index.tsx @@ -21,6 +21,7 @@ import { ChevronDownIcon, CodeBracketSquareIcon, GlobeAltIcon, + PencilSquareIcon, PlusIcon, TrashIcon, } from "@heroicons/react/24/outline"; @@ -47,6 +48,7 @@ import { CardHeader, CardTitle, } from "../../components/ui/card"; +import RenameLabel from "../../components/ui/rename-label"; export default function HomePage() { const { @@ -63,6 +65,7 @@ export default function HomePage() { const AlertWidth = 384; const { dark, setDark } = useContext(darkContext); const [activeTab, setActiveTab] = useState("myflow"); + const [rename, setRename] = useState(false); const { notificationCenter, setNotificationCenter } = useContext(alertContext); useEffect(() => { @@ -84,13 +87,26 @@ export default function HomePage() { ⛓️
- {flows[tabIndex].name} + {}} + rename={rename} + setRename={setRename} + /> Current Flow + { + setRename(true); + }} + > + + Rename + { openPopUp(); @@ -126,7 +142,7 @@ export default function HomePage() { {flows.map((flow, idx) => { return ( - {}}>{flow.name} + {flow.name} ); })}