import type { AnyModel } from "@yaakapp-internal/models"; import { patchModel } from "@yaakapp-internal/models"; import classNames from "classnames"; import type { ReactNode } from "react"; import { CopyIconButton } from "../CopyIconButton"; import { Checkbox } from "./Checkbox"; import { IconButton, type IconButtonProps } from "./IconButton"; import { PlainInput } from "./PlainInput"; import type { RadioDropdownItem } from "./RadioDropdown"; import { Select } from "./Select"; import { SelectFile } from "../SelectFile"; type ModelKeyOfValue = { [K in keyof T]-?: T[K] extends V ? K : never; }[keyof T]; type SettingRowBaseProps = { className?: string; controlClassName?: string; description?: ReactNode; disabled?: boolean; title: ReactNode; }; export function SettingsList({ children, className }: { children: ReactNode; className?: string }) { return
{children}
; } export function SettingsSection({ children, className, description, title, }: { children: ReactNode; className?: string; description?: ReactNode; title: ReactNode | null; }) { const showHeader = title != null || description != null; return (
{showHeader && (
{title != null &&
{title}
} {description != null &&

{description}

}
)}
{children}
); } export function SettingRow({ children, className, controlClassName, description, disabled, title, }: { children: ReactNode; } & SettingRowBaseProps) { return (
{title}
{description != null && (
{description}
)}
{children}
); } export function SettingValue({ actions, className, copyText, enableCopy = true, value, }: { actions?: SettingValueAction[]; className?: string; copyText?: string; enableCopy?: boolean; value: ReactNode; }) { const textValue = typeof value === "string" || typeof value === "number" ? `${value}` : null; const textToCopy = copyText ?? textValue; return ( <> {value} {actions?.map((action) => ( ))} {enableCopy && textToCopy != null && ( )} ); } type SettingValueAction = { icon: IconButtonProps["icon"]; onClick: () => void; title: string; }; export function SettingRowBoolean({ checked, checkboxSize = "md", onChange, title, ...props }: { checked: boolean; checkboxSize?: "sm" | "md"; onChange: (checked: boolean) => void; } & SettingRowBaseProps) { return ( ); } export function ModelSettingRowBoolean>({ model, modelKey, ...props }: { model: M; modelKey: K; } & Omit[0], "checked" | "onChange">) { return ( patchModel(model, { [modelKey]: value } as Partial)} {...props} /> ); } export function SettingRowNumber({ inputClassName, inputWidthClassName = "!w-48", name, onChange, placeholder, required, title, type = "number", validate, value, ...props }: { inputClassName?: string; inputWidthClassName?: string; name: string; onChange: (value: number) => void; placeholder?: string; required?: boolean; type?: "number"; validate?: (value: string) => boolean; value: number; } & SettingRowBaseProps) { return ( onChange(Number.parseInt(value, 10) || 0)} type={type} className={inputClassName} containerClassName={inputWidthClassName} disabled={props.disabled} /> ); } export function ModelSettingRowNumber>({ model, modelKey, name = String(modelKey), ...props }: { model: M; modelKey: K; name?: string; } & Omit[0], "name" | "onChange" | "value">) { return ( patchModel(model, { [modelKey]: value } as Partial)} {...props} /> ); } export function SettingRowText({ inputClassName, inputWidthClassName = "!w-80", name, onChange, placeholder, required, title, type = "text", value, ...props }: { inputClassName?: string; inputWidthClassName?: string; name: string; onChange: (value: string) => void; placeholder?: string; required?: boolean; type?: "text" | "password"; value: string; } & SettingRowBaseProps) { return ( ); } export function ModelSettingRowText>({ model, modelKey, name = String(modelKey), ...props }: { model: M; modelKey: K; name?: string; } & Omit[0], "name" | "onChange" | "value">) { return ( patchModel(model, { [modelKey]: value } as Partial)} {...props} /> ); } export function SettingRowFile({ buttonClassName, controlClassName = "min-w-0 max-w-[min(32rem,45vw)]", directory, filePath, nameOverride, noun, onChange, size = "xs", title, ...props }: { buttonClassName?: string; directory?: boolean; filePath: string | null; nameOverride?: string | null; noun?: string; onChange: (filePath: string | null) => void | Promise; size?: Parameters[0]["size"]; } & SettingRowBaseProps) { return ( onChange(filePath)} /> ); } export function SettingRowDirectory({ noun = "Directory", ...props }: Omit[0], "directory">) { return ; } export function SettingRowSelect({ defaultValue, name, onChange, options, selectClassName = "!w-48", title, value, ...props }: { defaultValue?: T; name: string; onChange: (value: T) => void; options: RadioDropdownItem[]; selectClassName?: string; value: T; } & SettingRowBaseProps) { return ( ); } export function SettingSelectControl({ defaultValue, disabled, label, name, onChange, options, selectClassName = "!w-48", value, }: { defaultValue?: T; disabled?: boolean; label: string; name: string; onChange: (value: T) => void; options: RadioDropdownItem[]; selectClassName?: string; value: T; }) { return (