forked from sanity-io/sanity-plugin-internationalized-array
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDocumentAddButtons.tsx
More file actions
183 lines (166 loc) · 5.56 KB
/
DocumentAddButtons.tsx
File metadata and controls
183 lines (166 loc) · 5.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
import {Box, Stack, Text, useToast} from '@sanity/ui'
import React, {useCallback} from 'react'
import {
FormInsertPatch,
FormSetIfMissingPatch,
insert,
isSanityDocument,
PatchEvent,
setIfMissing,
useSchema,
} from 'sanity'
import {useDocumentPane} from 'sanity/structure'
import {
DocumentsToTranslate,
getDocumentsToTranslate,
} from '../utils/getDocumentsToTranslate'
import AddButtons from './AddButtons'
import {useInternationalizedArrayContext} from './InternationalizedArrayContext'
type DocumentAddButtonsProps = {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
value: Record<string, any> | undefined
}
export default function DocumentAddButtons(
props: DocumentAddButtonsProps
): React.ReactElement {
const {filteredLanguages} = useInternationalizedArrayContext()
const value = isSanityDocument(props.value) ? props.value : undefined
const toast = useToast()
const {onChange} = useDocumentPane()
const schema = useSchema()
const documentsToTranslation = getDocumentsToTranslate(value, [])
// Helper function to determine if a field should be initialized as an array
const getInitialValueForType = useCallback(
(typeName: string): unknown => {
if (!typeName) return undefined
// Extract the base type name from internationalized array type
// e.g., "internationalizedArrayBodyValue" -> "body"
const match = typeName.match(/^internationalizedArray(.+)Value$/)
if (!match) return undefined
const baseTypeName = match[1].charAt(0).toLowerCase() + match[1].slice(1)
// Check if it's a known array-based type (Portable Text fields)
const arrayBasedTypes = [
'body',
'htmlContent',
'blockContent',
'portableText',
]
if (arrayBasedTypes.includes(baseTypeName)) {
return []
}
// Try to look up the schema type to determine if it's an array
try {
const schemaType = schema.get(typeName)
if (schemaType) {
// Check if this is an object type with a 'value' field
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const valueField = (schemaType as any)?.fields?.find(
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(f: any) => f.name === 'value'
)
if (valueField) {
const fieldType = valueField.type
// Check if the value field is an array type
if (
fieldType?.jsonType === 'array' ||
fieldType?.name === 'array' ||
fieldType?.type === 'array' ||
fieldType?.of !== undefined ||
arrayBasedTypes.includes(fieldType?.name)
) {
return []
}
}
}
} catch (error) {
// If we can't determine from schema, fall back to undefined
console.warn(
'Could not determine field type from schema:',
typeName,
error
)
}
return undefined
},
[schema]
)
const handleDocumentButtonClick = useCallback(
async (event: React.MouseEvent<HTMLButtonElement, MouseEvent>) => {
const languageId = event.currentTarget.value
if (!languageId) {
toast.push({
status: 'error',
title: 'No language selected',
})
return
}
const alreadyTranslated = documentsToTranslation.filter(
(translation) => translation?._key === languageId
)
const removeDuplicates = documentsToTranslation.reduce<
DocumentsToTranslate[]
>((filteredTranslations, translation) => {
if (
alreadyTranslated.filter(
(alreadyTranslation) =>
alreadyTranslation.pathString === translation.pathString
).length > 0
) {
return filteredTranslations
}
const translationAlreadyExists = filteredTranslations.filter(
(filteredTranslation) => filteredTranslation.path === translation.path
)
if (translationAlreadyExists.length > 0) {
return filteredTranslations
}
return [...filteredTranslations, translation]
}, [])
if (removeDuplicates.length === 0) {
toast.push({
status: 'error',
title: 'No internationalizedArray fields found in document root',
})
return
}
// Write a new patch for each empty field
const patches: (FormSetIfMissingPatch | FormInsertPatch)[] = []
for (const toTranslate of removeDuplicates) {
const path = toTranslate.path
// Get the appropriate initial value for this field type
const initialValue = getInitialValueForType(toTranslate._type)
const ifMissing = setIfMissing([], path)
const insertValue = insert(
[
{
_key: languageId,
_type: toTranslate._type,
value: initialValue, // Use the determined initial value instead of undefined
},
],
'after',
[...path, -1]
)
patches.push(ifMissing)
patches.push(insertValue)
}
onChange(PatchEvent.from(patches.flat()))
},
[documentsToTranslation, getInitialValueForType, onChange, toast]
)
return (
<Stack space={3}>
<Box>
<Text size={1} weight="semibold">
Add translation to internationalized fields
</Text>
</Box>
<AddButtons
languages={filteredLanguages}
readOnly={false}
value={undefined}
onClick={handleDocumentButtonClick}
/>
</Stack>
)
}