diff --git a/studio/schemas/documents/compensations.ts b/studio/schemas/documents/compensations.ts index 3d58f926e..89de74740 100644 --- a/studio/schemas/documents/compensations.ts +++ b/studio/schemas/documents/compensations.ts @@ -5,6 +5,9 @@ import offices from "../objects/offices"; import { title } from "../fields/text"; import { benefitId } from "./benefit"; +// smallest number of offices where ellipsis (...) will be used in preview +const OFFICES_PREVIEW_ELLIPSIS_LIMIT = 5; + export const compensationsId = "compensations"; const compensations = defineType({ @@ -33,21 +36,22 @@ const compensations = defineType({ ], preview: { select: { - // TODO: improve array handling - // https://www.sanity.io/docs/previews-list-views#62febb15a63a - office0: "offices.0.basicTitle", - office1: "offices.1.basicTitle", - office2: "offices.2.basicTitle", subtitle: "basicTitle", + // https://www.sanity.io/docs/previews-list-views#62febb15a63a + ...[...Array(OFFICES_PREVIEW_ELLIPSIS_LIMIT).keys()].reduce( + (o, i) => ({ ...o, [`office${i}`]: `offices.${i}.basicTitle` }), + {}, + ), }, - prepare({ office0, office1, office2, subtitle }) { - let title = office0; - if (office1 !== undefined) { - if (office2 !== undefined) { - title = `${office0} & ${office1} ++`; - } else { - title = `${office0} & ${office1}`; - } + prepare({ subtitle, ...officesMap }) { + const offices = Object.values(officesMap).filter( + (o) => o !== undefined, + ); + let title; + if (offices.length === OFFICES_PREVIEW_ELLIPSIS_LIMIT) { + title = offices.toSpliced(-1).join(", ") + ", ..."; + } else { + title = offices.join(", "); } return { title,