import { useState, useEffect, useRef } from "react"; import { useNavigate, useParams } from "react-router-dom"; import Prism from "prismjs"; import styles from "./Editor.module.css"; import "../prism-themes/prism-gruvbox-dark.css"; import { SERVER_BASE_URL, SUPPORTED_LANGUAGES } from "../../utils/constants"; const Editor = () => { const navigate = useNavigate(); const params = useParams(); const [text, setText] = useState(""); const [language, setLanguage] = useState("js"); const textareaRef = useRef(null); const lineNumberRef = useRef(null); const handleTextChange = (event) => { setText(event.target.value); }; const handleScroll = () => { if (textareaRef.current && lineNumberRef.current) { lineNumberRef.current.scrollTop = textareaRef.current.scrollTop; } }; const handleClick = async () => { const response = await fetch(`${SERVER_BASE_URL}/bin`, { method: "POST", headers: { "Content-Type": "application/json", }, body: JSON.stringify({ language, content: text, }), }); const data = await response.json(); if (response.ok) { navigate(`/${data.id}`); } else { console.error(data); } }; const handleLanguageChange = (event) => { setLanguage(event.target.value); }; useEffect(() => { Prism.highlightAll(); }, [text, language]); useEffect(() => { if (params.id) fetchData(); else { textareaRef.current.value = ""; setText(""); } }, [params.id]); const fetchData = async () => { const response = await fetch(`${SERVER_BASE_URL}/bin/${params.id}`); const data = await response.json(); if (response.ok) { setLanguage(data.language); setText(data.content); } }; const lines = text.split("\n"); return (
{!params.id && ( <> )}
{lines.map((_, index) => (
{index + 1}
))}