|
1 | 1 | import { useState } from "react" |
2 | 2 | import type { FieldProps } from "@rjsf/utils" |
| 3 | +import { useK8sList } from "@cozystack/k8s-client" |
| 4 | +import { APPS_GROUP, APPS_VERSION } from "@cozystack/types" |
| 5 | +import { useTenantContext } from "../lib/tenant-context.tsx" |
| 6 | + |
| 7 | +const IMAGE_PVC_PREFIX = "vm-default-images-" |
| 8 | + |
| 9 | +interface VMDisk { |
| 10 | + apiVersion: string |
| 11 | + kind: string |
| 12 | + metadata: { name: string; namespace: string } |
| 13 | + spec: { storage: string } |
| 14 | +} |
| 15 | + |
| 16 | +interface PVC { |
| 17 | + apiVersion: string |
| 18 | + kind: string |
| 19 | + metadata: { name: string; namespace: string } |
| 20 | +} |
| 21 | + |
| 22 | +function useVMDiskOptions(tenantNamespace: string | null | undefined) { |
| 23 | + const { data, isLoading } = useK8sList<VMDisk>({ |
| 24 | + apiGroup: APPS_GROUP, |
| 25 | + apiVersion: APPS_VERSION, |
| 26 | + plural: "vmdisks", |
| 27 | + namespace: tenantNamespace ?? undefined, |
| 28 | + }) |
| 29 | + return { disks: data?.items ?? [], isLoading } |
| 30 | +} |
| 31 | + |
| 32 | +function useImageOptions() { |
| 33 | + const { data, isLoading } = useK8sList<PVC>({ |
| 34 | + apiGroup: "", |
| 35 | + apiVersion: "v1", |
| 36 | + plural: "persistentvolumeclaims", |
| 37 | + namespace: "cozy-public", |
| 38 | + }) |
| 39 | + const images = (data?.items ?? []) |
| 40 | + .filter((pvc) => pvc.metadata.name.startsWith(IMAGE_PVC_PREFIX)) |
| 41 | + .map((pvc) => pvc.metadata.name.slice(IMAGE_PVC_PREFIX.length)) |
| 42 | + return { images, isLoading } |
| 43 | +} |
3 | 44 |
|
4 | 45 | export function SourceField(props: FieldProps) { |
5 | 46 | const { schema, formData, onChange, name, required, idSchema } = props |
6 | 47 | const properties = (schema as any).properties || {} |
7 | 48 | const options = Object.keys(properties) |
| 49 | + const { tenantNamespace } = useTenantContext() |
| 50 | + const { disks, isLoading: disksLoading } = useVMDiskOptions(tenantNamespace) |
| 51 | + const { images, isLoading: imagesLoading } = useImageOptions() |
8 | 52 |
|
9 | 53 | // Determine which option is currently selected |
10 | 54 | const currentOption = formData |
@@ -59,31 +103,79 @@ export function SourceField(props: FieldProps) { |
59 | 103 | const subProps = prop.properties || {} |
60 | 104 | return ( |
61 | 105 | <div className="ml-6 mt-2 space-y-2"> |
62 | | - {Object.entries(subProps).map(([key, subProp]: [string, any]) => ( |
63 | | - <div key={key} className="flex flex-col gap-1"> |
64 | | - <label className="text-sm font-medium text-slate-700"> |
65 | | - {subProp.title || key} |
66 | | - {prop.required?.includes(key) && <span className="text-red-500 ml-1">*</span>} |
67 | | - </label> |
68 | | - {subProp.description && ( |
69 | | - <p className="text-xs text-slate-500">{subProp.description}</p> |
70 | | - )} |
71 | | - <input |
72 | | - type="text" |
73 | | - value={(formData?.[option] as Record<string, string>)?.[key] || ""} |
74 | | - onChange={(e) => { |
75 | | - onChange({ |
76 | | - [option]: { |
77 | | - ...(formData?.[option] as Record<string, unknown>), |
78 | | - [key]: e.target.value, |
79 | | - }, |
80 | | - }) |
81 | | - }} |
82 | | - placeholder={subProp.title || key} |
83 | | - className="rounded-lg border border-slate-300 bg-white px-3 py-2 text-sm text-slate-900 outline-none focus:border-blue-400 focus:ring-1 focus:ring-blue-400" |
84 | | - /> |
85 | | - </div> |
86 | | - ))} |
| 106 | + {Object.entries(subProps).map(([key, subProp]: [string, any]) => { |
| 107 | + const currentValue = (formData?.[option] as Record<string, string>)?.[key] || "" |
| 108 | + const handleChange = (val: string) => { |
| 109 | + onChange({ |
| 110 | + [option]: { |
| 111 | + ...(formData?.[option] as Record<string, unknown>), |
| 112 | + [key]: val, |
| 113 | + }, |
| 114 | + }) |
| 115 | + } |
| 116 | + |
| 117 | + const isDiskName = option === "disk" && key === "name" |
| 118 | + const isImageName = option === "image" && key === "name" |
| 119 | + |
| 120 | + return ( |
| 121 | + <div key={key} className="flex flex-col gap-1"> |
| 122 | + <label className="text-sm font-medium text-slate-700"> |
| 123 | + {subProp.title || key} |
| 124 | + {prop.required?.includes(key) && <span className="text-red-500 ml-1">*</span>} |
| 125 | + </label> |
| 126 | + {subProp.description && ( |
| 127 | + <p className="text-xs text-slate-500">{subProp.description}</p> |
| 128 | + )} |
| 129 | + {isDiskName ? ( |
| 130 | + <select |
| 131 | + value={currentValue} |
| 132 | + onChange={(e) => handleChange(e.target.value)} |
| 133 | + disabled={disksLoading} |
| 134 | + className="w-full rounded-lg border border-slate-300 bg-white pl-3 pr-8 py-2 text-sm text-slate-900 outline-none focus:border-blue-400 focus:ring-1 focus:ring-blue-400 disabled:opacity-50" |
| 135 | + > |
| 136 | + <option value="">-- Select disk --</option> |
| 137 | + {disksLoading ? ( |
| 138 | + <option value="" disabled>Loading...</option> |
| 139 | + ) : disks.length === 0 ? ( |
| 140 | + <option value="" disabled>No disks available</option> |
| 141 | + ) : ( |
| 142 | + disks.map((disk) => ( |
| 143 | + <option key={disk.metadata.name} value={disk.metadata.name}> |
| 144 | + {disk.metadata.name} ({disk.spec.storage}) |
| 145 | + </option> |
| 146 | + )) |
| 147 | + )} |
| 148 | + </select> |
| 149 | + ) : isImageName ? ( |
| 150 | + <select |
| 151 | + value={currentValue} |
| 152 | + onChange={(e) => handleChange(e.target.value)} |
| 153 | + disabled={imagesLoading} |
| 154 | + className="w-full rounded-lg border border-slate-300 bg-white pl-3 pr-8 py-2 text-sm text-slate-900 outline-none focus:border-blue-400 focus:ring-1 focus:ring-blue-400 disabled:opacity-50" |
| 155 | + > |
| 156 | + <option value="">-- Select image --</option> |
| 157 | + {imagesLoading ? ( |
| 158 | + <option value="" disabled>Loading...</option> |
| 159 | + ) : images.length === 0 ? ( |
| 160 | + <option value="" disabled>No images available</option> |
| 161 | + ) : ( |
| 162 | + images.map((img) => ( |
| 163 | + <option key={img} value={img}>{img}</option> |
| 164 | + )) |
| 165 | + )} |
| 166 | + </select> |
| 167 | + ) : ( |
| 168 | + <input |
| 169 | + type="text" |
| 170 | + value={currentValue} |
| 171 | + onChange={(e) => handleChange(e.target.value)} |
| 172 | + placeholder={subProp.title || key} |
| 173 | + className="rounded-lg border border-slate-300 bg-white px-3 py-2 text-sm text-slate-900 outline-none focus:border-blue-400 focus:ring-1 focus:ring-blue-400" |
| 174 | + /> |
| 175 | + )} |
| 176 | + </div> |
| 177 | + ) |
| 178 | + })} |
87 | 179 | </div> |
88 | 180 | ) |
89 | 181 | } |
|
0 commit comments