Skip to content

Commit d74bbc1

Browse files
Fix SEO hook and Result type errors
The following errors were found and fixed: - `gtag` was not defined in `useSEO.ts`. - Web Vitals functions (`getCLS`, `getFID`, etc.) were not correctly imported or available in `useSEO.ts`. - Type errors in `Result.ts` related to assigning `undefined` and handling generic error types.
1 parent 918e31b commit d74bbc1

File tree

4 files changed

+43
-29
lines changed

4 files changed

+43
-29
lines changed

package-lock.json

Lines changed: 10 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@
7676
"tailwindcss-animate": "^1.0.7",
7777
"vaul": "^0.9.3",
7878
"vitest": "^3.2.4",
79+
"web-vitals": "^5.1.0",
7980
"zod": "^3.23.8"
8081
},
8182
"devDependencies": {

src/hooks/useSEO.ts

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -179,14 +179,19 @@ export const useSEO = (pageData: SEOPageData) => {
179179
]);
180180
};
181181

182+
// Declare gtag function
183+
declare global {
184+
function gtag(...args: any[]): void;
185+
}
186+
182187
// Hook for tracking SEO events
183188
export const useSEOAnalytics = () => {
184189
const location = useLocation();
185190

186191
const trackPageView = useCallback((title: string) => {
187192
// Google Analytics 4
188-
if (typeof gtag !== 'undefined') {
189-
gtag('config', 'GA_MEASUREMENT_ID', {
193+
if (typeof window !== 'undefined' && typeof (window as any).gtag === 'function') {
194+
(window as any).gtag('config', 'GA_MEASUREMENT_ID', {
190195
page_title: title,
191196
page_location: window.location.href
192197
});
@@ -208,8 +213,8 @@ export const useSEOAnalytics = () => {
208213
label?: string,
209214
value?: number
210215
) => {
211-
if (typeof gtag !== 'undefined') {
212-
gtag('event', action, {
216+
if (typeof window !== 'undefined' && typeof (window as any).gtag === 'function') {
217+
(window as any).gtag('event', action, {
213218
event_category: 'SEO',
214219
event_label: label,
215220
value: value
@@ -228,12 +233,12 @@ export const useCoreWebVitals = () => {
228233
useEffect(() => {
229234
const reportWebVitals = async () => {
230235
try {
231-
const { getCLS, getFID, getFCP, getLCP, getTTFB } = await import('web-vitals');
236+
const { onCLS, onINP, onFCP, onLCP, onTTFB } = await import('web-vitals');
232237

233238
const sendToAnalytics = ({ name, value, rating, delta, id }: any) => {
234239
// Send to Google Analytics
235-
if (typeof gtag !== 'undefined') {
236-
gtag('event', name, {
240+
if (typeof window !== 'undefined' && typeof (window as any).gtag === 'function') {
241+
(window as any).gtag('event', name, {
237242
event_category: 'Web Vitals',
238243
event_label: rating,
239244
value: Math.round(value),
@@ -254,11 +259,11 @@ export const useCoreWebVitals = () => {
254259
}
255260
};
256261

257-
getCLS(sendToAnalytics);
258-
getFID(sendToAnalytics);
259-
getFCP(sendToAnalytics);
260-
getLCP(sendToAnalytics);
261-
getTTFB(sendToAnalytics);
262+
onCLS(sendToAnalytics);
263+
onINP(sendToAnalytics);
264+
onFCP(sendToAnalytics);
265+
onLCP(sendToAnalytics);
266+
onTTFB(sendToAnalytics);
262267
} catch (error) {
263268
console.warn('Web Vitals not available:', error);
264269
}

src/utils/Result.ts

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,20 @@ export class Result<T, E = Error> {
66
private readonly _isSuccess: boolean = false
77
) {}
88

9-
static success<T>(value: T): Result<T> {
10-
return new Result(value, undefined, true);
9+
static success<T, E = Error>(value: T): Result<T, E> {
10+
return new Result<T, E>(value, undefined, true);
1111
}
1212

13-
static failure<E>(error: E): Result<never, E> {
14-
return new Result<never, E>(undefined, error, false);
13+
static failure<T = never, E = Error>(error: E): Result<T, E> {
14+
return new Result<T, E>(undefined, error, false);
1515
}
1616

1717
static async fromPromise<T>(promise: Promise<T>): Promise<Result<T, Error>> {
1818
try {
1919
const value = await promise;
20-
return Result.success(value);
20+
return Result.success<T, Error>(value);
2121
} catch (error) {
22-
return Result.failure(error instanceof Error ? error : new Error(String(error)));
22+
return Result.failure<T, Error>(error instanceof Error ? error : new Error(String(error)));
2323
}
2424
}
2525

@@ -49,28 +49,29 @@ export class Result<T, E = Error> {
4949
map<U>(fn: (value: T) => U): Result<U, E> {
5050
if (this.isSuccess()) {
5151
try {
52-
return Result.success(fn(this.value));
52+
const newValue = fn(this.value);
53+
return Result.success<U, E>(newValue);
5354
} catch (error) {
54-
return Result.failure(error as E);
55+
return Result.failure<U, E>(error as E);
5556
}
5657
}
57-
return Result.failure(this.error);
58+
return Result.failure<U, E>(this.error);
5859
}
5960

6061
// Transform the error if failure, otherwise return the same success
6162
mapError<F>(fn: (error: E) => F): Result<T, F> {
6263
if (this.isFailure()) {
63-
return Result.failure(fn(this.error));
64+
return Result.failure<T, F>(fn(this.error));
6465
}
65-
return Result.success(this.value);
66+
return Result.success<T, F>(this.value);
6667
}
6768

6869
// Chain operations that return Results
6970
flatMap<U>(fn: (value: T) => Result<U, E>): Result<U, E> {
7071
if (this.isSuccess()) {
7172
return fn(this.value);
7273
}
73-
return Result.failure(this.error);
74+
return Result.failure<U, E>(this.error);
7475
}
7576

7677
// Provide a default value if failure
@@ -118,12 +119,12 @@ export class Results {
118119

119120
for (let i = 0; i < results.length; i++) {
120121
if (results[i].isFailure()) {
121-
return Result.failure(results[i].error);
122+
return Result.failure<T, any>(results[i].error);
122123
}
123124
(values as any)[i] = results[i].value;
124125
}
125126

126-
return Result.success(values);
127+
return Result.success<T, any>(values);
127128
}
128129

129130
// Get the first success or the last failure

0 commit comments

Comments
 (0)