mirror of
https://github.com/MODSetter/SurfSense.git
synced 2026-04-28 02:23:53 +02:00
refactor: update Document and Connector selection components
- Replaced the use of search source connectors with document types for improved clarity and functionality. - Enhanced UI elements for document type selection, including better styling and loading states. - Updated selection logic to handle document types and their counts effectively. - Improved accessibility and user experience in the document and connector selection dialogs.
This commit is contained in:
parent
a09309af2a
commit
430249366c
2 changed files with 291 additions and 85 deletions
97
surfsense_web/hooks/use-document-types.ts
Normal file
97
surfsense_web/hooks/use-document-types.ts
Normal file
|
|
@ -0,0 +1,97 @@
|
|||
import { useCallback, useEffect, useState } from "react";
|
||||
|
||||
export interface DocumentTypeCount {
|
||||
type: string;
|
||||
count: number;
|
||||
}
|
||||
|
||||
/**
|
||||
* Hook to fetch document type counts from the API
|
||||
* @param searchSpaceId - The search space ID to filter document types
|
||||
* @param lazy - If true, types won't be fetched on mount
|
||||
*/
|
||||
export const useDocumentTypes = (searchSpaceId?: number, lazy: boolean = false) => {
|
||||
const [documentTypes, setDocumentTypes] = useState<DocumentTypeCount[]>([]);
|
||||
const [isLoading, setIsLoading] = useState(!lazy);
|
||||
const [isLoaded, setIsLoaded] = useState(false);
|
||||
const [error, setError] = useState<Error | null>(null);
|
||||
|
||||
const fetchDocumentTypes = useCallback(
|
||||
async (spaceId?: number) => {
|
||||
if (isLoaded && lazy) return;
|
||||
|
||||
try {
|
||||
setIsLoading(true);
|
||||
setError(null);
|
||||
const token = localStorage.getItem("surfsense_bearer_token");
|
||||
|
||||
if (!token) {
|
||||
throw new Error("No authentication token found");
|
||||
}
|
||||
|
||||
// Build URL with optional search_space_id query parameter
|
||||
const url = new URL(
|
||||
`${process.env.NEXT_PUBLIC_FASTAPI_BACKEND_URL}/api/v1/documents/type-counts/`
|
||||
);
|
||||
if (spaceId !== undefined) {
|
||||
url.searchParams.append("search_space_id", spaceId.toString());
|
||||
}
|
||||
|
||||
const response = await fetch(url.toString(), {
|
||||
method: "GET",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
Authorization: `Bearer ${token}`,
|
||||
},
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(`Failed to fetch document types: ${response.statusText}`);
|
||||
}
|
||||
|
||||
const data = await response.json();
|
||||
|
||||
// Convert the object to an array of DocumentTypeCount
|
||||
const typeCounts: DocumentTypeCount[] = Object.entries(data).map(([type, count]) => ({
|
||||
type,
|
||||
count: count as number,
|
||||
}));
|
||||
|
||||
setDocumentTypes(typeCounts);
|
||||
setIsLoaded(true);
|
||||
|
||||
return typeCounts;
|
||||
} catch (err) {
|
||||
setError(err instanceof Error ? err : new Error("An unknown error occurred"));
|
||||
console.error("Error fetching document types:", err);
|
||||
} finally {
|
||||
setIsLoading(false);
|
||||
}
|
||||
},
|
||||
[isLoaded, lazy]
|
||||
);
|
||||
|
||||
useEffect(() => {
|
||||
if (!lazy) {
|
||||
fetchDocumentTypes(searchSpaceId);
|
||||
}
|
||||
}, [lazy, fetchDocumentTypes, searchSpaceId]);
|
||||
|
||||
// Function to refresh the document types
|
||||
const refreshDocumentTypes = useCallback(
|
||||
async (spaceId?: number) => {
|
||||
setIsLoaded(false);
|
||||
await fetchDocumentTypes(spaceId !== undefined ? spaceId : searchSpaceId);
|
||||
},
|
||||
[fetchDocumentTypes, searchSpaceId]
|
||||
);
|
||||
|
||||
return {
|
||||
documentTypes,
|
||||
isLoading,
|
||||
isLoaded,
|
||||
error,
|
||||
fetchDocumentTypes,
|
||||
refreshDocumentTypes,
|
||||
};
|
||||
};
|
||||
Loading…
Add table
Add a link
Reference in a new issue