-
Notifications
You must be signed in to change notification settings - Fork 329
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1055 from datopian/feature/bucket-viewer-component
feature: Created bucket viewer component
- Loading branch information
Showing
5 changed files
with
138 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@portaljs/components': minor | ||
--- | ||
|
||
Creation of BucketViewer component to show the data of public buckets |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import { useEffect, useState } from 'react'; | ||
import LoadingSpinner from './LoadingSpinner'; | ||
|
||
export interface BucketViewerProps { | ||
domain: string; | ||
suffix?: string; | ||
className?: string; | ||
dataMapperFn: (rawData: Response) => Promise<BucketViewerData[]>; | ||
} | ||
|
||
export interface BucketViewerData { | ||
fileName: string; | ||
downloadFileUri: string; | ||
dateProps?: { | ||
date: Date; | ||
dateFormatter: (date: Date) => string; | ||
}; | ||
} | ||
|
||
export function BucketViewer({ | ||
domain, | ||
suffix, | ||
dataMapperFn, | ||
className, | ||
}: BucketViewerProps) { | ||
const [isLoading, setIsLoading] = useState<boolean>(false); | ||
const [bucketFiles, setBucketFiles] = useState<BucketViewerData[]>([]); | ||
suffix = suffix ?? '/'; | ||
|
||
useEffect(() => { | ||
setIsLoading(true); | ||
fetch(`${domain}${suffix}`) | ||
.then((res) => dataMapperFn(res)) | ||
.then((data) => setBucketFiles(data)) | ||
.finally(() => setIsLoading(false)); | ||
}, [domain, suffix]); | ||
return isLoading ? ( | ||
<div className="w-full flex items-center justify-center h-[300px]"> | ||
<LoadingSpinner /> | ||
</div> | ||
) : bucketFiles ? ( | ||
<> | ||
{...bucketFiles?.map((data, i) => ( | ||
<ul | ||
onClick={() => { | ||
const anchorId = `download_anchor_${i}`; | ||
const a: HTMLAnchorElement = | ||
(document.getElementById(anchorId) as HTMLAnchorElement | null) ?? | ||
document.createElement('a'); | ||
a.id = anchorId; | ||
if (a.download) a.click(); | ||
else { | ||
setIsLoading(true); | ||
fetch(data.downloadFileUri) | ||
.then((res) => res.blob()) | ||
.then((res) => { | ||
a.href = URL.createObjectURL(res); | ||
a.download = res.name ?? data.fileName; | ||
document.body.appendChild(a); | ||
a.click(); | ||
}) | ||
.finally(() => setIsLoading(false)); | ||
} | ||
}} | ||
key={i} | ||
className={`${ | ||
className ?? | ||
'mb-2 border-b-[2px] border-b-[red] hover:cursor-pointer' | ||
}`} | ||
> | ||
<li>{data.fileName}</li> | ||
{data.dateProps ? ( | ||
<li>{data.dateProps.dateFormatter(data.dateProps.date)}</li> | ||
) : ( | ||
<></> | ||
)} | ||
</ul> | ||
))} | ||
</> | ||
) : null; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { raw, type Meta, type StoryObj } from '@storybook/react'; | ||
|
||
import { BucketViewer, BucketViewerData, BucketViewerProps } from '../src/components/BucketViewer'; | ||
|
||
// More on how to set up stories at: https://storybook.js.org/docs/react/writing-stories/introduction | ||
const meta: Meta = { | ||
title: 'Components/BucketViewer', | ||
component: BucketViewer, | ||
tags: ['autodocs'], | ||
argTypes: { | ||
domain: { | ||
description: | ||
'Bucket domain URI', | ||
}, | ||
suffix: { | ||
description: | ||
'Suffix of bucket domain', | ||
}, | ||
}, | ||
}; | ||
|
||
export default meta; | ||
|
||
type Story = StoryObj<BucketViewerProps>; | ||
|
||
// More on writing stories with args: https://storybook.js.org/docs/react/writing-stories/args | ||
export const Normal: Story = { | ||
name: 'Bucket viewer', | ||
args: { | ||
domain: 'https://ssen-smart-meter.datopian.workers.dev', | ||
suffix: '/', | ||
dataMapperFn: async (rawData: Response) => { | ||
const result = await rawData.json(); | ||
return result.objects.map( | ||
e => ({ | ||
downloadFileUri: e.downloadLink, | ||
fileName: e.key.replace(/^(\w+\/)/g, '') , | ||
dateProps: { | ||
date: new Date(e.uploaded), | ||
dateFormatter: (date) => date.toLocaleDateString() | ||
} | ||
}) | ||
) | ||
} | ||
}, | ||
}; |
b55ec51
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
portaljs-storybook – ./packages/components/
portaljs-storybook-git-main-datopian1.vercel.app
portaljs-storybook.vercel.app
storybook.portaljs.org
portaljs-storybook-datopian1.vercel.app