70 lines
2.4 KiB
TypeScript
70 lines
2.4 KiB
TypeScript
import React, { useMemo } from "react";
|
|
import { Pressable, Text, View } from "react-native";
|
|
import { asNumber, asString, asStringArray } 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 MeridianIndexCollectionRenderer(props: CollectionRendererProps) {
|
|
const sortedItems = useMemo(
|
|
() => sortWithExtraPointsAfterDu(props.items),
|
|
[props.items],
|
|
);
|
|
return <CollectionScaffold {...props} items={sortedItems} />;
|
|
}
|
|
export function MeridianCollectionRenderer(props: CollectionRendererProps) {
|
|
return <CollectionScaffold {...props} />;
|
|
}
|
|
export function MeridianIndexCardRenderer({ item, onPress }: CollectionItemRendererProps) {
|
|
const a = item.attributes;
|
|
return (
|
|
<StandardIndexCard
|
|
title={asString(a.name) || item.title}
|
|
count={asNumber(a.content_count)}
|
|
countLabel={asString(a.content_label) || null}
|
|
onPress={onPress}
|
|
/>
|
|
);
|
|
}
|
|
export function MeridianCardRenderer({ item, onPress }: CollectionItemRendererProps) {
|
|
const a = item.attributes;
|
|
return (
|
|
<Pressable onPress={onPress}>
|
|
<View style={{ padding: 14, gap: 5 }}>
|
|
<Text style={{ fontSize: 17, fontWeight: "700" }}>{asString(a.title) || item.title}</Text>
|
|
<Text>{asString(a.name)}</Text>
|
|
<Text>{asStringArray(a.point_codes).length} referenced points</Text>
|
|
</View>
|
|
</Pressable>
|
|
);
|
|
}
|