feat: Show recent searches (#2868)

* stash

* root hookup

* recent searches UI

* feat: Add search query deletion

* simplify no results state

* lint
This commit is contained in:
Tom Moor
2021-12-19 11:08:28 -08:00
committed by GitHub
parent 81f3347ecf
commit 6fc1b5cc22
15 changed files with 267 additions and 100 deletions

View File

@@ -19,6 +19,7 @@ import apiWrapper from "./middlewares/apiWrapper";
import editor from "./middlewares/editor";
import notificationSettings from "./notificationSettings";
import revisions from "./revisions";
import searches from "./searches";
import shares from "./shares";
import team from "./team";
import users from "./users";
@@ -53,6 +54,7 @@ router.use("/", revisions.routes());
router.use("/", views.routes());
router.use("/", hooks.routes());
router.use("/", apiKeys.routes());
router.use("/", searches.routes());
router.use("/", shares.routes());
router.use("/", team.routes());
router.use("/", integrations.routes());

View File

@@ -0,0 +1,45 @@
import Router from "koa-router";
import auth from "@server/middlewares/authentication";
import { SearchQuery } from "@server/models";
import { presentSearchQuery } from "@server/presenters";
import { assertPresent } from "@server/validation";
import pagination from "./middlewares/pagination";
const router = new Router();
router.post("searches.list", auth(), pagination(), async (ctx) => {
const user = ctx.state.user;
const searches = await SearchQuery.findAll({
where: {
userId: user.id,
},
order: [["createdAt", "DESC"]],
offset: ctx.state.pagination.offset,
limit: ctx.state.pagination.limit,
});
ctx.body = {
pagination: ctx.state.pagination,
data: searches.map(presentSearchQuery),
};
});
router.post("searches.delete", auth(), async (ctx) => {
const { id, query } = ctx.body;
assertPresent(id || query, "id or query is required");
const { user } = ctx.state;
await SearchQuery.destroy({
where: {
...(id ? { id } : { query }),
userId: user.id,
},
});
ctx.body = {
success: true,
};
});
export default router;

View File

@@ -11,7 +11,7 @@ import pagination from "./middlewares/pagination";
const Op = Sequelize.Op;
const { authorize } = policy;
const router = new Router();
// @ts-expect-error ts-migrate(7030) FIXME: Not all code paths return a value.
router.post("shares.info", auth(), async (ctx) => {
const { id, documentId, apiVersion } = ctx.body;
assertUuid(id || documentId, "id or documentId is required");
@@ -40,7 +40,8 @@ router.post("shares.info", auth(), async (ctx) => {
// Deprecated API response returns just the share for the current documentId
if (apiVersion !== 2) {
if (!share || !share.document) {
return (ctx.response.status = 204);
ctx.response.status = 204;
return;
}
authorize(user, "read", share);
@@ -86,7 +87,8 @@ router.post("shares.info", auth(), async (ctx) => {
}
if (!shares.length) {
return (ctx.response.status = 204);
ctx.response.status = 204;
return;
}
ctx.body = {