|
|
@@ -4,6 +4,8 @@ import type {
|
|
|
WorkspaceEntryBase,
|
|
|
RequestBody,
|
|
|
WorkspaceEntry,
|
|
|
+ WorkspaceEnvironment,
|
|
|
+ EnvVariable,
|
|
|
} from "./types";
|
|
|
|
|
|
export type WorkspaceState = {
|
|
|
@@ -31,6 +33,16 @@ export type WorkspaceState = {
|
|
|
* All workspace entries.
|
|
|
*/
|
|
|
indexes: Record<number, WorkspaceEntry>;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Currently selected workspace environments.
|
|
|
+ */
|
|
|
+ environments: WorkspaceEnvironment[];
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Currently selected environment.
|
|
|
+ */
|
|
|
+ environment: WorkspaceEnvironment | null;
|
|
|
};
|
|
|
|
|
|
export const state: WorkspaceState = $state({
|
|
|
@@ -39,6 +51,8 @@ export const state: WorkspaceState = $state({
|
|
|
roots: [],
|
|
|
children: {},
|
|
|
indexes: {},
|
|
|
+ environments: [],
|
|
|
+ environment: null,
|
|
|
});
|
|
|
|
|
|
const index = (entry: WorkspaceEntry) => {
|
|
|
@@ -56,10 +70,46 @@ const index = (entry: WorkspaceEntry) => {
|
|
|
}
|
|
|
};
|
|
|
|
|
|
+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 });
|
|
|
}
|
|
|
@@ -69,10 +119,7 @@ export async function listWorkspaces(): Promise<Workspace[]> {
|
|
|
}
|
|
|
|
|
|
export async function loadWorkspace(ws: Workspace) {
|
|
|
- state.children = {};
|
|
|
- state.indexes = {};
|
|
|
- state.roots = [];
|
|
|
- state.entry = null;
|
|
|
+ reset();
|
|
|
|
|
|
state.workspace = ws;
|
|
|
|
|
|
@@ -96,9 +143,11 @@ export async function loadWorkspace(ws: Workspace) {
|
|
|
index(entry.data);
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ await loadEnvironments(state.workspace.id);
|
|
|
}
|
|
|
|
|
|
-export function createRequest(parent_id?: number) {
|
|
|
+export function createRequest(parentId?: number) {
|
|
|
if (state.workspace == null) {
|
|
|
console.warn("create workspace request called with no active workspace");
|
|
|
return;
|
|
|
@@ -108,7 +157,7 @@ export function createRequest(parent_id?: number) {
|
|
|
Request: {
|
|
|
name: "",
|
|
|
workspace_id: state.workspace.id,
|
|
|
- parent_id,
|
|
|
+ parent_id: parentId,
|
|
|
method: "GET",
|
|
|
url: "",
|
|
|
},
|
|
|
@@ -128,7 +177,7 @@ export function createRequest(parent_id?: number) {
|
|
|
});
|
|
|
}
|
|
|
|
|
|
-export function createCollection(parent_id?: number) {
|
|
|
+export function createCollection(parentId?: number) {
|
|
|
if (state.workspace == null) {
|
|
|
console.warn("create workspace request called with no active workspace");
|
|
|
return;
|
|
|
@@ -138,7 +187,7 @@ export function createCollection(parent_id?: number) {
|
|
|
Collection: {
|
|
|
name: "",
|
|
|
workspace_id: state.workspace.id,
|
|
|
- parent_id,
|
|
|
+ parent_id: parentId,
|
|
|
},
|
|
|
};
|
|
|
invoke<WorkspaceEntryBase>("create_workspace_entry", {
|
|
|
@@ -149,19 +198,77 @@ export function createCollection(parent_id?: number) {
|
|
|
});
|
|
|
}
|
|
|
|
|
|
-export function selectEntry(id: number) {
|
|
|
- state.entry = state.indexes[id];
|
|
|
- console.log("entry selected:", 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];
|
|
|
- }
|
|
|
+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 =
|