Skip to content

Commit 128d721

Browse files
authored
fix(ui): hide 'Create new' button entirely if user has no access to create a media item (#7348)
Makes it so that if you don't have access to create a new media you don't get the button shown at all: ![image](https://github.com/user-attachments/assets/2a9c1b24-a4cb-41f3-9145-514cd51a2f1f)
1 parent abc786d commit 128d721

File tree

3 files changed

+38
-10
lines changed

3 files changed

+38
-10
lines changed

packages/eslint-config/configs/react/index.mjs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ export const index = deepMerge(
3838
version: 'detect',
3939
},
4040
},
41+
rules: {
42+
'react-hooks/rules-of-hooks': 'warn',
43+
},
4144
},
4245
)
4346
export default index

packages/ui/src/fields/Upload/Input.tsx

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ import './index.scss'
2323
const baseClass = 'upload'
2424

2525
export type UploadInputProps = {
26+
/**
27+
* Controls the visibility of the "Create new collection" button
28+
*/
29+
allowNewUpload?: boolean
2630
api?: string
2731
collection?: ClientCollectionConfig
2832
customUploadActions?: React.ReactNode[]
@@ -39,6 +43,7 @@ export const UploadInput: React.FC<UploadInputProps> = (props) => {
3943
CustomDescription,
4044
CustomError,
4145
CustomLabel,
46+
allowNewUpload,
4247
api = '/api',
4348
className,
4449
collection,
@@ -164,13 +169,18 @@ export const UploadInput: React.FC<UploadInputProps> = (props) => {
164169
{(!fileDoc || missingFile) && (
165170
<div className={`${baseClass}__wrap`}>
166171
<div className={`${baseClass}__buttons`}>
167-
<DocumentDrawerToggler className={`${baseClass}__toggler`} disabled={readOnly}>
168-
<Button buttonStyle="secondary" disabled={readOnly} el="div">
169-
{t('fields:uploadNewLabel', {
170-
label: getTranslation(collection.labels.singular, i18n),
171-
})}
172-
</Button>
173-
</DocumentDrawerToggler>
172+
{allowNewUpload && (
173+
<DocumentDrawerToggler
174+
className={`${baseClass}__toggler`}
175+
disabled={readOnly}
176+
>
177+
<Button buttonStyle="secondary" disabled={readOnly} el="div">
178+
{t('fields:uploadNewLabel', {
179+
label: getTranslation(collection.labels.singular, i18n),
180+
})}
181+
</Button>
182+
</DocumentDrawerToggler>
183+
)}
174184
<ListDrawerToggler className={`${baseClass}__toggler`} disabled={readOnly}>
175185
<Button buttonStyle="secondary" disabled={readOnly} el="div">
176186
{t('fields:chooseFromExisting')}

packages/ui/src/fields/Upload/index.tsx

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
'use client'
22

3-
import React, { useCallback } from 'react'
3+
import React, { useCallback, useMemo } from 'react'
44

55
import type { UploadInputProps } from './Input.js'
66
import type { UploadFieldProps } from './types.js'
77

88
import { useFieldProps } from '../../forms/FieldPropsProvider/index.js'
99
import { useField } from '../../forms/useField/index.js'
1010
import { withCondition } from '../../forms/withCondition/index.js'
11+
import { useAuth } from '../../providers/Auth/index.js'
1112
import { useConfig } from '../../providers/Config/index.js'
1213
import { UploadInput } from './Input.js'
1314
import './index.scss'
@@ -36,10 +37,12 @@ const _Upload: React.FC<UploadFieldProps> = (props) => {
3637

3738
const {
3839
collections,
39-
routes: { api },
40+
routes: { api: apiRoute },
4041
serverURL,
4142
} = useConfig()
4243

44+
const { permissions } = useAuth()
45+
4346
const collection = collections.find((coll) => coll.slug === relationTo)
4447

4548
const memoizedValidate = useCallback(
@@ -53,6 +56,17 @@ const _Upload: React.FC<UploadFieldProps> = (props) => {
5356

5457
const { path: pathFromContext, readOnly: readOnlyFromContext } = useFieldProps()
5558

59+
// Checks if the user has permissions to create a new document in the related collection
60+
const canCreate = useMemo(() => {
61+
if (permissions?.collections && permissions.collections?.[relationTo]?.create) {
62+
if (permissions.collections[relationTo].create?.permission === true) {
63+
return true
64+
}
65+
}
66+
67+
return false
68+
}, [relationTo, permissions])
69+
5670
const { filterOptions, formInitializing, formProcessing, path, setValue, showError, value } =
5771
useField<string>({
5872
path: pathFromContext ?? pathFromProps,
@@ -75,7 +89,8 @@ const _Upload: React.FC<UploadFieldProps> = (props) => {
7589
CustomDescription={CustomDescription}
7690
CustomError={CustomError}
7791
CustomLabel={CustomLabel}
78-
api={api}
92+
allowNewUpload={canCreate}
93+
api={apiRoute}
7994
className={className}
8095
collection={collection}
8196
descriptionProps={descriptionProps}

0 commit comments

Comments
 (0)