2020-03-03 10:26:02 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
2020-05-10 23:41:10 +00:00
|
|
|
import { useMemo, useRef, useEffect } from '@wordpress/element';
|
2020-03-03 10:26:02 +00:00
|
|
|
|
2021-04-08 12:31:12 +00:00
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import { useStoreNoticesContext } from '../providers/store-notices/context';
|
|
|
|
|
2021-07-23 13:21:59 +00:00
|
|
|
type WPNoticeAction = {
|
|
|
|
label: string;
|
|
|
|
url?: string;
|
|
|
|
onClick?: () => void;
|
|
|
|
};
|
|
|
|
|
|
|
|
type WPNotice = {
|
|
|
|
id: string;
|
|
|
|
status: 'success' | 'info' | 'error' | 'warning';
|
|
|
|
content: string;
|
|
|
|
spokenMessage: string;
|
|
|
|
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
|
|
__unstableHTML: string;
|
|
|
|
isDismissible: boolean;
|
|
|
|
type: 'default' | 'snackbar';
|
|
|
|
speak: boolean;
|
|
|
|
actions: WPNoticeAction[];
|
|
|
|
};
|
|
|
|
|
|
|
|
type NoticeOptions = {
|
|
|
|
id: string;
|
2021-09-24 14:23:10 +00:00
|
|
|
type?: string;
|
2021-07-23 13:21:59 +00:00
|
|
|
isDismissible: boolean;
|
|
|
|
};
|
|
|
|
|
|
|
|
type NoticeCreator = ( text: string, noticeProps: NoticeOptions ) => void;
|
|
|
|
|
|
|
|
export const useStoreNotices = (): {
|
|
|
|
notices: WPNotice[];
|
|
|
|
hasNoticesOfType: ( type: string ) => boolean;
|
|
|
|
removeNotices: ( status: string | null ) => void;
|
|
|
|
removeNotice: ( id: string, context: string ) => void;
|
|
|
|
addDefaultNotice: NoticeCreator;
|
|
|
|
addErrorNotice: NoticeCreator;
|
|
|
|
addWarningNotice: NoticeCreator;
|
|
|
|
addInfoNotice: NoticeCreator;
|
|
|
|
addSuccessNotice: NoticeCreator;
|
|
|
|
setIsSuppressed: ( isSuppressed: boolean ) => void;
|
|
|
|
} => {
|
2020-03-16 20:57:12 +00:00
|
|
|
const {
|
|
|
|
notices,
|
|
|
|
createNotice,
|
|
|
|
removeNotice,
|
2020-05-14 23:55:22 +00:00
|
|
|
setIsSuppressed,
|
2020-03-16 20:57:12 +00:00
|
|
|
} = useStoreNoticesContext();
|
2020-05-10 23:41:10 +00:00
|
|
|
// Added to a ref so the surface for notices doesn't change frequently
|
|
|
|
// and thus can be used as dependencies on effects.
|
|
|
|
const currentNotices = useRef( notices );
|
|
|
|
|
|
|
|
// Update notices ref whenever they change
|
|
|
|
useEffect( () => {
|
|
|
|
currentNotices.current = notices;
|
|
|
|
}, [ notices ] );
|
2020-03-03 10:26:02 +00:00
|
|
|
|
2020-03-30 14:32:23 +00:00
|
|
|
const noticesApi = useMemo(
|
2020-05-10 23:41:10 +00:00
|
|
|
() => ( {
|
2021-07-23 13:21:59 +00:00
|
|
|
hasNoticesOfType: ( type: 'default' | 'snackbar' ): boolean => {
|
2020-05-10 23:41:10 +00:00
|
|
|
return currentNotices.current.some(
|
|
|
|
( notice ) => notice.type === type
|
|
|
|
);
|
|
|
|
},
|
|
|
|
removeNotices: ( status = null ) => {
|
2020-09-18 18:50:45 +00:00
|
|
|
currentNotices.current.forEach( ( notice ) => {
|
2020-05-10 23:41:10 +00:00
|
|
|
if ( status === null || notice.status === status ) {
|
|
|
|
removeNotice( notice.id );
|
|
|
|
}
|
|
|
|
} );
|
|
|
|
},
|
|
|
|
removeNotice,
|
|
|
|
} ),
|
|
|
|
[ removeNotice ]
|
|
|
|
);
|
|
|
|
|
|
|
|
const noticeCreators = useMemo(
|
2020-03-30 14:32:23 +00:00
|
|
|
() => ( {
|
2021-07-23 13:21:59 +00:00
|
|
|
addDefaultNotice: ( text: string, noticeProps = {} ) =>
|
2020-03-30 14:32:23 +00:00
|
|
|
void createNotice( 'default', text, {
|
|
|
|
...noticeProps,
|
|
|
|
} ),
|
2021-07-23 13:21:59 +00:00
|
|
|
addErrorNotice: ( text: string, noticeProps = {} ) =>
|
2020-03-30 14:32:23 +00:00
|
|
|
void createNotice( 'error', text, {
|
|
|
|
...noticeProps,
|
|
|
|
} ),
|
2021-07-23 13:21:59 +00:00
|
|
|
addWarningNotice: ( text: string, noticeProps = {} ) =>
|
2020-03-30 14:32:23 +00:00
|
|
|
void createNotice( 'warning', text, {
|
|
|
|
...noticeProps,
|
|
|
|
} ),
|
2021-07-23 13:21:59 +00:00
|
|
|
addInfoNotice: ( text: string, noticeProps = {} ) =>
|
2020-03-30 14:32:23 +00:00
|
|
|
void createNotice( 'info', text, {
|
|
|
|
...noticeProps,
|
|
|
|
} ),
|
2021-07-23 13:21:59 +00:00
|
|
|
addSuccessNotice: ( text: string, noticeProps = {} ) =>
|
2020-03-30 14:32:23 +00:00
|
|
|
void createNotice( 'success', text, {
|
|
|
|
...noticeProps,
|
|
|
|
} ),
|
|
|
|
} ),
|
2021-07-05 08:38:08 +00:00
|
|
|
[ createNotice ]
|
2020-03-30 14:32:23 +00:00
|
|
|
);
|
2020-03-03 10:26:02 +00:00
|
|
|
|
|
|
|
return {
|
|
|
|
notices,
|
|
|
|
...noticesApi,
|
2020-05-10 23:41:10 +00:00
|
|
|
...noticeCreators,
|
2020-05-14 23:55:22 +00:00
|
|
|
setIsSuppressed,
|
2020-03-03 10:26:02 +00:00
|
|
|
};
|
|
|
|
};
|