2022-01-16 16:08:04 +00:00
|
|
|
import axios from 'axios'
|
2022-01-24 12:50:43 +00:00
|
|
|
import useSWR, { SWRResponse } from 'swr'
|
|
|
|
import { Dispatch, Fragment, SetStateAction, useState } from 'react'
|
2022-01-16 16:08:04 +00:00
|
|
|
import AwesomeDebouncePromise from 'awesome-debounce-promise'
|
|
|
|
import { useAsync } from 'react-async-hook'
|
|
|
|
import useConstant from 'use-constant'
|
2022-02-06 10:35:03 +00:00
|
|
|
import { useTranslation } from 'next-i18next'
|
2022-01-16 16:08:04 +00:00
|
|
|
|
|
|
|
import Link from 'next/link'
|
2022-01-24 12:50:43 +00:00
|
|
|
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'
|
|
|
|
import { Dialog, Transition } from '@headlessui/react'
|
2022-01-16 16:08:04 +00:00
|
|
|
|
2022-02-02 08:41:30 +00:00
|
|
|
import type { OdDriveItem, OdSearchResult } from '../types'
|
2022-01-24 12:31:17 +00:00
|
|
|
import { LoadingIcon } from './Loading'
|
|
|
|
|
2022-01-21 13:42:21 +00:00
|
|
|
import { getFileIcon } from '../utils/getFileIcon'
|
2022-01-24 12:50:43 +00:00
|
|
|
import { fetcher } from '../utils/fetchWithSWR'
|
2022-02-01 14:19:00 +00:00
|
|
|
import siteConfig from '../config/site.config'
|
2022-01-21 13:42:21 +00:00
|
|
|
|
2022-01-24 12:31:17 +00:00
|
|
|
/**
|
|
|
|
* Extract the searched item's path in field 'parentReference' and convert it to the
|
|
|
|
* absolute path represented in onedrive-vercel-index
|
|
|
|
*
|
|
|
|
* @param path Path returned from the parentReference field of the driveItem
|
|
|
|
* @returns The absolute path of the driveItem in the search result
|
|
|
|
*/
|
|
|
|
function mapAbsolutePath(path: string): string {
|
2022-01-24 14:31:11 +00:00
|
|
|
// path is in the format of '/drive/root:/path/to/file', if baseDirectory is '/' then we split on 'root:',
|
|
|
|
// otherwise we split on the user defined 'baseDirectory'
|
|
|
|
const absolutePath = path.split(siteConfig.baseDirectory === '/' ? 'root:' : siteConfig.baseDirectory)[1]
|
|
|
|
// path returned by the API may contain #, by doing a decodeURIComponent and then encodeURIComponent we can
|
|
|
|
// replace URL sensitive characters such as the # with %23
|
2022-02-01 14:19:00 +00:00
|
|
|
return absolutePath
|
|
|
|
.split('/')
|
|
|
|
.map(p => encodeURIComponent(decodeURIComponent(p)))
|
|
|
|
.join('/')
|
2022-01-24 12:31:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Implements a debounced search function that returns a promise that resolves to an array of
|
|
|
|
* search results.
|
|
|
|
*
|
|
|
|
* @returns A react hook for a debounced async search of the drive
|
|
|
|
*/
|
2022-01-21 13:42:21 +00:00
|
|
|
function useDriveItemSearch() {
|
2022-01-16 16:08:04 +00:00
|
|
|
const [query, setQuery] = useState('')
|
|
|
|
const searchDriveItem = async (q: string) => {
|
2022-01-21 13:42:21 +00:00
|
|
|
const { data } = await axios.get<OdSearchResult>(`/api/search?q=${q}`)
|
|
|
|
|
|
|
|
// Map parentReference to the absolute path of the search result
|
|
|
|
data.map(item => {
|
2022-01-24 08:18:55 +00:00
|
|
|
item['path'] =
|
|
|
|
'path' in item.parentReference
|
|
|
|
? // OneDrive International have the path returned in the parentReference field
|
|
|
|
`${mapAbsolutePath(item.parentReference.path)}/${encodeURIComponent(item.name)}`
|
|
|
|
: // OneDrive for Business/Education does not, so we need extra steps here
|
|
|
|
''
|
2022-01-21 13:42:21 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
return data
|
2022-01-16 16:08:04 +00:00
|
|
|
}
|
|
|
|
|
2022-01-24 12:31:17 +00:00
|
|
|
const debouncedDriveItemSearch = useConstant(() => AwesomeDebouncePromise(searchDriveItem, 1000))
|
2022-01-16 16:08:04 +00:00
|
|
|
const results = useAsync(async () => {
|
|
|
|
if (query.length === 0) {
|
|
|
|
return []
|
|
|
|
} else {
|
2022-01-24 12:31:17 +00:00
|
|
|
return debouncedDriveItemSearch(query)
|
2022-01-16 16:08:04 +00:00
|
|
|
}
|
|
|
|
}, [query])
|
|
|
|
|
|
|
|
return {
|
|
|
|
query,
|
|
|
|
setQuery,
|
|
|
|
results,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-24 12:31:17 +00:00
|
|
|
function SearchResultItemTemplate({
|
|
|
|
driveItem,
|
|
|
|
driveItemPath,
|
|
|
|
itemDescription,
|
|
|
|
disabled,
|
|
|
|
}: {
|
|
|
|
driveItem: OdSearchResult[number]
|
|
|
|
driveItemPath: string
|
|
|
|
itemDescription: string
|
|
|
|
disabled: boolean
|
|
|
|
}) {
|
|
|
|
return (
|
|
|
|
<Link href={driveItemPath} passHref>
|
|
|
|
<a
|
|
|
|
className={`flex items-center space-x-4 border-b border-gray-400/30 px-4 py-1.5 hover:bg-gray-50 dark:hover:bg-gray-850 ${
|
|
|
|
disabled ? 'cursor-not-allowed pointer-events-none' : 'cursor-pointer'
|
|
|
|
}`}
|
|
|
|
>
|
|
|
|
<FontAwesomeIcon icon={driveItem.file ? getFileIcon(driveItem.name) : ['far', 'folder']} />
|
|
|
|
<div>
|
|
|
|
<div className="text-sm font-medium leading-8">{driveItem.name}</div>
|
|
|
|
<div
|
|
|
|
className={`text-xs font-mono opacity-60 truncate overflow-hidden ${
|
|
|
|
itemDescription === 'Loading ...' && 'animate-pulse'
|
|
|
|
}`}
|
|
|
|
>
|
|
|
|
{itemDescription}
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</a>
|
|
|
|
</Link>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
function SearchResultItemLoadRemote({ result }: { result: OdSearchResult[number] }) {
|
2022-01-24 12:50:43 +00:00
|
|
|
const { data, error }: SWRResponse<OdDriveItem, string> = useSWR(`/api/item?id=${result.id}`, fetcher)
|
2022-01-24 12:31:17 +00:00
|
|
|
|
2022-02-06 10:35:03 +00:00
|
|
|
const { t } = useTranslation()
|
|
|
|
|
2022-01-24 12:31:17 +00:00
|
|
|
if (error) {
|
|
|
|
return <SearchResultItemTemplate driveItem={result} driveItemPath={''} itemDescription={error} disabled={true} />
|
|
|
|
}
|
2022-01-24 12:50:43 +00:00
|
|
|
if (!data) {
|
2022-01-24 12:31:17 +00:00
|
|
|
return (
|
2022-02-06 10:35:03 +00:00
|
|
|
<SearchResultItemTemplate
|
|
|
|
driveItem={result}
|
|
|
|
driveItemPath={''}
|
|
|
|
itemDescription={t('Loading ...')}
|
|
|
|
disabled={true}
|
|
|
|
/>
|
2022-01-24 12:31:17 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-01-24 12:50:43 +00:00
|
|
|
const driveItemPath = `${mapAbsolutePath(data.parentReference.path)}/${encodeURIComponent(data.name)}`
|
2022-01-24 12:31:17 +00:00
|
|
|
return (
|
|
|
|
<SearchResultItemTemplate
|
|
|
|
driveItem={result}
|
|
|
|
driveItemPath={driveItemPath}
|
|
|
|
itemDescription={decodeURIComponent(driveItemPath)}
|
|
|
|
disabled={false}
|
|
|
|
/>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
function SearchResultItem({ result }: { result: OdSearchResult[number] }) {
|
|
|
|
if (result.path === '') {
|
|
|
|
// path is empty, which means we need to fetch the parentReference to get the path
|
|
|
|
return <SearchResultItemLoadRemote result={result} />
|
|
|
|
} else {
|
|
|
|
// path is not an empty string in the search result, such that we can directly render the component as is
|
|
|
|
const driveItemPath = decodeURIComponent(result.path)
|
|
|
|
return (
|
|
|
|
<SearchResultItemTemplate
|
|
|
|
driveItem={result}
|
2022-01-24 14:31:11 +00:00
|
|
|
driveItemPath={result.path}
|
2022-01-24 12:31:17 +00:00
|
|
|
itemDescription={driveItemPath}
|
|
|
|
disabled={false}
|
|
|
|
/>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default function SearchModal({
|
2022-01-21 13:42:21 +00:00
|
|
|
searchOpen,
|
|
|
|
setSearchOpen,
|
|
|
|
}: {
|
2022-01-16 16:08:04 +00:00
|
|
|
searchOpen: boolean
|
|
|
|
setSearchOpen: Dispatch<SetStateAction<boolean>>
|
2022-01-21 13:42:21 +00:00
|
|
|
}) {
|
2022-01-16 16:08:04 +00:00
|
|
|
const { query, setQuery, results } = useDriveItemSearch()
|
|
|
|
|
2022-02-06 10:35:03 +00:00
|
|
|
const { t } = useTranslation()
|
|
|
|
|
2022-01-24 12:56:02 +00:00
|
|
|
const closeSearchBox = () => {
|
|
|
|
setSearchOpen(false)
|
|
|
|
setQuery('')
|
|
|
|
}
|
|
|
|
|
2022-01-16 16:08:04 +00:00
|
|
|
return (
|
|
|
|
<Transition appear show={searchOpen} as={Fragment}>
|
2022-01-21 14:12:07 +00:00
|
|
|
<Dialog as="div" className="inset-0 z-[200] fixed overflow-y-auto" onClose={closeSearchBox}>
|
2022-01-16 16:08:04 +00:00
|
|
|
<div className="min-h-screen text-center px-4">
|
|
|
|
<Transition.Child
|
|
|
|
as={Fragment}
|
|
|
|
enter="ease-out duration-100"
|
|
|
|
enterFrom="opacity-0"
|
|
|
|
enterTo="opacity-100"
|
|
|
|
leave="ease-in duration-100"
|
|
|
|
leaveFrom="opacity-100"
|
|
|
|
leaveTo="opacity-0"
|
|
|
|
>
|
|
|
|
<Dialog.Overlay className="bg-white/80 inset-0 fixed dark:bg-gray-800/80" />
|
|
|
|
</Transition.Child>
|
|
|
|
|
|
|
|
<Transition.Child
|
|
|
|
as={Fragment}
|
|
|
|
enter="ease-out duration-100"
|
|
|
|
enterFrom="opacity-0 scale-95"
|
|
|
|
enterTo="opacity-100 scale-100"
|
|
|
|
leave="ease-in duration-100"
|
|
|
|
leaveFrom="opacity-100 scale-100"
|
|
|
|
leaveTo="opacity-0 scale-95"
|
|
|
|
>
|
2022-01-21 14:12:07 +00:00
|
|
|
<div className="border rounded border-gray-400/30 shadow-xl my-12 text-left w-full max-w-3xl transform transition-all inline-block overflow-hidden">
|
2022-01-21 13:42:21 +00:00
|
|
|
<Dialog.Title
|
|
|
|
as="h3"
|
|
|
|
className="flex items-center space-x-4 dark:text-white border-b bg-gray-50 border-gray-400/30 p-4 dark:bg-gray-800"
|
|
|
|
>
|
|
|
|
<FontAwesomeIcon icon="search" className="w-4 h-4" />
|
2022-01-16 16:08:04 +00:00
|
|
|
<input
|
|
|
|
type="text"
|
|
|
|
id="search-box"
|
2022-01-21 13:42:21 +00:00
|
|
|
className="w-full bg-transparent focus:outline-none focus-visible:outline-none"
|
2022-02-06 10:35:03 +00:00
|
|
|
placeholder={t('Search ...')}
|
2022-01-16 16:08:04 +00:00
|
|
|
value={query}
|
|
|
|
onChange={e => setQuery(e.target.value)}
|
|
|
|
/>
|
2022-01-24 12:31:17 +00:00
|
|
|
<div className="px-2 py-1 rounded-lg bg-gray-200 dark:bg-gray-700 font-medium text-xs">ESC</div>
|
2022-01-16 16:08:04 +00:00
|
|
|
</Dialog.Title>
|
2022-01-24 12:31:17 +00:00
|
|
|
<div
|
|
|
|
className="bg-white dark:text-white dark:bg-gray-900 max-h-[80vh] overflow-x-hidden overflow-y-scroll"
|
|
|
|
onClick={closeSearchBox}
|
|
|
|
>
|
2022-01-16 16:08:04 +00:00
|
|
|
{results.loading && (
|
2022-01-21 13:42:21 +00:00
|
|
|
<div className="text-center px-4 py-12 text-sm font-medium">
|
|
|
|
<LoadingIcon className="animate-spin w-4 h-4 mr-2 inline-block svg-inline--fa" />
|
2022-02-06 10:35:03 +00:00
|
|
|
<span>{t('Loading ...')}</span>
|
2022-01-16 16:08:04 +00:00
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
{results.error && (
|
2022-02-06 10:35:03 +00:00
|
|
|
<div className="text-center px-4 py-12 text-sm font-medium">
|
|
|
|
{t('Error: {{message}}', { message: results.error.message })}
|
|
|
|
</div>
|
2022-01-16 16:08:04 +00:00
|
|
|
)}
|
|
|
|
{results.result && (
|
|
|
|
<>
|
|
|
|
{results.result.length === 0 ? (
|
2022-02-06 10:35:03 +00:00
|
|
|
<div className="text-center px-4 py-12 text-sm font-medium">{t('Nothing here.')}</div>
|
2022-01-16 16:08:04 +00:00
|
|
|
) : (
|
2022-01-24 12:31:17 +00:00
|
|
|
results.result.map(result => <SearchResultItem key={result.id} result={result} />)
|
2022-01-16 16:08:04 +00:00
|
|
|
)}
|
|
|
|
</>
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</Transition.Child>
|
|
|
|
</div>
|
|
|
|
</Dialog>
|
|
|
|
</Transition>
|
|
|
|
)
|
|
|
|
}
|