mirror of
https://github.com/supabase/supabase.git
synced 2026-10-14 21:59:16 +03:00
* Refactor `StorageMenu` modals to replace deprecated patterns * add test for `DeleteBucketModal` and update test setup Note: Because this component uses `useParams`, it's necessary to have the dynamic route segment passed to `next-router-mock`'s `createDynamicRouteParser`. In order not to have to manually list all of these as the application grows, I added a glob utility that uses the `pages/` directory to automatically generate an array of dynamic route paths in this case. * add test for `EmptyBucketModal` * add test for `EditBucketModal` and add `isNonNullable` utility function * add test for `CreateBucketModal` * implement requested changes * implement visual fixes
376 lines
14 KiB
TypeScript
376 lines
14 KiB
TypeScript
import { useParams } from 'common'
|
|
import { ChevronDown } from 'lucide-react'
|
|
import Link from 'next/link'
|
|
import { useState } from 'react'
|
|
import { toast } from 'sonner'
|
|
import { type SubmitHandler, useForm } from 'react-hook-form'
|
|
import { zodResolver } from '@hookform/resolvers/zod'
|
|
import { z } from 'zod'
|
|
import {
|
|
Button,
|
|
CollapsibleContent_Shadcn_,
|
|
CollapsibleTrigger_Shadcn_,
|
|
Collapsible_Shadcn_,
|
|
Dialog,
|
|
DialogContent,
|
|
DialogFooter,
|
|
DialogHeader,
|
|
DialogSectionSeparator,
|
|
DialogSection,
|
|
DialogTitle,
|
|
FormControl_Shadcn_,
|
|
FormField_Shadcn_,
|
|
Form_Shadcn_,
|
|
Input_Shadcn_,
|
|
SelectContent_Shadcn_,
|
|
SelectItem_Shadcn_,
|
|
SelectTrigger_Shadcn_,
|
|
SelectValue_Shadcn_,
|
|
Select_Shadcn_,
|
|
Switch,
|
|
cn,
|
|
} from 'ui'
|
|
|
|
import { StorageSizeUnits } from 'components/interfaces/Storage/StorageSettings/StorageSettings.constants'
|
|
import {
|
|
convertFromBytes,
|
|
convertToBytes,
|
|
} from 'components/interfaces/Storage/StorageSettings/StorageSettings.utils'
|
|
import { InlineLink } from 'components/ui/InlineLink'
|
|
import { useProjectStorageConfigQuery } from 'data/config/project-storage-config-query'
|
|
import { useBucketUpdateMutation } from 'data/storage/bucket-update-mutation'
|
|
import { IS_PLATFORM } from 'lib/constants'
|
|
import { Admonition } from 'ui-patterns'
|
|
import { Bucket } from 'data/storage/buckets-query'
|
|
import { FormItemLayout } from 'ui-patterns/form/FormItemLayout/FormItemLayout'
|
|
import { isNonNullable } from 'lib/isNonNullable'
|
|
|
|
export interface EditBucketModalProps {
|
|
visible: boolean
|
|
bucket: Bucket
|
|
onClose: () => void
|
|
}
|
|
|
|
const BucketSchema = z.object({
|
|
name: z.string(),
|
|
public: z.boolean().default(false),
|
|
has_file_size_limit: z.boolean().default(false),
|
|
formatted_size_limit: z.coerce
|
|
.number()
|
|
.min(0, 'File size upload limit has to be at least 0')
|
|
.default(0),
|
|
allowed_mime_types: z.string().trim().default(''),
|
|
})
|
|
|
|
const formId = 'edit-storage-bucket-form'
|
|
|
|
export const EditBucketModal = ({ visible, bucket, onClose }: EditBucketModalProps) => {
|
|
const { ref } = useParams()
|
|
|
|
const { mutate: updateBucket, isLoading: isUpdating } = useBucketUpdateMutation()
|
|
const { data } = useProjectStorageConfigQuery({ projectRef: ref }, { enabled: IS_PLATFORM })
|
|
const { value, unit } = convertFromBytes(data?.fileSizeLimit ?? 0)
|
|
const formattedGlobalUploadLimit = `${value} ${unit}`
|
|
|
|
const [selectedUnit, setSelectedUnit] = useState<string>(StorageSizeUnits.BYTES)
|
|
const [showConfiguration, setShowConfiguration] = useState(false)
|
|
const { value: fileSizeLimit } = convertFromBytes(bucket?.file_size_limit ?? 0)
|
|
|
|
const form = useForm<z.infer<typeof BucketSchema>>({
|
|
resolver: zodResolver(BucketSchema),
|
|
defaultValues: {
|
|
name: bucket?.name ?? '',
|
|
public: bucket?.public,
|
|
has_file_size_limit: isNonNullable(bucket?.file_size_limit),
|
|
formatted_size_limit: fileSizeLimit ?? 0,
|
|
allowed_mime_types: (bucket?.allowed_mime_types ?? []).join(', '),
|
|
},
|
|
values: {
|
|
name: bucket?.name ?? '',
|
|
public: bucket?.public,
|
|
has_file_size_limit: isNonNullable(bucket?.file_size_limit),
|
|
formatted_size_limit: fileSizeLimit ?? 0,
|
|
allowed_mime_types: (bucket?.allowed_mime_types ?? []).join(', '),
|
|
},
|
|
mode: 'onSubmit',
|
|
})
|
|
|
|
const isPublicBucket = form.watch('public')
|
|
const hasFileSizeLimit = form.watch('has_file_size_limit')
|
|
const formattedSizeLimit = form.watch('formatted_size_limit')
|
|
const isChangingBucketVisibility = bucket?.public !== isPublicBucket
|
|
const isMakingBucketPrivate = bucket?.public && !isPublicBucket
|
|
const isMakingBucketPublic = !bucket?.public && isPublicBucket
|
|
|
|
const onSubmit: SubmitHandler<z.infer<typeof BucketSchema>> = async (values) => {
|
|
if (bucket === undefined) return console.error('Bucket is required')
|
|
if (ref === undefined) return console.error('Project ref is required')
|
|
|
|
updateBucket(
|
|
{
|
|
projectRef: ref,
|
|
id: bucket.id,
|
|
isPublic: values.public,
|
|
file_size_limit: values.has_file_size_limit
|
|
? convertToBytes(values.formatted_size_limit, selectedUnit as StorageSizeUnits)
|
|
: null,
|
|
allowed_mime_types:
|
|
values.allowed_mime_types.length > 0
|
|
? values.allowed_mime_types.split(',').map((x: string) => x.trim())
|
|
: null,
|
|
},
|
|
{
|
|
onSuccess: () => {
|
|
toast.success(`Successfully updated bucket "${bucket?.name}"`)
|
|
onClose()
|
|
},
|
|
}
|
|
)
|
|
}
|
|
|
|
return (
|
|
<Dialog
|
|
open={visible}
|
|
onOpenChange={(open) => {
|
|
if (!open) {
|
|
form.reset()
|
|
onClose()
|
|
}
|
|
}}
|
|
>
|
|
<DialogContent>
|
|
<DialogHeader>
|
|
<DialogTitle>{`Edit bucket "${bucket?.name}"`}</DialogTitle>
|
|
</DialogHeader>
|
|
<DialogSectionSeparator />
|
|
<DialogSection>
|
|
<Form_Shadcn_ {...form}>
|
|
<form
|
|
id={formId}
|
|
className="flex flex-col gap-4"
|
|
onSubmit={form.handleSubmit(onSubmit)}
|
|
>
|
|
<FormField_Shadcn_
|
|
key="name"
|
|
name="name"
|
|
control={form.control}
|
|
render={({ field }) => (
|
|
<FormItemLayout
|
|
name="name"
|
|
label="Name of bucket"
|
|
labelOptional="Buckets cannot be renamed once created."
|
|
>
|
|
<FormControl_Shadcn_>
|
|
<Input_Shadcn_ id="name" {...field} disabled />
|
|
</FormControl_Shadcn_>
|
|
</FormItemLayout>
|
|
)}
|
|
/>
|
|
<FormField_Shadcn_
|
|
key="public"
|
|
name="public"
|
|
control={form.control}
|
|
render={({ field }) => (
|
|
<FormItemLayout
|
|
name="public"
|
|
label="Public bucket"
|
|
description="Anyone can read any object without any authorization"
|
|
layout="flex"
|
|
>
|
|
<FormControl_Shadcn_>
|
|
<Switch
|
|
id="public"
|
|
size="large"
|
|
checked={field.value}
|
|
onCheckedChange={field.onChange}
|
|
/>
|
|
</FormControl_Shadcn_>
|
|
</FormItemLayout>
|
|
)}
|
|
/>
|
|
{isChangingBucketVisibility && (
|
|
<Admonition
|
|
type="warning"
|
|
className="rounded-none border-x-0 border-b-0 mb-0 pb-0 px-0 [&>svg]:left-0 [&>div>p]:!leading-normal"
|
|
title={
|
|
isMakingBucketPublic
|
|
? 'Warning: Making bucket public'
|
|
: isMakingBucketPrivate
|
|
? 'Warning: Making bucket private'
|
|
: ''
|
|
}
|
|
description={
|
|
<>
|
|
{isMakingBucketPublic ? (
|
|
<p>`This will make all objects in your bucket publicly accessible.`</p>
|
|
) : isMakingBucketPrivate ? (
|
|
<p>
|
|
`All objects in your bucket will be private and only accessible via signed
|
|
URLs, or downloaded with the right authorisation headers.`
|
|
</p>
|
|
) : null}
|
|
|
|
{isMakingBucketPrivate && (
|
|
<p>
|
|
{
|
|
'Assets cached in the CDN may still be publicly accessible. You can consider '
|
|
}
|
|
<InlineLink href="https://supabase.com/docs/guides/storage/cdn/smart-cdn#cache-eviction">
|
|
purging the cache
|
|
</InlineLink>
|
|
{' or moving your assets to a new bucket.'}
|
|
</p>
|
|
)}
|
|
</>
|
|
}
|
|
/>
|
|
)}
|
|
<Collapsible_Shadcn_
|
|
open={showConfiguration}
|
|
onOpenChange={() => setShowConfiguration(!showConfiguration)}
|
|
>
|
|
<CollapsibleTrigger_Shadcn_ asChild>
|
|
<button className="w-full cursor-pointer py-3 flex items-center justify-between border-t border-default">
|
|
<p className="text-sm">Additional configuration</p>
|
|
<ChevronDown
|
|
size={18}
|
|
strokeWidth={2}
|
|
className={cn('text-foreground-light', showConfiguration && 'rotate-180')}
|
|
/>
|
|
</button>
|
|
</CollapsibleTrigger_Shadcn_>
|
|
<CollapsibleContent_Shadcn_ className="py-4 space-y-4">
|
|
<div className="space-y-2">
|
|
<FormField_Shadcn_
|
|
key="has_file_size_limit"
|
|
name="has_file_size_limit"
|
|
control={form.control}
|
|
render={({ field }) => (
|
|
<FormItemLayout
|
|
name="has_file_size_limit"
|
|
label="Restrict file upload size for bucket"
|
|
description="Prevent uploading of file sizes greater than a specified limit"
|
|
layout="flex"
|
|
>
|
|
<FormControl_Shadcn_>
|
|
<Switch
|
|
id="has_file_size_limit"
|
|
size="large"
|
|
checked={field.value}
|
|
onCheckedChange={field.onChange}
|
|
/>
|
|
</FormControl_Shadcn_>
|
|
</FormItemLayout>
|
|
)}
|
|
/>
|
|
{hasFileSizeLimit && (
|
|
<div className="grid grid-cols-12 col-span-12 gap-x-2 gap-y-1">
|
|
<div className="col-span-8">
|
|
<FormField_Shadcn_
|
|
key="formatted_size_limit"
|
|
name="formatted_size_limit"
|
|
control={form.control}
|
|
render={({ field }) => (
|
|
<FormItemLayout
|
|
name="formatted_size_limit"
|
|
description={`Equivalent to ${convertToBytes(
|
|
formattedSizeLimit,
|
|
selectedUnit as StorageSizeUnits
|
|
).toLocaleString()} bytes.`}
|
|
>
|
|
<FormControl_Shadcn_>
|
|
<Input_Shadcn_
|
|
id="formatted_size_limit"
|
|
aria-label="File size limit"
|
|
type="number"
|
|
min={0}
|
|
{...field}
|
|
/>
|
|
</FormControl_Shadcn_>
|
|
</FormItemLayout>
|
|
)}
|
|
/>
|
|
</div>
|
|
<Select_Shadcn_ value={selectedUnit} onValueChange={setSelectedUnit}>
|
|
<SelectTrigger_Shadcn_
|
|
aria-label="File size limit unit"
|
|
size="small"
|
|
className="col-span-4"
|
|
>
|
|
<SelectValue_Shadcn_ asChild>
|
|
<>{selectedUnit}</>
|
|
</SelectValue_Shadcn_>
|
|
</SelectTrigger_Shadcn_>
|
|
<SelectContent_Shadcn_>
|
|
{Object.values(StorageSizeUnits).map((unit: string) => (
|
|
<SelectItem_Shadcn_ key={unit} value={unit} className="text-xs">
|
|
<div>{unit}</div>
|
|
</SelectItem_Shadcn_>
|
|
))}
|
|
</SelectContent_Shadcn_>
|
|
</Select_Shadcn_>
|
|
{IS_PLATFORM && (
|
|
<div className="col-span-12 mt-2">
|
|
<p className="text-foreground-light text-sm">
|
|
Note: Individual bucket upload will still be capped at the{' '}
|
|
<Link
|
|
href={`/project/${ref}/settings/storage`}
|
|
className="font-bold underline"
|
|
>
|
|
global upload limit
|
|
</Link>{' '}
|
|
of {formattedGlobalUploadLimit}
|
|
</p>
|
|
</div>
|
|
)}
|
|
</div>
|
|
)}
|
|
</div>
|
|
<FormField_Shadcn_
|
|
key="allowed_mime_types"
|
|
name="allowed_mime_types"
|
|
control={form.control}
|
|
render={({ field }) => (
|
|
<FormItemLayout
|
|
name="allowed_mime_types"
|
|
label="Allowed MIME types"
|
|
labelOptional="Comma separated values"
|
|
description="Wildcards are allowed, e.g. image/*. Leave blank to allow any MIME type."
|
|
>
|
|
<FormControl_Shadcn_>
|
|
<Input_Shadcn_
|
|
id="allowed_mime_types"
|
|
{...field}
|
|
placeholder="e.g image/jpeg, image/png, audio/mpeg, video/mp4, etc"
|
|
/>
|
|
</FormControl_Shadcn_>
|
|
</FormItemLayout>
|
|
)}
|
|
/>
|
|
</CollapsibleContent_Shadcn_>
|
|
</Collapsible_Shadcn_>
|
|
</form>
|
|
</Form_Shadcn_>
|
|
</DialogSection>
|
|
<DialogFooter>
|
|
<Button
|
|
type="default"
|
|
disabled={isUpdating}
|
|
onClick={() => {
|
|
form.reset()
|
|
onClose()
|
|
}}
|
|
>
|
|
Cancel
|
|
</Button>
|
|
<Button form={formId} htmlType="submit" loading={isUpdating}>
|
|
Save
|
|
</Button>
|
|
</DialogFooter>
|
|
</DialogContent>
|
|
</Dialog>
|
|
)
|
|
}
|
|
|
|
export default EditBucketModal
|