diff --git a/components/FileListing.tsx b/components/FileListing.tsx index 66eb144..08b741a 100644 --- a/components/FileListing.tsx +++ b/components/FileListing.tsx @@ -4,7 +4,7 @@ import emojiRegex from 'emoji-regex' import { useClipboard } from 'use-clipboard-copy' import { ParsedUrlQuery } from 'querystring' -import { FunctionComponent, useState } from 'react' +import { FunctionComponent, useEffect, useRef, useState } from 'react' import { ImageDecorator } from 'react-viewer/lib/ViewerProps' import { useRouter } from 'next/router' @@ -99,9 +99,43 @@ const FileListItem: FunctionComponent<{ ) } +const Checkbox: FunctionComponent<{ + checked: 0|1|2, onChange: () => void, title: string, indeterminate?: boolean +}> = ({ checked, onChange, title, indeterminate }) => { + const ref = useRef(null) + useEffect(() => { + if (ref.current) { + ref.current.checked = Boolean(checked) + if (indeterminate) { + ref.current.indeterminate = checked == 1 + } + } + }, [ref, checked, indeterminate]) + const handleClick = () => { ref.current ? ref.current.click() : null } + + return ( + + + + ) +} + const FileListing: FunctionComponent<{ query?: ParsedUrlQuery }> = ({ query }) => { const [imageViewerVisible, setImageViewerVisibility] = useState(false) const [activeImageIdx, setActiveImageIdx] = useState(0) + const [selected, setSelected] = useState<{[key: string]: boolean}>({}) + const [totalSelected, setTotalSelected] = useState<0|1|2>(0) const router = useRouter() const clipboard = useClipboard() @@ -173,6 +207,34 @@ const FileListing: FunctionComponent<{ query?: ParsedUrlQuery }> = ({ query }) = } }) + // File selection + const genTotalSelected = (selected: {[key: string]: boolean}) => { + const selectInfo = children.map((c :any) => Boolean(selected[c.id])) + const [hasT, hasF] = [selectInfo.some(i => i), selectInfo.some(i => !i)] + console.log(hasT, hasF) + return hasT && hasF ? 1 : (!hasF ? 2 : 0) + } + const toggleItemSelected = (id: string) => { + let val + if (selected[id]) { + val = {...selected} + delete val[id] + } else { + val = {...selected, [id]: true} + } + setSelected(val) + setTotalSelected(genTotalSelected(val)) + } + const toggleTotalSelected = () => { + if (genTotalSelected(selected) == 2) { + setSelected({}) + setTotalSelected(0) + } else { + setSelected(Object.fromEntries(children.map((c :any) => [c.id, true]))) + setTotalSelected(2) + } + } + return (
@@ -182,18 +244,12 @@ const FileListing: FunctionComponent<{ query?: ParsedUrlQuery }> = ({ query }) =
Actions
- - - + = ({ query }) =
)}
- - - + toggleItemSelected(c.id)} + title={"Select file"} + />
))}