Environments data model

This commit is contained in:
Gregory Schier
2023-10-22 18:28:56 -07:00
parent afe6a3bf57
commit 8328d20150
26 changed files with 132 additions and 1179 deletions

View File

@@ -83,12 +83,12 @@ export const WorkspaceActionsDropdown = memo(function WorkspaceDropdown({ classN
workspaces.length <= 1
? []
: [
...workspaceItems,
{
type: 'separator',
label: activeWorkspace?.name,
},
];
...workspaceItems,
{
type: 'separator',
label: activeWorkspace?.name,
},
];
return [
...activeWorkspaceItems,

View File

@@ -0,0 +1,22 @@
import { useQuery } from '@tanstack/react-query';
import { invoke } from '@tauri-apps/api';
import type { Environment } from '../lib/models';
import { useActiveWorkspaceId } from './useActiveWorkspaceId';
export function environmentsQueryKey({ workspaceId }: { workspaceId: string }) {
return ['environments', { workspaceId }];
}
export function useEnvironments() {
const workspaceId = useActiveWorkspaceId();
return (
useQuery({
enabled: workspaceId != null,
queryKey: environmentsQueryKey({ workspaceId: workspaceId ?? 'n/a' }),
queryFn: async () => {
if (workspaceId == null) return [];
return (await invoke('environments', { workspaceId })) as Environment[];
},
}).data ?? []
);
}

View File

@@ -1,3 +1,15 @@
export const BODY_TYPE_NONE = null;
export const BODY_TYPE_GRAPHQL = 'graphql';
export const BODY_TYPE_JSON = 'application/json';
export const BODY_TYPE_XML = 'text/xml';
export const AUTH_TYPE_NONE = null;
export const AUTH_TYPE_BASIC = 'basic';
export const AUTH_TYPE_BEARER = 'bearer';
export type Model = Workspace | HttpRequest | HttpResponse | KeyValue | Environment;
export interface BaseModel {
readonly id: string;
readonly createdAt: string;
@@ -16,25 +28,11 @@ export interface HttpHeader {
enabled?: boolean;
}
export const BODY_TYPE_NONE = null;
export const BODY_TYPE_GRAPHQL = 'graphql';
export const BODY_TYPE_JSON = 'application/json';
export const BODY_TYPE_XML = 'text/xml';
export const AUTH_TYPE_NONE = null;
export const AUTH_TYPE_BASIC = 'basic';
export const AUTH_TYPE_BEARER = 'bearer';
export type Model = Workspace | HttpRequest | HttpResponse | KeyValue;
export function modelsEq(a: Model, b: Model) {
if (a.model === 'key_value' && b.model === 'key_value') {
return a.key === b.key && a.namespace === b.namespace;
}
if ('id' in a && 'id' in b) {
return a.id === b.id;
}
return false;
export interface Environment extends BaseModel {
readonly workspaceId: string;
readonly model: 'environment';
name: string;
data: Record<string, string | number | boolean | null | undefined>;
}
export interface HttpRequest extends BaseModel {
@@ -78,3 +76,13 @@ export interface HttpResponse extends BaseModel {
export function isResponseLoading(response: HttpResponse): boolean {
return !(response.body || response.status || response.error);
}
export function modelsEq(a: Model, b: Model) {
if (a.model === 'key_value' && b.model === 'key_value') {
return a.key === b.key && a.namespace === b.namespace;
}
if ('id' in a && 'id' in b) {
return a.id === b.id;
}
return false;
}