diff --git a/src/components/FileInput/FileInput.stories.tsx b/src/components/FileInput/FileInput.stories.tsx deleted file mode 100644 index 13699a2..0000000 --- a/src/components/FileInput/FileInput.stories.tsx +++ /dev/null @@ -1,12 +0,0 @@ -import React, { useEffect, useState } from 'react' -import { FileInput } from './index.js' - -export default { - title: 'FileInput (WIP)', - component: FileInput, -} - -export const Default = () => { - // const [value, setValue] = useState() - return -} diff --git a/src/components/FileInput/index.tsx b/src/components/FileInput/index.tsx deleted file mode 100644 index e4f4dea..0000000 --- a/src/components/FileInput/index.tsx +++ /dev/null @@ -1,287 +0,0 @@ -import { useRef, useState } from 'react' -import { colors } from '../../utils/colors.js' -import { radius } from '../../utils/radius.js' -import { Button } from '../Button/index.js' -import { Icon } from '../Icon/index.js' -import { Text } from '../Text/index.js' -import { useDialog } from '../Dialog/index.js' -import { useClient } from '@based/react' -import { Loader } from '../Loader/index.js' -import { prettyNumber } from '@based/pretty-number' -import { styled } from 'inlines' -import { IconButton } from '../IconButton/index.js' -import { Menu } from '../Menu/index.js' - -// TODO download button -// TODO individual errors per uploaded files -// TODO handle outside state/value correctly -// TODO correctly handle partial UIFiles - -type UIFile = { - name?: string - src: string - size?: number -} - -type FileInptuProps = { - value?: UIFile | UIFile[] - onChange: (value: UIFile | UIFile[]) => void - error?: string -} - -function FileInput(props: FileInptuProps) { - const dialog = useDialog() - const client = useClient() - const fileInputRef = useRef() - const [dragOver, setDragOver] = useState(false) - const [files, setFiles] = useState< - { - tempId: string - name: string - size: number - progress: number - error?: string - }[] - >([]) - const [itemWithOpenMenuId, setItemWithOpenMenuId] = useState() - - async function uploadFiles(files: File[]) { - await Promise.allSettled( - files.map(async (file) => { - const tempId = crypto.randomUUID() - setFiles((p) => [ - ...p, - { tempId, name: file.name, size: file.size, progress: 0 }, - ]) - - await client.stream( - 'db:file-upload', - { contents: file, fileName: file.name, mimeType: file.type }, - (progress) => { - setFiles((p) => - p.map((e) => (e.tempId === tempId ? { ...e, progress } : e)), - ) - }, - ) - }), - ) - } - - return ( - <> -
- { - e.preventDefault() - setDragOver(false) - - const files = e.dataTransfer.files - if (!files.length) return - - uploadFiles([...e.dataTransfer.files]) - }} - onDragOver={(e) => { - e.preventDefault() - setDragOver(true) - }} - onDragLeave={() => { - setDragOver(false) - }} - > - {files.length === 0 ? ( -
-
- - - Drag your file(s) here - -
-
- ) : ( -
- {files.map((file) => ( - -
- {file.progress < 1 ? ( - - ) : ( - - )} -
- - - {file.name} - -
- - {prettyNumber(file.size, 'number-bytes').toUpperCase()} - -
- { - setItemWithOpenMenuId(open ? file.tempId : undefined) - }} - > - - - - - Download - { - setFiles((p) => - p.filter((e) => e.tempId !== file.tempId), - ) - }} - > - Delete - - - -
- - ))} -
- )} -
-
- - {!!files.length && ( - - )} -
-
- { - const files = e.target.files - if (!files.length) return - - uploadFiles([...e.target.files]) - }} - /> - - ) -} - -export { FileInput } -export type { FileInptuProps } diff --git a/src/components/ReferenceInput/index.tsx b/src/components/ReferenceInput/index.tsx index e391c3d..c4ec05f 100644 --- a/src/components/ReferenceInput/index.tsx +++ b/src/components/ReferenceInput/index.tsx @@ -1,5 +1,5 @@ import { useClient, useQuery } from '@based/react' -import { useEffect, useMemo, useState } from 'react' +import { useEffect, useMemo, useRef, useState } from 'react' import { Sidebar } from '../Sidebar/index.js' import { BasedSchemaField, @@ -14,6 +14,10 @@ import { Modal } from '../Modal/index.js' import { Button } from '../Button/index.js' import { List } from '../List/index.js' import { BasedGrid } from '../Grid/index.js' +import { Icon } from '../Icon/index.js' +import { Text } from '../Text/index.js' +import { radius } from '../../utils/radius.js' +import { Menu } from '../Menu/index.js' function generateFinderFieldFromBasedSchemaField( key: string, @@ -118,6 +122,9 @@ function getBasicFinderPropsForType( } } +// TODO somehow integrate the progress into a loader for when files are uploading +// this means we might be able to remove the createObjectURL previews. look into figma + list component's rendering + type ReferenceInputProps = { type: string value?: string[] @@ -129,7 +136,21 @@ function ReferenceInput({ type, value = [], onChange }: ReferenceInputProps) { const { data } = useQuery('db:schema') const [select, setSelect] = useState { + const files = e.target.files + if (!files.length) return + + uploadFiles([...e.target.files]) + }} + /> + )} ) } diff --git a/src/index.ts b/src/index.ts index 48ed18a..8e0dce2 100644 --- a/src/index.ts +++ b/src/index.ts @@ -26,7 +26,6 @@ export * from './components/Modal/index.js' export * from './components/SelectInput/index.js' export * from './components/MultiSelect/index.js' export * from './components/DateInput/index.js' -export * from './components/FileInput/index.js' export * from './components/Table/index.js' export * from './components/ColorInput/index.js' export * from './components/Grid/index.js'