From 72486b448c85c4e04aeafd521ebf1411eb2ea2ef Mon Sep 17 00:00:00 2001 From: Gregory Schier Date: Fri, 24 Feb 2023 16:43:47 -0800 Subject: [PATCH] Codemirror initial value support --- src-web/App.tsx | 86 +++++++++++++++++----------- src-web/components/Editor/Editor.tsx | 9 +-- src-web/components/UrlBar.tsx | 18 ++---- src-web/hooks/useCodemirror.ts | 15 +++-- 4 files changed, 71 insertions(+), 57 deletions(-) diff --git a/src-web/App.tsx b/src-web/App.tsx index d9313718..5dfa2072 100644 --- a/src-web/App.tsx +++ b/src-web/App.tsx @@ -1,4 +1,4 @@ -import { useState } from 'react'; +import { useEffect, useState } from 'react'; import { invoke } from '@tauri-apps/api/tauri'; import Editor from './components/Editor/Editor'; import { HStack, VStack } from './components/Stacks'; @@ -8,6 +8,7 @@ import { IconButton } from './components/IconButton'; import { Sidebar } from './components/Sidebar'; import { UrlBar } from './components/UrlBar'; import { Grid } from './components/Grid'; +import { motion } from 'framer-motion'; interface Response { url: string; @@ -20,16 +21,18 @@ interface Response { } function App() { + const [loading, setLoading] = useState(false); const [error, setError] = useState(null); const [response, setResponse] = useState(null); const [url, setUrl] = useState('https://go-server.schier.dev/debug'); - const [body, setBody] = useState(''); + const [body, setBody] = useState(`{\n "foo": "bar"\n}`); const [method, setMethod] = useState<{ label: string; value: string }>({ label: 'GET', value: 'GET', }); async function sendRequest() { + setLoading(true); setError(null); try { @@ -44,11 +47,23 @@ function App() { setResponse(resp); } catch (err) { setError(`${err}`); + } finally { + setLoading(false); } } const contentType = response?.headers['content-type']?.split(';')[0] ?? 'text/plain'; + useEffect(() => { + const listener = async (e: KeyboardEvent) => { + if (e.metaKey && (e.key === 'Enter' || e.key === 'r')) { + await sendRequest(); + } + }; + document.documentElement.addEventListener('keypress', listener); + return () => document.documentElement.removeEventListener('keypress', listener); + }, []); + return ( <>
@@ -60,15 +75,12 @@ function App() { - + @@ -88,32 +100,40 @@ function App() { - - {error &&
{error}
} - {response !== null && ( - <> - - {response.status} -  •  - {response.elapsed}ms  •  - {response.elapsed2}ms - - {contentType.includes('html') ? ( -