73 lines
2.4 KiB
TypeScript
73 lines
2.4 KiB
TypeScript
import React, { useMemo } from "react";
|
|
import { asString } from "../helpers/value";
|
|
import { CollectionScaffold } from "../shared/CollectionScaffold";
|
|
import { StandardIndexCard } from "../shared/StandardIndexCard";
|
|
import type {
|
|
CollectionItemRendererProps,
|
|
CollectionRendererProps,
|
|
SearchResultItem,
|
|
} from "../../types/renderer.types";
|
|
|
|
function sortWithExtraPointsAfterDu(items: SearchResultItem[]): SearchResultItem[] {
|
|
const isExtra = (item: SearchResultItem) => {
|
|
const name = (asString(item.attributes.name) || item.title || "").toLowerCase();
|
|
const code = (asString(item.attributes.code) || item.slug || "").toLowerCase();
|
|
return name.includes("extra") || code.includes("extra");
|
|
};
|
|
|
|
const extraItems = items.filter(isExtra);
|
|
const regularItems = items.filter((item) => !isExtra(item));
|
|
|
|
regularItems.sort((a, b) => {
|
|
const aOrder =
|
|
typeof a.attributes.sort_order === "number"
|
|
? a.attributes.sort_order
|
|
: 999;
|
|
const bOrder =
|
|
typeof b.attributes.sort_order === "number"
|
|
? b.attributes.sort_order
|
|
: 999;
|
|
return aOrder - bOrder;
|
|
});
|
|
|
|
return [...regularItems, ...extraItems];
|
|
}
|
|
|
|
export function AcuPointIndexCollectionRenderer(props: CollectionRendererProps) {
|
|
const sortedItems = useMemo(
|
|
() => sortWithExtraPointsAfterDu(props.items),
|
|
[props.items],
|
|
);
|
|
return <CollectionScaffold {...props} items={sortedItems} />;
|
|
}
|
|
export function AcuPointCollectionRenderer(props: CollectionRendererProps) {
|
|
return <CollectionScaffold {...props} />;
|
|
}
|
|
export function AcuPointIndexCardRenderer({ item, onPress }: CollectionItemRendererProps) {
|
|
const a = item.attributes;
|
|
const altNames = [asString(a.alt_name_1), asString(a.alt_name_2)].filter(Boolean).join(" · ");
|
|
return (
|
|
<StandardIndexCard
|
|
title={asString(a.point_name) || asString(a.name) || item.title}
|
|
subtitle={altNames || null}
|
|
onPress={onPress}
|
|
/>
|
|
);
|
|
}
|
|
export function AcuPointCardRenderer({ item, onPress }: CollectionItemRendererProps) {
|
|
const a = item.attributes;
|
|
const altNames = [asString(a.alt_name_1), asString(a.alt_name_2)].filter(Boolean).join(" · ");
|
|
const subtitle =
|
|
altNames ||
|
|
asString(a.pt_meaning) ||
|
|
asString(a.channel);
|
|
|
|
return (
|
|
<StandardIndexCard
|
|
title={asString(a.point_name) || asString(a.name) || item.title}
|
|
subtitle={subtitle || null}
|
|
onPress={onPress}
|
|
/>
|
|
);
|
|
}
|