mirror of
https://github.com/mountain-loop/yaak.git
synced 2026-04-23 17:18:32 +02:00
Send requests with active environment
This commit is contained in:
@@ -7,7 +7,7 @@
|
|||||||
#[macro_use]
|
#[macro_use]
|
||||||
extern crate objc;
|
extern crate objc;
|
||||||
|
|
||||||
use crate::models::{find_environments, generate_id};
|
use crate::models::generate_id;
|
||||||
use base64::Engine;
|
use base64::Engine;
|
||||||
use http::header::{HeaderName, ACCEPT, USER_AGENT};
|
use http::header::{HeaderName, ACCEPT, USER_AGENT};
|
||||||
use http::{HeaderMap, HeaderValue, Method};
|
use http::{HeaderMap, HeaderValue, Method};
|
||||||
@@ -64,28 +64,27 @@ async fn migrate_db(
|
|||||||
#[tauri::command]
|
#[tauri::command]
|
||||||
async fn send_ephemeral_request(
|
async fn send_ephemeral_request(
|
||||||
request: models::HttpRequest,
|
request: models::HttpRequest,
|
||||||
|
environment_id: &str,
|
||||||
app_handle: AppHandle<Wry>,
|
app_handle: AppHandle<Wry>,
|
||||||
db_instance: State<'_, Mutex<Pool<Sqlite>>>,
|
db_instance: State<'_, Mutex<Pool<Sqlite>>>,
|
||||||
) -> Result<models::HttpResponse, String> {
|
) -> Result<models::HttpResponse, String> {
|
||||||
let pool = &*db_instance.lock().await;
|
let pool = &*db_instance.lock().await;
|
||||||
let response = models::HttpResponse::default();
|
let response = models::HttpResponse::default();
|
||||||
return actually_send_ephemeral_request(request, &response, &app_handle, pool).await;
|
return actually_send_ephemeral_request(request, &response, &environment_id, &app_handle, pool).await;
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn actually_send_ephemeral_request(
|
async fn actually_send_ephemeral_request(
|
||||||
request: models::HttpRequest,
|
request: models::HttpRequest,
|
||||||
response: &models::HttpResponse,
|
response: &models::HttpResponse,
|
||||||
|
environment_id: &str,
|
||||||
app_handle: &AppHandle<Wry>,
|
app_handle: &AppHandle<Wry>,
|
||||||
pool: &Pool<Sqlite>,
|
pool: &Pool<Sqlite>,
|
||||||
) -> Result<models::HttpResponse, String> {
|
) -> Result<models::HttpResponse, String> {
|
||||||
let start = std::time::Instant::now();
|
let start = std::time::Instant::now();
|
||||||
|
|
||||||
let environments = find_environments(&request.workspace_id, pool)
|
let environment = models::get_environment(environment_id, pool).await.ok();
|
||||||
.await
|
|
||||||
.expect("Failed to find environments");
|
|
||||||
|
|
||||||
// TODO: Use active environment
|
// TODO: Use active environment
|
||||||
let environment = environments.first();
|
|
||||||
let mut url_string = match environment {
|
let mut url_string = match environment {
|
||||||
Some(e) => render::render(&request.url, e.clone()),
|
Some(e) => render::render(&request.url, e.clone()),
|
||||||
None => request.url.to_string(),
|
None => request.url.to_string(),
|
||||||
@@ -250,6 +249,7 @@ async fn send_request(
|
|||||||
window: Window<Wry>,
|
window: Window<Wry>,
|
||||||
db_instance: State<'_, Mutex<Pool<Sqlite>>>,
|
db_instance: State<'_, Mutex<Pool<Sqlite>>>,
|
||||||
request_id: &str,
|
request_id: &str,
|
||||||
|
environment_id: Option<&str>,
|
||||||
) -> Result<models::HttpResponse, String> {
|
) -> Result<models::HttpResponse, String> {
|
||||||
let pool = &*db_instance.lock().await;
|
let pool = &*db_instance.lock().await;
|
||||||
|
|
||||||
@@ -262,10 +262,12 @@ async fn send_request(
|
|||||||
.expect("Failed to create response");
|
.expect("Failed to create response");
|
||||||
|
|
||||||
let response2 = response.clone();
|
let response2 = response.clone();
|
||||||
|
let environment_id2 = environment_id.unwrap_or("n/a").to_string();
|
||||||
let app_handle2 = window.app_handle().clone();
|
let app_handle2 = window.app_handle().clone();
|
||||||
let pool2 = pool.clone();
|
let pool2 = pool.clone();
|
||||||
|
|
||||||
tokio::spawn(async move {
|
tokio::spawn(async move {
|
||||||
actually_send_ephemeral_request(req, &response2, &app_handle2, &pool2)
|
actually_send_ephemeral_request(req, &response2, &environment_id2, &app_handle2, &pool2)
|
||||||
.await
|
.await
|
||||||
.expect("Failed to send request");
|
.expect("Failed to send request");
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -30,6 +30,7 @@ import { GraphQLEditor } from './GraphQLEditor';
|
|||||||
import { HeaderEditor } from './HeaderEditor';
|
import { HeaderEditor } from './HeaderEditor';
|
||||||
import { ParametersEditor } from './ParameterEditor';
|
import { ParametersEditor } from './ParameterEditor';
|
||||||
import { UrlBar } from './UrlBar';
|
import { UrlBar } from './UrlBar';
|
||||||
|
import { useActiveEnvironmentId } from '../hooks/useActiveEnvironmentId';
|
||||||
|
|
||||||
interface Props {
|
interface Props {
|
||||||
style?: CSSProperties;
|
style?: CSSProperties;
|
||||||
@@ -42,6 +43,7 @@ const useActiveTab = createGlobalState<string>('body');
|
|||||||
export const RequestPane = memo(function RequestPane({ style, fullHeight, className }: Props) {
|
export const RequestPane = memo(function RequestPane({ style, fullHeight, className }: Props) {
|
||||||
const activeRequest = useActiveRequest();
|
const activeRequest = useActiveRequest();
|
||||||
const activeRequestId = activeRequest?.id ?? null;
|
const activeRequestId = activeRequest?.id ?? null;
|
||||||
|
const activeEnvironmentId = useActiveEnvironmentId();
|
||||||
const updateRequest = useUpdateRequest(activeRequestId);
|
const updateRequest = useUpdateRequest(activeRequestId);
|
||||||
const [activeTab, setActiveTab] = useActiveTab();
|
const [activeTab, setActiveTab] = useActiveTab();
|
||||||
const [forceUpdateHeaderEditorKey, setForceUpdateHeaderEditorKey] = useState<number>(0);
|
const [forceUpdateHeaderEditorKey, setForceUpdateHeaderEditorKey] = useState<number>(0);
|
||||||
@@ -144,9 +146,9 @@ export const RequestPane = memo(function RequestPane({ style, fullHeight, classN
|
|||||||
'send_request',
|
'send_request',
|
||||||
async ({ windowLabel }) => {
|
async ({ windowLabel }) => {
|
||||||
if (windowLabel !== appWindow.label) return;
|
if (windowLabel !== appWindow.label) return;
|
||||||
await invoke('send_request', { requestId: activeRequestId });
|
await invoke('send_request', { requestId: activeRequestId, environmentId: activeEnvironmentId });
|
||||||
},
|
},
|
||||||
[activeRequestId],
|
[activeRequestId, activeEnvironmentId],
|
||||||
);
|
);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
|
|||||||
@@ -23,6 +23,8 @@ import { useActiveEnvironmentId } from '../hooks/useActiveEnvironmentId';
|
|||||||
import { WorkspaceActionsDropdown } from './WorkspaceActionsDropdown';
|
import { WorkspaceActionsDropdown } from './WorkspaceActionsDropdown';
|
||||||
import { IconButton } from './core/IconButton';
|
import { IconButton } from './core/IconButton';
|
||||||
import { useCreateRequest } from '../hooks/useCreateRequest';
|
import { useCreateRequest } from '../hooks/useCreateRequest';
|
||||||
|
import { appWindow } from '@tauri-apps/api/window';
|
||||||
|
import { invoke } from '@tauri-apps/api';
|
||||||
|
|
||||||
interface Props {
|
interface Props {
|
||||||
className?: string;
|
className?: string;
|
||||||
@@ -48,6 +50,9 @@ export const Sidebar = memo(function Sidebar({ className }: Props) {
|
|||||||
const [hasFocus, setHasFocus] = useState<boolean>(false);
|
const [hasFocus, setHasFocus] = useState<boolean>(false);
|
||||||
const [selectedIndex, setSelectedIndex] = useState<number>();
|
const [selectedIndex, setSelectedIndex] = useState<number>();
|
||||||
|
|
||||||
|
// TODO: Move these listeners to a central place
|
||||||
|
useListenToTauriEvent('create_request', async () => createRequest.mutate({}));
|
||||||
|
|
||||||
const focusActiveRequest = useCallback(
|
const focusActiveRequest = useCallback(
|
||||||
(forcedIndex?: number) => {
|
(forcedIndex?: number) => {
|
||||||
const index = forcedIndex ?? requests.findIndex((r) => r.id === activeRequestId);
|
const index = forcedIndex ?? requests.findIndex((r) => r.id === activeRequestId);
|
||||||
@@ -169,7 +174,12 @@ export const Sidebar = memo(function Sidebar({ className }: Props) {
|
|||||||
className="text-left mb-0"
|
className="text-left mb-0"
|
||||||
justify="start"
|
justify="start"
|
||||||
/>
|
/>
|
||||||
<IconButton size="sm" icon="plusCircle" title="Create Request" onClick={() => createRequest.mutate({})} />
|
<IconButton
|
||||||
|
size="sm"
|
||||||
|
icon="plusCircle"
|
||||||
|
title="Create Request"
|
||||||
|
onClick={() => createRequest.mutate({})}
|
||||||
|
/>
|
||||||
</HStack>
|
</HStack>
|
||||||
<VStack
|
<VStack
|
||||||
as="ul"
|
as="ul"
|
||||||
|
|||||||
@@ -13,6 +13,7 @@ import { baseExtensions, getLanguageExtension, multiLineExtensions } from './ext
|
|||||||
import type { GenericCompletionConfig } from './genericCompletion';
|
import type { GenericCompletionConfig } from './genericCompletion';
|
||||||
import { singleLineExt } from './singleLine';
|
import { singleLineExt } from './singleLine';
|
||||||
import { useEnvironments } from '../../../hooks/useEnvironments';
|
import { useEnvironments } from '../../../hooks/useEnvironments';
|
||||||
|
import { useActiveEnvironment } from '../../../hooks/useActiveEnvironment';
|
||||||
|
|
||||||
// Export some things so all the code-split parts are in this file
|
// Export some things so all the code-split parts are in this file
|
||||||
export { buildClientSchema, getIntrospectionQuery } from 'graphql/utilities';
|
export { buildClientSchema, getIntrospectionQuery } from 'graphql/utilities';
|
||||||
@@ -67,8 +68,7 @@ const _Editor = forwardRef<EditorView | undefined, EditorProps>(function Editor(
|
|||||||
}: EditorProps,
|
}: EditorProps,
|
||||||
ref,
|
ref,
|
||||||
) {
|
) {
|
||||||
const environments = useEnvironments();
|
const environment = useActiveEnvironment();
|
||||||
const environment = environments[0] ?? null;
|
|
||||||
|
|
||||||
const cm = useRef<{ view: EditorView; languageCompartment: Compartment } | null>(null);
|
const cm = useRef<{ view: EditorView; languageCompartment: Compartment } | null>(null);
|
||||||
useImperativeHandle(ref, () => cm.current?.view);
|
useImperativeHandle(ref, () => cm.current?.view);
|
||||||
@@ -121,7 +121,6 @@ const _Editor = forwardRef<EditorView | undefined, EditorProps>(function Editor(
|
|||||||
if (cm.current === null) return;
|
if (cm.current === null) return;
|
||||||
const { view, languageCompartment } = cm.current;
|
const { view, languageCompartment } = cm.current;
|
||||||
const ext = getLanguageExtension({ contentType, environment, useTemplating, autocomplete });
|
const ext = getLanguageExtension({ contentType, environment, useTemplating, autocomplete });
|
||||||
console.log("EXT", ext);
|
|
||||||
view.dispatch({ effects: languageCompartment.reconfigure(ext) });
|
view.dispatch({ effects: languageCompartment.reconfigure(ext) });
|
||||||
}, [contentType, autocomplete, useTemplating, environment]);
|
}, [contentType, autocomplete, useTemplating, environment]);
|
||||||
|
|
||||||
|
|||||||
@@ -1,9 +1,11 @@
|
|||||||
import { useMutation } from '@tanstack/react-query';
|
import { useMutation } from '@tanstack/react-query';
|
||||||
import { invoke } from '@tauri-apps/api';
|
import { invoke } from '@tauri-apps/api';
|
||||||
import type { HttpResponse } from '../lib/models';
|
import type { HttpResponse } from '../lib/models';
|
||||||
|
import { useActiveEnvironmentId } from './useActiveEnvironmentId';
|
||||||
|
|
||||||
export function useSendRequest(id: string | null) {
|
export function useSendRequest(id: string | null) {
|
||||||
|
const environmentId = useActiveEnvironmentId();
|
||||||
return useMutation<HttpResponse, string>({
|
return useMutation<HttpResponse, string>({
|
||||||
mutationFn: () => invoke('send_request', { requestId: id }),
|
mutationFn: () => invoke('send_request', { requestId: id, environmentId }),
|
||||||
}).mutate;
|
}).mutate;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user