feat: Add reordering to starred documents (#2953)

* draft

* reordering

* JIT Index stars on first load

* test

* Remove unused code on client

* small unrefactor
This commit is contained in:
Tom Moor
2022-01-21 18:11:50 -08:00
committed by GitHub
parent 49533d7a3f
commit 79e2cad5b9
32 changed files with 931 additions and 132 deletions

View File

@@ -1,44 +0,0 @@
import fractionalIndex from "fractional-index";
import naturalSort from "@shared/utils/naturalSort";
import { Collection } from "@server/models";
export default async function collectionIndexing(teamId: string) {
const collections = await Collection.findAll({
where: {
teamId,
deletedAt: null,
},
//no point in maintaining index of deleted collections.
attributes: ["id", "index", "name"],
});
let sortableCollections: [Collection, string | null][] = collections.map(
(collection) => {
return [collection, collection.index];
}
);
sortableCollections = naturalSort(
sortableCollections,
(collection) => collection[0].name
);
//for each collection with null index, use previous collection index to create new index
let previousCollectionIndex = null;
for (const collection of sortableCollections) {
if (collection[1] === null) {
const index = fractionalIndex(previousCollectionIndex, collection[1]);
collection[0].index = index;
await collection[0].save();
}
previousCollectionIndex = collection[0].index;
}
const indexedCollections = {};
sortableCollections.forEach((collection) => {
indexedCollections[collection[0].id] = collection[0].index;
});
return indexedCollections;
}

82
server/utils/indexing.ts Normal file
View File

@@ -0,0 +1,82 @@
import fractionalIndex from "fractional-index";
import naturalSort from "@shared/utils/naturalSort";
import { Collection, Document, Star } from "@server/models";
export async function collectionIndexing(
teamId: string
): Promise<{ [id: string]: string }> {
const collections = await Collection.findAll({
where: {
teamId,
// no point in maintaining index of deleted collections.
deletedAt: null,
},
attributes: ["id", "index", "name"],
});
const sortable = naturalSort(collections, (collection) => collection.name);
// for each collection with null index, use previous collection index to create new index
let previousIndex = null;
const promises = [];
for (const collection of sortable) {
if (collection.index === null) {
collection.index = fractionalIndex(previousIndex, null);
promises.push(collection.save());
}
previousIndex = collection.index;
}
await Promise.all(promises);
const indexedCollections = {};
sortable.forEach((collection) => {
indexedCollections[collection.id] = collection.index;
});
return indexedCollections;
}
export async function starIndexing(
userId: string
): Promise<{ [id: string]: string }> {
const stars = await Star.findAll({
where: { userId },
});
const documents = await Document.findAll({
attributes: ["id", "updatedAt"],
where: {
id: stars.map((star) => star.documentId),
},
order: [["updatedAt", "DESC"]],
});
const sortable = stars.sort(function (a, b) {
return (
documents.findIndex((d) => d.id === a.documentId) -
documents.findIndex((d) => d.id === b.documentId)
);
});
let previousIndex = null;
const promises = [];
for (const star of sortable) {
if (star.index === null) {
star.index = fractionalIndex(previousIndex, null);
promises.push(star.save());
}
previousIndex = star.index;
}
await Promise.all(promises);
const indexedStars = {};
sortable.forEach((star) => {
indexedStars[star.id] = star.index;
});
return indexedStars;
}