mirror of
https://github.com/mountain-loop/yaak.git
synced 2026-07-09 22:32:45 +02:00
Return struct for response
This commit is contained in:
+24
-2
@@ -9,16 +9,38 @@ fn greet(name: &str) -> String {
|
|||||||
format!("Hello, {}! You've been greeted from Rust!", name)
|
format!("Hello, {}! You've been greeted from Rust!", name)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(serde::Serialize)]
|
||||||
|
struct CustomResponse {
|
||||||
|
status: String,
|
||||||
|
body: String,
|
||||||
|
elapsed: u128,
|
||||||
|
elapsed2: u128,
|
||||||
|
}
|
||||||
|
|
||||||
#[tauri::command]
|
#[tauri::command]
|
||||||
async fn send_request(url: &str) -> Result<String, String> {
|
async fn send_request(url: &str) -> Result<CustomResponse, String> {
|
||||||
|
let start = std::time::Instant::now();
|
||||||
|
|
||||||
let mut url = url.to_string();
|
let mut url = url.to_string();
|
||||||
if !url.starts_with("http://") && !url.starts_with("https://") {
|
if !url.starts_with("http://") && !url.starts_with("https://") {
|
||||||
url = format!("http://{}", url);
|
url = format!("http://{}", url);
|
||||||
}
|
}
|
||||||
|
|
||||||
let resp = reqwest::get(url).await;
|
let resp = reqwest::get(url).await;
|
||||||
|
let elapsed = start.elapsed().as_millis();
|
||||||
|
|
||||||
match resp {
|
match resp {
|
||||||
Ok(v) => Ok(v.text().await.unwrap()),
|
Ok(v) => {
|
||||||
|
let status = v.status().to_string();
|
||||||
|
let body = v.text().await.unwrap();
|
||||||
|
let elapsed2 = start.elapsed().as_millis();
|
||||||
|
Ok(CustomResponse {
|
||||||
|
status,
|
||||||
|
body,
|
||||||
|
elapsed,
|
||||||
|
elapsed2,
|
||||||
|
})
|
||||||
|
}
|
||||||
Err(e) => Err(e.to_string()),
|
Err(e) => Err(e.to_string()),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+35
-21
@@ -1,38 +1,52 @@
|
|||||||
import {useState} from "react";
|
import {useState} from "react";
|
||||||
import reactLogo from "./assets/react.svg";
|
|
||||||
import {invoke} from "@tauri-apps/api/tauri";
|
import {invoke} from "@tauri-apps/api/tauri";
|
||||||
import "./App.css";
|
import "./App.css";
|
||||||
import Editor from "./Editor";
|
import Editor from "./Editor";
|
||||||
|
|
||||||
|
interface Response {
|
||||||
|
body: string;
|
||||||
|
status: string;
|
||||||
|
elapsed: number;
|
||||||
|
elapsed2: number;
|
||||||
|
}
|
||||||
|
|
||||||
function App() {
|
function App() {
|
||||||
const [responseBody, setResponseBody] = useState<string>("");
|
const [responseBody, setResponseBody] = useState<Response | null>(null);
|
||||||
const [url, setUrl] = useState("");
|
const [url, setUrl] = useState("");
|
||||||
|
|
||||||
async function sendRequest() {
|
async function sendRequest() {
|
||||||
const body = await invoke("send_request", {url: url}) as string;
|
const body = await invoke("send_request", {url: url}) as Response;
|
||||||
setResponseBody(body);
|
setResponseBody(body);
|
||||||
}
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="container">
|
<div className="container">
|
||||||
<h1>Welcome to Twosomnia!</h1>
|
<h1>Welcome, Friend!</h1>
|
||||||
|
<form
|
||||||
<div className="row">
|
onSubmit={(e) => {
|
||||||
<form
|
e.preventDefault();
|
||||||
onSubmit={(e) => {
|
sendRequest();
|
||||||
e.preventDefault();
|
}}
|
||||||
sendRequest();
|
>
|
||||||
}}
|
<input
|
||||||
>
|
id="greet-input"
|
||||||
<input
|
onChange={(e) => setUrl(e.currentTarget.value)}
|
||||||
id="greet-input"
|
placeholder="Enter a URL..."
|
||||||
onChange={(e) => setUrl(e.currentTarget.value)}
|
/>
|
||||||
placeholder="Enter a URL..."
|
<button type="submit">Send</button>
|
||||||
/>
|
</form>
|
||||||
<button type="submit">Send</button>
|
{responseBody !== null && (
|
||||||
<Editor value={responseBody}/>
|
<>
|
||||||
</form>
|
<div style={{paddingTop: "2rem"}}>
|
||||||
</div>
|
{responseBody?.status}
|
||||||
|
•
|
||||||
|
{responseBody?.elapsed}ms
|
||||||
|
•
|
||||||
|
{responseBody?.elapsed2}ms
|
||||||
|
</div>
|
||||||
|
<Editor value={responseBody?.body}/>
|
||||||
|
</>
|
||||||
|
)}
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -23,3 +23,11 @@
|
|||||||
.cm-editor .cm-cursor {
|
.cm-editor .cm-cursor {
|
||||||
border-left: 2px solid red;
|
border-left: 2px solid red;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.cm-editor .cm-selectionBackground {
|
||||||
|
background-color: rgba(180, 180, 180, 0.3);
|
||||||
|
}
|
||||||
|
|
||||||
|
.cm-editor.cm-focused .cm-selectionBackground {
|
||||||
|
background-color: rgba(180, 180, 180, 0.3);
|
||||||
|
}
|
||||||
|
|||||||
+32
-28
@@ -3,42 +3,45 @@ import {EditorView, minimalSetup} from "codemirror";
|
|||||||
import {javascript} from "@codemirror/lang-javascript";
|
import {javascript} from "@codemirror/lang-javascript";
|
||||||
import {json} from "@codemirror/lang-json";
|
import {json} from "@codemirror/lang-json";
|
||||||
import {html} from "@codemirror/lang-html";
|
import {html} from "@codemirror/lang-html";
|
||||||
|
import {EditorState} from "@codemirror/state";
|
||||||
import {tags} from "@lezer/highlight"
|
import {tags} from "@lezer/highlight"
|
||||||
import {HighlightStyle, syntaxHighlighting} from "@codemirror/language"
|
import {HighlightStyle, syntaxHighlighting} from "@codemirror/language"
|
||||||
|
|
||||||
|
const myHighlightStyle = HighlightStyle.define([
|
||||||
|
{
|
||||||
|
tag: [
|
||||||
|
tags.documentMeta,
|
||||||
|
tags.blockComment,
|
||||||
|
tags.lineComment,
|
||||||
|
tags.docComment,
|
||||||
|
tags.comment,
|
||||||
|
],
|
||||||
|
color: "#757b93"
|
||||||
|
},
|
||||||
|
{tag: tags.name, color: "#4b92ff"},
|
||||||
|
{tag: tags.variableName, color: "#4bff4e"},
|
||||||
|
{tag: tags.attributeName, color: "#b06fff"},
|
||||||
|
{tag: tags.attributeValue, color: "#ff964b"},
|
||||||
|
{tag: tags.keyword, color: "#fc6"},
|
||||||
|
{tag: tags.comment, color: "#f5d", fontStyle: "italic"}
|
||||||
|
]);
|
||||||
|
|
||||||
|
const extensions = [
|
||||||
|
minimalSetup,
|
||||||
|
syntaxHighlighting(myHighlightStyle),
|
||||||
|
html(),
|
||||||
|
javascript(),
|
||||||
|
json(),
|
||||||
|
];
|
||||||
|
|
||||||
export default function useCodeMirror({value}: { value: string }) {
|
export default function useCodeMirror({value}: { value: string }) {
|
||||||
const [cm, setCm] = useState<EditorView | null>(null);
|
const [cm, setCm] = useState<EditorView | null>(null);
|
||||||
const ref = useRef(null);
|
const ref = useRef(null);
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (ref.current === null) return;
|
if (ref.current === null) return;
|
||||||
|
|
||||||
const myHighlightStyle = HighlightStyle.define([
|
|
||||||
{
|
|
||||||
tag: [
|
|
||||||
tags.documentMeta,
|
|
||||||
tags.blockComment,
|
|
||||||
tags.lineComment,
|
|
||||||
tags.docComment,
|
|
||||||
tags.comment,
|
|
||||||
],
|
|
||||||
color: "#757b93"
|
|
||||||
},
|
|
||||||
{tag: tags.name, color: "#4b92ff"},
|
|
||||||
{tag: tags.variableName, color: "#4bff4e"},
|
|
||||||
{tag: tags.attributeName, color: "#b06fff"},
|
|
||||||
{tag: tags.attributeValue, color: "#ff964b"},
|
|
||||||
{tag: tags.keyword, color: "#fc6"},
|
|
||||||
{tag: tags.comment, color: "#f5d", fontStyle: "italic"}
|
|
||||||
])
|
|
||||||
|
|
||||||
const view = new EditorView({
|
const view = new EditorView({
|
||||||
extensions: [
|
extensions,
|
||||||
minimalSetup,
|
|
||||||
syntaxHighlighting(myHighlightStyle),
|
|
||||||
html(),
|
|
||||||
javascript(),
|
|
||||||
json(),
|
|
||||||
],
|
|
||||||
parent: ref.current
|
parent: ref.current
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -49,8 +52,9 @@ export default function useCodeMirror({value}: { value: string }) {
|
|||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (cm === null) return;
|
if (cm === null) return;
|
||||||
let transaction = cm.state.update({changes: {from: 0, to: cm.state.doc.length, insert: value}})
|
|
||||||
cm.dispatch(transaction)
|
const newState = EditorState.create({doc: value, extensions});
|
||||||
|
cm.setState(newState);
|
||||||
}, [cm, value]);
|
}, [cm, value]);
|
||||||
|
|
||||||
return {ref, cm};
|
return {ref, cm};
|
||||||
|
|||||||
+10
-1
@@ -14,6 +14,14 @@
|
|||||||
-webkit-text-size-adjust: 100%;
|
-webkit-text-size-adjust: 100%;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
:not(input):not(textarea),
|
||||||
|
:not(input):not(textarea)::after,
|
||||||
|
:not(input):not(textarea)::before {
|
||||||
|
-webkit-user-select: none;
|
||||||
|
user-select: none;
|
||||||
|
cursor: default;
|
||||||
|
}
|
||||||
|
|
||||||
div, form, p {
|
div, form, p {
|
||||||
width: 100%;
|
width: 100%;
|
||||||
}
|
}
|
||||||
@@ -25,7 +33,8 @@ div, form, p {
|
|||||||
flex-direction: column;
|
flex-direction: column;
|
||||||
justify-content: center;
|
justify-content: center;
|
||||||
text-align: center;
|
text-align: center;
|
||||||
width: 700px;
|
width: 80vw;
|
||||||
|
max-width: 800px;
|
||||||
}
|
}
|
||||||
|
|
||||||
.logo {
|
.logo {
|
||||||
|
|||||||
Reference in New Issue
Block a user