import type { OdFolderObject } from '../types'
import { useState } from 'react'
import Link from 'next/link'
import emojiRegex from 'emoji-regex'
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'
import { useClipboard } from 'use-clipboard-copy'
import { getFileIcon } from '../utils/getFileIcon'
import { getBaseUrl } from '../utils/getBaseUrl'
import { formatModifiedDateTime } from '../utils/fileDetails'
import { Checkbox, Downloading } from './FileListing'
type OdFolderChildren = OdFolderObject['value'][number]
const GridItem = ({ c }: { c: OdFolderChildren }) => {
const emojiIcon = emojiRegex().exec(c.name)
const renderEmoji = emojiIcon && !emojiIcon.index
const ChildIcon = () =>
renderEmoji ? (
{emojiIcon ? emojiIcon[0] : '📁'}
) : (
)
// We use the generated medium thumbnail for rendering preview images
const thumbnail = c.thumbnails && c.thumbnails.length > 0 ? c.thumbnails[0].medium : null
// Some thumbnails are broken, so we check for onerror event in the image component
const [brokenThumbnail, setBrokenThumbnail] = useState(false)
return (
{thumbnail && !brokenThumbnail ? (
// eslint-disable-next-line @next/next/no-img-element

setBrokenThumbnail(true)}
/>
) : (
{c.folder?.childCount}
)}
{renderEmoji ? c.name.replace(emojiIcon ? emojiIcon[0] : '', '').trim() : c.name}
{formatModifiedDateTime(c.lastModifiedDateTime)}
)
}
const FolderGridLayout = ({
path,
folderChildren,
selected,
toggleItemSelected,
totalSelected,
toggleTotalSelected,
totalGenerating,
handleSelectedDownload,
folderGenerating,
handleFolderDownload,
toast,
}) => {
const clipboard = useClipboard()
return (
{folderChildren.length} items
{totalGenerating ? (
) : (
)}
{folderChildren.map((c: OdFolderChildren) => (
{c.folder ? (
{
clipboard.copy(`${getBaseUrl()}${path === '/' ? '' : path}/${encodeURIComponent(c.name)}`)
toast('Copied folder permalink.', { icon: '👌' })
}}
>
{folderGenerating[c.id] ? (
) : (
{
const p = `${path === '/' ? '' : path}/${encodeURIComponent(c.name)}`
handleFolderDownload(p, c.id, c.name)()
}}
>
)}
) : (
{
clipboard.copy(
`${getBaseUrl()}/api?path=${path === '/' ? '' : path}/${encodeURIComponent(c.name)}&raw=true`
)
toast.success('Copied raw file permalink.')
}}
>
)}
{!c.folder && !(c.name === '.password') && (
toggleItemSelected(c.id)}
title="Select file"
/>
)}
))}
)
}
export default FolderGridLayout