feat: sort by rating by default
This commit is contained in:
parent
78b1699152
commit
517b1ba23d
42
lib/crud.ts
42
lib/crud.ts
@ -6,18 +6,10 @@ import {
|
||||
} from "@lib/documents.ts";
|
||||
import { Root } from "https://esm.sh/remark-frontmatter@4.0.1";
|
||||
import { getThumbhash } from "@lib/cache/image.ts";
|
||||
import { GenericResource } from "@lib/types.ts";
|
||||
import { parseRating } from "@lib/helpers.ts";
|
||||
|
||||
type Resource = {
|
||||
name: string;
|
||||
id: string;
|
||||
meta: {
|
||||
image?: string;
|
||||
author?: string;
|
||||
thumbnail?: string;
|
||||
};
|
||||
};
|
||||
|
||||
export async function addThumbnailToResource<T = Resource>(
|
||||
export async function addThumbnailToResource<T = GenericResource>(
|
||||
res: T,
|
||||
): Promise<T> {
|
||||
const imageUrl = res?.meta?.image;
|
||||
@ -34,7 +26,27 @@ export async function addThumbnailToResource<T = Resource>(
|
||||
};
|
||||
}
|
||||
|
||||
export function createCrud<T>(
|
||||
type SortType = "rating" | "date" | "name" | "author";
|
||||
|
||||
function sortFunction<T extends GenericResource>(sortType: SortType) {
|
||||
return (a: T, b: T) => {
|
||||
switch (sortType) {
|
||||
case "rating":
|
||||
return parseRating(a.meta?.rating || 0) >
|
||||
parseRating(b.meta?.rating || 0)
|
||||
? -1
|
||||
: 1;
|
||||
case "date":
|
||||
return (a.meta?.date || 0) > (b.meta?.date || 0) ? -1 : 1;
|
||||
case "name":
|
||||
return a.name.localeCompare(b.name);
|
||||
case "author":
|
||||
return a.meta?.author?.localeCompare(b.meta?.author || "");
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
export function createCrud<T extends GenericResource>(
|
||||
{ prefix, parse, render, hasThumbnails = false }: {
|
||||
prefix: string;
|
||||
hasThumbnails?: boolean;
|
||||
@ -80,9 +92,9 @@ export function createCrud<T>(
|
||||
await createDocument(path, newDoc);
|
||||
}
|
||||
|
||||
async function readAll() {
|
||||
async function readAll({ sort = "rating" }: { sort?: SortType } = {}) {
|
||||
const allDocuments = await getDocuments();
|
||||
return Promise.all(
|
||||
return (await Promise.all(
|
||||
allDocuments.filter((d) => {
|
||||
return d.name.startsWith(prefix) &&
|
||||
d.contentType === "text/markdown" &&
|
||||
@ -91,7 +103,7 @@ export function createCrud<T>(
|
||||
const id = doc.name.replace(prefix, "").replace(/\.md$/, "");
|
||||
return read(id);
|
||||
}),
|
||||
);
|
||||
)).sort(sortFunction<T>(sort));
|
||||
}
|
||||
|
||||
return {
|
||||
|
@ -88,3 +88,10 @@ export function debounce<T extends (...args: Parameters<T>) => void>(
|
||||
timer = setTimeout(() => fn.apply(this, args), delay);
|
||||
};
|
||||
}
|
||||
|
||||
export function parseRating(rating: string | number) {
|
||||
if (typeof rating === "string") {
|
||||
return [...rating.matchAll(/⭐/)].length;
|
||||
}
|
||||
return rating;
|
||||
}
|
||||
|
@ -43,6 +43,7 @@ export type GenericResource = {
|
||||
author?: string;
|
||||
rating?: number;
|
||||
average?: string;
|
||||
date?: Date | string;
|
||||
thumbnail?: string;
|
||||
};
|
||||
};
|
||||
|
Loading…
x
Reference in New Issue
Block a user