11import { create } from "@bufbuild/protobuf" ;
22import { FieldMaskSchema } from "@bufbuild/protobuf/wkt" ;
3- import React , { useEffect , useState } from "react" ;
3+ import { useEffect , useState } from "react" ;
44import { toast } from "react-hot-toast" ;
55import { Button } from "@/components/ui/button" ;
66import { Dialog , DialogContent , DialogFooter , DialogHeader , DialogTitle } from "@/components/ui/dialog" ;
@@ -11,6 +11,7 @@ import { shortcutServiceClient } from "@/connect";
1111import { useAuth } from "@/contexts/AuthContext" ;
1212import useCurrentUser from "@/hooks/useCurrentUser" ;
1313import useLoading from "@/hooks/useLoading" ;
14+ import { handleError } from "@/lib/error" ;
1415import { Shortcut , ShortcutSchema } from "@/types/proto/api/v1/shortcut_service_pb" ;
1516import { useTranslate } from "@/utils/i18n" ;
1617
@@ -33,31 +34,34 @@ function CreateShortcutDialog({ open, onOpenChange, shortcut: initialShortcut, o
3334 } ) ,
3435 ) ;
3536 const requestState = useLoading ( false ) ;
36- const isCreating = ! initialShortcut ;
37+ const isCreating = shortcut . name === "" ;
3738
3839 useEffect ( ( ) => {
39- if ( initialShortcut ) {
40- setShortcut (
41- create ( ShortcutSchema , {
42- name : initialShortcut . name ,
43- title : initialShortcut . title ,
44- filter : initialShortcut . filter ,
45- } ) ,
46- ) ;
47- } else {
48- setShortcut ( create ( ShortcutSchema , { name : "" , title : "" , filter : "" } ) ) ;
40+ if ( shortcut . name ) {
41+ setShortcut ( shortcut ) ;
4942 }
50- } , [ initialShortcut ] ) ;
43+ } , [ shortcut . name , shortcut . title , shortcut . filter ] ) ;
5144
5245 const onShortcutTitleChange = ( e : React . ChangeEvent < HTMLInputElement > ) => {
53- setShortcut ( { ...shortcut , title : e . target . value } ) ;
46+ setPartialState ( {
47+ title : e . target . value ,
48+ } ) ;
5449 } ;
5550
5651 const onShortcutFilterChange = ( e : React . ChangeEvent < HTMLTextAreaElement > ) => {
57- setShortcut ( { ...shortcut , filter : e . target . value } ) ;
52+ setPartialState ( {
53+ filter : e . target . value ,
54+ } ) ;
5855 } ;
5956
60- const handleConfirm = async ( ) => {
57+ const setPartialState = ( partialState : Partial < Shortcut > ) => {
58+ setShortcut ( {
59+ ...shortcut ,
60+ ...partialState ,
61+ } ) ;
62+ } ;
63+
64+ const handleSaveBtnClick = async ( ) => {
6165 if ( ! shortcut . title || ! shortcut . filter ) {
6266 toast . error ( "Title and filter cannot be empty" ) ;
6367 return ;
@@ -69,7 +73,7 @@ function CreateShortcutDialog({ open, onOpenChange, shortcut: initialShortcut, o
6973 await shortcutServiceClient . createShortcut ( {
7074 parent : user ?. name ,
7175 shortcut : {
72- name : "" , // Will be set by server
76+ name : "" ,
7377 title : shortcut . title ,
7478 filter : shortcut . filter ,
7579 } ,
@@ -79,21 +83,21 @@ function CreateShortcutDialog({ open, onOpenChange, shortcut: initialShortcut, o
7983 await shortcutServiceClient . updateShortcut ( {
8084 shortcut : {
8185 ...shortcut ,
82- name : initialShortcut ! . name , // Keep the original resource name
86+ name : initialShortcut ! . name ,
8387 } ,
8488 updateMask : create ( FieldMaskSchema , { paths : [ "title" , "filter" ] } ) ,
8589 } ) ;
8690 toast . success ( "Update shortcut successfully" ) ;
8791 }
88- // Refresh shortcuts.
8992 await refetchSettings ( ) ;
9093 requestState . setFinish ( ) ;
9194 onSuccess ?.( ) ;
9295 onOpenChange ( false ) ;
93- } catch ( error : any ) {
94- console . error ( error ) ;
95- toast . error ( error . message ) ;
96- requestState . setError ( ) ;
96+ } catch ( error : unknown ) {
97+ await handleError ( error , toast . error , {
98+ context : isCreating ? "Create shortcut" : "Update shortcut" ,
99+ onError : ( ) => requestState . setError ( ) ,
100+ } ) ;
97101 }
98102 } ;
99103
@@ -118,28 +122,13 @@ function CreateShortcutDialog({ open, onOpenChange, shortcut: initialShortcut, o
118122 onChange = { onShortcutFilterChange }
119123 />
120124 </ div >
121- < div className = "text-sm text-muted-foreground" >
122- < p className = "mb-2" > { t ( "common.learn-more" ) } :</ p >
123- < ul className = "list-disc list-inside space-y-1" >
124- < li >
125- < a
126- className = "text-primary hover:underline"
127- href = "https://www.usememos.com/docs/guides/shortcuts"
128- target = "_blank"
129- rel = "noopener noreferrer"
130- >
131- Docs - Shortcuts
132- </ a >
133- </ li >
134- </ ul >
135- </ div >
136125 </ div >
137126 < DialogFooter >
138127 < Button variant = "ghost" disabled = { requestState . isLoading } onClick = { ( ) => onOpenChange ( false ) } >
139128 { t ( "common.cancel" ) }
140129 </ Button >
141- < Button disabled = { requestState . isLoading } onClick = { handleConfirm } >
142- { t ( "common.confirm " ) }
130+ < Button disabled = { requestState . isLoading } onClick = { handleSaveBtnClick } >
131+ { t ( "common.save " ) }
143132 </ Button >
144133 </ DialogFooter >
145134 </ DialogContent >
0 commit comments