| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290 |
- 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<number, number[]>;
- /**
- * All workspace entries.
- */
- indexes: Record<number, WorkspaceEntry>;
- /**
- * 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<Workspace> {
- return invoke<Workspace>("create_workspace", { name });
- }
- export async function listWorkspaces(): Promise<Workspace[]> {
- return invoke<Workspace[]>("list_workspaces");
- }
- export async function loadWorkspace(ws: Workspace) {
- reset();
- state.workspace = ws;
- const entries = await invoke<WorkspaceEntryResponse[]>(
- "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<WorkspaceEntryBase>("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<WorkspaceEntryBase>("create_workspace_entry", {
- data,
- }).then((entry) => {
- index(entry);
- console.log("collection created:", entry);
- });
- }
- export async function loadEnvironments(workspaceId: number) {
- state.environments = await invoke<WorkspaceEnvironment[]>(
- "list_environments",
- { workspaceId },
- );
- }
- export async function createEnvironment(workspaceId: number, name: string) {
- console.debug("creating environment in", workspaceId);
- const env = await invoke<WorkspaceEnvironment>("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<EnvVariable>("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 };
- };
|