feat: Allow filtering searches by 'source'

fix: Do not show API searches in recent list in app
This commit is contained in:
Tom Moor
2023-12-27 16:56:27 -05:00
parent 820e4839d5
commit 551f569896
8 changed files with 65 additions and 20 deletions

View File

@@ -11,7 +11,9 @@ export default function SearchActions() {
React.useEffect(() => {
if (!searches.isLoaded) {
void searches.fetchPage({});
void searches.fetchPage({
source: "app",
});
}
}, [searches]);

View File

@@ -4,10 +4,16 @@ import Model from "./base/Model";
class SearchQuery extends Model {
static modelName = "Search";
id: string;
/**
* The query string, automatically truncated to 255 characters.
*/
query: string;
/**
* Where the query originated.
*/
source: "api" | "app" | "slack";
delete = async () => {
this.isSaving = true;

View File

@@ -28,7 +28,9 @@ function RecentSearches(
const [isPreloaded] = React.useState(searches.recent.length > 0);
React.useEffect(() => {
void searches.fetchPage({});
void searches.fetchPage({
source: "app",
});
}, [searches]);
const content = searches.recent.length ? (

View File

@@ -13,6 +13,8 @@ export default class SearchesStore extends Store<SearchQuery> {
@computed
get recent(): SearchQuery[] {
return uniqBy(this.orderedData, "query").slice(0, 8);
return uniqBy(this.orderedData, "query")
.filter((search) => search.source === "app")
.slice(0, 8);
}
}

View File

@@ -4,6 +4,7 @@ export default function presentSearchQuery(searchQuery: SearchQuery) {
return {
id: searchQuery.id,
query: searchQuery.query,
source: searchQuery.source,
createdAt: searchQuery.createdAt,
answer: searchQuery.answer,
score: searchQuery.score,

View File

@@ -21,3 +21,13 @@ export const SearchesUpdateSchema = BaseSchema.extend({
});
export type SearchesUpdateReq = z.infer<typeof SearchesUpdateSchema>;
export const SearchesListSchema = BaseSchema.extend({
body: z
.object({
source: z.string().optional(),
})
.optional(),
});
export type SearchesListReq = z.infer<typeof SearchesListSchema>;

View File

@@ -24,6 +24,7 @@ describe("#searches.list", () => {
userId: user.id,
teamId: user.teamId,
query: "bar",
source: "api",
}),
]);
});
@@ -42,6 +43,18 @@ describe("#searches.list", () => {
expect(queries).toContain("foo");
expect(queries).toContain("bar");
});
it("should allow filtering by source", async () => {
const res = await server.post("/api/searches.list", {
body: {
token: user.getJwtToken(),
source: "api",
},
});
const body = await res.json();
expect(res.status).toEqual(200);
expect(body.data).toHaveLength(1);
});
});
describe("#searches.update", () => {

View File

@@ -10,23 +10,32 @@ import * as T from "./schema";
const router = new Router();
router.post("searches.list", auth(), pagination(), async (ctx: APIContext) => {
const { user } = ctx.state.auth;
router.post(
"searches.list",
auth(),
validate(T.SearchesListSchema),
pagination(),
async (ctx: APIContext<T.SearchesListReq>) => {
const { user } = ctx.state.auth;
const source = ctx.input.body?.source;
const searches = await SearchQuery.findAll({
where: {
userId: user.id,
},
order: [["createdAt", "DESC"]],
offset: ctx.state.pagination.offset,
limit: ctx.state.pagination.limit,
});
const searches = await SearchQuery.findAll({
where: {
...(source ? { source } : {}),
teamId: user.teamId,
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),
};
});
ctx.body = {
pagination: ctx.state.pagination,
data: searches.map(presentSearchQuery),
};
}
);
router.post(
"searches.update",