Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 24 additions & 45 deletions catalog/app/containers/Bucket/FallbackToDir.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import Placeholder from 'components/Placeholder'
import * as Model from 'model'
import * as AWS from 'utils/AWS'
import { useData } from 'utils/Data'
import AsyncResult from 'utils/AsyncResult'
import * as NamedRoutes from 'utils/NamedRoutes'
import * as s3paths from 'utils/s3paths'

Expand All @@ -24,52 +23,32 @@ export default function FallbackToDir({ children, handle }: FallbackToDirProps)

const objData = useData(requests.getObjectExistence, { s3, ...handle })

const noAutoFetch = React.useMemo(
() =>
AsyncResult.case(
{
Ok: requests.ObjectExistence.case({ Exists: R.T, _: R.F }),
_: R.T,
},
objData.result,
),
[objData.result],
)
const noAutoFetch = objData.case({
Ok: requests.ObjectExistence.case({ Exists: R.T, _: R.F }),
_: R.T,
})

const dirData = useData(requests.bucketListing, { s3, ...handle }, { noAutoFetch })

const redirectResult = React.useMemo(
() =>
AsyncResult.case(
{
Ok: requests.ObjectExistence.case({
Exists: R.F,
_: () =>
AsyncResult.mapCase(
{
Ok: (isDirectory: boolean) =>
isDirectory
? urls.bucketDir(handle.bucket, s3paths.ensureSlash(handle.key))
: undefined,
},
dirData.result,
),
}),
Err: AsyncResult.Err,
Pending: AsyncResult.Pending,
Init: AsyncResult.Init,
},
objData.result,
),
[objData.result, dirData.result, handle, urls],
)

return AsyncResult.case(
{
Ok: (to?: string) => (to ? <RRDom.Redirect to={to} /> : children),
Err: displayError(),
_: () => <Placeholder color="text.secondary" />,
},
redirectResult,
const shouldRedirect: null | Error | boolean = objData.case({
Ok: requests.ObjectExistence.case({
Exists: () => false,
_: () =>
dirData.case({
Ok: ({ dirs, files }: any) => !!dirs.length || !!files.length,
Err: (e: Error) => e,
_: () => null,
}),
}),
Err: (e: Error) => e,
_: () => null,
})

if (shouldRedirect === null) return <Placeholder color="text.secondary" />
if (shouldRedirect instanceof Error) return displayError()(shouldRedirect)
return shouldRedirect ? (
<RRDom.Redirect to={urls.bucketDir(handle.bucket, s3paths.ensureSlash(handle.key))} />
) : (
children
)
}
Loading