linted modules

converted input fields to use a modal to save space in settings
updated descriptions
This commit is contained in:
Adrian 2025-01-25 14:52:23 -06:00
parent 03fd7508de
commit 0cd406f35e
4 changed files with 239 additions and 90 deletions

View File

@ -5,15 +5,15 @@ import (
"context" "context"
"encoding/json" "encoding/json"
"fmt" "fmt"
"github.com/coder/websocket/wsjson"
"net/http" "net/http"
"net/url" "net/url"
"github.com/coder/websocket/wsjson"
"time" "time"
"github.com/coreos/go-oidc/v3/oidc" "github.com/coreos/go-oidc/v3/oidc"
"github.com/gin-gonic/gin"
"github.com/coder/websocket" "github.com/coder/websocket"
"github.com/gin-gonic/gin"
) )
type CloudRegisterRequest struct { type CloudRegisterRequest struct {

View File

@ -0,0 +1,187 @@
import { GridCard } from "@/components/Card";
import {useCallback, useState} from "react";
import { Button } from "@components/Button";
import LogoBlueIcon from "@/assets/logo-blue.svg";
import LogoWhiteIcon from "@/assets/logo-white.svg";
import Modal from "@components/Modal";
import { InputFieldWithLabel } from "./InputField";
import { useJsonRpc } from "@/hooks/useJsonRpc";
import { useUsbConfigModalStore } from "@/hooks/stores";
export default function UsbConfigDialog({
open,
setOpen,
}: {
open: boolean;
setOpen: (open: boolean) => void;
}) {
return (
<Modal open={open} onClose={() => setOpen(false)}>
<Dialog setOpen={setOpen} />
</Modal>
);
}
export function Dialog({ setOpen }: { setOpen: (open: boolean) => void }) {
const { modalView, setModalView } = useUsbConfigModalStore();
const [error, setError] = useState<string | null>(null);
const [send] = useJsonRpc();
const handleUsbConfigChange = useCallback((usbConfig: object) => {
send("setUsbConfig", { usbConfig }, resp => {
if ("error" in resp) {
setError(`Failed to update USB Config: ${resp.error.data || "Unknown error"}`,);
return;
}
setModalView("updateUsbConfigSuccess");
});
}, [send, setModalView]);
return (
<GridCard cardClassName="relative max-w-lg mx-auto text-left pointer-events-auto dark:bg-slate-800">
<div className="p-10">
{modalView === "updateUsbConfig" && (
<UpdateUsbConfigModal
onSetUsbConfig={handleUsbConfigChange}
onCancel={() => setOpen(false)}
error={error}
/>
)}
{modalView === "updateUsbConfigSuccess" && (
<SuccessModal
headline="USB Configuration Updated Successfully"
description="You've successfully updated the USB Configuration"
onClose={() => setOpen(false)}
/>
)}
</div>
</GridCard>
);
}
function UpdateUsbConfigModal({
onSetUsbConfig,
onCancel,
error,
}: {
onSetUsbConfig: (usb_config: object) => void;
onCancel: () => void;
error: string | null;
}) {
const [usbConfig, setUsbConfig] = useState({
usb_vendor_id: '',
usb_product_id: '',
usb_serial_number: '',
usb_manufacturer: '',
usb_name: '',
})
const handleUsbVendorIdChange = (vendorId: string) => {
setUsbConfig({... usbConfig, usb_vendor_id: vendorId})
};
const handleUsbProductIdChange = (productId: string) => {
setUsbConfig({... usbConfig, usb_product_id: productId})
};
const handleUsbSerialChange = (serialNumber: string) => {
setUsbConfig({... usbConfig, usb_serial_number: serialNumber})
};
const handleUsbManufacturer = (manufacturer: string) => {
setUsbConfig({... usbConfig, usb_manufacturer: manufacturer})
};
const handleUsbName = (name: string) => {
setUsbConfig({... usbConfig, usb_name: name})
};
return (
<div className="flex flex-col items-start justify-start space-y-4 text-left">
<div>
<img src={LogoWhiteIcon} alt="" className="h-[24px] hidden dark:block" />
<img src={LogoBlueIcon} alt="" className="h-[24px] dark:hidden" />
</div>
<div className="space-y-4">
<div>
<h2 className="text-lg font-semibold dark:text-white">USB Emulation Configuration</h2>
<p className="text-sm text-slate-600 dark:text-slate-400">
Set custom USB parameters to control the device USB emulation. The device will rebind once the parameters are updated.
</p>
</div>
<InputFieldWithLabel
required
label="Vendor ID"
placeholder="Enter USB Vendor ID"
value={usbConfig.usb_vendor_id || ""}
onChange={e => handleUsbVendorIdChange(e.target.value)}
/>
<InputFieldWithLabel
required
label="Product ID"
placeholder="Enter USB Product ID"
value={usbConfig.usb_product_id || ""}
onChange={e => handleUsbProductIdChange(e.target.value)}
/>
<InputFieldWithLabel
required
label="Serial Number"
placeholder="Enter USB Serial Number"
value={usbConfig.usb_serial_number || ""}
onChange={e => handleUsbSerialChange(e.target.value)}
/>
<InputFieldWithLabel
required
label="Manufacturer"
placeholder="Enter USB Manufacturer"
value={usbConfig.usb_manufacturer || ""}
onChange={e => handleUsbManufacturer(e.target.value)}
/>
<InputFieldWithLabel
required
label="Name"
placeholder="Enter USB Name"
value={usbConfig.usb_name || ""}
onChange={e => handleUsbName(e.target.value)}
/>
<div className="flex gap-x-2">
<Button
size="SM"
theme="primary"
text="Update USB Config"
onClick={() => onSetUsbConfig(usbConfig)}
/>
<Button size="SM" theme="light" text="Not Now" onClick={onCancel} />
</div>
{error && <p className="text-sm text-red-500">{error}</p>}
</div>
</div>
);
}
function SuccessModal({
headline,
description,
onClose,
}: {
headline: string;
description: string;
onClose: () => void;
}) {
return (
<div className="flex flex-col items-start justify-start w-full max-w-lg space-y-4 text-left">
<div>
<img src={LogoWhiteIcon} alt="" className="h-[24px] hidden dark:block" />
<img src={LogoBlueIcon} alt="" className="h-[24px] dark:hidden" />
</div>
<div className="space-y-4">
<div>
<h2 className="text-lg font-semibold dark:text-white">{headline}</h2>
<p className="text-sm text-slate-600 dark:text-slate-400">{description}</p>
</div>
<Button size="SM" theme="primary" text="Close" onClick={onClose} />
</div>
</div>
);
}

View File

@ -3,14 +3,13 @@ import {
useLocalAuthModalStore, useLocalAuthModalStore,
useSettingsStore, useSettingsStore,
useUiStore, useUiStore,
useUpdateStore, useUpdateStore, useUsbConfigModalStore,
} from "@/hooks/stores"; } from "@/hooks/stores";
import { Checkbox } from "@components/Checkbox"; import { Checkbox } from "@components/Checkbox";
import { Button, LinkButton } from "@components/Button"; import { Button, LinkButton } from "@components/Button";
import { TextAreaWithLabel } from "@components/TextArea"; import { TextAreaWithLabel } from "@components/TextArea";
import { SectionHeader } from "@components/SectionHeader"; import { SectionHeader } from "@components/SectionHeader";
import { GridCard } from "@components/Card"; import { GridCard } from "@components/Card";
import { InputFieldWithLabel } from "@components/InputField";
import { CheckCircleIcon } from "@heroicons/react/20/solid"; import { CheckCircleIcon } from "@heroicons/react/20/solid";
import { cx } from "@/cva.config"; import { cx } from "@/cva.config";
import React, { useCallback, useEffect, useRef, useState } from "react"; import React, { useCallback, useEffect, useRef, useState } from "react";
@ -26,6 +25,7 @@ import LocalAuthPasswordDialog from "@/components/LocalAuthPasswordDialog";
import { LocalDevice } from "@routes/devices.$id"; import { LocalDevice } from "@routes/devices.$id";
import { useRevalidator } from "react-router-dom"; import { useRevalidator } from "react-router-dom";
import { ShieldCheckIcon } from "@heroicons/react/20/solid"; import { ShieldCheckIcon } from "@heroicons/react/20/solid";
import UsbConfigDialog from "@components/UsbConfigDialog";
export function SettingsItem({ export function SettingsItem({
title, title,
@ -110,14 +110,6 @@ export default function SettingsSidebar() {
}); });
}, [send]); }, [send]);
const [usbConfig, setUsbConfig] = useState({
usb_product_id: '',
usb_vendor_id: '',
usb_serial_number: '',
usb_manufacturer: '',
usb_name: '',
})
const handleUsbEmulationToggle = useCallback( const handleUsbEmulationToggle = useCallback(
(enabled: boolean) => { (enabled: boolean) => {
send("setUsbEmulationState", { enabled: enabled }, resp => { send("setUsbEmulationState", { enabled: enabled }, resp => {
@ -215,17 +207,6 @@ export default function SettingsSidebar() {
}); });
}; };
const handleUsbConfigChange = useCallback((usbConfig: object) => {
send("setUsbConfig", { usbConfig }, resp => {
if ("error" in resp) {
notifications.error(
`Failed to update USB Config: ${resp.error.data || "Unknown error"}`,
);
return;
}
});
}, [send]);
const handleSSHKeyChange = (newKey: string) => { const handleSSHKeyChange = (newKey: string) => {
setSSHKey(newKey); setSSHKey(newKey);
}; };
@ -260,26 +241,6 @@ export default function SettingsSidebar() {
}); });
}, [send, sshKey]); }, [send, sshKey]);
const handleUsbProductIdChange = (productId: string) => {
setUsbConfig({... usbConfig, usb_product_id: productId})
};
const handleUsbVendorIdChange = (vendorId: string) => {
setUsbConfig({... usbConfig, usb_vendor_id: vendorId})
};
const handleUsbSerialChange = (serialNumber: string) => {
setUsbConfig({... usbConfig, usb_serial_number: serialNumber})
};
const handleUsbName = (name: string) => {
setUsbConfig({... usbConfig, usb_name: name})
};
const handleUsbManufacturer = (manufacturer: string) => {
setUsbConfig({... usbConfig, usb_manufacturer: manufacturer})
};
const { setIsUpdateDialogOpen, setModalView, otaState } = useUpdateStore(); const { setIsUpdateDialogOpen, setModalView, otaState } = useUpdateStore();
const handleCheckForUpdates = () => { const handleCheckForUpdates = () => {
if (otaState.updating) { if (otaState.updating) {
@ -380,7 +341,9 @@ export default function SettingsSidebar() {
}, []); }, []);
const { setModalView: setLocalAuthModalView } = useLocalAuthModalStore(); const { setModalView: setLocalAuthModalView } = useLocalAuthModalStore();
const { setModalView: setUsbConfigModalView } = useUsbConfigModalStore();
const [isLocalAuthDialogOpen, setIsLocalAuthDialogOpen] = useState(false); const [isLocalAuthDialogOpen, setIsLocalAuthDialogOpen] = useState(false);
const [isUsbConfigDialogOpen, setIsUsbConfigDialogOpen] = useState(false);
useEffect(() => { useEffect(() => {
if (isOnDevice) getDevice(); if (isOnDevice) getDevice();
@ -394,6 +357,14 @@ export default function SettingsSidebar() {
} }
}, [getDevice, isLocalAuthDialogOpen]); }, [getDevice, isLocalAuthDialogOpen]);
useEffect(() => {
if (!isOnDevice) return;
// Refresh device status when the local usb config dialog is closed
if (!isUsbConfigDialogOpen) {
getDevice();
}
}, [getDevice, isUsbConfigDialogOpen]);
const revalidator = useRevalidator(); const revalidator = useRevalidator();
const [currentTheme, setCurrentTheme] = useState(() => { const [currentTheme, setCurrentTheme] = useState(() => {
@ -877,53 +848,20 @@ export default function SettingsSidebar() {
</div> </div>
)} )}
{settings.developerMode && ( {settings.developerMode && (
<div className="space-y-4"> <SettingsItem
<InputFieldWithLabel title="USB Configuration"
label="USB Vendor Id" description="Set the USB Descriptors for the JetKVM"
value={usbConfig.usb_vendor_id || ""} >
onChange={e => handleUsbVendorIdChange(e.target.value)}
placeholder="Enter USB Vendor Id"
/>
<InputFieldWithLabel
label="USB Product Id"
value={usbConfig.usb_product_id || ""}
onChange={e => handleUsbProductIdChange(e.target.value)}
placeholder="Enter USB Product Id"
/>
<InputFieldWithLabel
label="USB Serial Number"
value={usbConfig.usb_serial_number || ""}
onChange={e => handleUsbSerialChange(e.target.value)}
placeholder="Enter USB Serial Number"
/>
<InputFieldWithLabel
label="USB Name"
value={usbConfig.usb_name || ""}
onChange={e => handleUsbName(e.target.value)}
placeholder="Enter USB Name"
/>
<InputFieldWithLabel
label="USB Manufacturer"
value={usbConfig.usb_manufacturer || ""}
onChange={e => handleUsbManufacturer(e.target.value)}
placeholder="Enter USB Manufacturer"
/>
<div className="flex items-center gap-x-2">
<Button <Button
size="SM" size="SM"
theme="primary" theme="light"
text="Update USB Config" text="Update USB Config"
onClick={() => { onClick={() => {
if (Object.values(usbConfig).every(function(i) { return Boolean(i); })) { setUsbConfigModalView("updateUsbConfig")
handleUsbConfigChange(usbConfig); setIsUsbConfigDialogOpen(true);
notifications.success("Successfully updated USB Config")
} else {
notifications.error("Failed to update USB config");
}
}} }}
/> />
</div> </SettingsItem>
</div>
)} )}
<SettingsItem <SettingsItem
title="Troubleshooting Mode" title="Troubleshooting Mode"
@ -983,6 +921,14 @@ export default function SettingsSidebar() {
setIsLocalAuthDialogOpen(x); setIsLocalAuthDialogOpen(x);
}} }}
/> />
<UsbConfigDialog
open={isUsbConfigDialogOpen}
setOpen={x => {
// Revalidate the current route to refresh the local device status and dependent UI components
revalidator.revalidate();
setIsUsbConfigDialogOpen(x);
}}
/>
</div> </div>
); );
} }

View File

@ -528,3 +528,19 @@ export const useLocalAuthModalStore = create<LocalAuthModalState>(set => ({
setModalView: view => set({ modalView: view }), setModalView: view => set({ modalView: view }),
setErrorMessage: message => set({ errorMessage: message }), setErrorMessage: message => set({ errorMessage: message }),
})); }));
interface UsbConfigModalState {
modalView:
| "updateUsbConfig"
| "updateUsbConfigSuccess";
errorMessage: string | null;
setModalView: (view: UsbConfigModalState["modalView"]) => void;
setErrorMessage: (message: string | null) => void;
}
export const useUsbConfigModalStore = create<UsbConfigModalState>(set => ({
modalView: "updateUsbConfig",
errorMessage: null,
setModalView: view => set({ modalView: view }),
setErrorMessage: message => set({ errorMessage: message }),
}));