Skip to content

Commit

Permalink
feat: update search memo filter
Browse files Browse the repository at this point in the history
  • Loading branch information
boojack committed May 27, 2024
1 parent ba0876a commit f0e5a72
Show file tree
Hide file tree
Showing 11 changed files with 216 additions and 77 deletions.
27 changes: 27 additions & 0 deletions server/router/api/v1/memo_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -883,6 +883,9 @@ func (s *APIV1Service) buildMemoFindWithFilter(ctx context.Context, find *store.
if find == nil {
find = &store.FindMemo{}
}
if find.PayloadFind == nil {
find.PayloadFind = &store.FindMemoPayload{}
}
if filter != "" {
filter, err := parseSearchMemosFilter(filter)
if err != nil {
Expand Down Expand Up @@ -956,6 +959,15 @@ func (s *APIV1Service) buildMemoFindWithFilter(ctx context.Context, find *store.
if filter.IncludeComments {
find.ExcludeComments = false
}
if filter.HasLink {
find.PayloadFind.HasLink = true
}
if filter.HasTaskList {
find.PayloadFind.HasTaskList = true
}
if filter.HasCode {
find.PayloadFind.HasCode = true
}
}

user, err := s.GetCurrentUser(ctx)
Expand Down Expand Up @@ -1006,6 +1018,9 @@ var SearchMemosFilterCELAttributes = []cel.EnvOption{
cel.Variable("random", cel.BoolType),
cel.Variable("limit", cel.IntType),
cel.Variable("include_comments", cel.BoolType),
cel.Variable("has_link", cel.BoolType),
cel.Variable("has_task_list", cel.BoolType),
cel.Variable("has_code", cel.BoolType),
}

type SearchMemosFilter struct {
Expand All @@ -1021,6 +1036,9 @@ type SearchMemosFilter struct {
Random bool
Limit *int
IncludeComments bool
HasLink bool
HasTaskList bool
HasCode bool
}

func parseSearchMemosFilter(expression string) (*SearchMemosFilter, error) {
Expand Down Expand Up @@ -1090,6 +1108,15 @@ func findSearchMemosField(callExpr *expr.Expr_Call, filter *SearchMemosFilter) {
} else if idExpr.Name == "include_comments" {
value := callExpr.Args[1].GetConstExpr().GetBoolValue()
filter.IncludeComments = value
} else if idExpr.Name == "has_link" {
value := callExpr.Args[1].GetConstExpr().GetBoolValue()
filter.HasLink = value
} else if idExpr.Name == "has_task_list" {
value := callExpr.Args[1].GetConstExpr().GetBoolValue()
filter.HasTaskList = value
} else if idExpr.Name == "has_code" {
value := callExpr.Args[1].GetConstExpr().GetBoolValue()
filter.HasCode = value
}
return
}
Expand Down
9 changes: 9 additions & 0 deletions store/db/mysql/memo.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,15 @@ func (d *DB) ListMemos(ctx context.Context, find *store.FindMemo) ([]*store.Memo
if v.Tag != nil {
where, args = append(where, "JSON_CONTAINS(JSON_EXTRACT(`memo`.`payload`, '$.property.tags'), ?)"), append(args, fmt.Sprintf(`["%s"]`, *v.Tag))
}
if v.HasLink {
where = append(where, "JSON_EXTRACT(`memo`.`payload`, '$.property.hasLink') IS TRUE")
}
if v.HasTaskList {
where = append(where, "JSON_EXTRACT(`memo`.`payload`, '$.property.hasTaskList') IS TRUE")
}
if v.HasCode {
where = append(where, "JSON_EXTRACT(`memo`.`payload`, '$.property.hasCode') IS TRUE")
}
}
if find.ExcludeComments {
having = append(having, "`parent_id` IS NULL")
Expand Down
9 changes: 9 additions & 0 deletions store/db/postgres/memo.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,15 @@ func (d *DB) ListMemos(ctx context.Context, find *store.FindMemo) ([]*store.Memo
if v.Tag != nil {
where, args = append(where, "memo.payload->'property'->'tags' @> "+placeholder(len(args)+1)), append(args, fmt.Sprintf(`["%s"]`, *v.Tag))
}
if v.HasLink {
where = append(where, "(memo.payload->'property'->>'hasLink')::BOOLEAN IS TRUE")
}
if v.HasTaskList {
where = append(where, "(memo.payload->'property'->>'hasTaskList')::BOOLEAN IS TRUE")
}
if v.HasCode {
where = append(where, "(memo.payload->'property'->>'hasCode')::BOOLEAN IS TRUE")
}
}
if find.ExcludeComments {
where = append(where, "memo_relation.related_memo_id IS NULL")
Expand Down
9 changes: 9 additions & 0 deletions store/db/sqlite/memo.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,15 @@ func (d *DB) ListMemos(ctx context.Context, find *store.FindMemo) ([]*store.Memo
if v.Tag != nil {
where, args = append(where, "JSON_EXTRACT(`memo`.`payload`, '$.property.tags') LIKE ?"), append(args, fmt.Sprintf(`%%"%s"%%`, *v.Tag))
}
if v.HasLink {
where = append(where, "JSON_EXTRACT(`memo`.`payload`, '$.property.hasLink') IS TRUE")
}
if v.HasTaskList {
where = append(where, "JSON_EXTRACT(`memo`.`payload`, '$.property.hasTaskList') IS TRUE")
}
if v.HasCode {
where = append(where, "JSON_EXTRACT(`memo`.`payload`, '$.property.hasCode') IS TRUE")
}
}
if find.ExcludeComments {
where = append(where, "`parent_id` IS NULL")
Expand Down
7 changes: 5 additions & 2 deletions store/memo.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,11 @@ type FindMemo struct {
}

type FindMemoPayload struct {
Raw *string
Tag *string
Raw *string
Tag *string
HasLink bool
HasTaskList bool
HasCode bool
}

type UpdateMemo struct {
Expand Down
112 changes: 74 additions & 38 deletions web/src/components/MemoFilter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,14 @@ const MemoFilter = (props: Props) => {
const location = useLocation();
const filterStore = useFilterStore();
const filter = filterStore.state;
const { tag: tagQuery, text: textQuery, visibility } = filter;
const showFilter = Boolean(tagQuery || textQuery || visibility);
const showFilter = Boolean(
filter.tag ||
filter.text ||
filter.visibility ||
filter.memoPropertyFilter?.hasLink ||
filter.memoPropertyFilter?.hasTaskList ||
filter.memoPropertyFilter?.hasCode,
);

useEffect(() => {
filterStore.clearFilter();
Expand All @@ -36,42 +42,72 @@ const MemoFilter = (props: Props) => {
<Icon.Filter className="w-4 h-auto mr-1" />
<span>{t("common.filter")}:</span>
</div>
<div
className={
"max-w-xs flex flex-row justify-start items-center px-2 mr-2 cursor-pointer dark:text-gray-400 bg-gray-200 dark:bg-zinc-800 rounded whitespace-nowrap truncate hover:line-through " +
(tagQuery ? "" : "!hidden")
}
onClick={() => {
filterStore.setTagFilter(undefined);
}}
>
<Icon.Hash className="w-4 h-auto mr-1 text-gray-500 dark:text-gray-400" /> {tagQuery}
<Icon.X className="w-4 h-auto ml-1 opacity-40" />
</div>
<div
className={
"max-w-xs flex flex-row justify-start items-center px-2 mr-2 cursor-pointer dark:text-gray-400 bg-gray-200 dark:bg-zinc-800 rounded whitespace-nowrap truncate hover:line-through " +
(visibility ? "" : "!hidden")
}
onClick={() => {
filterStore.setMemoVisibilityFilter(undefined);
}}
>
<Icon.Eye className="w-4 h-auto mr-1 text-gray-500 dark:text-gray-400" /> {visibility}
<Icon.X className="w-4 h-auto ml-1 opacity-40" />
</div>
<div
className={
"max-w-xs flex flex-row justify-start items-center px-2 mr-2 cursor-pointer dark:text-gray-400 bg-gray-200 dark:bg-zinc-800 rounded whitespace-nowrap truncate hover:line-through " +
(textQuery ? "" : "!hidden")
}
onClick={() => {
filterStore.setTextFilter(undefined);
}}
>
<Icon.Search className="w-4 h-auto mr-1 text-gray-500 dark:text-gray-400" /> {textQuery}
<Icon.X className="w-4 h-auto ml-1 opacity-40" />
</div>
{filter.tag && (
<div
className="max-w-xs flex flex-row justify-start items-center px-2 cursor-pointer dark:text-gray-400 bg-gray-200 dark:bg-zinc-800 rounded whitespace-nowrap truncate hover:line-through"
onClick={() => {
filterStore.setTagFilter(undefined);
}}
>
<Icon.Hash className="w-4 h-auto mr-1 text-gray-500 dark:text-gray-400" /> {filter.tag}
<Icon.X className="w-4 h-auto ml-1 opacity-40" />
</div>
)}
{filter.visibility && (
<div
className="max-w-xs flex flex-row justify-start items-center px-2 cursor-pointer dark:text-gray-400 bg-gray-200 dark:bg-zinc-800 rounded whitespace-nowrap truncate hover:line-through"
onClick={() => {
filterStore.setMemoVisibilityFilter(undefined);
}}
>
<Icon.Eye className="w-4 h-auto mr-1 text-gray-500 dark:text-gray-400" /> {filter.visibility}
<Icon.X className="w-4 h-auto ml-1 opacity-40" />
</div>
)}
{filter.text && (
<div
className="max-w-xs flex flex-row justify-start items-center px-2 cursor-pointer dark:text-gray-400 bg-gray-200 dark:bg-zinc-800 rounded whitespace-nowrap truncate hover:line-through"
onClick={() => {
filterStore.setTextFilter(undefined);
}}
>
<Icon.Search className="w-4 h-auto mr-1 text-gray-500 dark:text-gray-400" /> {filter.text}
<Icon.X className="w-4 h-auto ml-1 opacity-40" />
</div>
)}
{filter.memoPropertyFilter?.hasLink && (
<div
className="max-w-xs flex flex-row justify-start items-center px-2 cursor-pointer dark:text-gray-400 bg-gray-200 dark:bg-zinc-800 rounded whitespace-nowrap truncate hover:line-through"
onClick={() => {
filterStore.setMemoPropertyFilter({ hasLink: false });
}}
>
<Icon.Link className="w-4 h-auto mr-1 text-gray-500 dark:text-gray-400" /> Has Link
<Icon.X className="w-4 h-auto ml-1 opacity-40" />
</div>
)}
{filter.memoPropertyFilter?.hasTaskList && (
<div
className="max-w-xs flex flex-row justify-start items-center px-2 cursor-pointer dark:text-gray-400 bg-gray-200 dark:bg-zinc-800 rounded whitespace-nowrap truncate hover:line-through"
onClick={() => {
filterStore.setMemoPropertyFilter({ hasTaskList: false });
}}
>
<Icon.CheckCircle className="w-4 h-auto mr-1 text-gray-500 dark:text-gray-400" /> Has Task
<Icon.X className="w-4 h-auto ml-1 opacity-40" />
</div>
)}
{filter.memoPropertyFilter?.hasCode && (
<div
className="max-w-xs flex flex-row justify-start items-center px-2 cursor-pointer dark:text-gray-400 bg-gray-200 dark:bg-zinc-800 rounded whitespace-nowrap truncate hover:line-through"
onClick={() => {
filterStore.setMemoPropertyFilter({ hasCode: false });
}}
>
<Icon.Code2 className="w-4 h-auto mr-1 text-gray-500 dark:text-gray-400" /> Has Code
<Icon.X className="w-4 h-auto ml-1 opacity-40" />
</div>
)}
</div>
);
};
Expand Down
62 changes: 34 additions & 28 deletions web/src/components/UserStatisticsView.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { Divider } from "@mui/joy";
import { useEffect, useState } from "react";
import { memoServiceClient } from "@/grpcweb";
import { useMemoStore, useTagStore } from "@/store/v1";
import { useFilterStore } from "@/store/module";
import { useMemoStore } from "@/store/v1";
import { User } from "@/types/proto/api/v1/user_service";
import { useTranslate } from "@/utils/i18n";
import Icon from "./Icon";
Expand All @@ -19,13 +21,12 @@ const UserStatisticsView = (props: Props) => {
const { user } = props;
const t = useTranslate();
const memoStore = useMemoStore();
const tagStore = useTagStore();
const filterStore = useFilterStore();
const [memoAmount, setMemoAmount] = useState(0);
const [isRequesting, setIsRequesting] = useState(false);
const [memoStats, setMemoStats] = useState<UserMemoStats>({ links: 0, todos: 0, code: 0 });
const days = Math.ceil((Date.now() - user.createTime!.getTime()) / 86400000);
const memos = Object.values(memoStore.getState().memoMapByName);
const tags = tagStore.sortedTags().length;

useEffect(() => {
if (memos.length === 0) {
Expand Down Expand Up @@ -60,7 +61,7 @@ const UserStatisticsView = (props: Props) => {
<div className="w-full flex flex-row justify-between items-center">
<p className="text-sm font-medium leading-6 dark:text-gray-500">{t("common.statistics")}</p>
</div>
<div className="w-full grid grid-cols-2 gap-x-4">
<div className="w-full grid grid-cols-1 gap-x-4">
<div className="w-full flex justify-between items-center">
<div className="w-auto flex justify-start items-center">
<Icon.CalendarDays className="w-4 h-auto mr-1" />
Expand All @@ -75,33 +76,38 @@ const UserStatisticsView = (props: Props) => {
</div>
{isRequesting ? <Icon.Loader className="animate-spin w-4 h-auto text-gray-400" /> : <span className="">{memoAmount}</span>}
</div>
<div className="w-full flex justify-between items-center">
<div className="w-auto flex justify-start items-center">
<Icon.Hash className="w-4 h-auto mr-1" />
<span className="block text-base sm:text-sm">{t("common.tags")}</span>
</div>
<span>{tags}</span>
</div>
<div className="w-full flex justify-between items-center">
<div className="w-auto flex justify-start items-center">
<Icon.Link className="w-4 h-auto mr-1" />
<span className="block text-base sm:text-sm">Links</span>
<Divider className="!my-1 opacity-50" />
<div className="w-full mt-1 flex flex-row justify-start items-center gap-x-2 gap-y-1 flex-wrap">
<div
className="w-auto border dark:border-zinc-800 px-1 rounded-md flex justify-between items-center cursor-pointer hover:opacity-80"
onClick={() => filterStore.setMemoPropertyFilter({ hasLink: true })}
>
<div className="w-auto flex justify-start items-center mr-1">
<Icon.Link className="w-4 h-auto mr-1" />
<span className="block text-sm">Links</span>
</div>
<span className="text-sm truncate">{memoStats.links}</span>
</div>
<span className="">{memoStats.links}</span>
</div>
<div className="w-full flex justify-between items-center">
<div className="w-auto flex justify-start items-center">
<Icon.CheckCircle className="w-4 h-auto mr-1" />
<span className="block text-base sm:text-sm">Todos</span>
<div
className="w-auto border dark:border-zinc-800 px-1 rounded-md flex justify-between items-center cursor-pointer hover:opacity-80"
onClick={() => filterStore.setMemoPropertyFilter({ hasTaskList: true })}
>
<div className="w-auto flex justify-start items-center mr-1">
<Icon.CheckCircle className="w-4 h-auto mr-1" />
<span className="block text-sm">Todos</span>
</div>
<span className="text-sm truncate">{memoStats.todos}</span>
</div>
<span className="">{memoStats.todos}</span>
</div>
<div className="w-full flex justify-between items-center">
<div className="w-auto flex justify-start items-center">
<Icon.Code2 className="w-4 h-auto mr-1" />
<span className="block text-base sm:text-sm">Code</span>
<div
className="w-auto border dark:border-zinc-800 px-1 rounded-md flex justify-between items-center cursor-pointer hover:opacity-80"
onClick={() => filterStore.setMemoPropertyFilter({ hasCode: true })}
>
<div className="w-auto flex justify-start items-center mr-1">
<Icon.Code2 className="w-4 h-auto mr-1" />
<span className="block text-sm">Code</span>
</div>
<span className="text-sm truncate">{memoStats.code}</span>
</div>
<span className="">{memoStats.code}</span>
</div>
</div>
</div>
Expand Down
3 changes: 2 additions & 1 deletion web/src/hooks/useFilterWithUrlParams.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { useFilterStore } from "@/store/module";
const useFilterWithUrlParams = () => {
const location = useLocation();
const filterStore = useFilterStore();
const { tag, text } = filterStore.state;
const { tag, text, memoPropertyFilter } = filterStore.state;

useEffect(() => {
const urlParams = new URLSearchParams(location.search);
Expand Down Expand Up @@ -38,6 +38,7 @@ const useFilterWithUrlParams = () => {
return {
tag,
text,
memoPropertyFilter,
};
};

Expand Down
Loading

0 comments on commit f0e5a72

Please sign in to comment.