This commit is contained in:
Claire Chabas
2025-01-13 19:44:04 +01:00
parent bc9a8146ac
commit d4257ba5f0
5 changed files with 36 additions and 25 deletions
@@ -192,31 +192,14 @@ export function InsightsProvider(props: InsightsProviderProps) {
React.useEffect(() => {
getVisitorId().then((visitorId) => {
visitorIdRef.current = visitorId;
// When the page is unloaded, flush all events, but only if the visitor ID is set
window.addEventListener('beforeunload', flushEventsSync);
});
}, []);
// When the page is unloaded, flush all events
React.useEffect(() => {
window.addEventListener('beforeunload', flushEventsSync);
return () => {
window.removeEventListener('beforeunload', flushEventsSync);
};
}, [flushEventsSync]);
/**
* Get the visitor ID and store it in a ref.
*/
// React.useEffect(() => {
// getVisitorId().then((visitorId) => {
// visitorIdRef.current = visitorId;
// // When the page is unloaded, flush all events, but only if the visitor ID is set
// window.addEventListener('beforeunload', flushEventsSync);
// });
// return () => {
// window.removeEventListener('beforeunload', flushEventsSync);
// };
// }, [flushEventsSync]);
return (
<InsightsContext.Provider value={trackEvent}>
<OpenAPIOperationContextProvider
@@ -81,13 +81,27 @@ export const SearchResults = React.forwardRef(function SearchResults(
let cancelled = false;
getRecommendedQuestions(spaceId).then((questions) => {
if (!questions) {
if (!cancelled) {
setResults([]);
setIsLoading(false);
}
captureException(
new Error(`corrupt-cache: getRecommendedQuestions is ${questions}`),
);
return;
}
const results = questions.map((question) => ({
type: 'recommended-question',
id: question,
question: question,
})) satisfies ResultType[];
suggestedQuestionsRef.current = results;
if (cancelled) {
setIsLoading(false);
return;
}
@@ -104,13 +118,30 @@ export const SearchResults = React.forwardRef(function SearchResults(
setIsLoading(false);
}
let cancelled = false;
debounceTimeout.current = setTimeout(async () => {
const fetchedResults = await (global
? searchAllSiteContent(query, pointer)
: searchSiteSpaceContent(query, pointer, revisionId));
setResults(withAsk ? withQuestionResult(fetchedResults, query) : fetchedResults);
if (cancelled) {
return;
}
if (!results) {
captureException(
new Error(
`corrupt-cache: ${global ? 'searchAllSiteContent' : 'searchSiteSpaceContent'} is ${results}`,
),
{ extra: { results } },
);
setResults([]);
setIsLoading(false);
return;
}
setResults(withAsk ? withQuestionResult(fetchedResults, query) : fetchedResults);
setIsLoading(false);
trackEvent({
@@ -120,6 +151,8 @@ export const SearchResults = React.forwardRef(function SearchResults(
}, 350);
return () => {
cancelled = true;
if (debounceTimeout.current) {
clearTimeout(debounceTimeout.current);
debounceTimeout.current = null;
@@ -59,8 +59,6 @@ async function searchSiteContent(args: {
cacheBust?: string;
}): Promise<OrderedComputedResult[]> {
const { pointer, scope, query, cacheBust } = args;
console.log('searchSiteContent called');
console.log('searchSiteContent', { pointer, scope, query, cacheBust });
if (query.length <= 1) {
return [];
@@ -76,8 +76,6 @@ export async function SpaceLayout(props: {
};
const apiHost = (await api()).client.endpoint;
const visitorAuthToken = await getCurrentVisitorToken();
console.log('loading SpaceLayout');
console.log('Auth...', visitorAuthToken);
const enabled = await shouldTrackEvents();
return (
-1
View File
@@ -10,7 +10,6 @@ import { filterOutNullable } from './typescript';
export async function getContentSecurityPolicyNonce(): Promise<string> {
const headersList = await headers();
const nonce = headersList.get('x-nonce');
if (!nonce) {
throw new Error('No nonce found in headers');
}