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