xonecas.io
53129
Pa

PAT / commit a726b30

refactor: pre-commit is broken

xonecas2025-12-19 a726b30ad5022c02a0c6142ad0cd3310e4ab1610 parent f4fe683

3 files changed

src/components/Notification.tsx+19−0
@@ -0,0 +1,19 @@
1+"use client";
2+
3+import type { ReactNode } from "react";
4+
5+type NotificationProps = {
6+ icon?: ReactNode;
7+ children: ReactNode;
8+ action?: ReactNode;
9+};
10+
11+export default function Notification({ icon, children, action }: NotificationProps) {
12+ return (
13+ <div className="pointer-events-auto mb-2 flex w-fit items-center gap-2 rounded-lg bg-neutral-300/80 px-3 py-1 dark:bg-neutral-800/80">
14+ {icon}
15+ {children}
16+ {action}
17+ </div>
18+ );
19+}
src/hooks/useFetch.ts+59−0
@@ -0,0 +1,59 @@
1+import { useCallback } from "react";
2+import { getErrorMessage, getParseErrorMessage, handleError } from "@/src/lib/errors";
3+
4+export interface FetchOptions extends RequestInit {
5+ errorMessage?: string;
6+ throwOnError?: boolean;
7+ onError?: (error: string) => void;
8+}
9+
10+/**
11+ * Custom hook that provides a reusable fetch function with standardized error handling.
12+ * Handles response validation, JSON parsing, and error callbacks.
13+ *
14+ * @returns A fetch function that handles errors automatically
15+ */
16+export function useFetch() {
17+ const fetchWithErrorHandling = useCallback(
18+ async <T = unknown>(url: string, options: FetchOptions = {}): Promise<T | null> => {
19+ const { errorMessage, throwOnError, onError, ...fetchOptions } = options;
20+
21+ try {
22+ const res = await fetch(url, fetchOptions);
23+ if (!res.ok) {
24+ const defaultError = errorMessage || `Failed to fetch: ${res.status}`;
25+ try {
26+ const errorData = await res.json();
27+ throw new Error(errorData.error || defaultError);
28+ } catch (parseError) {
29+ if (parseError instanceof Error && parseError.message !== defaultError) {
30+ throw parseError;
31+ }
32+ throw new Error(getParseErrorMessage(parseError));
33+ }
34+ }
35+ // Handle empty responses (e.g., 204 No Content)
36+ const contentType = res.headers.get("content-type");
37+ if (!contentType || !contentType.includes("application/json")) {
38+ return {} as T;
39+ }
40+ const text = await res.text();
41+ if (!text.trim()) {
42+ return {} as T;
43+ }
44+ const data = JSON.parse(text);
45+ return data as T;
46+ } catch (error) {
47+ const errorMsg = getErrorMessage(error, errorMessage || "Unknown error");
48+ handleError(errorMsg, onError);
49+ if (throwOnError) {
50+ throw error;
51+ }
52+ return null;
53+ }
54+ },
55+ []
56+ );
57+
58+ return fetchWithErrorHandling;
59+}
tsconfig.tsbuildinfo+1−1
@@ -1 +1 @@
1-{"fileNames":["./node_modules/typescript/lib/lib.es5.d.ts","./node_modules/typescript/lib/lib.es2015.d.ts","./node_modules/typescript/lib/lib.es2016.d.ts","./node_modules/typescript/lib/lib.es2017.d.ts","./node_modules/typescript/lib/lib.es2018.d.ts","./node_modules/typescript/lib/lib.es2019.d.ts","./node_modules/typescript/lib/lib.es2020.d.ts","./node_modules/typescript/lib/lib.es2021.d.ts","./node_modules/typescript/lib/lib.es2022.d.ts","./node_modules/typescript/lib/lib.es2023.d.ts","./node_m …
1+{"fileNames":["./node_modules/typescript/lib/lib.es5.d.ts","./node_modules/typescript/lib/lib.es2015.d.ts","./node_modules/typescript/lib/lib.es2016.d.ts","./node_modules/typescript/lib/lib.es2017.d.ts","./node_modules/typescript/lib/lib.es2018.d.ts","./node_modules/typescript/lib/lib.es2019.d.ts","./node_modules/typescript/lib/lib.es2020.d.ts","./node_modules/typescript/lib/lib.es2021.d.ts","./node_modules/typescript/lib/lib.es2022.d.ts","./node_modules/typescript/lib/lib.es2023.d.ts","./node_m …