import { Button, Center, FormControl, FormHelperText, FormLabel, HStack, Input, Select, Switch as HopeSwitch, Text, Textarea, VStack, } from "@hope-ui/solid" import { For, Match, Show, Switch } from "solid-js" import { useT } from "~/hooks" import { DriverItem, Type } from "~/types" import { FolderChooseInput, SelectOptions } from "~/components" import { getFileSize } from "~/utils" export type ItemProps = DriverItem & { readonly?: boolean full_name_path?: string options_prefix?: string driver?: string additionValues?: Record } & ( | { type: Type.Bool onChange?: (value: boolean) => void value: boolean } | { type: Type.Number onChange?: (value: number) => void value: number } | { type: Type.Float onChange?: (value: number) => void value: number } | { type: Type.String | Type.Text onChange?: (value: string) => void value: string } | { type: Type.Select searchable?: boolean onChange?: (value: string) => void value: string } ) const Item = (props: ItemProps) => { const t = useT() const isFolderPathField = props.type === Type.String && !props.readonly && (props.name === "root_folder_path" || props.name === "remote_path") const isChunkSizeField = props.type === Type.Number && props.driver === "Chunker" && props.name === "chunk_size" const isChunkerRemotePathsField = props.type === Type.Text && props.driver === "Chunker" && props.name === "remote_paths" const isStrmPathsField = props.type === Type.Text && props.driver === "Strm" && props.name === "paths" const isPathSelectionTextField = isChunkerRemotePathsField || isStrmPathsField const isChunkerStoreChunksInPrimaryField = props.type === Type.Bool && props.driver === "Chunker" && props.name === "store_chunks_in_primary" const numberOnChange = props.type === Type.Number ? props.onChange : undefined const chunkerExtraRemotePaths = () => String(props.additionValues?.remote_paths ?? "") .split("\n") .map((path) => path.trim()) .filter(Boolean) const chunkerStoreChunksInPrimaryHint = () => { if (!isChunkerStoreChunksInPrimaryField) { return "" } if (chunkerExtraRemotePaths().length === 0) { return t("drivers.Chunker.store_chunks_in_primary-empty_hint") } return props.value ? t("drivers.Chunker.store_chunks_in_primary-enabled_hint") : t("drivers.Chunker.store_chunks_in_primary-disabled_hint") } const pathSelectionEntries = () => { if (!isPathSelectionTextField) { return [] } const value = (props.value as string) ?? "" return value === "" ? [""] : value.split("\n") } const updatePathSelectionEntries = (entries: string[]) => { if (props.type !== Type.Text) { return } props.onChange?.(entries.join("\n")) } return ( {t( props.full_name_path ?? props.driver === "common" ? `storages.common.${props.name}` : `drivers.${props.driver}.${props.name}`, )} {t("settings.unknown_type")}}> { if (props.type === Type.String) { props.onChange?.(value) } }} /> props.onChange?.(e.currentTarget.value) : undefined } /> props.onChange?.(parseInt(e.currentTarget.value)) : undefined } /> } > { const mb = parseFloat(e.currentTarget.value) numberOnChange?.( Number.isFinite(mb) ? Math.round(mb * 1024 * 1024) : 0, ) }} /> MB props.onChange?.(parseFloat(e.currentTarget.value)) : undefined } /> props.onChange?.(e.currentTarget.checked) : undefined } /> props.onChange?.(e.currentTarget.value) : undefined } /> } > {(entry, index) => ( { const next = [...pathSelectionEntries()] next[index()] = value updatePathSelectionEntries(next) }} /> )} {t( props.driver === "common" ? `storages.common.${props.name}-tips` : `drivers.${props.driver}.${props.name}-tips`, )} {`${(props.value as number).toLocaleString()} bytes (${getFileSize( props.value as number, )})`} {chunkerStoreChunksInPrimaryHint()} ) } export { Item }