import { useEffect, useState } from 'react'; import { invoke } from '@tauri-apps/api/tauri'; import Editor from './components/Editor/Editor'; import { HStack, VStack } from './components/Stacks'; import { Dropdown } from './components/Dropdown'; import { WindowDragRegion } from './components/WindowDragRegion'; 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'; import { useRequests } from './hooks/useWorkspaces'; import { useParams } from 'react-router-dom'; interface Response { url: string; method: string; body: string; status: string; elapsed: number; elapsed2: number; headers: Record; } type Params = { workspaceId: string; requestId?: string; }; function App() { const p = useParams(); const workspaceId = p.workspaceId ?? ''; const requestId = p.requestId; const { data: requests } = useRequests(workspaceId); const request = requests?.find((r) => r.id === requestId); const [loading, setLoading] = useState(false); const [error, setError] = useState(null); const [response, setResponse] = useState(null); const [url, setUrl] = useState(request?.url ?? ''); const [body, setBody] = useState(request?.body ?? ''); const [method, setMethod] = useState<{ label: string; value: string }>({ label: request?.method ?? 'GET', value: request?.method ?? 'GET', }); 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); }, []); async function sendRequest() { setLoading(true); setError(null); try { const resp = (await invoke('send_request', { method: method.value, url, body: body || undefined, })) as Response; if (resp.body.includes('')) { resp.body = resp.body.replace(//gi, ``); } setResponse(resp); } catch (err) { setError(`${err}`); } finally { setLoading(false); } } const contentType = response?.headers['content-type']?.split(';')[0] ?? 'text/plain'; return ( <>
Test Request setResponse(null), disabled: !response, }, { label: 'Other Thing', }, ]} > {(response || error) && ( {error &&
{error}
} {response && ( <> {response.status}  •  {response.elapsed}ms  •  {response.elapsed2}ms {contentType.includes('html') ? (