mirror of
https://github.com/mountain-loop/yaak.git
synced 2026-03-19 07:53:54 +01:00
Got key values working
This commit is contained in:
@@ -2,15 +2,20 @@ import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
|
||||
import { listen } from '@tauri-apps/api/event';
|
||||
import { MotionConfig } from 'framer-motion';
|
||||
import { HelmetProvider } from 'react-helmet-async';
|
||||
import { keyValueQueryKey } from '../hooks/useKeyValues';
|
||||
import { requestsQueryKey } from '../hooks/useRequests';
|
||||
import { responsesQueryKey } from '../hooks/useResponses';
|
||||
import { DEFAULT_FONT_SIZE } from '../lib/constants';
|
||||
import type { HttpRequest, HttpResponse } from '../lib/models';
|
||||
import type { HttpRequest, HttpResponse, KeyValue } from '../lib/models';
|
||||
import { convertDates } from '../lib/models';
|
||||
import { AppRouter } from './AppRouter';
|
||||
|
||||
const queryClient = new QueryClient();
|
||||
|
||||
await listen('updated_key_value', ({ payload: keyValue }: { payload: KeyValue }) => {
|
||||
queryClient.setQueryData(keyValueQueryKey(keyValue.namespace, keyValue.key), keyValue);
|
||||
});
|
||||
|
||||
await listen('updated_request', ({ payload: request }: { payload: HttpRequest }) => {
|
||||
queryClient.setQueryData(
|
||||
requestsQueryKey(request.workspaceId),
|
||||
|
||||
@@ -19,7 +19,6 @@ interface Props {
|
||||
export function Sidebar({ className }: Props) {
|
||||
const requests = useRequests();
|
||||
const activeRequest = useActiveRequest();
|
||||
const deleteRequest = useDeleteRequest(activeRequest);
|
||||
const createRequest = useCreateRequest({ navigateAfter: true });
|
||||
const { appearance, toggleAppearance } = useTheme();
|
||||
return (
|
||||
|
||||
@@ -8,7 +8,7 @@ export function useCreateRequest({ navigateAfter }: { navigateAfter: boolean })
|
||||
const workspace = useActiveWorkspace();
|
||||
const navigate = useNavigate();
|
||||
return useMutation<string, unknown, Pick<HttpRequest, 'name'>>({
|
||||
mutationFn: async (patch) => {
|
||||
mutationFn: (patch) => {
|
||||
if (workspace === null) {
|
||||
throw new Error("Cannot create request when there's no active workspace");
|
||||
}
|
||||
|
||||
26
src-web/hooks/useKeyValues.ts
Normal file
26
src-web/hooks/useKeyValues.ts
Normal file
@@ -0,0 +1,26 @@
|
||||
import { useMutation, useQuery } from '@tanstack/react-query';
|
||||
import { invoke } from '@tauri-apps/api';
|
||||
import type { KeyValue } from '../lib/models';
|
||||
|
||||
export function keyValueQueryKey(namespace: string, key: string) {
|
||||
return ['key_value', { namespace, key }];
|
||||
}
|
||||
|
||||
export function useKeyValues(namespace: string, key: string) {
|
||||
const query = useQuery<KeyValue | null>({
|
||||
initialData: null,
|
||||
queryKey: keyValueQueryKey(namespace, key),
|
||||
queryFn: async () => invoke('get_key_value', { namespace, key }),
|
||||
});
|
||||
|
||||
const mutate = useMutation<KeyValue, unknown, KeyValue['value']>({
|
||||
mutationFn: (value) => {
|
||||
return invoke('set_key_value', { namespace, key, value });
|
||||
},
|
||||
});
|
||||
|
||||
return {
|
||||
value: query.data?.value ?? null,
|
||||
set: (value: KeyValue['value']) => mutate.mutate(value),
|
||||
};
|
||||
}
|
||||
@@ -5,7 +5,7 @@ import { convertDates } from '../lib/models';
|
||||
import { useActiveWorkspace } from './useActiveWorkspace';
|
||||
|
||||
export function requestsQueryKey(workspaceId: string) {
|
||||
return ['requests', { workspaceId }];
|
||||
return ['http_requests', { workspaceId }];
|
||||
}
|
||||
|
||||
export function useRequests() {
|
||||
|
||||
@@ -5,7 +5,7 @@ import { convertDates } from '../lib/models';
|
||||
import { useActiveRequest } from './useActiveRequest';
|
||||
|
||||
export function responsesQueryKey(requestId: string) {
|
||||
return ['responses', { requestId }];
|
||||
return ['http_responses', { requestId }];
|
||||
}
|
||||
|
||||
export function useResponses() {
|
||||
|
||||
@@ -1,12 +1,13 @@
|
||||
export interface BaseModel {
|
||||
id: string;
|
||||
workspaceId: string;
|
||||
createdAt: Date;
|
||||
updatedAt: Date;
|
||||
deletedAt: Date | null;
|
||||
readonly id: string;
|
||||
readonly workspaceId: string;
|
||||
readonly createdAt: Date;
|
||||
readonly updatedAt: Date;
|
||||
readonly deletedAt: Date | null;
|
||||
}
|
||||
|
||||
export interface Workspace extends BaseModel {
|
||||
readonly model: 'workspace';
|
||||
name: string;
|
||||
description: string;
|
||||
}
|
||||
@@ -17,6 +18,7 @@ export interface HttpHeader {
|
||||
}
|
||||
|
||||
export interface HttpRequest extends BaseModel {
|
||||
readonly model: 'http_request';
|
||||
name: string;
|
||||
url: string;
|
||||
body: string | null;
|
||||
@@ -25,19 +27,28 @@ export interface HttpRequest extends BaseModel {
|
||||
headers: HttpHeader[];
|
||||
}
|
||||
|
||||
export interface HttpResponse extends BaseModel {
|
||||
id: string;
|
||||
requestId: string;
|
||||
body: string;
|
||||
error: string;
|
||||
status: number;
|
||||
elapsed: number;
|
||||
statusReason: string;
|
||||
url: string;
|
||||
headers: HttpHeader[];
|
||||
export interface KeyValue extends Omit<BaseModel, 'id'> {
|
||||
readonly model: 'key_value';
|
||||
readonly key: string;
|
||||
readonly namespace: string;
|
||||
value: string;
|
||||
}
|
||||
|
||||
export function convertDates<T extends BaseModel>(m: T): T {
|
||||
export interface HttpResponse extends BaseModel {
|
||||
readonly model: 'http_response';
|
||||
readonly requestId: string;
|
||||
readonly body: string;
|
||||
readonly error: string;
|
||||
readonly status: number;
|
||||
readonly elapsed: number;
|
||||
readonly statusReason: string;
|
||||
readonly url: string;
|
||||
readonly headers: HttpHeader[];
|
||||
}
|
||||
|
||||
export function convertDates<T extends Pick<BaseModel, 'createdAt' | 'updatedAt' | 'deletedAt'>>(
|
||||
m: T,
|
||||
): T {
|
||||
return {
|
||||
...m,
|
||||
createdAt: convertDate(m.createdAt),
|
||||
|
||||
Reference in New Issue
Block a user