1
1
'use client'
2
- import type { PayloadRequest } from 'payload'
2
+ import type { FieldMap , PayloadRequest } from 'payload'
3
3
4
+ import { getTranslation } from '@payloadcms/translations'
4
5
import {
5
6
CopyToClipboard ,
7
+ FieldDescription ,
6
8
FieldLabel ,
7
9
GenerateConfirmation ,
10
+ useComponentMap ,
8
11
useConfig ,
12
+ useDocumentInfo ,
9
13
useField ,
10
14
useFormFields ,
11
15
useTranslation ,
@@ -18,16 +22,24 @@ const path = 'apiKey'
18
22
const baseClass = 'api-key'
19
23
const fieldBaseClass = 'field-type'
20
24
21
- export const APIKey : React . FC < { enabled : boolean ; readOnly ?: boolean } > = ( {
25
+ export const APIKey : React . FC < { readonly enabled : boolean ; readonly readOnly ?: boolean } > = ( {
22
26
enabled,
23
27
readOnly,
24
28
} ) => {
25
29
const [ initialAPIKey ] = useState ( uuidv4 ( ) )
26
30
const [ highlightedField , setHighlightedField ] = useState ( false )
27
- const { t } = useTranslation ( )
31
+ const { i18n , t } = useTranslation ( )
28
32
const config = useConfig ( )
29
-
30
33
const apiKey = useFormFields ( ( [ fields ] ) => ( fields && fields [ path ] ) || null )
34
+ const { collectionSlug, docPermissions } = useDocumentInfo ( )
35
+
36
+ const { getFieldMap } = useComponentMap ( )
37
+
38
+ const fieldMap : FieldMap = getFieldMap ( {
39
+ collectionSlug,
40
+ } )
41
+
42
+ const apiKeyField = fieldMap . find ( ( field ) => 'name' in field && field . name === 'apiKey' )
31
43
32
44
const validate = ( val ) =>
33
45
text ( val , {
@@ -48,15 +60,30 @@ export const APIKey: React.FC<{ enabled: boolean; readOnly?: boolean }> = ({
48
60
49
61
const apiKeyValue = apiKey ?. value
50
62
51
- const APIKeyLabel = useMemo (
52
- ( ) => (
63
+ const apiKeyLabel = useMemo ( ( ) => {
64
+ let label : Record < string , string > | string = 'API Key'
65
+
66
+ if ( apiKeyField && apiKeyField . fieldComponentProps . label ) {
67
+ label = apiKeyField . fieldComponentProps . label
68
+ }
69
+
70
+ return getTranslation ( label , i18n )
71
+ } , [ apiKeyField , i18n ] )
72
+
73
+ const APIKeyLabelComponent = useMemo ( ( ) => {
74
+ return (
53
75
< div className = { `${ baseClass } __label` } >
54
- < span > API Key </ span >
76
+ < span > { apiKeyLabel } </ span >
55
77
< CopyToClipboard value = { apiKeyValue as string } />
56
78
</ div >
57
- ) ,
58
- [ apiKeyValue ] ,
59
- )
79
+ )
80
+ } , [ apiKeyLabel , apiKeyValue ] )
81
+
82
+ const canUpdateAPIKey = useMemo ( ( ) => {
83
+ if ( docPermissions && docPermissions ?. fields ?. apiKey ) {
84
+ return docPermissions . fields . apiKey . update . permission
85
+ }
86
+ } , [ docPermissions ] )
60
87
61
88
const fieldType = useField ( {
62
89
path : 'apiKey' ,
@@ -98,9 +125,9 @@ export const APIKey: React.FC<{ enabled: boolean; readOnly?: boolean }> = ({
98
125
return (
99
126
< React . Fragment >
100
127
< div className = { [ fieldBaseClass , 'api-key' , 'read-only' ] . filter ( Boolean ) . join ( ' ' ) } >
101
- < FieldLabel CustomLabel = { APIKeyLabel } htmlFor = { path } />
128
+ < FieldLabel CustomLabel = { APIKeyLabelComponent } htmlFor = { path } />
102
129
< input
103
- aria-label = "API Key"
130
+ aria-label = { apiKeyLabel }
104
131
className = { highlightedField ? 'highlight' : undefined }
105
132
disabled
106
133
id = "apiKey"
@@ -109,7 +136,7 @@ export const APIKey: React.FC<{ enabled: boolean; readOnly?: boolean }> = ({
109
136
value = { ( value as string ) || '' }
110
137
/>
111
138
</ div >
112
- { ! readOnly && (
139
+ { ! readOnly && canUpdateAPIKey && (
113
140
< GenerateConfirmation highlightField = { highlightField } setKey = { ( ) => setValue ( uuidv4 ( ) ) } />
114
141
) }
115
142
</ React . Fragment >
0 commit comments