diff --git a/package.json b/package.json index 4bf8d37..a6e1972 100644 --- a/package.json +++ b/package.json @@ -12,7 +12,6 @@ "@lezer/highlight": "^1.0.0", "@uiw/codemirror-themes": "^4.11.4", "@uiw/react-codemirror": "^4.11.4", - "axios": "^0.27.2", "react": "^18.0.0", "react-dom": "^18.0.0", "react-router-dom": "^6.3.0" diff --git a/src/api/bin.ts b/src/api/bin.ts new file mode 100644 index 0000000..c92593b --- /dev/null +++ b/src/api/bin.ts @@ -0,0 +1,16 @@ +export const setPaste = async (code: string): Promise => { + return await fetch("http://localhost:8080/bin/paste", { + method: "POST", + headers: { "Content-type": "text/plain" }, + body: code, + }); +}; + +export const getPaste = async (pasteId: string) => { + return await fetch(`http://localhost:8080/bin/paste/${pasteId}`); +}; + +export interface SuccessResponse { + message: string; + endpoint: string; +} diff --git a/src/components/Editor.tsx b/src/components/Editor.tsx index c3706cc..86769ae 100644 --- a/src/components/Editor.tsx +++ b/src/components/Editor.tsx @@ -1,48 +1,19 @@ import CodeMirror from "@uiw/react-codemirror"; -import React, { useCallback, useState } from "react"; -import { GithubDark } from "./EditorTheme"; -import axios from "axios"; -import { useNavigate } from "react-router-dom"; +import { FunctionComponent } from "react"; +import Theme from "./EditorTheme"; -const Editor = () => { - const [code, setCode] = useState(""); - let navigate = useNavigate(); - - // TODO: fix this callback thing later - const postPaste = useCallback(async () => { - let res = await axios.post("http://localhost:8080/bin/paste", code, { - headers: { "Content-type": "text/plain" }, - }); - - if (res.status !== 200) { - alert("Something went wrong!"); - console.log(res); - } - navigate(`/${res.data["endpoint"]}`); - }, [code]); - - const handleSavePaste = async ( - event: React.KeyboardEvent, - ) => { - if ((event.ctrlKey || event.metaKey) && event.code === "KeyS") { - event.preventDefault(); - postPaste(); - } - }; - - const placeholder = `// paste code here, use ctrl + s to save`; +const Editor: FunctionComponent = props => { return ( // TODO: custom implementation? // TODO: fix line wrap props.keyEvent(e)} + placeholder={props.placeholder} height="100%" - onChange={(value: string) => { - setCode(value); - }} + onChange={val => props.setCode(val)} style={{ width: "100%", flex: 1, @@ -50,9 +21,27 @@ const Editor = () => { fontFamily: "IBM Plex Mono", fontSize: "1rem", }} - theme={GithubDark} + editable={props.editable} + readOnly={props.readonly} + theme={Theme} /> ); }; +Editor.defaultProps = { + placeholder: `// paste code here, use ctrl + s to save`, + readonly: false, + editable: true, + value: "", +}; + +interface propTypes { + readonly?: boolean; + editable?: boolean; + placeholder?: string; + value: string; + setCode: Function; + keyEvent: Function; +} + export default Editor; diff --git a/src/components/EditorTheme.tsx b/src/components/EditorTheme.ts similarity index 93% rename from src/components/EditorTheme.tsx rename to src/components/EditorTheme.ts index 49f60e5..d75ba94 100644 --- a/src/components/EditorTheme.tsx +++ b/src/components/EditorTheme.ts @@ -1,7 +1,8 @@ import { tags as t } from "@lezer/highlight"; import { createTheme } from "@uiw/codemirror-themes"; -export const GithubDark = createTheme({ +// custom theme based on GitHub dark theme +export default createTheme({ theme: "dark", settings: { background: "black", diff --git a/src/index.css b/src/index.css index 34a9145..7b74f0a 100644 --- a/src/index.css +++ b/src/index.css @@ -6,10 +6,8 @@ } body { - font-family: "IBM Plex Mono", monospace; background: black; color: white; - /* min-height: 100%; */ } #root { diff --git a/src/pages/Home.tsx b/src/pages/Home.tsx index 5832c9b..fab0218 100644 --- a/src/pages/Home.tsx +++ b/src/pages/Home.tsx @@ -1,10 +1,33 @@ +import { useCallback, useState } from "react"; +import { useNavigate } from "react-router-dom"; +import { setPaste, SuccessResponse } from "../api/bin"; import BottomBar from "../components/BottomBar"; import Editor from "../components/Editor"; function Home() { + const [code, setCode] = useState(""); + const navigate = useNavigate(); + + const postPaste = useCallback(async () => { + const res: Response = await setPaste(code); + if (res.status !== 200) { + alert("Something went wrong!"); + console.log(res); + } + let data: SuccessResponse = await res.json(); + navigate(`/${data.endpoint}`); + }, [code]); + + const handleSaveKey = async (event: React.KeyboardEvent) => { + if ((event.ctrlKey || event.metaKey) && event.code === "KeyS") { + event.preventDefault(); + postPaste(); + } + }; + return ( <> - + ); diff --git a/src/pages/Paste.tsx b/src/pages/Paste.tsx index 2b69f50..887c066 100644 --- a/src/pages/Paste.tsx +++ b/src/pages/Paste.tsx @@ -1,8 +1,48 @@ +import { useEffect, useState } from "react"; +import { useNavigate, useParams } from "react-router-dom"; +import { getPaste } from "../api/bin"; +import BottomBar from "../components/BottomBar"; +import Editor from "../components/Editor"; + function Paste() { + const { id } = useParams(); + const [code, setCode] = useState(""); + const navigate = useNavigate(); + + const getCode = async () => { + if (id == undefined) { + alert("Something went wrong!"); + return navigate("/"); + } + + const res: Response = await getPaste(id); + + if (res.status == 404) { + alert("Paste not found!"); + navigate("/"); + } + + if (res.status == 200) { + setCode(await res.text()); + } + }; + + useEffect(() => { + getCode(); + }, []); + return ( -
-

Paste Page

-
+ <> + {}} + value={code} + keyEvent={() => {}} + editable={false} + readonly={true} + placeholder={""} + /> + + ); }