Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow sorting learning resources with a priority #65

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 18 additions & 3 deletions src/hooks/useQuickStarts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,23 @@ export type FavoriteQuickStart = {
quickstartName: string;
};

const sortFnc = (q1: QuickStart, q2: QuickStart) =>
q1.spec.displayName.localeCompare(q2.spec.displayName);
const DEFAULT_PRIORITY = 1000;

const makeSortFn =
(targetBundle?: string) => (q1: QuickStart, q2: QuickStart) => {
if (targetBundle !== undefined) {
const priority1 =
q1.metadata.bundle_priority?.[targetBundle] ?? DEFAULT_PRIORITY;
const priority2 =
q2.metadata.bundle_priority?.[targetBundle] ?? DEFAULT_PRIORITY;

// A lower priority value makes the quickstart appear earlier in the list.
if (priority1 < priority2) return -1;
if (priority1 > priority2) return 1;
}

return q1.spec.displayName.localeCompare(q2.spec.displayName);
};

function isFavorite(quickStart: QuickStart, favorites: FavoriteQuickStart[]) {
return !!favorites.find((f) => f.quickstartName === quickStart.metadata.name);
Expand All @@ -41,7 +56,7 @@ const useQuickStarts = (targetBundle?: string) => {
filter?.keyword || '',
filter?.status?.statusFilters,
allQuickStartStates || {}
).sort(sortFnc);
).sort(makeSortFn(targetBundle));
return filteredQuickStarts.reduce<{
bookmarks: QuickStart[];
documentation: QuickStart[];
Expand Down