Skip to content

Commit

Permalink
chore(plugin-search): improves types (#10955)
Browse files Browse the repository at this point in the history
There were a number of areas within the Search Plugin where typings
could have been improved, namely:
- The `customProps` sent to the `ReindexButton`. This now uses the
`satisfies` keyword to ensure type strictness.
- The `collectionLabels` prop sent to the `ReindexButtonClient`
component. This is now standardized behind a new
`ResolvedCollectionLabels` type to closely reflect `CollectionLabels`.
This was also converted from unnecessarily invoking a function to being
a basic object.
- The `locale` type sent through `SyncDocArgs`. This now uses
`Locale['code']` from Payload.
  • Loading branch information
jacobsfletch authored Feb 3, 2025
1 parent 0a1cc6a commit 1771271
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 28 deletions.
3 changes: 2 additions & 1 deletion packages/plugin-search/src/Search/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { CollectionConfig, Field } from 'payload'

import type { SearchPluginConfigWithLocales } from '../types.js'
import type { ReindexButtonServerProps } from './ui/ReindexButton/types.js'

import { generateReindexHandler } from '../utilities/generateReindexHandler.js'

Expand Down Expand Up @@ -73,7 +74,7 @@ export const generateSearchCollection = (
collectionLabels,
searchCollections,
searchSlug,
},
} satisfies ReindexButtonServerProps,
},
],
},
Expand Down
31 changes: 15 additions & 16 deletions packages/plugin-search/src/Search/ui/ReindexButton/index.tsx
Original file line number Diff line number Diff line change
@@ -1,32 +1,31 @@
import type { ResolvedCollectionLabels } from '../../../types.js'
import type { SearchReindexButtonServerComponent } from './types.js'

import { ReindexButtonClient } from './index.client.js'

export const ReindexButton: SearchReindexButtonServerComponent = (props) => {
const { collectionLabels, i18n, searchCollections, searchSlug } = props

const getStaticLocalizedPluralLabels = () => {
return Object.fromEntries(
searchCollections.map((collection) => {
const labels = collectionLabels[collection]
const pluralLabel = labels?.plural
const resolvedCollectionLabels: ResolvedCollectionLabels = Object.fromEntries(
searchCollections.map((collection) => {
const labels = collectionLabels[collection]
const pluralLabel = labels?.plural

if (typeof pluralLabel === 'function') {
return [collection, pluralLabel({ t: i18n.t })]
}
if (typeof pluralLabel === 'function') {
return [collection, pluralLabel({ t: i18n.t })]
}

if (pluralLabel) {
return [collection, pluralLabel]
}
if (pluralLabel) {
return [collection, pluralLabel]
}

return [collection, collection]
}),
)
}
return [collection, collection]
}),
)

return (
<ReindexButtonClient
collectionLabels={getStaticLocalizedPluralLabels()}
collectionLabels={resolvedCollectionLabels}
searchCollections={searchCollections}
searchSlug={searchSlug}
/>
Expand Down
11 changes: 6 additions & 5 deletions packages/plugin-search/src/Search/ui/ReindexButton/types.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
import type { CustomComponent, PayloadServerReactComponent, StaticLabel } from 'payload'
import type { CustomComponent, PayloadServerReactComponent } from 'payload'

import type { CollectionLabels } from '../../../types.js'
import type { CollectionLabels, ResolvedCollectionLabels } from '../../../types.js'

export type ReindexButtonProps = {
collectionLabels: Record<string, StaticLabel>
collectionLabels: ResolvedCollectionLabels
searchCollections: string[]
searchSlug: string
}

type ReindexButtonServerProps = {
export type ReindexButtonServerProps = {
collectionLabels: CollectionLabels
} & ReindexButtonProps
} & Omit<ReindexButtonProps, 'collectionLabels'>

export type SearchReindexButtonClientComponent = ReindexButtonProps

export type SearchReindexButtonServerComponent = PayloadServerReactComponent<
CustomComponent<ReindexButtonServerProps>
>
12 changes: 6 additions & 6 deletions packages/plugin-search/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import type {
CollectionAfterDeleteHook,
CollectionConfig,
Field,
LabelFunction,
Locale,
Payload,
PayloadRequest,
Expand Down Expand Up @@ -51,10 +50,11 @@ export type SearchPluginConfig = {
}

export type CollectionLabels = {
[collection: string]: {
plural?: LabelFunction | StaticLabel
singular?: LabelFunction | StaticLabel
}
[collection: string]: CollectionConfig['labels']
}

export type ResolvedCollectionLabels = {
[collection: string]: StaticLabel
}

export type SearchPluginConfigWithLocales = {
Expand All @@ -68,7 +68,7 @@ export type SyncWithSearchArgs = {
} & Omit<Parameters<CollectionAfterChangeHook>[0], 'collection'>

export type SyncDocArgs = {
locale?: string
locale?: Locale['code']
onSyncError?: () => void
} & Omit<SyncWithSearchArgs, 'context' | 'previousDoc'>

Expand Down

0 comments on commit 1771271

Please sign in to comment.