Skip to content

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

More refactors to reduce props drilldown, and implement expiry options in context menu

Joshen Lim8cc319e8630c
diff --git a/studio/components/layouts/StorageLayout/BucketRow.tsx b/studio/components/layouts/StorageLayout/BucketRow.tsx
new file mode 100644
index 00000000000..f96a5d96b4c
--- /dev/null
+++ b/studio/components/layouts/StorageLayout/BucketRow.tsx
@@ -0,0 +1,73 @@
+import { FC } from 'react'
+import { Badge, Dropdown, IconLoader, IconMoreVertical, IconTrash } from 'ui'
+
+import ProductMenuItem from 'components/ui/ProductMenu/ProductMenuItem'
+import { STORAGE_ROW_STATUS } from 'components/to-be-cleaned/Storage/Storage.constants'
+
+interface Props {
+ bucket: any
+ projectRef?: string
+ isSelected: boolean
+ onSelectDeleteBucket: (bucket: any) => void
+ onSelectToggleBucketPublic: (bucket: any) => void
+}
+
+const BucketRow: FC<Props> = ({
+ bucket = {},
+ projectRef = '',
+ isSelected = false,
+ onSelectDeleteBucket = () => {},
+ onSelectToggleBucketPublic = () => {},
+}) => {
+ return (
+ <ProductMenuItem
+ key={bucket.id}
+ name={
+ <div className="flex items-center justify-between space-x-2 truncate w-full">
+ <p className="truncate" title={bucket.name}>
+ {bucket.name}
+ </p>
+ {bucket.public && <Badge color="yellow">Public</Badge>}
+ </div>
+ }
+ url={`/project/${projectRef}/storage/buckets/${bucket.id}`}
+ isActive={isSelected}
+ action={
+ bucket.status === STORAGE_ROW_STATUS.LOADING ? (
+ <IconLoader className="animate-spin" size={16} strokeWidth={2} />
+ ) : bucket.status === STORAGE_ROW_STATUS.READY ? (
+ <Dropdown
+ side="bottom"
+ align="start"
+ overlay={[
+ <Dropdown.Item
+ key="toggle-private"
+ onClick={() => onSelectToggleBucketPublic(bucket)}
+ >
+ {bucket.public ? 'Make private' : 'Make public'}
+ </Dropdown.Item>,
+ <Dropdown.Separator key="bucket-separator" />,
+ <Dropdown.Item
+ icon={<IconTrash size="tiny" />}
+ key="delete-bucket"
+ onClick={() => onSelectDeleteBucket(bucket)}
+ >
+ Delete bucket
+ </Dropdown.Item>,
+ ]}
+ >
+ <IconMoreVertical
+ className="opacity-0 group-hover:opacity-100"
+ size="tiny"
+ strokeWidth={2}
+ />
+ </Dropdown>
+ ) : (
+ <div />
+ )
+ }
+ />
+ )
+}
+
+export default BucketRow
diff --git a/studio/components/layouts/StorageLayout/StorageMenu.tsx b/studio/components/layouts/StorageLayout/StorageMenu.tsx
index 2889292f42f..441e07c40a9 100644
--- a/studio/components/layouts/StorageLayout/StorageMenu.tsx
+++ b/studio/components/layouts/StorageLayout/StorageMenu.tsx
@@ -2,28 +2,18 @@ import { FC } from 'react'
import Link from 'next/link'
import { useRouter } from 'next/router'
import { observer } from 'mobx-react-lite'
-import { useStore } from 'hooks'
-import {
- Badge,
- Button,
- Dropdown,
- Menu,
- IconLoader,
- IconMoreVertical,
- Alert,
- IconEdit,
- IconTrash,
-} from 'ui'
+import { useParams } from 'hooks'
+import { Button, Menu, IconLoader, Alert, IconEdit } from 'ui'
-import ProductMenuItem from 'components/ui/ProductMenu/ProductMenuItem'
-import { STORAGE_ROW_STATUS } from 'components/to-be-cleaned/Storage/Storage.constants'
+import BucketRow from './BucketRow'
import { useStorageStore } from 'localStores/storageExplorer/StorageExplorerStore'
interface Props {}
const StorageMenu: FC<Props> = () => {
const router = useRouter()
- const { ref, bucketId } = router.query
+ const { ref, bucketId } = useParams()
+
const page = router.pathname.split('/')[4] as
| undefined
| 'policies'
@@ -31,9 +21,6 @@ const StorageMenu: FC<Props> = () => {
| 'usage'
| 'logs'
- const { ui } = useStore()
- const projectRef = ui.selectedProject?.ref
-
const storageExplorerStore = useStorageStore()
const {
loaded,
@@ -98,7 +85,7 @@ const StorageMenu: FC<Props> = () => {
<div className="h-px w-full bg-scale-500"></div>
<div className="">
<Menu.Group title="Configuration" />
- <Link href={`/project/${projectRef}/storage/policies`}>
+ <Link href={`/project/${ref}/storage/policies`}>
<Menu.Item rounded active={page === 'policies'}>
<p className="truncate">Policies</p>
</Menu.Item>
@@ -110,61 +97,3 @@ const StorageMenu: FC<Props> = () => {
}
export default observer(StorageMenu)
-
-const BucketRow = ({
- bucket = {},
- projectRef = '',
- isSelected = false,
- onSelectDeleteBucket = () => {},
- onSelectToggleBucketPublic = () => {},
-}: any) => {
- return (
- <ProductMenuItem
- key={bucket.id}
- name={
- <div className="flex items-center justify-between space-x-2 truncate w-full">
- <p className="truncate" title={bucket.name}>
- {bucket.name}
- </p>
- {bucket.public && <Badge color="yellow">Public</Badge>}
- </div>
- }
- url={`/project/${projectRef}/storage/buckets/${bucket.id}`}
- isActive={isSelected}
- action={
- bucket.status === STORAGE_ROW_STATUS.LOADING ? (
- <IconLoader className="animate-spin" size={16} strokeWidth={2} />
- ) : bucket.status === STORAGE_ROW_STATUS.READY ? (
- <Dropdown
- side="bottom"
- align="start"
- overlay={[
- <Dropdown.Item
- key="toggle-private"
- onClick={() => onSelectToggleBucketPublic(bucket)}
- >
- {bucket.public ? 'Make private' : 'Make public'}
- </Dropdown.Item>,
- <Dropdown.Separator key="bucket-separator" />,
- <Dropdown.Item
- icon={<IconTrash size="tiny" />}
- key="delete-bucket"
- onClick={() => onSelectDeleteBucket(bucket)}
- >
- Delete bucket
- </Dropdown.Item>,
- ]}
- >
- <IconMoreVertical
- className="opacity-0 group-hover:opacity-100"
- size="tiny"
- strokeWidth={2}
- />
- </Dropdown>
- ) : (
- <div />
- )
- }
- />
- )
-}
diff --git a/studio/components/to-be-cleaned/Storage/StorageExplorer/CustomExpiryModal.tsx b/studio/components/to-be-cleaned/Storage/StorageExplorer/CustomExpiryModal.tsx
new file mode 100644
index 00000000000..53e9ed0a043
--- /dev/null
+++ b/studio/components/to-be-cleaned/Storage/StorageExplorer/CustomExpiryModal.tsx
@@ -0,0 +1,80 @@
+import { FC } from 'react'
+import { Form, Modal, Input, Button } from 'ui'
+import { observer } from 'mobx-react-lite'
+import { useStorageStore } from 'localStores/storageExplorer/StorageExplorerStore'
+
+interface Props {}
+
+const CustomExpiryModal: FC<Props> = () => {
+ const storageExplorerStore = useStorageStore()
+ const { selectedFileCustomExpiry, setSelectedFileCustomExpiry, copyFileURLToClipboard } =
+ storageExplorerStore
+
+ const visible = selectedFileCustomExpiry !== undefined
+ const onClose = () => setSelectedFileCustomExpiry(undefined)
+
+ console.log(selectedFileCustomExpiry)
+
+ return (
+ <Modal
+ hideFooter
+ size="small"
+ header="Custom expiry for signed URL"
+ visible={visible}
+ alignFooter="right"
+ confirmText="Get URL"
+ onCancel={() => onClose()}
+ >
+ <Form
+ validateOnBlur
+ initialValues={{ expiresIn: '' }}
+ onSubmit={async (values: any, { setSubmitting }: any) => {
+ setSubmitting(true)
+ await copyFileURLToClipboard(selectedFileCustomExpiry, values.expiresIn)
+ setSubmitting(false)
+ onClose()
+ }}
+ validate={(values: any) => {
+ const errors: any = {}
+ if (values.expiresIn !== '' && values.expiresIn <= 0)
+ errors.expiresIn = 'Expiry duration cannot be less than 0'
+ return errors
+ }}
+ >
+ {({ values, isSubmitting }: { values: any; isSubmitting: boolean }) => (
+ <>
+ <div className="py-6">
+ <Modal.Content>
+ <Input
+ disabled={isSubmitting}
+ type="number"
+ id="expiresIn"
+ label="Enter the duration for which the URL will be valid for"
+ actions={<p className="text-sm text-scale-1000 mr-2">seconds</p>}
+ />
+ </Modal.Content>
+ </div>
+ <Modal.Separator />
+ <Modal.Content>
+ <div className="flex items-center justify-end space-x-2 pt-1 pb-3">
+ <Button type="default" onClick={() => onClose()}>
+ Cancel
+ </Button>
+ <Button
+ disabled={values.expiresIn === '' || isSubmitting}
+ loading={isSubmitting}
+ htmlType="submit"
+ type="primary"
+ >
+ Get signed URL
+ </Button>
+ </div>
+ </Modal.Content>
+ </>
+ )}
+ </Form>
+ </Modal>
+ )
+}
+
+export default observer(CustomExpiryModal)
diff --git a/studio/components/to-be-cleaned/Storage/StorageExplorer/FileExplorer.js b/studio/components/to-be-cleaned/Storage/StorageExplorer/FileExplorer.js
index 21b30091755..4e0f9af240b 100644
--- a/studio/components/to-be-cleaned/Storage/StorageExplorer/FileExplorer.js
+++ b/studio/components/to-be-cleaned/Storage/StorageExplorer/FileExplorer.js
@@ -1,4 +1,5 @@
import React, { useEffect, useRef } from 'react'
+import { observer } from 'mobx-react-lite'
import { STORAGE_VIEWS, CONTEXT_MENU_KEYS } from '../Storage.constants'
import ItemContextMenu from './ItemContextMenu'
@@ -25,13 +26,7 @@ const FileExplorer = ({
const fileExplorerRef = useRef(null)
const storageExplorerStore = useStorageStore()
- const {
- downloadFile,
- setSelectedItemsToDelete,
- setSelectedItemToRename,
- setSelectedItemsToMove,
- copyFileURLToClipboard,
- } = storageExplorerStore
+ const { setSelectedItemToRename } = storageExplorerStore
useEffect(() => {
if (fileExplorerRef) {
@@ -55,14 +50,7 @@ const FileExplorer = ({
onSelectSort={onChangeSortBy}
onSelectSortByOrder={onChangeSortByOrder}
/>
- <ItemContextMenu
- id={CONTEXT_MENU_KEYS.STORAGE_ITEM}
- onSelectItemRename={(file) => setSelectedItemToRename(file)}
- onSelectItemMove={(file) => setSelectedItemsToMove([file])}
- onSelectItemDelete={(file) => setSelectedItemsToDelete([file])}
- onCopyFileURL={async (file) => await copyFileURLToClipboard(file)}
- onDownloadFile={async (file) => await downloadFile(file)}
- />
+ <ItemContextMenu id={CONTEXT_MENU_KEYS.STORAGE_ITEM} />
<FolderContextMenu
id={CONTEXT_MENU_KEYS.STORAGE_FOLDER}
onRenameFolder={(folder) => setSelectedItemToRename(folder)}
@@ -110,4 +98,4 @@ const FileExplorer = ({
)
}
-export default FileExplorer
+export default observer(FileExplorer)
diff --git a/studio/components/to-be-cleaned/Storage/StorageExplorer/FileExplorerRow.tsx b/studio/components/to-be-cleaned/Storage/StorageExplorer/FileExplorerRow.tsx
index fcb43622051..7795203fa55 100644
--- a/studio/components/to-be-cleaned/Storage/StorageExplorer/FileExplorerRow.tsx
+++ b/studio/components/to-be-cleaned/Storage/StorageExplorer/FileExplorerRow.tsx
@@ -208,6 +208,12 @@ const FileExplorerRow: FC<Props> = ({
name: 'Get URL',
icon: <IconClipboard size="tiny" />,
onClick: async () => await copyFileURLToClipboard(itemWithColumnIndex),
+ children: [
+ { name: 'Expire in 1 week' },
+ { name: 'Expire in 1 month' },
+ { name: 'Expire in 1 year' },
+ { name: 'Custom expiry' },
+ ],
},
{
name: 'Rename',
diff --git a/studio/components/to-be-cleaned/Storage/StorageExplorer/ItemContextMenu.js b/studio/components/to-be-cleaned/Storage/StorageExplorer/ItemContextMenu.js
index 993fd661b14..1b948c8bf12 100644
--- a/studio/components/to-be-cleaned/Storage/StorageExplorer/ItemContextMenu.js
+++ b/studio/components/to-be-cleaned/Storage/StorageExplorer/ItemContextMenu.js
@@ -1,43 +1,89 @@
-import { Menu, Item } from 'react-contexify'
+import { Menu, Item, Separator, Submenu } from 'react-contexify'
import 'react-contexify/dist/ReactContexify.css'
+import { IconClipboard, IconEdit, IconMove, IconDownload, IconTrash2, IconChevronRight } from 'ui'
+import { useStorageStore } from 'localStores/storageExplorer/StorageExplorerStore'
+import { observer } from 'mobx-react-lite'
+
+const ItemContextMenu = ({ id = '' }) => {
+ const storageExplorerStore = useStorageStore()
+ const {
+ downloadFile,
+ selectedBucket,
+ setSelectedItemsToDelete,
+ setSelectedItemToRename,
+ setSelectedItemsToMove,
+ setSelectedFileCustomExpiry,
+ copyFileURLToClipboard,
+ } = storageExplorerStore
+ const isPublic = selectedBucket.public
+
+ const onHandleClick = async (event, item, expiresIn) => {
+ if (item.isCorrupted) return
+ switch (event) {
+ case 'copy':
+ if (expiresIn < 0) return setSelectedFileCustomExpiry(item)
+ else return await copyFileURLToClipboard(item, expiresIn)
+ case 'rename':
+ return setSelectedItemToRename(item)
+ case 'move':
+ return setSelectedItemsToMove([item])
+ case 'download':
+ return await downloadFile(item)
+ default:
+ break
+ }
+ }
-const ItemContextMenu = ({
- id = '',
- onCopyFileURL = () => {},
- onSelectItemRename = () => {},
- onSelectItemMove = () => {},
- onDownloadFile = () => {},
- onSelectItemDelete = () => {},
-}) => {
return (
<Menu id={id} animation="fade" className="!bg-scale-300 border border-scale-500">
- <Item
- onClick={({ props }) => (!props.item.isCorrupted ? onCopyFileURL(props.item) : () => {})}
- >
- <span className="text-xs">Copy URL</span>
- </Item>
- <Item
- onClick={({ props }) =>
- !props.item.isCorrupted ? onSelectItemRename(props.item) : () => {}
- }
- >
- <span className="text-xs">Rename</span>
+ {isPublic ? (
+ <Item onClick={({ props }) => onHandleClick('copy', props.item)}>
+ <IconClipboard size="tiny" />
+ <span className="ml-2 text-xs">Copy URL</span>
+ </Item>
+ ) : (
+ <Submenu
+ label={
+ <div className="flex items-center space-x-2">
+ <IconClipboard size="tiny" />
+ <span className="text-xs">Copy URL</span>
+ </div>
+ }
+ arrow={<IconChevronRight size="tiny" />}
+ >
+ <Item onClick={({ props }) => onHandleClick('copy', props.item, 60 * 60 * 24 * 7)}>
+ <span className="ml-2 text-xs">Expire in 1 week</span>
+ </Item>
+ <Item onClick={({ props }) => onHandleClick('copy', props.item, 60 * 60 * 24 * 30)}>
+ <span className="ml-2 text-xs">Expire in 1 month</span>
+ </Item>
+ <Item onClick={({ props }) => onHandleClick('copy', props.item, 60 * 60 * 24 * 365)}>
+ <span className="ml-2 text-xs">Expire in 1 year</span>
+ </Item>
+ <Item onClick={({ props }) => onHandleClick('copy', props.item, -1)}>
+ <span className="ml-2 text-xs">Custom expiry</span>
+ </Item>
+ </Submenu>
+ )}
+ <Item onClick={({ props }) => onHandleClick('rename', props.item)}>
+ <IconEdit size="tiny" />
+ <span className="ml-2 text-xs">Rename</span>
</Item>
- <Item
- onClick={({ props }) => (!props.item.isCorrupted ? onSelectItemMove(props.item) : () => {})}
- >
- <span className="text-xs">Move</span>
+ <Item onClick={({ props }) => onHandleClick('move', props.item)}>
+ <IconMove size="tiny" />
+ <span className="ml-2 text-xs">Move</span>
</Item>
- <Item
- onClick={({ props }) => (!props.item.isCorrupted ? onDownloadFile(props.item) : () => {})}
- >
- <span className="text-xs">Download</span>
+ <Item onClick={({ props }) => onHandleClick('download', props.item)}>
+ <IconDownload size="tiny" />
+ <span className="ml-2 text-xs">Download</span>
</Item>
- <Item onClick={({ props }) => onSelectItemDelete(props.item)}>
- <span className="text-xs">Delete</span>
+ <Separator />
+ <Item onClick={({ props }) => setSelectedItemsToDelete([props.item])}>
+ <IconTrash2 size="tiny" />
+ <span className="ml-2 text-xs">Delete</span>
</Item>
</Menu>
)
}
-export default ItemContextMenu
+export default observer(ItemContextMenu)
diff --git a/studio/components/to-be-cleaned/Storage/StorageExplorer/PreviewPane.tsx b/studio/components/to-be-cleaned/Storage/StorageExplorer/PreviewPane.tsx
index 2b64d35abd9..d4b8d87b1d2 100644
--- a/studio/components/to-be-cleaned/Storage/StorageExplorer/PreviewPane.tsx
+++ b/studio/components/to-be-cleaned/Storage/StorageExplorer/PreviewPane.tsx
@@ -101,10 +101,9 @@ const PreviewPane = () => {
copyFileURLToClipboard,
closeFilePreview,
setSelectedItemsToDelete,
+ setSelectedFileCustomExpiry,
} = storageExplorerStore
- const [showExpiryInput, setShowExpiryInput] = useState(false)
-
const width = 450
const isOpen = !isEmpty(file)
const size = file.metadata ? formatBytes(file.metadata.size) : null
@@ -220,7 +219,7 @@ const PreviewPane = () => {
>
Expire in 1 year
</Dropdown.Item>,
- <Dropdown.Item onClick={() => setShowExpiryInput(true)}>
+ <Dropdown.Item onClick={() => setSelectedFileCustomExpiry(file)}>
Custom expiry
</Dropdown.Item>,
]}
@@ -248,64 +247,6 @@ const PreviewPane = () => {
</div>
</div>
</Transition>
- <Modal
- hideFooter
- size="small"
- header="Custom expiry for signed URL"
- visible={showExpiryInput}
- alignFooter="right"
- confirmText="Get URL"
- onCancel={() => setShowExpiryInput(false)}
- >
- <Form
- validateOnBlur
- initialValues={{ expiresIn: '' }}
- onSubmit={async (values: any, { setSubmitting }: any) => {
- setSubmitting(true)
- await copyFileURLToClipboard(file, values.expiresIn)
- setSubmitting(false)
- setShowExpiryInput(false)
- }}
- validate={(values: any) => {
- const errors: any = {}
- if (values.expiresIn !== '' && values.expiresIn <= 0)
- errors.expiresIn = 'Expiry duration cannot be less than 0'
- return errors
- }}
- >
- {({ values, isSubmitting }: { values: any; isSubmitting: boolean }) => (
- <>
- <div className="py-6">
- <Modal.Content>
- <Input
- disabled={isSubmitting}
- type="number"
- id="expiresIn"
- label="Enter the duration for which the URL will be valid for"
- actions={<p className="text-sm text-scale-1000 mr-2">seconds</p>}
- />
- </Modal.Content>
- </div>
- <Modal.Separator />
- <Modal.Content>
- <div className="flex items-center justify-end space-x-2 pt-1 pb-3">
- <Button type="default" onClick={() => setShowExpiryInput(false)}>
- Cancel
- </Button>
- <Button
- disabled={values.expiresIn === '' || isSubmitting}
- loading={isSubmitting}
- htmlType="submit"
- type="primary"
- >
- Get signed URL
- </Button>
- </div>
- </Modal.Content>
- </>
- )}
- </Form>
- </Modal>
</>
)
}
diff --git a/studio/components/to-be-cleaned/Storage/StorageExplorer/StorageExplorer.js b/studio/components/to-be-cleaned/Storage/StorageExplorer/StorageExplorer.js
index 53aa7b512e5..b19374ff0ba 100644
--- a/studio/components/to-be-cleaned/Storage/StorageExplorer/StorageExplorer.js
+++ b/studio/components/to-be-cleaned/Storage/StorageExplorer/StorageExplorer.js
@@ -11,6 +11,7 @@ import FileExplorerHeaderSelection from './FileExplorerHeaderSelection'
import ConfirmDeleteModal from './ConfirmDeleteModal'
import MoveItemsModal from './MoveItemsModal'
import PreviewPane from './PreviewPane'
+import CustomExpiryModal from './CustomExpiryModal'
const StorageExplorer = observer(({ bucket }) => {
const storageExplorerStore = useStorageStore()
@@ -213,6 +214,7 @@ const StorageExplorer = observer(({ bucket }) => {
onSelectCancel={clearSelectedItemsToMove}
onSelectMove={onMoveSelectedFiles}
/>
+ <CustomExpiryModal />
</div>
)
})
diff --git a/studio/localStores/storageExplorer/StorageExplorerStore.js b/studio/localStores/storageExplorer/StorageExplorerStore.js
index 323a51d10ca..b9a1742668b 100644
--- a/studio/localStores/storageExplorer/StorageExplorerStore.js
+++ b/studio/localStores/storageExplorer/StorageExplorerStore.js
@@ -58,6 +58,7 @@ class StorageExplorerStore {
selectedItemsToDelete = []
selectedItemsToMove = []
selectedFilePreview = {}
+ selectedFileCustomExpiry = undefined
DEFAULT_OPTIONS = {
limit: LIMIT,
@@ -300,6 +301,10 @@ class StorageExplorerStore {
this.selectedItemsToMove = []
}
+ setSelectedFileCustomExpiry = (item) => {
+ this.selectedFileCustomExpiry = item
+ }
+
addNewFolderPlaceholder = (columnIndex) => {
const isPrepend = true
const folderName = 'Untitled folder'
diff --git a/studio/styles/contextMenu.scss b/studio/styles/contextMenu.scss
index 88d9680880d..418b2fcd9a0 100644
--- a/studio/styles/contextMenu.scss
+++ b/studio/styles/contextMenu.scss
@@ -4,6 +4,9 @@
.react-contexify__item__content {
@apply dark:text-white text-sm;
}
+ .react-contexify__submenu {
+ @apply dark:bg-gray-300 border;
+ }
}
.react-contexify__item:not(.react-contexify__item--disabled):hover
> .react-contexify__item__content,