-
Notifications
You must be signed in to change notification settings - Fork 0
/
gatsby-browser.jsx
125 lines (118 loc) · 4.19 KB
/
gatsby-browser.jsx
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
import "@fontsource/righteous";
// Supports weights 100-900 in a single small file.
import "@fontsource-variable/roboto-flex";
import "./global-styles.css";
import React, { StrictMode } from "react";
import mixpanel from "mixpanel-browser";
import { ErrorBoundary } from "react-error-boundary";
import { MutationCache, QueryCache, QueryClient, QueryClientProvider } from "@tanstack/react-query";
import toast from "react-hot-toast";
import { enableMapSet } from "immer";
import { LoginProvider } from "./src/context/LoginContext";
import { MixpanelProvider } from "./src/context/MixpanelContext";
import { UserInfoProvider } from "./src/context/UserInfoContext";
// Enable Map/Set in immer
enableMapSet();
// Create a query client
const queryClient = new QueryClient({
queryCache: new QueryCache({
onError: (error, query) => {
console.error(error);
if (error?.cause?.code === 429) {
toast.error("Traffic is high right now. Try again in a few minutes!", { id: "tooManyRequests" });
} else if (error?.cause?.code === 403) {
toast.error("User not authenticated. Please login!", { id: "unauthenticated" });
} else if (error?.cause?.code === 401) {
toast.error("User not authenticated. Please login!", { id: "unauthenticated" });
} else if (query?.meta?.errorMessage) {
toast.error(query.meta.errorMessage, { id: query.meta.errorMessage });
} else if (query?.meta?.errorMessage === false) {
// do nothing
} else if (error?.message) {
toast.error(error.message, { id: error.message });
} else {
toast.error("Unknown error occured");
}
},
onSuccess: (data, query) => {
if (query.meta?.successMessage) {
toast.success(query.meta.successMessage);
}
},
}),
mutationCache: new MutationCache({
onError: (error, variables, context, mutation) => {
console.error(error);
if (error?.cause?.code === 429) {
toast.error("Traffic is high right now. Try again in a few minutes!", { id: "429" });
} else if (error?.cause?.code === 403) {
toast.error("User not authenticated. Please login!", { id: "403" });
} else if (mutation?.meta?.errorMessage) {
toast.error(mutation.meta.errorMessage);
} else if (mutation?.meta?.errorMessage === false) {
// do nothing
} else if (error.message) {
toast.error(error.message);
} else {
toast.error("Unknown error occured");
}
},
onSuccess: (data, variables, context, mutation) => {
if (mutation.meta?.successMessage) {
toast.success(mutation.meta.successMessage);
}
},
}),
defaultOptions: {
queries: {
retryDelay: (attemptIndex, error) => {
let startingDelay = 1000;
let maxDelay = 30000;
if (error?.cause?.code === 429) {
startingDelay = 5000;
maxDelay = 60000;
}
Math.min(startingDelay * 2 ** attemptIndex, maxDelay);
},
},
},
});
if (process.env.GATSBY_MIXPANEL_TOKEN && process.env.GATSBY_BACKEND_URL) {
if (process.env.NODE_ENV !== "production") {
mixpanel.init(process.env.GATSBY_MIXPANEL_TOKEN, {
debug: true,
});
mixpanel.disable();
} else {
mixpanel.init(process.env.GATSBY_MIXPANEL_TOKEN);
}
mixpanel.set_config({ persistence: "localStorage", ignore_dnt: true, api_host: process.env.GATSBY_BACKEND_URL });
} else {
console.warn("Mixpanel token not set");
}
// export function wrapPageElement({ element }) {
// return (
// <>
// <Toaster position="top-right" containerStyle={{ marginTop: "60px" }} />
// {element}
// </>
// );
// }
// eslint-disable-next-line import/prefer-default-export
export function wrapRootElement({ element }) {
return (
<StrictMode>
<ErrorBoundary fallback={<div>Something went wrong</div>}>
{/* <SpotifyInterceptor> */}
<MixpanelProvider mixpanel={mixpanel}>
<LoginProvider>
<QueryClientProvider client={queryClient}>
<UserInfoProvider>{element}</UserInfoProvider>
</QueryClientProvider>
</LoginProvider>
</MixpanelProvider>
{/* </SpotifyInterceptor> */}
</ErrorBoundary>
</StrictMode>
);
}