feat: Add ability to star collection (#3327)

* Migrations, models, commands

* ui

* Move starred hint to location state

* lint

* tsc

* refactor

* Add collection empty state in expanded sidebar

* Add empty placeholder within starred collections

* Drag and drop improves, Relative refactor

* fix: Starring untitled draft leaves empty space

* fix: Creating draft in starred collection shouldnt open main

* fix: Dupe drop cursor

* Final fixes

* fix: Canonical redirect replaces starred location state

* fix: Don't show reorder cursor at the top of collection with no permission to edit when dragging
This commit is contained in:
Tom Moor
2022-04-03 18:51:01 -07:00
committed by GitHub
parent 3de06b8005
commit 84d6bf8ddf
36 changed files with 988 additions and 635 deletions

View File

@@ -1,17 +1,19 @@
import fractionalIndex from "fractional-index";
import { Sequelize, WhereOptions } from "sequelize";
import { sequelize } from "@server/database/sequelize";
import { Sequelize, Transaction, WhereOptions } from "sequelize";
import { Star, User, Event } from "@server/models";
type Props = {
/** The user creating the star */
user: User;
/** The document to star */
documentId: string;
documentId?: string;
/** The collection to star */
collectionId?: string;
/** The sorted index for the star in the sidebar If no index is provided then it will be at the end */
index?: string;
/** The IP address of the user creating the star */
ip: string;
transaction: Transaction;
};
/**
@@ -24,7 +26,9 @@ type Props = {
export default async function starCreator({
user,
documentId,
collectionId,
ip,
transaction,
...rest
}: Props): Promise<Star> {
let { index } = rest;
@@ -43,46 +47,43 @@ export default async function starCreator({
Sequelize.literal('"star"."index" collate "C"'),
["updatedAt", "DESC"],
],
transaction,
});
// create a star at the beginning of the list
index = fractionalIndex(null, stars.length ? stars[0].index : null);
}
const transaction = await sequelize.transaction();
let star;
try {
const response = await Star.findOrCreate({
where: {
userId: user.id,
documentId,
},
defaults: {
index,
},
transaction,
});
star = response[0];
if (response[1]) {
await Event.create(
{
name: "stars.create",
modelId: star.id,
const response = await Star.findOrCreate({
where: documentId
? {
userId: user.id,
actorId: user.id,
documentId,
ip,
}
: {
userId: user.id,
collectionId,
},
{ transaction }
);
}
defaults: {
index,
},
transaction,
});
const star = response[0];
await transaction.commit();
} catch (err) {
await transaction.rollback();
throw err;
if (response[1]) {
await Event.create(
{
name: "stars.create",
modelId: star.id,
userId: user.id,
actorId: user.id,
documentId,
collectionId,
ip,
},
{ transaction }
);
}
return star;