diff options
author | Blaster4385 <blaster4385@tablaster.dev> | 2024-02-19 20:43:31 +0530 |
---|---|---|
committer | Blaster4385 <blaster4385@tablaster.dev> | 2024-02-21 23:52:45 +0530 |
commit | 96c2c2b16ff36b6fc01f4bcae26ff3d1e885b73d (patch) | |
tree | 7e264fddc3d6fda7b5026495dbc68345dacd78a9 /client/src/components/Editor/Editor.jsx | |
parent | 2186af44ecc1924f8343e06714999c76a15a1c0e (diff) |
feat: Add support for shortening URLs
Diffstat (limited to 'client/src/components/Editor/Editor.jsx')
-rw-r--r-- | client/src/components/Editor/Editor.jsx | 18 |
1 files changed, 14 insertions, 4 deletions
diff --git a/client/src/components/Editor/Editor.jsx b/client/src/components/Editor/Editor.jsx index dd07e5f..aaa9ac3 100644 --- a/client/src/components/Editor/Editor.jsx +++ b/client/src/components/Editor/Editor.jsx @@ -1,14 +1,15 @@ import React, { useState, useEffect, useRef, useMemo } from "react"; -import { useNavigate, useParams } from "react-router-dom"; +import { useLocation, 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 } from "../../utils/constants"; +import { CLIENT_BASE_URL, SERVER_BASE_URL, URL_REGEX } from "../../utils/constants"; import Header from "../Header/Header"; const Editor = () => { const { id } = useParams(); const navigate = useNavigate(); + const location = useLocation(); const [text, setText] = useState(""); const [language, setLanguage] = useState("none"); const textareaRef = useRef(null); @@ -60,8 +61,17 @@ const Editor = () => { const response = await fetch(`${SERVER_BASE_URL}/bin/${id}`); const data = await response.json(); if (response.ok) { - setLanguage(data.language); - setText(data.content); + const isURL = URL_REGEX.test(data.content); + if (isURL) { + setText(`Your shortened URL: ${CLIENT_BASE_URL}/r/${id}`); + if (location.pathname === `/r/${id}`) { + window.location.href = data.content; + } + } + else { + setLanguage(data.language); + setText(data.content); + } } }; |