mirror of
https://github.com/MODSetter/SurfSense.git
synced 2026-07-22 23:31:12 +02:00
- Updated all hooks to import and use AUTH_TOKEN_KEY constant from lib/constants - Updated all components to use AUTH_TOKEN_KEY instead of hardcoded string - Updated all atoms to use AUTH_TOKEN_KEY - Updated all app pages to use AUTH_TOKEN_KEY - Made CORS origin configurable via CORS_ORIGINS environment variable - Added CORS_ORIGINS to .env.example with documentation This improves maintainability by centralizing the auth token key string, preventing typos and making it easier to change the key name if needed.
83 lines
2 KiB
TypeScript
83 lines
2 KiB
TypeScript
"use client";
|
|
|
|
import { useEffect, useState } from "react";
|
|
import { toast } from "sonner";
|
|
import { AUTH_TOKEN_KEY } from "@/lib/constants";
|
|
|
|
interface SearchSpace {
|
|
id: number;
|
|
name: string;
|
|
description: string;
|
|
created_at: string;
|
|
// Add other fields from your SearchSpaceRead model
|
|
}
|
|
|
|
export function useSearchSpaces() {
|
|
const [searchSpaces, setSearchSpaces] = useState<SearchSpace[]>([]);
|
|
const [loading, setLoading] = useState(true);
|
|
const [error, setError] = useState<string | null>(null);
|
|
|
|
useEffect(() => {
|
|
const fetchSearchSpaces = async () => {
|
|
try {
|
|
setLoading(true);
|
|
const response = await fetch(
|
|
`${process.env.NEXT_PUBLIC_FASTAPI_BACKEND_URL}/api/v1/searchspaces`,
|
|
{
|
|
headers: {
|
|
Authorization: `Bearer ${localStorage.getItem(AUTH_TOKEN_KEY)}`,
|
|
},
|
|
method: "GET",
|
|
}
|
|
);
|
|
|
|
if (!response.ok) {
|
|
toast.error("Not authenticated");
|
|
throw new Error("Not authenticated");
|
|
}
|
|
|
|
const data = await response.json();
|
|
setSearchSpaces(data);
|
|
setError(null);
|
|
} catch (err: any) {
|
|
setError(err.message || "Failed to fetch search spaces");
|
|
console.error("Error fetching search spaces:", err);
|
|
} finally {
|
|
setLoading(false);
|
|
}
|
|
};
|
|
|
|
fetchSearchSpaces();
|
|
}, []);
|
|
|
|
// Function to refresh the search spaces list
|
|
const refreshSearchSpaces = async () => {
|
|
setLoading(true);
|
|
try {
|
|
const response = await fetch(
|
|
`${process.env.NEXT_PUBLIC_FASTAPI_BACKEND_URL}/api/v1/searchspaces`,
|
|
{
|
|
headers: {
|
|
Authorization: `Bearer ${localStorage.getItem(AUTH_TOKEN_KEY)}`,
|
|
},
|
|
method: "GET",
|
|
}
|
|
);
|
|
|
|
if (!response.ok) {
|
|
toast.error("Not authenticated");
|
|
throw new Error("Not authenticated");
|
|
}
|
|
|
|
const data = await response.json();
|
|
setSearchSpaces(data);
|
|
setError(null);
|
|
} catch (err: any) {
|
|
setError(err.message || "Failed to fetch search spaces");
|
|
} finally {
|
|
setLoading(false);
|
|
}
|
|
};
|
|
|
|
return { searchSpaces, loading, error, refreshSearchSpaces };
|
|
}
|