import type { PartialTreeFile, PartialTreeFolderNode } from '@uppy/core'
import { useRemoteSource } from '@uppy/react'
import type { AvailablePluginsKeys } from '@uppy/remote-sources'
import { useEffect, useRef } from 'react'
const dtf = new Intl.DateTimeFormat('en-US', {
dateStyle: 'short',
timeStyle: 'short',
})
function File({
item,
checkbox,
}: {
item: PartialTreeFile
checkbox: (item: PartialTreeFile, checked: boolean) => void
}) {
return (
checkbox(item, false)}
checked={item.status === 'checked'}
/>
{item.data.thumbnail && (
)}
{item.data.name}
{dtf.format(new Date(item.data.modifiedDate))}
)
}
function Folder({
item,
checkbox,
open,
}: {
item: PartialTreeFolderNode
checkbox: (item: PartialTreeFolderNode, checked: boolean) => void
open: (folderId: string | null) => Promise
}) {
const ref = useRef(null)
useEffect(() => {
if (ref.current && item.status === 'partial') {
// Can only be set via JS
ref.current.indeterminate = true
}
}, [item.status])
return (
checkbox(item, false)}
checked={item.status === 'checked'}
/>
)
}
export function RemoteSource({
close,
id,
}: {
close: () => void
id: AvailablePluginsKeys
}) {
const { state, login, logout, checkbox, open, done, cancel } =
useRemoteSource(id)
if (!state.authenticated) {
return (
)
}
return (
{state.breadcrumbs.map((breadcrumb, index) => (
<>
{index > 0 &&
>}{' '}
{index === state.breadcrumbs.length - 1 ? (
{breadcrumb.type === 'root' ? 'Dropbox' : breadcrumb.data.name}
) : (
)}
>
))}
{state.loading ? (
loading...
) : (
state.partialTree.map((item) => {
if (item.type === 'file') {
return
}
if (item.type === 'folder') {
return (
)
}
return null
})
)}
{state.selectedAmount > 0 && (
Selected {state.selectedAmount} items
)}
)
}