import { invoke } from "@tauri-apps/api/core"; import type { Workspace, WorkspaceEntryBase, RequestBody, WorkspaceEntry, WorkspaceEnvironment, EnvVariable, } from "./types"; export type WorkspaceState = { /** * Currently selected workspace. */ workspace: Workspace | null; /** * Currently selected workspace entry. */ entry: WorkspaceEntry | null; /** * Workspace root entries. */ roots: number[]; /** * Workspace entry parent => children mappings. */ children: Record; /** * All workspace entries. */ indexes: Record; /** * Currently selected workspace environments. */ environments: WorkspaceEnvironment[]; /** * Currently selected environment. */ environment: WorkspaceEnvironment | null; }; export const state: WorkspaceState = $state({ workspace: null, entry: null, roots: [], children: {}, indexes: {}, environments: [], environment: null, }); const index = (entry: WorkspaceEntry) => { console.log("indexing", entry); state.indexes[entry.id] = entry; if (entry.parent_id !== null) { if (state.children[entry.parent_id]) { state.children[entry.parent_id].push(entry.id); } else { state.children[entry.parent_id] = [entry.id]; } } else { state.roots.push(entry.id); } }; function reset() { state.children = {}; state.indexes = {}; state.roots = []; state.entry = null; state.environment = null; state.environments = []; } export function selectEnvironment(id: number | null) { if (id === null) { state.environment = null; return; } console.debug("selecting environment:", state.environments[id]); state.environment = state.environments.find((e) => e.id === id) ?? null; } export function selectWorkspace(ws: Workspace) { console.debug("selecting workspace:", ws.name); state.workspace = ws; } export function selectEntry(id: number) { console.log("selecting entry:", id); state.entry = state.indexes[id]; if (state.entry.parent_id !== null) { let parent = state.indexes[state.entry.parent_id]; while (parent) { parent.open = true; if (parent.parent_id === null) { break; } parent = state.indexes[parent.parent_id]; } } } // COMMANDS export async function createWorkspace(name: string): Promise { return invoke("create_workspace", { name }); } export async function listWorkspaces(): Promise { return invoke("list_workspaces"); } export async function loadWorkspace(ws: Workspace) { reset(); state.workspace = ws; const entries = await invoke( "get_workspace_entries", { id: state.workspace.id, }, ); for (const entry of entries) { if (entry.type === "Request") { index({ ...entry.data.entry, method: entry.data.method, url: entry.data.url, headers: entry.data.headers, body: entry.data.body, }); } else { index(entry.data); } } await loadEnvironments(state.workspace.id); } export function createRequest(parentId?: number) { if (state.workspace == null) { console.warn("create workspace request called with no active workspace"); return; } const data = { Request: { name: "", workspace_id: state.workspace.id, parent_id: parentId, method: "GET", url: "", }, }; invoke("create_workspace_entry", { data, }).then((entry) => { index({ ...entry, method: data.Request.method, url: data.Request.url, body: null, headers: {}, }); console.log("request created:", entry); }); } export function createCollection(parentId?: number) { if (state.workspace == null) { console.warn("create workspace request called with no active workspace"); return; } const data = { Collection: { name: "", workspace_id: state.workspace.id, parent_id: parentId, }, }; invoke("create_workspace_entry", { data, }).then((entry) => { index(entry); console.log("collection created:", entry); }); } export async function loadEnvironments(workspaceId: number) { state.environments = await invoke( "list_environments", { workspaceId }, ); } export async function createEnvironment(workspaceId: number, name: string) { console.debug("creating environment in", workspaceId); const env = await invoke("create_env", { workspaceId, name, }); state.environment = env; state.environments.push(state.environment); } export async function updateEnvironment() { if (!state.environment) { console.warn("attempted to persist null env"); return; } console.debug("updating environment", state.environment); await invoke("update_env", { id: state.environment.id, name: state.environment.name, }); } export async function insertEnvVariable( workspaceId: number, envId: number, name: string = "", value: string = "", secret: boolean = false, ) { const v = await invoke("insert_env_var", { workspaceId, envId, name, value, secret, }); state.environment?.variables.push(v); } export async function updateEnvVariable(v: EnvVariable) { if (v.name.length === 0 && v.value.length === 0) { console.debug("deleting var:", v); return deleteEnvVariable(v.id); } console.debug("updating var:", v); return invoke("update_env_var", { id: v.id, name: v.name, value: v.value, secret: v.secret, }); } export async function deleteEnvVariable(id: number) { await invoke("delete_env_var", { id }); state.environment!!.variables = state.environment!!.variables.filter( (v) => v.id !== id, ); } type WorkspaceEntryResponse = | { type: "Collection"; data: WorkspaceEntryBase; } | { type: "Request"; data: WorkspaceRequestResponse; }; type WorkspaceRequestResponse = { entry: WorkspaceEntryBase; method: string; url: string; body: RequestBody | null; headers: { [key: string]: string }; };