core: fix sorting of newly created notes

This commit is contained in:
Abdullah Atta
2024-02-17 21:30:31 +05:00
parent 6b14b29103
commit 72d5072cac

View File

@@ -51,7 +51,7 @@ const formats = {
year: "%Y", year: "%Y",
week: "%Y-%W", week: "%Y-%W",
abc: null, abc: null,
default: null, default: "%Y-%W",
none: null none: null
} satisfies Record<GroupOptions["groupBy"], string | null>; } satisfies Record<GroupOptions["groupBy"], string | null>;
@@ -505,9 +505,10 @@ export class FilteredSelector<T extends Item> {
return <T>( return <T>(
qb: SelectQueryBuilder<DatabaseSchema, keyof DatabaseSchema, T> qb: SelectQueryBuilder<DatabaseSchema, keyof DatabaseSchema, T>
) => { ) => {
if (this.type === "notes") qb = qb.orderBy("conflicted desc"); if (this.type === "notes")
qb = qb.orderBy(sql`IFNULL(conflicted, 0) desc`);
if (this.type === "notes" || this.type === "notebooks") if (this.type === "notes" || this.type === "notebooks")
qb = qb.orderBy("pinned desc"); qb = qb.orderBy(sql`IFNULL(pinned, 0) desc`);
for (const item of sortBy) { for (const item of sortBy) {
if (item === "title") { if (item === "title") {
@@ -521,7 +522,7 @@ export class FilteredSelector<T extends Item> {
const timeFormat = isGroupOptions(options) const timeFormat = isGroupOptions(options)
? formats[options.groupBy] ? formats[options.groupBy]
: null; : null;
if (!timeFormat) { if (!timeFormat || isSortByDate(options)) {
qb = qb.orderBy(item, options.sortDirection); qb = qb.orderBy(item, options.sortDirection);
continue; continue;
} }
@@ -559,3 +560,13 @@ function isGroupOptions(
): options is GroupOptions { ): options is GroupOptions {
return "groupBy" in options; return "groupBy" in options;
} }
function isSortByDate(options: SortOptions | GroupOptions) {
return (
options.sortBy === "dateCreated" ||
options.sortBy === "dateEdited" ||
options.sortBy === "dateDeleted" ||
options.sortBy === "dateModified" ||
options.sortBy === "dateUploaded"
);
}