Skip to content

Commit

Permalink
feat(web): add hook to get config model
Browse files Browse the repository at this point in the history
  • Loading branch information
joseivanlopez committed Nov 19, 2024
1 parent 636332c commit e39588d
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 3 deletions.
6 changes: 5 additions & 1 deletion web/src/api/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import { get, post, put } from "~/api/http";
import { Job } from "~/types/job";
import { calculate, fetchSettings } from "~/api/storage/proposal";
import { config } from "~/api/storage/types";
import { config, configModel } from "~/api/storage/types";

/**
* Starts the storage probing process.
Expand All @@ -38,6 +38,9 @@ const fetchConfig = (): Promise<config.Config | undefined> =>
const fetchSolvedConfig = (): Promise<config.Config | undefined> =>
get("/api/storage/solved_config").then((config) => config.storage);

const fetchConfigModel = (): Promise<configModel.Config | undefined> =>
get("/api/storage/config_model");

const setConfig = (config: config.Config) => put("/api/storage/config", config);

/**
Expand Down Expand Up @@ -68,6 +71,7 @@ export {
probe,
fetchConfig,
fetchSolvedConfig,
fetchConfigModel,
setConfig,
fetchStorageJobs,
findStorageJob,
Expand Down
3 changes: 2 additions & 1 deletion web/src/api/storage/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
*/

import * as config from "./types/config";
import * as configModel from "./types/config-model";

export * from "./types/openapi";
export { config };
export { config, configModel };
65 changes: 65 additions & 0 deletions web/src/api/storage/types/config-model.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/* eslint-disable */
/**
* This file was automatically generated by json-schema-to-typescript.
* DO NOT MODIFY IT BY HAND. Instead, modify the source JSONSchema file,
* and run json-schema-to-typescript to regenerate this file.
*/

export type FilesystemType =
| "bcachefs"
| "btrfs"
| "exfat"
| "ext2"
| "ext3"
| "ext4"
| "f2fs"
| "jfs"
| "nfs"
| "nilfs2"
| "ntfs"
| "reiserfs"
| "swap"
| "tmpfs"
| "vfat"
| "xfs";
export type SpacePolicy = "delete" | "resize" | "keep" | "custom";
export type PtableType = "gpt" | "msdos" | "dasd";
export type PartitionId = "linux" | "swap" | "lvm" | "raid" | "esp" | "prep" | "bios_boot";

/**
* Config model
*/
export interface Config {
drives?: Drive[];
}
export interface Drive {
name: string;
alias?: string;
mountPath?: string;
filesystem?: Filesystem;
spacePolicy?: SpacePolicy;
ptableType?: PtableType;
partitions?: Partition[];
}
export interface Filesystem {
default: boolean;
type?: FilesystemType;
snapshots?: boolean;
}
export interface Partition {
name?: string;
alias?: string;
id?: PartitionId;
mountPath?: string;
filesystem?: Filesystem;
size?: Size;
delete?: boolean;
deleteIfNeeded?: boolean;
resize?: boolean;
resizeIfNeeded?: boolean;
}
export interface Size {
default: boolean;
min: number;
max?: number;
}
20 changes: 19 additions & 1 deletion web/src/queries/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import {
useSuspenseQuery,
} from "@tanstack/react-query";
import React from "react";
import { fetchConfig, fetchSolvedConfig, setConfig } from "~/api/storage";
import { fetchConfig, fetchSolvedConfig, fetchConfigModel, setConfig } from "~/api/storage";
import { fetchDevices, fetchDevicesDirty } from "~/api/storage/devices";
import {
calculate,
Expand All @@ -40,6 +40,7 @@ import {
import { useInstallerClient } from "~/context/installer";
import {
config,
configModel,
ProductParams,
Volume as APIVolume,
ProposalSettingsPatch,
Expand Down Expand Up @@ -67,6 +68,12 @@ const solvedConfigQuery = {
staleTime: Infinity,
};

const configModelQuery = {
queryKey: ["storage", "configModel"],
queryFn: fetchConfigModel,
staleTime: Infinity,
};

const devicesQuery = (scope: "result" | "system") => ({
queryKey: ["storage", "devices", scope],
queryFn: () => fetchDevices(scope),
Expand Down Expand Up @@ -137,6 +144,16 @@ const useSolvedConfig = (options?: QueryHookOptions): config.Config => {
return data;
};

/**
* Hook that returns the config model.
*/
const useConfigModel = (options?: QueryHookOptions): configModel.Config => {
const query = configModelQuery;
const func = options?.suspense ? useSuspenseQuery : useQuery;
const { data } = func(query);
return data;
};

/**
* Hook for setting a new config.
*/
Expand Down Expand Up @@ -347,6 +364,7 @@ export {
useConfig,
useSolvedConfig,
useConfigMutation,
useConfigModel,
useConfigDevices,
useDevices,
useAvailableDevices,
Expand Down

0 comments on commit e39588d

Please sign in to comment.