Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

Refactor PreviewPane to typescript and simplify props

Joshen Lim8e486e64e61d
diff --git a/studio/components/to-be-cleaned/Storage/StorageExplorer/FileExplorerRow.js b/studio/components/to-be-cleaned/Storage/StorageExplorer/FileExplorerRow.js
index efc6aa48d98..b6d525e2d9d 100644
--- a/studio/components/to-be-cleaned/Storage/StorageExplorer/FileExplorerRow.js
+++ b/studio/components/to-be-cleaned/Storage/StorageExplorer/FileExplorerRow.js
@@ -146,8 +146,17 @@ const FileExplorerRow = ({
? [{ name: 'Delete', onClick: () => onSelectItemDelete(itemWithColumnIndex) }]
: item.type === STORAGE_ROW_TYPES.FOLDER
? [
- { name: 'Rename', onClick: () => onSelectItemRename(itemWithColumnIndex) },
- { name: 'Delete', onClick: () => onSelectItemDelete(itemWithColumnIndex) },
+ {
+ name: 'Rename',
+ icon: <IconEdit size="tiny" />,
+ onClick: () => onSelectItemRename(itemWithColumnIndex),
+ },
+ { name: 'Separator' },
+ {
+ name: 'Delete',
+ icon: <IconTrash size="tiny" />,
+ onClick: () => onSelectItemDelete(itemWithColumnIndex),
+ },
]
: [
...(!item.isCorrupted
diff --git a/studio/components/to-be-cleaned/Storage/StorageExplorer/PreviewPane.js b/studio/components/to-be-cleaned/Storage/StorageExplorer/PreviewPane.tsx
similarity index 90%
rename from studio/components/to-be-cleaned/Storage/StorageExplorer/PreviewPane.js
rename to studio/components/to-be-cleaned/Storage/StorageExplorer/PreviewPane.tsx
index 38b05a2c691..49066467eae 100644
--- a/studio/components/to-be-cleaned/Storage/StorageExplorer/PreviewPane.js
+++ b/studio/components/to-be-cleaned/Storage/StorageExplorer/PreviewPane.tsx
@@ -1,3 +1,4 @@
+import { isEmpty } from 'lodash'
import {
Button,
IconX,
@@ -11,13 +12,13 @@ import SVG from 'react-inlinesvg'
import { formatBytes } from 'lib/helpers'
import { Transition } from '@headlessui/react'
import Link from 'next/link'
+import { useStorageStore } from 'localStores/storageExplorer/StorageExplorerStore'
-const PreviewFile = ({ mimeType, previewUrl }) => {
+const PreviewFile = ({ mimeType, previewUrl }: { mimeType: string; previewUrl: string }) => {
if (!mimeType || !previewUrl) {
return (
<SVG
src={'/img/file-filled.svg'}
- alt={'No preview'}
preProcessor={(code) =>
code.replace(/svg/, 'svg class="mx-auto w-32 h-32 text-color-inherit opacity-75"')
}
@@ -36,7 +37,6 @@ const PreviewFile = ({ mimeType, previewUrl }) => {
<div className="flex h-full w-full flex-col items-center justify-center">
<SVG
src={'/img/file-filled.svg'}
- alt={'No preview'}
preProcessor={(code) =>
code.replace(/svg/, 'svg class="mx-auto w-32 h-32 text-color-inherit opacity-75"')
}
@@ -80,7 +80,6 @@ const PreviewFile = ({ mimeType, previewUrl }) => {
return (
<SVG
src={'/img/file-filled.svg'}
- alt={'No preview'}
preProcessor={(code) =>
code.replace(/svg/, 'svg class="mx-auto w-32 h-32 text-color-inherit opacity-75"')
}
@@ -88,14 +87,17 @@ const PreviewFile = ({ mimeType, previewUrl }) => {
)
}
-const PreviewPane = ({
- isOpen = false,
- file = {},
- width = 400,
- onCopyFileURL = () => {},
- onSelectFileDelete = () => {},
- onClosePreviewPane = () => {},
-}) => {
+const PreviewPane = () => {
+ const storageExplorerStore = useStorageStore()
+ const {
+ selectedFilePreview: file,
+ copyFileURLToClipboard,
+ closeFilePreview,
+ setSelectedItemsToDelete,
+ } = storageExplorerStore
+
+ const width = 450
+ const isOpen = !isEmpty(file)
const size = file.metadata ? formatBytes(file.metadata.size) : null
const mimeType = file.metadata ? file.metadata.mimetype : null
const createdAt = file.created_at ? new Date(file.created_at).toLocaleString() : 'Unknown'
@@ -123,7 +125,7 @@ const PreviewPane = ({
className="cursor-pointer"
size={14}
strokeWidth={2}
- onClick={onClosePreviewPane}
+ onClick={() => closeFilePreview()}
/>
</div>
@@ -182,7 +184,7 @@ const PreviewPane = ({
<Button
type="outline"
icon={<IconClipboard size={16} strokeWidth={2} />}
- onClick={() => onCopyFileURL(file)}
+ onClick={async () => await copyFileURLToClipboard(file)}
disabled={file.isCorrupted}
>
Copy URL
@@ -193,7 +195,7 @@ const PreviewPane = ({
shadow={false}
size="tiny"
icon={<IconTrash2 size={16} strokeWidth={2} />}
- onClick={() => onSelectFileDelete(file)}
+ onClick={() => setSelectedItemsToDelete([file])}
>
Delete file
</Button>
diff --git a/studio/components/to-be-cleaned/Storage/StorageExplorer/StorageExplorer.js b/studio/components/to-be-cleaned/Storage/StorageExplorer/StorageExplorer.js
index f1c1a10451a..95cdec1e7ae 100644
--- a/studio/components/to-be-cleaned/Storage/StorageExplorer/StorageExplorer.js
+++ b/studio/components/to-be-cleaned/Storage/StorageExplorer/StorageExplorer.js
@@ -68,7 +68,6 @@ const StorageExplorer = observer(({ bucket }) => {
const [isSearching, setIsSearching] = useState(false)
const [itemSearchString, setItemSearchString] = useState('')
- const previewPaneWidth = 450
// Requires a fixed height to ensure that explorer is constrained to the viewport
const fileExplorerHeight = window.innerHeight - 122
@@ -313,14 +312,7 @@ const StorageExplorer = observer(({ bucket }) => {
fetchMoreFolderContents(index, column, itemSearchString)
}
/>
- <PreviewPane
- isOpen={!isEmpty(selectedFilePreview)}
- file={selectedFilePreview}
- width={previewPaneWidth}
- onCopyFileURL={onCopyFileURL}
- onSelectFileDelete={onSelectItemDelete}
- onClosePreviewPane={closeFilePreview}
- />
+ <PreviewPane />
</div>
<ConfirmDeleteModal
visible={selectedItemsToDelete.length > 0}