-
Notifications
You must be signed in to change notification settings - Fork 274
/
ReportError.js
153 lines (136 loc) · 4.08 KB
/
ReportError.js
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/**
* ReportError component.
*
* Site Kit by Google, Copyright 2021 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.
*/
/**
* External dependencies
*/
import PropTypes from 'prop-types';
import { uniqWith } from 'lodash';
/**
* WordPress dependencies
*/
import { Fragment } from '@wordpress/element';
import { __, sprintf } from '@wordpress/i18n';
/**
* Internal dependencies
*/
import Data from 'googlesitekit-data';
import { CORE_MODULES } from '../googlesitekit/modules/datastore/constants';
import {
isInsufficientPermissionsError,
getReportErrorMessage,
} from '../util/errors';
import { getInsufficientPermissionsErrorDescription } from '../util/insufficient-permissions-error-description';
import { purify } from '../util/purify';
import ErrorText from '../components/ErrorText';
import CTA from './notifications/CTA';
import ReportErrorActions from './ReportErrorActions';
import useViewOnly from '../hooks/useViewOnly';
const { useSelect } = Data;
export default function ReportError( { moduleSlug, error } ) {
const isViewOnly = useViewOnly();
const module = useSelect( ( select ) =>
select( CORE_MODULES ).getModule( moduleSlug )
);
const errors = Array.isArray( error ) ? error : [ error ];
let title;
const getMessage = ( err ) => {
if ( isInsufficientPermissionsError( err ) ) {
if ( isViewOnly ) {
title = sprintf(
/* translators: %s: module name */
__( 'Access lost to %s', 'google-site-kit' ),
module?.name
);
return sprintf(
/* translators: %s: module name */
__(
'The administrator sharing this module with you has lost access to the %s service, so you won’t be able to see stats from it on the Site Kit dashboard. You can contact them or another administrator to restore access.',
'google-site-kit'
),
module?.name
);
}
title = sprintf(
/* translators: %s: module name */
__( 'Insufficient permissions in %s', 'google-site-kit' ),
module?.name
);
return getInsufficientPermissionsErrorDescription(
err.message,
module
);
}
return getReportErrorMessage( err );
};
const uniqueErrors = uniqWith(
errors.map( ( err ) => ( {
...err,
message: getMessage( err ),
reconnectURL: err.data?.reconnectURL,
} ) ),
( errorA, errorB ) =>
errorA.message === errorB.message &&
errorA.reconnectURL === errorB.reconnectURL
);
const hasInsufficientPermissionsError = errors.some( ( err ) =>
isInsufficientPermissionsError( err )
);
if ( ! hasInsufficientPermissionsError && uniqueErrors.length === 1 ) {
title = sprintf(
/* translators: %s: module name */
__( 'Data error in %s', 'google-site-kit' ),
module?.name
);
} else if ( ! hasInsufficientPermissionsError && uniqueErrors.length > 1 ) {
title = sprintf(
/* translators: %s: module name */
__( 'Data errors in %s', 'google-site-kit' ),
module?.name
);
}
const description = (
<Fragment>
{ uniqueErrors.map( ( err ) => {
const reconnectURL = error?.data?.reconnectURL;
return reconnectURL ? (
<ErrorText
key={ err.message }
message={ err.message }
reconnectURL={ reconnectURL }
/>
) : (
<p key={ err.message }>
{ purify.sanitize( err.message, { ALLOWED_TAGS: [] } ) }
</p>
);
} ) }
</Fragment>
);
return (
<CTA title={ title } description={ description } error>
<ReportErrorActions moduleSlug={ moduleSlug } error={ error } />
</CTA>
);
}
ReportError.propTypes = {
moduleSlug: PropTypes.string.isRequired,
error: PropTypes.oneOfType( [
PropTypes.arrayOf( PropTypes.object ),
PropTypes.object,
] ).isRequired,
};