refactor: 收口推荐流展示模型
This commit is contained in:
@@ -33,24 +33,119 @@ export type PlatformPublicCategoryGroup = {
|
||||
entries: PlatformPublicGalleryCard[];
|
||||
};
|
||||
|
||||
export type PlatformRecommendFeedWindow = {
|
||||
activeEntry: PlatformPublicGalleryCard | null;
|
||||
activeEntryKey: string | null;
|
||||
activeIndex: number;
|
||||
nextEntry: PlatformPublicGalleryCard | null;
|
||||
previousEntry: PlatformPublicGalleryCard | null;
|
||||
};
|
||||
|
||||
export function buildPublicGalleryCardKey(entry: PlatformPublicGalleryCard) {
|
||||
return getPlatformPublicGalleryEntryKey(entry);
|
||||
}
|
||||
|
||||
export function dedupePlatformPublicGalleryEntries(
|
||||
entries: PlatformPublicGalleryCard[],
|
||||
) {
|
||||
const entryMap = new Map<string, PlatformPublicGalleryCard>();
|
||||
entries.forEach((entry) => {
|
||||
entryMap.set(buildPublicGalleryCardKey(entry), entry);
|
||||
});
|
||||
|
||||
return Array.from(entryMap.values());
|
||||
}
|
||||
|
||||
export function buildPlatformRecommendFeedEntries(
|
||||
featuredEntries: PlatformPublicGalleryCard[],
|
||||
latestEntries: PlatformPublicGalleryCard[],
|
||||
) {
|
||||
return dedupePlatformPublicGalleryEntries(
|
||||
filterGeneralPublicWorks([...featuredEntries, ...latestEntries]),
|
||||
);
|
||||
}
|
||||
|
||||
export function selectAdjacentPlatformRecommendEntry(
|
||||
entries: PlatformPublicGalleryCard[],
|
||||
direction: 1 | -1,
|
||||
baseEntryKey?: string | null,
|
||||
) {
|
||||
if (entries.length <= 1) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const normalizedBaseEntryKey = baseEntryKey?.trim() ?? '';
|
||||
const activeIndex = normalizedBaseEntryKey
|
||||
? entries.findIndex(
|
||||
(entry) => buildPublicGalleryCardKey(entry) === normalizedBaseEntryKey,
|
||||
)
|
||||
: -1;
|
||||
const baseIndex = activeIndex >= 0 ? activeIndex : 0;
|
||||
const nextIndex =
|
||||
(baseIndex + direction + entries.length) % entries.length;
|
||||
const nextEntry = entries[nextIndex] ?? null;
|
||||
if (
|
||||
nextEntry &&
|
||||
normalizedBaseEntryKey &&
|
||||
buildPublicGalleryCardKey(nextEntry) === normalizedBaseEntryKey
|
||||
) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return nextEntry;
|
||||
}
|
||||
|
||||
export function selectPlatformRecommendFeedWindow(
|
||||
entries: PlatformPublicGalleryCard[],
|
||||
activeEntryKey?: string | null,
|
||||
): PlatformRecommendFeedWindow {
|
||||
const normalizedActiveEntryKey = activeEntryKey?.trim() ?? '';
|
||||
const activeEntry =
|
||||
(normalizedActiveEntryKey
|
||||
? entries.find(
|
||||
(entry) =>
|
||||
buildPublicGalleryCardKey(entry) === normalizedActiveEntryKey,
|
||||
)
|
||||
: null) ??
|
||||
entries[0] ??
|
||||
null;
|
||||
const selectedActiveEntryKey = activeEntry
|
||||
? buildPublicGalleryCardKey(activeEntry)
|
||||
: null;
|
||||
const activeIndex = selectedActiveEntryKey
|
||||
? entries.findIndex(
|
||||
(entry) => buildPublicGalleryCardKey(entry) === selectedActiveEntryKey,
|
||||
)
|
||||
: -1;
|
||||
|
||||
return {
|
||||
activeEntry,
|
||||
activeEntryKey: selectedActiveEntryKey,
|
||||
activeIndex,
|
||||
nextEntry: selectAdjacentPlatformRecommendEntry(
|
||||
entries,
|
||||
1,
|
||||
selectedActiveEntryKey,
|
||||
),
|
||||
previousEntry: selectAdjacentPlatformRecommendEntry(
|
||||
entries,
|
||||
-1,
|
||||
selectedActiveEntryKey,
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
export function buildPublicCategoryGroups(
|
||||
featuredEntries: PlatformPublicGalleryCard[],
|
||||
latestEntries: PlatformPublicGalleryCard[],
|
||||
): PlatformPublicCategoryGroup[] {
|
||||
const publicEntryMap = new Map<string, PlatformPublicGalleryCard>();
|
||||
|
||||
filterGeneralPublicWorks([...featuredEntries, ...latestEntries]).forEach(
|
||||
(entry) => {
|
||||
publicEntryMap.set(buildPublicGalleryCardKey(entry), entry);
|
||||
},
|
||||
const publicEntries = buildPlatformRecommendFeedEntries(
|
||||
featuredEntries,
|
||||
latestEntries,
|
||||
);
|
||||
|
||||
const categoryMap = new Map<string, PlatformPublicGalleryCard[]>();
|
||||
Array.from(publicEntryMap.values()).forEach((entry) => {
|
||||
publicEntries.forEach((entry) => {
|
||||
const tags = buildPlatformWorldDisplayTags(entry, 3);
|
||||
const normalizedTags = tags.length > 0 ? tags : ['回响'];
|
||||
|
||||
@@ -76,28 +171,17 @@ export function getPlatformPublicEntries(
|
||||
featuredEntries: PlatformPublicGalleryCard[],
|
||||
latestEntries: PlatformPublicGalleryCard[],
|
||||
) {
|
||||
const entryMap = new Map<string, PlatformPublicGalleryCard>();
|
||||
|
||||
filterGeneralPublicWorks([...featuredEntries, ...latestEntries]).forEach(
|
||||
(entry) => {
|
||||
entryMap.set(buildPublicGalleryCardKey(entry), entry);
|
||||
},
|
||||
);
|
||||
|
||||
return Array.from(entryMap.values());
|
||||
return buildPlatformRecommendFeedEntries(featuredEntries, latestEntries);
|
||||
}
|
||||
|
||||
export function getAllPlatformPublicEntries(
|
||||
featuredEntries: PlatformPublicGalleryCard[],
|
||||
latestEntries: PlatformPublicGalleryCard[],
|
||||
) {
|
||||
const entryMap = new Map<string, PlatformPublicGalleryCard>();
|
||||
|
||||
[...featuredEntries, ...latestEntries].forEach((entry) => {
|
||||
entryMap.set(buildPublicGalleryCardKey(entry), entry);
|
||||
});
|
||||
|
||||
return Array.from(entryMap.values());
|
||||
return dedupePlatformPublicGalleryEntries([
|
||||
...featuredEntries,
|
||||
...latestEntries,
|
||||
]);
|
||||
}
|
||||
|
||||
function normalizePlatformSearchText(value: string | null | undefined) {
|
||||
|
||||
Reference in New Issue
Block a user