import { create } from "@bufbuild/protobuf"; import { FieldMaskSchema } from "@bufbuild/protobuf/wkt"; import { CheckCircle2Icon, ClipboardCheckIcon, Clock3Icon, ExternalLinkIcon, FilterIcon, MoreVerticalIcon, PencilIcon, PinIcon, PlusIcon, SaveIcon, SearchIcon, ShieldIcon, TagsIcon, Trash2Icon, XIcon, } from "lucide-react"; import { useEffect, useState } from "react"; import toast from "react-hot-toast"; import { useLocation, useNavigate } from "react-router-dom"; import ConfirmDialog from "@/components/ConfirmDialog"; import { Badge } from "@/components/ui/badge"; import { Button } from "@/components/ui/button"; import { DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuTrigger } from "@/components/ui/dropdown-menu"; import { Input } from "@/components/ui/input"; import { Label } from "@/components/ui/label"; import { Textarea } from "@/components/ui/textarea"; import { shortcutServiceClient } from "@/connect"; import { useAuth } from "@/contexts/AuthContext"; import useCurrentUser from "@/hooks/useCurrentUser"; import useLoading from "@/hooks/useLoading"; import { handleError } from "@/lib/error"; import { cn } from "@/lib/utils"; import { Shortcut, ShortcutSchema } from "@/types/proto/api/v1/shortcut_service_pb"; import { useTranslate } from "@/utils/i18n"; const shortcutExamples = [ { title: "Pinned", filter: "pinned", description: "Only pinned memos.", icon: PinIcon, }, { title: "Recent notes", filter: 'created_ts >= now - duration("1h")', description: "Memos created in the last hour.", icon: Clock3Icon, }, { title: "Public memos", filter: 'visibility == "PUBLIC"', description: "Only public memos.", icon: ShieldIcon, }, { title: "Project tags", filter: 'tag in ["work", "personal"]', description: "Match one or more exact tags.", icon: TagsIcon, }, { title: "Untagged", filter: "size(tags) == 0", description: "Memos without any tags.", icon: TagsIcon, }, { title: "Archive tree", filter: 'tags.exists(t, t.startsWith("archive"))', description: "Match hierarchical tags by prefix.", icon: TagsIcon, }, { title: "Open tasks", filter: "has_task_list && has_incomplete_tasks", description: "Memos with unfinished tasks.", icon: ClipboardCheckIcon, }, { title: "Links or code", filter: "has_link || has_code", description: "Memos containing links or code blocks.", icon: FilterIcon, }, { title: "Content search", filter: 'content.contains("TODO")', description: "Search text inside memo content.", icon: SearchIcon, }, { title: "Starts with", filter: 'content.startsWith("TODO")', description: "Memos whose content begins with text (also endsWith).", icon: SearchIcon, }, { title: "Regex match", filter: 'content.matches("v[0-9]+")', description: "Match content with a regular expression.", icon: FilterIcon, }, { title: "All tags match", filter: 'tags.all(t, t.startsWith("work/"))', description: "Every tag must satisfy the predicate (tagged memos only).", icon: TagsIcon, }, { title: "One project tag", filter: 'tags.exists_one(t, t.startsWith("project/"))', description: "Exactly one tag matches the predicate.", icon: TagsIcon, }, { title: "Any of these tags", filter: 'sets.intersects(tags, ["work", "urgent"])', description: "Tags intersect the given set.", icon: TagsIcon, }, { title: "Exactly these tags", filter: 'sets.equivalent(tags, ["inbox"])', description: "Tagged with exactly this set, nothing more.", icon: TagsIcon, }, { title: "Memos from 2024", filter: "created_ts.getFullYear() == 2024", description: "Filter by calendar year.", icon: Clock3Icon, }, { title: "Weekend notes", filter: "created_ts.getDayOfWeek() == 0 || created_ts.getDayOfWeek() == 6", description: "Created on a Sunday or Saturday (0 = Sunday).", icon: Clock3Icon, }, { title: "Long notes", filter: "size(content) > 280", description: "Memos longer than 280 characters.", icon: FilterIcon, }, ]; const filterFields = [ "content.contains(...)", "content.startsWith(...)", "content.endsWith(...)", "content.matches(...)", "visibility", "pinned", "tag in [...]", "tags.exists(...)", "tags.all(...)", "tags.exists_one(...)", "sets.contains(tags, [...])", "sets.intersects(tags, [...])", "sets.equivalent(tags, [...])", "size(tags) == ...", "size(content) > ...", "has_task_list", "has_incomplete_tasks", "has_link", "has_code", 'created_ts >= now - duration("24h")', "created_ts.getFullYear() == ...", "created_ts.getMonth() == ... (0 = Jan)", "created_ts.getDayOfWeek() == ... (0 = Sun)", "updated_ts", "now", 'timestamp("2025-01-01T00:00:00Z")', ]; const getShortcutId = (name: string): string => { const parts = name.split("/"); return parts.length === 4 ? parts[3] : name; }; const createEmptyShortcut = () => create(ShortcutSchema, { name: "", title: "", filter: "", }); interface ShortcutGuideProps { onUseExample: (example: (typeof shortcutExamples)[number]) => void; } interface ShortcutsRouteState { openCreate?: boolean; shortcut?: Shortcut; } const ShortcutGuide = ({ onUseExample }: ShortcutGuideProps) => { return ( ); }; const Shortcuts = () => { const t = useTranslate(); const location = useLocation(); const navigate = useNavigate(); const user = useCurrentUser(); const { shortcuts, refetchSettings } = useAuth(); const [isCreateFormOpen, setIsCreateFormOpen] = useState(false); const [draft, setDraft] = useState(createEmptyShortcut()); const [deleteTarget, setDeleteTarget] = useState(); const createState = useLoading(false); const validateState = useLoading(false); const updateState = useLoading(false); const isEditing = draft.name !== ""; const isSaving = createState.isLoading || updateState.isLoading; useEffect(() => { const state = location.state as ShortcutsRouteState | null; if (!state) return; if (state.shortcut) { setDraft( create(ShortcutSchema, { name: state.shortcut.name, title: state.shortcut.title, filter: state.shortcut.filter, }), ); setIsCreateFormOpen(true); } else if (state.openCreate) { setDraft(createEmptyShortcut()); setIsCreateFormOpen(true); } navigate(location.pathname, { replace: true, state: null }); }, [location.key, location.pathname, location.state, navigate]); const setDraftState = (state: Partial) => { setDraft((current) => ({ ...current, ...state })); }; const handleUseExample = (example: (typeof shortcutExamples)[number]) => { setDraft( create(ShortcutSchema, { name: draft.name, title: draft.title || example.title, filter: example.filter, }), ); setIsCreateFormOpen(true); }; const handleOpenCreateForm = () => { setDraft(createEmptyShortcut()); setIsCreateFormOpen(true); }; const handleCloseForm = () => { setDraft(createEmptyShortcut()); setIsCreateFormOpen(false); }; const handleEditShortcut = (shortcut: Shortcut) => { setDraft( create(ShortcutSchema, { name: shortcut.name, title: shortcut.title, filter: shortcut.filter, }), ); setIsCreateFormOpen(true); }; const validateDraft = async () => { if (!draft.title || !draft.filter) { toast.error("Title and filter cannot be empty"); return false; } if (!user?.name) { toast.error("No current user"); return false; } try { validateState.setLoading(); await shortcutServiceClient.createShortcut({ parent: user.name, shortcut: { name: "", title: draft.title, filter: draft.filter }, validateOnly: true, }); validateState.setFinish(); toast.success("Filter expression looks valid"); return true; } catch (error: unknown) { await handleError(error, toast.error, { context: "Validate shortcut filter", onError: () => validateState.setError(), }); return false; } }; const handleCreateShortcut = async () => { if (!draft.title || !draft.filter) { toast.error("Title and filter cannot be empty"); return; } if (!user?.name) { toast.error("No current user"); return; } try { createState.setLoading(); await shortcutServiceClient.createShortcut({ parent: user.name, shortcut: { name: "", title: draft.title, filter: draft.filter }, }); await refetchSettings(); createState.setFinish(); setDraft(createEmptyShortcut()); setIsCreateFormOpen(false); toast.success("Create shortcut successfully"); } catch (error: unknown) { await handleError(error, toast.error, { context: "Create shortcut", onError: () => createState.setError(), }); } }; const handleUpdateShortcut = async () => { if (!draft.title || !draft.filter) { toast.error("Title and filter cannot be empty"); return; } try { updateState.setLoading(); await shortcutServiceClient.updateShortcut({ shortcut: draft, updateMask: create(FieldMaskSchema, { paths: ["title", "filter"] }), }); await refetchSettings(); updateState.setFinish(); setDraft(createEmptyShortcut()); setIsCreateFormOpen(false); toast.success("Update shortcut successfully"); } catch (error: unknown) { await handleError(error, toast.error, { context: "Update shortcut", onError: () => updateState.setError(), }); } }; const handleSaveShortcut = async () => { if (isEditing) { await handleUpdateShortcut(); return; } await handleCreateShortcut(); }; const confirmDeleteShortcut = async () => { if (!deleteTarget) return; try { await shortcutServiceClient.deleteShortcut({ name: deleteTarget.name }); await refetchSettings(); toast.success(t("setting.shortcut.delete-success", { title: deleteTarget.title })); setDeleteTarget(undefined); } catch (error: unknown) { await handleError(error, toast.error, { context: "Delete shortcut", }); } }; return (
{t("common.shortcuts")}

Shortcut filters

Create reusable memo filters with fields, operators, time helpers, and tag matching. Use examples as starting points, then validate before saving.

{isEditing ? "Edit shortcut" : "Create shortcut"}

Name the shortcut and define the memo filter expression it should apply.

Docs
setDraftState({ title: event.target.value })} />

Prefix the title with an emoji if you want it to appear in the sidebar.