|
| 1 | +import { useStableCallback } from "@prismicio/editor-support/React"; |
| 2 | +import { |
| 3 | + Box, |
| 4 | + Text, |
| 5 | + TreeView, |
| 6 | + TreeViewCheckbox, |
| 7 | + TreeViewCheckboxProps, |
| 8 | + TreeViewSection, |
| 9 | +} from "@prismicio/editor-ui"; |
| 10 | +import { FormikContext, useField, useFormik } from "formik"; |
| 11 | +import { useEffect } from "react"; |
| 12 | +import { useSelector } from "react-redux"; |
| 13 | + |
| 14 | +import { selectAllCustomTypes } from "@/modules/availableCustomTypes"; |
| 15 | + |
| 16 | +type FieldMap = Record<string, boolean>; |
| 17 | +export type CustomTypeFieldMap = Record<string, FieldMap>; |
| 18 | + |
| 19 | +type CustomTypeField = string | { id: string; fields: string[] }; |
| 20 | + |
| 21 | +interface ContentRelationshipFieldPickerProps { |
| 22 | + initialValues: CustomTypeField[] | undefined; |
| 23 | + onChange: (fields: CustomTypeField[]) => void; |
| 24 | +} |
| 25 | + |
| 26 | +export function ContentRelationshipFieldPicker( |
| 27 | + props: ContentRelationshipFieldPickerProps, |
| 28 | +) { |
| 29 | + const { initialValues, onChange } = props; |
| 30 | + const customTypes = useCustomTypes(); |
| 31 | + |
| 32 | + const stableOnChange = useStableCallback(onChange); |
| 33 | + const form = useFormik<CustomTypeFieldMap>({ |
| 34 | + initialValues: initialValues ? convertCustomTypesToForm(initialValues) : {}, |
| 35 | + onSubmit: () => undefined, // values will be updated on change |
| 36 | + }); |
| 37 | + |
| 38 | + useEffect(() => { |
| 39 | + stableOnChange(convertFormToCustomTypes(form.values)); |
| 40 | + }, [form.values, stableOnChange]); |
| 41 | + |
| 42 | + return ( |
| 43 | + <FormikContext.Provider value={form}> |
| 44 | + <Box overflow="hidden" flexDirection="column" border borderRadius={6}> |
| 45 | + <Box |
| 46 | + border={{ bottom: true }} |
| 47 | + padding={{ inline: 16, bottom: 16, top: 12 }} |
| 48 | + flexDirection="column" |
| 49 | + gap={8} |
| 50 | + > |
| 51 | + <Box flexDirection="column"> |
| 52 | + <Text variant="h4" color="grey12"> |
| 53 | + Types |
| 54 | + </Text> |
| 55 | + <Text color="grey12"> |
| 56 | + Choose which fields you want to expose from the linked document. |
| 57 | + </Text> |
| 58 | + </Box> |
| 59 | + <TreeView |
| 60 | + title="Exposed fields" |
| 61 | + subtitle={`(${countPickedFields(form.values)})`} |
| 62 | + > |
| 63 | + {customTypes.map((ct) => { |
| 64 | + const count = countPickedFields(form.values[ct.id]); |
| 65 | + const countLabel = count === 1 ? "1 field" : `${count} fields`; |
| 66 | + |
| 67 | + return ( |
| 68 | + <TreeViewSection |
| 69 | + key={ct.id} |
| 70 | + title={ct.label} |
| 71 | + subtitle={count > 0 ? `(${countLabel} exposed)` : undefined} |
| 72 | + badge="Custom type" |
| 73 | + > |
| 74 | + {ct.fields.map((field) => ( |
| 75 | + <TreeViewCheckboxField |
| 76 | + key={field.id} |
| 77 | + id={field.id} |
| 78 | + title={field.label} |
| 79 | + customTypeId={ct.id} |
| 80 | + /> |
| 81 | + ))} |
| 82 | + </TreeViewSection> |
| 83 | + ); |
| 84 | + })} |
| 85 | + </TreeView> |
| 86 | + </Box> |
| 87 | + <Box backgroundColor="white" flexDirection="column" padding={12}> |
| 88 | + <Text variant="normal" color="grey11"> |
| 89 | + Have ideas for improving this field?{" "} |
| 90 | + <a |
| 91 | + // TODO: Add real URL: https://linear.app/prismic/issue/DT-2693 |
| 92 | + href="https://community.prismic.io/t/TODO" |
| 93 | + target="_blank" |
| 94 | + rel="noopener noreferrer" |
| 95 | + style={{ color: "inherit", textDecoration: "underline" }} |
| 96 | + > |
| 97 | + Please provide your feedback here. |
| 98 | + </a> |
| 99 | + </Text> |
| 100 | + </Box> |
| 101 | + </Box> |
| 102 | + </FormikContext.Provider> |
| 103 | + ); |
| 104 | +} |
| 105 | + |
| 106 | +function TreeViewCheckboxField( |
| 107 | + props: { |
| 108 | + id: string; |
| 109 | + customTypeId: string; |
| 110 | + } & Omit<TreeViewCheckboxProps, "checked" | "onCheckedChange">, |
| 111 | +) { |
| 112 | + const { id, customTypeId, ...checkboxProps } = props; |
| 113 | + const [field, _, helpers] = useField<boolean>(`${customTypeId}.${id}`); |
| 114 | + |
| 115 | + return ( |
| 116 | + <TreeViewCheckbox |
| 117 | + {...checkboxProps} |
| 118 | + checked={field.value} |
| 119 | + onCheckedChange={(checked) => helpers.setValue(checked)} |
| 120 | + /> |
| 121 | + ); |
| 122 | +} |
| 123 | + |
| 124 | +type SimplifiedCustomTypeField = { |
| 125 | + id: string; |
| 126 | + label: string; |
| 127 | +}; |
| 128 | + |
| 129 | +interface SimplifiedCustomType { |
| 130 | + id: string; |
| 131 | + label: string; |
| 132 | + fields: SimplifiedCustomTypeField[]; |
| 133 | +} |
| 134 | + |
| 135 | +function useCustomTypes() { |
| 136 | + const customTypes = useSelector(selectAllCustomTypes); |
| 137 | + const simplifiedCustomTypes = customTypes.flatMap<SimplifiedCustomType>( |
| 138 | + (customType) => { |
| 139 | + // In the store we have remote and local custom types, we want to show |
| 140 | + // the local ones, so that the user is able to create a content |
| 141 | + // relationship with custom types present on the user's computer (pushed |
| 142 | + // or not). |
| 143 | + if (!("local" in customType)) return []; |
| 144 | + |
| 145 | + const { id, label, tabs } = customType.local; |
| 146 | + |
| 147 | + const fields = tabs.flatMap<SimplifiedCustomTypeField>((tab) => { |
| 148 | + return tab.value.flatMap((field) => { |
| 149 | + // filter out uid fields because it's a special field returned by the |
| 150 | + // API and is not part of the data object in the document. |
| 151 | + if (field.value.type === "UID" || field.key === "uid") { |
| 152 | + return []; |
| 153 | + } |
| 154 | + |
| 155 | + return { |
| 156 | + id: field.key, |
| 157 | + label: field.value.config?.label ?? field.key, |
| 158 | + }; |
| 159 | + }); |
| 160 | + }); |
| 161 | + |
| 162 | + if (fields.length === 0) return []; |
| 163 | + |
| 164 | + return { id, label: label ?? id, fields }; |
| 165 | + }, |
| 166 | + ); |
| 167 | + |
| 168 | + simplifiedCustomTypes.sort((a, b) => a.id.localeCompare(b.id)); |
| 169 | + return simplifiedCustomTypes; |
| 170 | +} |
| 171 | + |
| 172 | +function countPickedFields(fields: CustomTypeFieldMap | FieldMap | undefined) { |
| 173 | + if (!fields) return 0; |
| 174 | + |
| 175 | + return Object.values(fields).reduce<number>( |
| 176 | + (count, value: boolean | FieldMap) => { |
| 177 | + if (typeof value === "boolean" && value) return count + 1; |
| 178 | + return count + Object.values(value).filter(Boolean).length; |
| 179 | + }, |
| 180 | + 0, |
| 181 | + ); |
| 182 | +} |
| 183 | + |
| 184 | +function convertCustomTypesToForm(value: CustomTypeField[]) { |
| 185 | + return value.reduce<CustomTypeFieldMap>((customTypes, customType) => { |
| 186 | + if (typeof customType === "string") { |
| 187 | + customTypes[customType] = {}; |
| 188 | + return customTypes; |
| 189 | + } |
| 190 | + |
| 191 | + const { id, fields } = customType; |
| 192 | + customTypes[id] = fields.reduce<FieldMap>((customTypeFields, field) => { |
| 193 | + customTypeFields[field] = true; |
| 194 | + return customTypeFields; |
| 195 | + }, {}); |
| 196 | + |
| 197 | + return customTypes; |
| 198 | + }, {}); |
| 199 | +} |
| 200 | + |
| 201 | +/** Convert the picked fields map to the customtypes config and filter out empty customtypes */ |
| 202 | +function convertFormToCustomTypes(fields: CustomTypeFieldMap) { |
| 203 | + return Object.entries(fields).flatMap<CustomTypeField>(([ctId, fields]) => { |
| 204 | + const fieldEntries = Object.entries(fields); |
| 205 | + if (!fieldEntries.some(([_, checked]) => checked)) return []; |
| 206 | + return [ |
| 207 | + { |
| 208 | + id: ctId, |
| 209 | + fields: fieldEntries.flatMap(([id, checked]) => (checked ? [id] : [])), |
| 210 | + }, |
| 211 | + ]; |
| 212 | + }); |
| 213 | +} |
0 commit comments