Skip to content
This repository was archived by the owner on Jun 26, 2026. It is now read-only.

Commit b123f71

Browse files
Merge pull request #4 from cozystack/feat/ui-audit-fixes
fix(console): ui audit fixes — forms, VNC, events, status badges
2 parents 2ff7cae + de2f98b commit b123f71

16 files changed

Lines changed: 296 additions & 78 deletions

‎apps/console/src/App.tsx‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,10 @@ import type { AppConfig } from "./lib/config.ts"
1414

1515
interface ShellProps {
1616
config: AppConfig
17+
username?: string
1718
}
1819

19-
function Shell({ config }: ShellProps) {
20+
function Shell({ config, username }: ShellProps) {
2021
const { pathname } = useLocation()
2122
const inMarketplace = pathname.startsWith("/marketplace")
2223
const marketplaceSections = useMarketplaceSidebarSections()
@@ -32,6 +33,7 @@ function Shell({ config }: ShellProps) {
3233
version={import.meta.env.VITE_APP_VERSION}
3334
logoSvg={config.logoSvg}
3435
logoText={config.logoText}
36+
username={username}
3537
>
3638
<CommandPalette />
3739
<Routes>
@@ -45,13 +47,14 @@ function Shell({ config }: ShellProps) {
4547

4648
export interface AppProps {
4749
config?: AppConfig
50+
username?: string
4851
}
4952

50-
export default function App({ config = {} }: AppProps) {
53+
export default function App({ config = {}, username }: AppProps) {
5154
return (
5255
<TenantProvider>
5356
<CommandPaletteProvider>
54-
<Shell config={config} />
57+
<Shell config={config} username={username} />
5558
</CommandPaletteProvider>
5659
</TenantProvider>
5760
)

‎apps/console/src/components/ResourceQuotasField.tsx‎

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useState } from "react"
1+
import { useState, useEffect, useRef } from "react"
22
import type { FieldProps } from "@rjsf/utils"
33
import { X, Plus } from "lucide-react"
44

@@ -62,12 +62,31 @@ function KnownRowEditor({ row, value, onChange, readonly }: KnownRowEditorProps)
6262
return parseSize(value, row.units).unit
6363
})
6464

65+
// expectedValueRef tracks values we emitted ourselves so we can distinguish
66+
// an external reset (e.g. form data cleared from outside) from our own onChange.
67+
const expectedValueRef = useRef(value)
68+
69+
useEffect(() => {
70+
if (value === expectedValueRef.current) return
71+
expectedValueRef.current = value
72+
const hasValue = value !== undefined && value !== ""
73+
setChecked(hasValue)
74+
if (hasValue && value) {
75+
setLocalNum(row.units ? parseSize(value, row.units).num : value)
76+
if (row.units) setLocalUnit(parseSize(value, row.units).unit)
77+
} else {
78+
setLocalNum("")
79+
}
80+
// eslint-disable-next-line react-hooks/exhaustive-deps
81+
}, [value])
82+
6583
const enabled = checked
6684

6785
const handleToggle = () => {
6886
if (enabled) {
6987
setChecked(false)
7088
setLocalNum("")
89+
expectedValueRef.current = undefined
7190
onChange(undefined)
7291
} else {
7392
setChecked(true)
@@ -80,20 +99,21 @@ function KnownRowEditor({ row, value, onChange, readonly }: KnownRowEditorProps)
8099
const trimmed = num.trim()
81100
if (!trimmed) {
82101
// Clear from parent data but keep checkbox checked locally
102+
expectedValueRef.current = undefined
83103
onChange(undefined)
84104
return
85105
}
86-
if (row.units) {
87-
onChange(formatSize(trimmed, localUnit))
88-
} else {
89-
onChange(trimmed)
90-
}
106+
const next = row.units ? formatSize(trimmed, localUnit) : trimmed
107+
expectedValueRef.current = next
108+
onChange(next)
91109
}
92110

93111
const handleUnitChange = (unit: string) => {
94112
setLocalUnit(unit)
95113
if (localNum.trim()) {
96-
onChange(formatSize(localNum, unit))
114+
const next = formatSize(localNum, unit)
115+
expectedValueRef.current = next
116+
onChange(next)
97117
}
98118
}
99119

‎apps/console/src/components/SchemaForm.tsx‎

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
import { useMemo } from "react"
1+
import { useMemo, useEffect, useRef } from "react"
22
import Form from "@rjsf/core"
33
import validator from "@rjsf/validator-ajv8"
4+
import { getDefaultFormState } from "@rjsf/utils"
45
import type { RJSFSchema, UiSchema, TemplatesType } from "@rjsf/utils"
56
import { keysOrderToUiSchema, sanitizeSchema } from "../lib/keys-order.ts"
67
import { customTemplates, customWidgets } from "./rjsf-templates.tsx"
@@ -165,6 +166,24 @@ export function SchemaForm({
165166
}
166167
}, [openAPISchema])
167168

169+
const onChangeRef = useRef(onChange)
170+
onChangeRef.current = onChange
171+
const initialFormDataRef = useRef(formData)
172+
const emittedSchemaRef = useRef<RJSFSchema | null>(null)
173+
174+
// Emit defaults to parent once per schema so spec is never empty on first submit.
175+
// Uses initialFormDataRef so edit-mode existing values are preserved as base.
176+
// emittedSchemaRef prevents re-running on unrelated re-renders and avoids
177+
// overwriting user data if the schema object changes identity unexpectedly.
178+
useEffect(() => {
179+
if (!schema || Object.keys(schema).length === 0) return
180+
if (emittedSchemaRef.current === schema) return
181+
emittedSchemaRef.current = schema
182+
const defaults = getDefaultFormState(validator, schema, initialFormDataRef.current ?? {}, schema)
183+
onChangeRef.current(defaults)
184+
// eslint-disable-next-line react-hooks/exhaustive-deps
185+
}, [schema])
186+
168187
const uiSchema = useMemo<UiSchema>(() => {
169188
const baseUiSchema: UiSchema = {
170189
"ui:submitButtonOptions": { norender: true },

‎apps/console/src/components/SourceField.tsx‎

Lines changed: 117 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,54 @@
11
import { useState } from "react"
22
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+
}
344

445
export function SourceField(props: FieldProps) {
546
const { schema, formData, onChange, name, required, idSchema } = props
647
const properties = (schema as any).properties || {}
748
const options = Object.keys(properties)
49+
const { tenantNamespace } = useTenantContext()
50+
const { disks, isLoading: disksLoading } = useVMDiskOptions(tenantNamespace)
51+
const { images, isLoading: imagesLoading } = useImageOptions()
852

953
// Determine which option is currently selected
1054
const currentOption = formData
@@ -59,31 +103,79 @@ export function SourceField(props: FieldProps) {
59103
const subProps = prop.properties || {}
60104
return (
61105
<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+
})}
87179
</div>
88180
)
89181
}

‎apps/console/src/components/VMDiskWidget.tsx‎

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useEffect } from "react"
1+
import { useEffect, useRef } from "react"
22
import type { WidgetProps } from "@rjsf/utils"
33
import { useK8sList } from "@cozystack/k8s-client"
44
import { APPS_GROUP, APPS_VERSION } from "@cozystack/types"
@@ -30,12 +30,20 @@ export function VMDiskWidget(props: WidgetProps) {
3030

3131
const disks = diskList?.items || []
3232

33-
// Auto-select first disk if required and no value set
33+
const onChangeRef = useRef(onChange)
34+
onChangeRef.current = onChange
35+
36+
// Auto-select the first disk once when disks become available and no value is set.
37+
// A ref prevents re-triggering on every k8s watch event (new array reference)
38+
// while still firing correctly when the list loads after mount.
39+
const autoSelectedRef = useRef(false)
3440
useEffect(() => {
41+
if (autoSelectedRef.current) return
3542
if (required && !value && disks.length > 0 && !isLoading) {
36-
onChange(disks[0].metadata.name)
43+
autoSelectedRef.current = true
44+
onChangeRef.current(disks[0].metadata.name)
3745
}
38-
}, [required, value, disks, isLoading, onChange])
46+
}, [required, value, disks, isLoading])
3947

4048
return (
4149
<select

‎apps/console/src/components/rjsf-templates.tsx‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ function IconButton<
1919
S extends StrictRJSFSchema = RJSFSchema,
2020
F extends FormContextType = any,
2121
>(props: IconButtonProps<T, S, F>) {
22-
const { icon, className, ...btnProps } = props
22+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
23+
const { icon, className, uiSchema, registry, iconType, ...btnProps } = props
2324
return (
2425
<button
2526
type="button"
@@ -70,7 +71,7 @@ export const customTemplates = {
7071
<IconButton
7172
{...props}
7273
icon="+ add"
73-
className="mt-0.5 flex items-center gap-1 px-2 py-1 text-xs font-medium text-slate-500 hover:text-blue-600 rounded-md border border-dashed border-slate-300 hover:border-blue-400 hover:bg-blue-50/60 bg-white transition-all duration-150"
74+
className="mt-0.5 flex items-center gap-1 px-2 py-1 text-xs font-medium text-slate-500 hover:text-blue-600 rounded-md border border-dashed border-slate-300 hover:border-blue-400 hover:bg-blue-50/60 bg-white transition-all duration-150 cursor-pointer"
7475
/>
7576
),
7677
RemoveButton: (props: IconButtonProps) => (

‎apps/console/src/lib/config.ts‎

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,15 @@ export interface AppConfig {
99
const CONFIG_NAMESPACE = "cozy-dashboard"
1010
const CONFIG_MAP_NAME = "cozy-dashboard-console-config"
1111

12+
function fetchWithTimeout(url: string, ms = 5000): Promise<Response> {
13+
const ctrl = new AbortController()
14+
const timer = setTimeout(() => ctrl.abort(), ms)
15+
return fetch(url, { signal: ctrl.signal }).finally(() => clearTimeout(timer))
16+
}
17+
1218
export async function loadConfig(): Promise<AppConfig> {
1319
try {
14-
const resp = await fetch(
20+
const resp = await fetchWithTimeout(
1521
`/api/v1/namespaces/${CONFIG_NAMESPACE}/configmaps/${CONFIG_MAP_NAME}`,
1622
)
1723
if (!resp.ok) return {}
@@ -23,3 +29,14 @@ export async function loadConfig(): Promise<AppConfig> {
2329
return {}
2430
}
2531
}
32+
33+
export async function loadUsername(): Promise<string | undefined> {
34+
try {
35+
const resp = await fetchWithTimeout("/oauth2/userinfo")
36+
if (!resp.ok) return undefined
37+
const info = await resp.json() as { user?: string; email?: string }
38+
return info.email ?? info.user ?? undefined
39+
} catch {
40+
return undefined
41+
}
42+
}

0 commit comments

Comments
 (0)