Skip to content

Commit

Permalink
feat(compensations): improved offices array preview
Browse files Browse the repository at this point in the history
  • Loading branch information
mathiazom committed Aug 30, 2024
1 parent a07c54e commit 84879a3
Showing 1 changed file with 17 additions and 13 deletions.
30 changes: 17 additions & 13 deletions studio/schemas/documents/compensations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = 3;

export const compensationsId = "compensations";

const compensations = defineType({
Expand Down Expand Up @@ -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<string>(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,
Expand Down

0 comments on commit 84879a3

Please sign in to comment.