-
Notifications
You must be signed in to change notification settings - Fork 362
Expand file tree
/
Copy pathSetupErrorMessageNotification.js
More file actions
134 lines (123 loc) · 4.01 KB
/
Copy pathSetupErrorMessageNotification.js
File metadata and controls
134 lines (123 loc) · 4.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/**
* SetupErrorMessageNotification component.
*
* Site Kit by Google, Copyright 2024 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';
import { useCallback } from '@wordpress/element';
/**
* Internal dependencies
*/
import { useSelect, useRegistry } from 'googlesitekit-data';
import {
CORE_USER,
FORM_TEMPORARY_PERSIST_PERMISSION_ERROR,
} from '@/js/googlesitekit/datastore/user/constants';
import { CORE_SITE } from '@/js/googlesitekit/datastore/site/constants';
import useViewContext from '@/js/hooks/useViewContext';
import BannerNotification, {
TYPES,
} from '@/js/googlesitekit/notifications/components/layout/BannerNotification';
import { snapshotAllStores } from '@/js/googlesitekit/data/create-snapshot-store';
import useFormValue from '@/js/hooks/useFormValue';
export default function SetupErrorMessageNotification( { Notification } ) {
const id = 'setup_error';
const viewContext = useViewContext();
const isAuthenticated = useSelect( ( select ) =>
select( CORE_USER ).isAuthenticated()
);
// These will be `null` if no errors exist.
const setupErrorCode = useSelect( ( select ) =>
select( CORE_SITE ).getSetupErrorCode()
);
const setupErrorMessage = useSelect( ( select ) =>
select( CORE_SITE ).getSetupErrorMessage()
);
const [ temporaryPersistedPermissionsError ] = useFormValue(
FORM_TEMPORARY_PERSIST_PERMISSION_ERROR,
'permissionsError'
);
const setupErrorRedoURL = useSelect( ( select ) => {
if ( temporaryPersistedPermissionsError?.data ) {
return select( CORE_USER ).getConnectURL( {
additionalScopes:
temporaryPersistedPermissionsError?.data?.scopes,
redirectURL:
temporaryPersistedPermissionsError?.data?.redirectURL ||
global.location.href,
} );
} else if (
setupErrorCode === 'access_denied' &&
! temporaryPersistedPermissionsError?.data &&
isAuthenticated
) {
return null;
}
return select( CORE_SITE ).getSetupErrorRedoURL();
} );
const errorTroubleshootingLinkURL = useSelect( ( select ) =>
select( CORE_SITE ).getErrorTroubleshootingLinkURL( {
code: setupErrorCode,
} )
);
let title = __( 'Error connecting Site Kit', 'google-site-kit' );
let ctaLabel = __( 'Redo the plugin setup', 'google-site-kit' );
if ( setupErrorCode === 'access_denied' ) {
title = __( 'Permissions Error', 'google-site-kit' );
if ( temporaryPersistedPermissionsError?.data ) {
ctaLabel = __( 'Grant permission', 'google-site-kit' );
} else if (
! temporaryPersistedPermissionsError?.data &&
isAuthenticated
) {
ctaLabel = null;
}
}
const registry = useRegistry();
const snapshotCoreFormsStore = useCallback( async () => {
if ( temporaryPersistedPermissionsError?.data ) {
// Snapshot `CORE_FORMS` store to ensure the form data with current error data
// is retained across page navigations.
await snapshotAllStores( registry );
}
}, [ temporaryPersistedPermissionsError, registry ] );
const gaTrackingProps = {
gaTrackingEventArgs: { category: `${ viewContext }_${ id }` },
};
return (
<Notification { ...gaTrackingProps }>
<BannerNotification
notificationID={ id }
title={ title }
type={ TYPES.ERROR }
description={ setupErrorMessage }
ctaButton={
setupErrorRedoURL && {
label: ctaLabel,
href: setupErrorRedoURL,
onClick: snapshotCoreFormsStore,
}
}
learnMoreLink={ {
label: __( 'Get help', 'google-site-kit' ),
href: errorTroubleshootingLinkURL,
} }
/>
</Notification>
);
}