mirror of
https://github.com/mountain-loop/yaak.git
synced 2026-04-24 01:28:35 +02:00
Dedicated event for model creation
This commit is contained in:
@@ -241,7 +241,7 @@ async fn send_request(
|
|||||||
.await
|
.await
|
||||||
.expect("Failed to create response");
|
.expect("Failed to create response");
|
||||||
window
|
window
|
||||||
.emit_all("updated_model", &response)
|
.emit_all("created_model", &response)
|
||||||
.expect("Failed to emit updated_model");
|
.expect("Failed to emit updated_model");
|
||||||
|
|
||||||
actually_send_ephemeral_request(req, response, window, pool).await?;
|
actually_send_ephemeral_request(req, response, window, pool).await?;
|
||||||
@@ -302,7 +302,9 @@ async fn create_workspace(
|
|||||||
.await
|
.await
|
||||||
.expect("Failed to create workspace");
|
.expect("Failed to create workspace");
|
||||||
|
|
||||||
emit_all_others(&window, "updated_model", &created_workspace);
|
window
|
||||||
|
.emit_all("created_model", &created_workspace)
|
||||||
|
.expect("Failed to emit event");
|
||||||
|
|
||||||
Ok(created_workspace.id)
|
Ok(created_workspace.id)
|
||||||
}
|
}
|
||||||
@@ -334,7 +336,9 @@ async fn create_request(
|
|||||||
.await
|
.await
|
||||||
.expect("Failed to create request");
|
.expect("Failed to create request");
|
||||||
|
|
||||||
emit_all_others(&window, "updated_model", &created_request);
|
window
|
||||||
|
.emit_all("created_model", &created_request)
|
||||||
|
.expect("Failed to emit event");
|
||||||
|
|
||||||
Ok(created_request.id)
|
Ok(created_request.id)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,16 +1,32 @@
|
|||||||
import { memo } from 'react';
|
import { memo, useCallback } from 'react';
|
||||||
|
import { useCreateRequest } from '../hooks/useCreateRequest';
|
||||||
import { useSidebarDisplay } from '../hooks/useSidebarDisplay';
|
import { useSidebarDisplay } from '../hooks/useSidebarDisplay';
|
||||||
import { IconButton } from './core/IconButton';
|
import { IconButton } from './core/IconButton';
|
||||||
|
import { HStack } from './core/Stacks';
|
||||||
|
|
||||||
export const SidebarDisplayToggle = memo(function SidebarDisplayToggle() {
|
export const SidebarDisplayToggle = memo(function SidebarDisplayToggle() {
|
||||||
const sidebarDisplay = useSidebarDisplay();
|
const sidebarDisplay = useSidebarDisplay();
|
||||||
|
const createRequest = useCreateRequest({ navigateAfter: true });
|
||||||
|
const handleCreateRequest = useCallback(() => {
|
||||||
|
createRequest.mutate({ name: 'New Request' });
|
||||||
|
}, []);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<IconButton
|
<HStack space={1}>
|
||||||
onClick={sidebarDisplay.toggle}
|
<IconButton
|
||||||
className="pointer-events-auto"
|
onClick={sidebarDisplay.toggle}
|
||||||
size="sm"
|
className="pointer-events-auto"
|
||||||
title="Show sidebar"
|
size="sm"
|
||||||
icon={sidebarDisplay.hidden ? 'leftPanelHidden' : 'leftPanelVisible'}
|
title="Show sidebar"
|
||||||
/>
|
icon={sidebarDisplay.hidden ? 'leftPanelHidden' : 'leftPanelVisible'}
|
||||||
|
/>
|
||||||
|
<IconButton
|
||||||
|
onClick={handleCreateRequest}
|
||||||
|
className="pointer-events-auto"
|
||||||
|
size="sm"
|
||||||
|
title="Show sidebar"
|
||||||
|
icon="plusCircle"
|
||||||
|
/>
|
||||||
|
</HStack>
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -2,18 +2,21 @@ import { useMutation } from '@tanstack/react-query';
|
|||||||
import { invoke } from '@tauri-apps/api';
|
import { invoke } from '@tauri-apps/api';
|
||||||
import type { HttpRequest } from '../lib/models';
|
import type { HttpRequest } from '../lib/models';
|
||||||
import { useActiveWorkspace } from './useActiveWorkspace';
|
import { useActiveWorkspace } from './useActiveWorkspace';
|
||||||
|
import { useRequests } from './useRequests';
|
||||||
import { useRoutes } from './useRoutes';
|
import { useRoutes } from './useRoutes';
|
||||||
|
|
||||||
export function useCreateRequest({ navigateAfter }: { navigateAfter: boolean }) {
|
export function useCreateRequest({ navigateAfter }: { navigateAfter: boolean }) {
|
||||||
const workspace = useActiveWorkspace();
|
const workspace = useActiveWorkspace();
|
||||||
const routes = useRoutes();
|
const routes = useRoutes();
|
||||||
|
const requests = useRequests();
|
||||||
|
|
||||||
return useMutation<string, unknown, Pick<HttpRequest, 'name' | 'sortPriority'>>({
|
return useMutation<string, unknown, Partial<Pick<HttpRequest, 'name' | 'sortPriority'>>>({
|
||||||
mutationFn: (patch) => {
|
mutationFn: (patch) => {
|
||||||
if (workspace === null) {
|
if (workspace === null) {
|
||||||
throw new Error("Cannot create request when there's no active workspace");
|
throw new Error("Cannot create request when there's no active workspace");
|
||||||
}
|
}
|
||||||
return invoke('create_request', { ...patch, workspaceId: workspace.id });
|
const sortPriority = maxSortPriority(requests) + 1000;
|
||||||
|
return invoke('create_request', { sortPriority, workspaceId: workspace.id, ...patch });
|
||||||
},
|
},
|
||||||
onSuccess: async (requestId) => {
|
onSuccess: async (requestId) => {
|
||||||
if (navigateAfter && workspace !== null) {
|
if (navigateAfter && workspace !== null) {
|
||||||
@@ -22,3 +25,8 @@ export function useCreateRequest({ navigateAfter }: { navigateAfter: boolean })
|
|||||||
},
|
},
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function maxSortPriority(requests: HttpRequest[]) {
|
||||||
|
if (requests.length === 0) return 1000;
|
||||||
|
return Math.max(...requests.map((r) => r.sortPriority));
|
||||||
|
}
|
||||||
|
|||||||
@@ -8,7 +8,6 @@ export function useDeleteRequest(id: string | null) {
|
|||||||
const queryClient = useQueryClient();
|
const queryClient = useQueryClient();
|
||||||
return useMutation<void, string>({
|
return useMutation<void, string>({
|
||||||
mutationFn: async () => {
|
mutationFn: async () => {
|
||||||
console.log('DELETE REQUEST2', id, workspaceId);
|
|
||||||
if (id === null) return;
|
if (id === null) return;
|
||||||
await invoke('delete_request', { requestId: id });
|
await invoke('delete_request', { requestId: id });
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -49,7 +49,7 @@ export function useTauriListeners() {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
listenDebounced('updated_model', ({ payload }: { payload: Model }) => {
|
listenDebounced('created_model', ({ payload }: { payload: Model }) => {
|
||||||
const queryKey =
|
const queryKey =
|
||||||
payload.model === 'http_request'
|
payload.model === 'http_request'
|
||||||
? requestsQueryKey(payload)
|
? requestsQueryKey(payload)
|
||||||
@@ -63,7 +63,7 @@ export function useTauriListeners() {
|
|||||||
|
|
||||||
if (queryKey === null) {
|
if (queryKey === null) {
|
||||||
if (payload.model) {
|
if (payload.model) {
|
||||||
console.log('Unrecognized updated model:', payload);
|
console.log('Unrecognized created model:', payload);
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -71,27 +71,33 @@ export function useTauriListeners() {
|
|||||||
const skipSync = payload.model === 'key_value' && payload.namespace === NAMESPACE_NO_SYNC;
|
const skipSync = payload.model === 'key_value' && payload.namespace === NAMESPACE_NO_SYNC;
|
||||||
|
|
||||||
if (!skipSync) {
|
if (!skipSync) {
|
||||||
queryClient.setQueryData(queryKey, (values: Model[] = []) => {
|
queryClient.setQueryData(queryKey, (values: Model[] = []) => [...values, payload]);
|
||||||
const newValues = [];
|
}
|
||||||
let found = false;
|
});
|
||||||
for (const v of values) {
|
|
||||||
if (modelsEq(v, payload)) {
|
|
||||||
found = true;
|
|
||||||
newValues.push(payload);
|
|
||||||
} else {
|
|
||||||
newValues.push(v);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Doesn't exist already, so add it to the list
|
listenDebounced('updated_model', ({ payload }: { payload: Model }) => {
|
||||||
if (!found) newValues.push(payload);
|
const queryKey =
|
||||||
|
payload.model === 'http_request'
|
||||||
|
? requestsQueryKey(payload)
|
||||||
|
: payload.model === 'http_response'
|
||||||
|
? responsesQueryKey(payload)
|
||||||
|
: payload.model === 'workspace'
|
||||||
|
? workspacesQueryKey(payload)
|
||||||
|
: payload.model === 'key_value'
|
||||||
|
? keyValueQueryKey(payload)
|
||||||
|
: null;
|
||||||
|
|
||||||
if (payload.model === 'http_request') {
|
if (queryKey === null) {
|
||||||
setTimeout(() => wasUpdatedExternally(payload.id), 50);
|
console.log('Unrecognized updated model:', payload);
|
||||||
}
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
return newValues;
|
const skipSync = payload.model === 'key_value' && payload.namespace === NAMESPACE_NO_SYNC;
|
||||||
});
|
|
||||||
|
if (!skipSync) {
|
||||||
|
queryClient.setQueryData(queryKey, (values: Model[] = []) =>
|
||||||
|
values.map((v) => (modelsEq(v, payload) ? payload : v)),
|
||||||
|
);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user