|
| 1 | +'use client' |
| 2 | + |
| 3 | +import type { Where } from 'payload' |
| 4 | + |
| 5 | +import { |
| 6 | + RadioGroupField, |
| 7 | + useDocumentInfo, |
| 8 | + useField, |
| 9 | + useListQuery, |
| 10 | + useSelection, |
| 11 | + useTranslation, |
| 12 | +} from '@payloadcms/ui' |
| 13 | +import React, { useEffect, useMemo } from 'react' |
| 14 | + |
| 15 | +const isWhereEmpty = (where: Where): boolean => { |
| 16 | + if (!where || typeof where !== 'object') { |
| 17 | + return true |
| 18 | + } |
| 19 | + |
| 20 | + // Flatten one level of OR/AND wrappers |
| 21 | + if (Array.isArray(where.and)) { |
| 22 | + return where.and.length === 0 |
| 23 | + } |
| 24 | + if (Array.isArray(where.or)) { |
| 25 | + return where.or.length === 0 |
| 26 | + } |
| 27 | + |
| 28 | + return Object.keys(where).length === 0 |
| 29 | +} |
| 30 | + |
| 31 | +export const SelectionToUseField: React.FC = () => { |
| 32 | + const { id } = useDocumentInfo() |
| 33 | + const { query } = useListQuery() |
| 34 | + const { selectAll, selected } = useSelection() |
| 35 | + const { t } = useTranslation() |
| 36 | + |
| 37 | + const { setValue: setSelectionToUseValue, value: selectionToUseValue } = useField({ |
| 38 | + path: 'selectionToUse', |
| 39 | + }) |
| 40 | + |
| 41 | + const { setValue: setWhere } = useField({ |
| 42 | + path: 'where', |
| 43 | + }) |
| 44 | + |
| 45 | + const hasMeaningfulFilters = query?.where && !isWhereEmpty(query.where) |
| 46 | + |
| 47 | + const availableOptions = useMemo(() => { |
| 48 | + const options = [ |
| 49 | + { |
| 50 | + // @ts-expect-error - this is not correctly typed in plugins right now |
| 51 | + label: t('plugin-import-export:selectionToUse-allDocuments'), |
| 52 | + value: 'all', |
| 53 | + }, |
| 54 | + ] |
| 55 | + |
| 56 | + if (hasMeaningfulFilters) { |
| 57 | + options.unshift({ |
| 58 | + // @ts-expect-error - this is not correctly typed in plugins right now |
| 59 | + label: t('plugin-import-export:selectionToUse-currentFilters'), |
| 60 | + value: 'currentFilters', |
| 61 | + }) |
| 62 | + } |
| 63 | + |
| 64 | + if (['allInPage', 'some'].includes(selectAll)) { |
| 65 | + options.unshift({ |
| 66 | + // @ts-expect-error - this is not correctly typed in plugins right now |
| 67 | + label: t('plugin-import-export:selectionToUse-currentSelection'), |
| 68 | + value: 'currentSelection', |
| 69 | + }) |
| 70 | + } |
| 71 | + |
| 72 | + return options |
| 73 | + }, [hasMeaningfulFilters, selectAll, t]) |
| 74 | + |
| 75 | + // Auto-set default |
| 76 | + useEffect(() => { |
| 77 | + if (id) { |
| 78 | + return |
| 79 | + } |
| 80 | + |
| 81 | + let defaultSelection: 'all' | 'currentFilters' | 'currentSelection' = 'all' |
| 82 | + |
| 83 | + if (['allInPage', 'some'].includes(selectAll)) { |
| 84 | + defaultSelection = 'currentSelection' |
| 85 | + } else if (query?.where) { |
| 86 | + defaultSelection = 'currentFilters' |
| 87 | + } |
| 88 | + |
| 89 | + setSelectionToUseValue(defaultSelection) |
| 90 | + }, [id, selectAll, query?.where, setSelectionToUseValue]) |
| 91 | + |
| 92 | + // Sync where clause with selected option |
| 93 | + useEffect(() => { |
| 94 | + if (id) { |
| 95 | + return |
| 96 | + } |
| 97 | + |
| 98 | + if (selectionToUseValue === 'currentFilters' && query?.where) { |
| 99 | + setWhere(query.where) |
| 100 | + } else if (selectionToUseValue === 'currentSelection' && selected) { |
| 101 | + const ids = [...selected.entries()].filter(([_, isSelected]) => isSelected).map(([id]) => id) |
| 102 | + |
| 103 | + setWhere({ id: { in: ids } }) |
| 104 | + } else if (selectionToUseValue === 'all') { |
| 105 | + setWhere({}) |
| 106 | + } |
| 107 | + }, [id, selectionToUseValue, query?.where, selected, setWhere]) |
| 108 | + |
| 109 | + // Hide component if no other options besides "all" are available |
| 110 | + if (availableOptions.length <= 1) { |
| 111 | + return null |
| 112 | + } |
| 113 | + |
| 114 | + return ( |
| 115 | + <RadioGroupField |
| 116 | + field={{ |
| 117 | + name: 'selectionToUse', |
| 118 | + type: 'radio', |
| 119 | + admin: {}, |
| 120 | + // @ts-expect-error - this is not correctly typed in plugins right now |
| 121 | + label: t('plugin-import-export:field-selectionToUse-label'), |
| 122 | + options: availableOptions, |
| 123 | + }} |
| 124 | + // @ts-expect-error - this is not correctly typed in plugins right now |
| 125 | + label={t('plugin-import-export:field-selectionToUse-label')} |
| 126 | + options={availableOptions} |
| 127 | + path="selectionToUse" |
| 128 | + /> |
| 129 | + ) |
| 130 | +} |
0 commit comments