SurfSense/surfsense_web/hooks/use-documents.ts

79 lines
1.9 KiB
TypeScript
Raw Normal View History

"use client";
2025-07-27 10:44:27 -07:00
import { useCallback, useEffect, useState } from "react";
import { toast } from "sonner";
import { authenticatedFetch } from "@/lib/auth-utils";
2025-04-07 23:47:06 -07:00
export interface Document {
2025-07-27 10:05:37 -07:00
id: number;
title: string;
document_type: DocumentType;
document_metadata: any;
content: string;
created_at: string;
search_space_id: number;
2025-04-07 23:47:06 -07:00
}
export type DocumentType =
2025-07-27 10:05:37 -07:00
| "EXTENSION"
| "CRAWLED_URL"
| "SLACK_CONNECTOR"
| "NOTION_CONNECTOR"
| "FILE"
| "YOUTUBE_VIDEO"
| "GITHUB_CONNECTOR"
| "LINEAR_CONNECTOR"
| "DISCORD_CONNECTOR"
2025-07-29 11:46:40 -07:00
| "JIRA_CONNECTOR"
| "CONFLUENCE_CONNECTOR"
| "CLICKUP_CONNECTOR"
| "GOOGLE_CALENDAR_CONNECTOR"
| "GOOGLE_GMAIL_CONNECTOR"
2025-09-28 14:59:10 -07:00
| "AIRTABLE_CONNECTOR"
2025-10-12 09:39:04 +05:30
| "LUMA_CONNECTOR"
| "ELASTICSEARCH_CONNECTOR";
2025-04-07 23:47:06 -07:00
export interface UseDocumentsOptions {
page?: number;
pageSize?: number;
lazy?: boolean;
2025-10-21 21:53:55 -07:00
documentTypes?: string[];
}
2025-10-01 18:50:36 -07:00
export function useDocuments(searchSpaceId: number, options?: UseDocumentsOptions | boolean) {
// Support both old boolean API and new options API for backward compatibility
const opts = typeof options === "boolean" ? { lazy: options } : options || {};
2025-07-27 10:05:37 -07:00
const [error, setError] = useState<string | null>(null);
2025-07-27 10:05:37 -07:00
// Function to delete a document
const deleteDocument = useCallback(
async (documentId: number) => {
try {
const response = await authenticatedFetch(
2025-07-27 10:05:37 -07:00
`${process.env.NEXT_PUBLIC_FASTAPI_BACKEND_URL}/api/v1/documents/${documentId}`,
{ method: "DELETE" }
2025-07-27 10:05:37 -07:00
);
2025-04-07 23:47:06 -07:00
2025-07-27 10:05:37 -07:00
if (!response.ok) {
toast.error("Failed to delete document");
throw new Error("Failed to delete document");
}
2025-07-27 10:05:37 -07:00
toast.success("Document deleted successfully");
// Note: The caller should handle refetching the documents list
return true;
2025-10-21 21:53:55 -07:00
} catch (err: any) {
toast.error(err.message || "Failed to delete document");
console.error("Error deleting document:", err);
return false;
2025-10-21 21:53:55 -07:00
}
},
[]
);
2025-10-21 21:53:55 -07:00
2025-07-27 10:05:37 -07:00
return {
error,
deleteDocument,
};
}