Skip to content

Commit

Permalink
Refactor preference value retrieval for sorting object mapping
Browse files Browse the repository at this point in the history
  • Loading branch information
jmetrikat committed Feb 22, 2025
1 parent 12f52b6 commit 5d8cdbe
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 22 deletions.
4 changes: 2 additions & 2 deletions src/helpers/object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ export function processObject(
layout: string;
isPinned: boolean;
} {
const dateToSortAfter = getPreferenceValues().sort;
const date = object.details.find((detail) => detail.id === dateToSortAfter)?.details.date as string;
const { sort } = getPreferenceValues();
const date = object.details.find((detail) => detail.id === sort)?.details.date as string;
const hasValidDate = date && new Date(date).getTime() !== 0;

return {
Expand Down
4 changes: 2 additions & 2 deletions src/helpers/strings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export function pluralize(
* Get the label for the date field based on the sort preference.
*/
export function getDateLabel(): string | undefined {
const sort = getPreferenceValues().sort;
const { sort } = getPreferenceValues();
switch (sort) {
case "created_date":
return "Created Date";
Expand All @@ -33,7 +33,7 @@ export function getDateLabel(): string | undefined {
* Get the short date label based on the sort preference.
*/
export function getShortDateLabel(): string {
const sort = getPreferenceValues().sort;
const { sort } = getPreferenceValues();
switch (sort) {
case "created_date":
return "Created";
Expand Down
38 changes: 20 additions & 18 deletions src/mappers/objects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,26 @@ import { getPreferenceValues } from "@raycast/api";
import { getIconWithFallback } from "../helpers/icon";
import { Member, SpaceObject } from "../helpers/schemas";

/**
* Efficiently map raw `SpaceObject` items to essential display-ready data.
* Only includes necessary fields for list rendering for performance.
*/
export async function mapObjects(objects: SpaceObject[]): Promise<SpaceObject[]> {
const { sort } = getPreferenceValues();

return Promise.all(
objects.map(async (object) => {
return {
...object,
icon: await getIconWithFallback(object.icon, object.layout),
name: object.name || object.snippet || "Untitled",
type: object.type || "Unknown Type",
details: object.details?.filter((detail) => detail.id === sort) || [],
};
}),
);
}

/**
* Map raw `SpaceObject` item into display-ready data, including details, icons, etc.
*/
Expand Down Expand Up @@ -86,21 +106,3 @@ export async function mapObject(object: SpaceObject): Promise<SpaceObject> {
details: mappedDetails,
};
}

/**
* Efficiently map raw `SpaceObject` items to essential display-ready data.
* Only includes necessary fields for list rendering for performance.
*/
export async function mapObjects(objects: SpaceObject[]): Promise<SpaceObject[]> {
return Promise.all(
objects.map(async (object) => {
return {
...object,
icon: await getIconWithFallback(object.icon, object.layout),
name: object.name || object.snippet || "Untitled",
type: object.type || "Unknown Type",
details: object.details?.filter((detail) => detail.id === getPreferenceValues().sort) || [],
};
}),
);
}

0 comments on commit 5d8cdbe

Please sign in to comment.