@@ -16,90 +16,74 @@ interface ExperienceAlertState {
1616 actions : {
1717 show : ( variant : AlertVariant , title : string , description : string ) => void ;
1818 close : ( id : string ) => void ;
19- closeAll : ( ) => void ;
2019 } ;
2120}
2221
2322let alertIdCounter = 0 ;
2423
25- export const useExperienceAlertStore = create < ExperienceAlertState > (
26- ( set , get ) => ( {
27- alerts : [ ] ,
28- actions : {
29- show : ( variant , title , description ) => {
30- const { alerts } = get ( ) ;
31- const isDuplicate = alerts . some (
32- ( a ) =>
33- a . variant === variant &&
34- a . title === title &&
35- a . description === description
36- ) ;
37- if ( isDuplicate ) return ;
38-
39- const id = `exp-alert-${ ++ alertIdCounter } ` ;
40- set ( ( state ) => ( {
41- alerts : [ ...state . alerts , { id, variant, title, description } ] ,
42- } ) ) ;
43- } ,
44- close : ( id ) => {
45- set ( ( state ) => ( {
46- alerts : state . alerts . filter ( ( a ) => a . id !== id ) ,
47- } ) ) ;
48- } ,
49- closeAll : ( ) => {
50- set ( { alerts : [ ] } ) ;
51- } ,
24+ const useExperienceAlertStore = create < ExperienceAlertState > ( ( set , get ) => ( {
25+ alerts : [ ] ,
26+ actions : {
27+ show : ( variant , title , description ) => {
28+ const { alerts } = get ( ) ;
29+ const lastAlert = alerts [ alerts . length - 1 ] ;
30+
31+ const isDuplicate =
32+ lastAlert != null &&
33+ lastAlert . variant === variant &&
34+ lastAlert . title === title &&
35+ lastAlert . description === description ;
36+
37+ if ( isDuplicate ) return ;
38+
39+ const id = `exp-alert-${ ++ alertIdCounter } ` ;
40+ set ( ( state ) => ( {
41+ alerts : [ ...state . alerts , { id, variant, title, description } ] ,
42+ } ) ) ;
5243 } ,
53- } )
54- ) ;
55-
56- export const showExperienceError = ( message : string ) => {
57- const { show } = useExperienceAlertStore . getState ( ) . actions ;
58- show ( "error" , "오류" , message ) ;
59- } ;
60-
61- export const showExperienceSuccess = ( message : string , title = "완료" ) => {
62- const { show } = useExperienceAlertStore . getState ( ) . actions ;
63- show ( "success" , title , message ) ;
64- } ;
65-
66- export const showExperienceInfo = ( message : string ) => {
67- const { show } = useExperienceAlertStore . getState ( ) . actions ;
68- show ( "info" , "안내" , message ) ;
69- } ;
7044
71- export const showExperienceWarning = ( message : string ) => {
72- const { show } = useExperienceAlertStore . getState ( ) . actions ;
73- show ( "warning" , "주의" , message ) ;
45+ close : ( id ) => {
46+ set ( ( state ) => ( {
47+ alerts : state . alerts . filter ( ( a ) => a . id !== id ) ,
48+ } ) ) ;
49+ } ,
50+ } ,
51+ } ) ) ;
52+
53+ const showAlert = (
54+ variant : AlertVariant ,
55+ title : string ,
56+ description : string
57+ ) => {
58+ useExperienceAlertStore . getState ( ) . actions . show ( variant , title , description ) ;
7459} ;
7560
7661export const showValidationError = ( title : string , description : string ) => {
77- const { show } = useExperienceAlertStore . getState ( ) . actions ;
78- show ( "error" , title , description ) ;
62+ showAlert ( "error" , title , description ) ;
7963} ;
8064
8165export const showSaveError = ( ) => {
82- showExperienceError ( EXPERIENCE_MESSAGES . API . SAVE_FAILED ) ;
66+ showAlert ( "error" , "오류" , EXPERIENCE_MESSAGES . API . SAVE_FAILED ) ;
8367} ;
8468
8569export const showDeleteError = ( ) => {
86- showExperienceError ( EXPERIENCE_MESSAGES . API . DELETE_FAILED ) ;
70+ showAlert ( "error" , "오류" , EXPERIENCE_MESSAGES . API . DELETE_FAILED ) ;
8771} ;
8872
8973export const showDefaultSettingError = ( ) => {
90- showExperienceError ( EXPERIENCE_MESSAGES . API . DEFAULT_SETTING_FAILED ) ;
74+ showAlert ( "error" , "오류" , EXPERIENCE_MESSAGES . API . DEFAULT_SETTING_FAILED ) ;
9175} ;
9276
9377export const showSaveSuccess = ( title ?: string ) => {
94- showExperienceSuccess ( EXPERIENCE_MESSAGES . SUCCESS . SAVED , title ) ;
78+ showAlert ( "success" , title ?? "완료" , EXPERIENCE_MESSAGES . SUCCESS . SAVED ) ;
9579} ;
9680
9781export const showDeleteSuccess = ( ) => {
98- showExperienceSuccess ( EXPERIENCE_MESSAGES . SUCCESS . DELETED ) ;
82+ showAlert ( "success" , "완료" , EXPERIENCE_MESSAGES . SUCCESS . DELETED ) ;
9983} ;
10084
10185export const useExperienceAlerts = ( ) =>
10286 useExperienceAlertStore ( ( s ) => s . alerts ) ;
10387
10488export const useExperienceAlertActions = ( ) =>
105- useExperienceAlertStore ( ( s ) => s . actions ) ;
89+ useExperienceAlertStore ( ( s ) => s . actions ) ;
0 commit comments