Skip to content

Commit 42c7887

Browse files
author
Juarez Mota
committed
refactor: update TopBarSupport to use useAuthStatus, add a timeout for fetching authentication headers, and centralize error reporting.
1 parent 5dda22e commit 42c7887

File tree

1 file changed

+49
-12
lines changed

1 file changed

+49
-12
lines changed

dotcom-rendering/src/components/TopBarSupport.importable.tsx

Lines changed: 49 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,10 @@ import {
2121
getPurchaseInfo,
2222
shouldHideSupportMessaging,
2323
} from '../lib/contributions';
24+
import { getOptionsHeaders } from '../lib/identity';
2425
import { getHeader } from '../lib/sdcRequests';
2526
import { useBetaAB } from '../lib/useAB';
26-
import { useIsSignedIn } from '../lib/useAuthStatus';
27+
import { useAuthStatus } from '../lib/useAuthStatus';
2728
import { useCountryCode } from '../lib/useCountryCode';
2829
import { usePageViewId } from '../lib/usePageViewId';
2930
import { useConfig } from './ConfigContext';
@@ -57,11 +58,26 @@ const ReaderRevenueLinksRemote = ({
5758
useState<ModuleData<HeaderProps> | null>(null);
5859
const [SupportHeader, setSupportHeader] =
5960
useState<React.ElementType<HeaderProps> | null>(null);
60-
const isSignedIn = useIsSignedIn();
61+
const authStatus = useAuthStatus();
62+
const isSignedIn =
63+
authStatus.kind === 'Pending'
64+
? 'Pending'
65+
: authStatus.kind === 'SignedIn';
6166

6267
const { renderingTarget } = useConfig();
6368
const abTests = useBetaAB();
6469

70+
const reportError = (error: unknown, context: string) => {
71+
const msg = `Error importing RR header links for ${context}: ${String(
72+
error,
73+
)}`;
74+
console.log(msg);
75+
window.guardian.modules.sentry.reportError(
76+
new Error(msg),
77+
'rr-header-links',
78+
);
79+
};
80+
6581
useEffect((): void => {
6682
if (isUndefined(countryCode) || isSignedIn === 'Pending') {
6783
return;
@@ -92,9 +108,35 @@ const ReaderRevenueLinksRemote = ({
92108
},
93109
};
94110

95-
void getAuthHeaders()
96-
.then((headers) =>
97-
getHeader(contributionsServiceUrl, requestData, headers),
111+
const headers =
112+
authStatus.kind === 'SignedIn'
113+
? getOptionsHeaders(authStatus).headers
114+
: undefined;
115+
116+
const fetchAuthHeadersWithTimeout = () => {
117+
return Promise.race([
118+
getAuthHeaders(),
119+
new Promise<undefined>((resolve) => {
120+
setTimeout(() => {
121+
resolve(undefined);
122+
}, 2000);
123+
}),
124+
]);
125+
};
126+
127+
void fetchAuthHeadersWithTimeout()
128+
.catch((error: unknown) => {
129+
// Catch any errors from getAuthHeaders itself or the timeout
130+
reportError(error, 'getAuthHeaders');
131+
return undefined;
132+
})
133+
.then((fallbackHeaders) =>
134+
// Fallback to fallbackHeaders if present, otherwise use real headers
135+
getHeader(
136+
contributionsServiceUrl,
137+
requestData,
138+
fallbackHeaders ?? headers,
139+
),
98140
)
99141
.then((response: ModuleDataResponse<HeaderProps>) => {
100142
if (!response.data) {
@@ -121,13 +163,7 @@ const ReaderRevenueLinksRemote = ({
121163
);
122164
})
123165
.catch((error) => {
124-
const msg = `Error importing RR header links: ${String(error)}`;
125-
126-
console.log(msg);
127-
window.guardian.modules.sentry.reportError(
128-
new Error(msg),
129-
'rr-header-links',
130-
);
166+
reportError(error, 'fetching headers or importing component');
131167
});
132168
}, [
133169
countryCode,
@@ -136,6 +172,7 @@ const ReaderRevenueLinksRemote = ({
136172
pageViewId,
137173
pageUrl,
138174
abTests,
175+
authStatus,
139176
]);
140177

141178
if (SupportHeader !== null && supportHeaderResponse) {

0 commit comments

Comments
 (0)