feat: monorepo

This commit is contained in:
DESKTOP-RTLN3BA\$punk 2025-04-07 23:47:06 -07:00
parent fe39077849
commit a1474ca49e
144 changed files with 43821 additions and 1 deletions

View file

@ -0,0 +1 @@
export * from './useSearchSourceConnectors';

View file

@ -0,0 +1,58 @@
import { useState, useEffect, useCallback } from 'react'
import { toast } from 'sonner'
interface UseApiKeyReturn {
apiKey: string | null
isLoading: boolean
copied: boolean
copyToClipboard: () => Promise<void>
}
export function useApiKey(): UseApiKeyReturn {
const [apiKey, setApiKey] = useState<string | null>(null)
const [copied, setCopied] = useState(false)
const [isLoading, setIsLoading] = useState(true)
useEffect(() => {
// Load API key from localStorage
const loadApiKey = () => {
try {
const token = localStorage.getItem('surfsense_bearer_token')
setApiKey(token)
} catch (error) {
console.error('Error loading API key:', error)
toast.error('Failed to load API key')
} finally {
setIsLoading(false)
}
}
// Add a small delay to simulate loading
const timer = setTimeout(loadApiKey, 500)
return () => clearTimeout(timer)
}, [])
const copyToClipboard = useCallback(async () => {
if (!apiKey) return
try {
await navigator.clipboard.writeText(apiKey)
setCopied(true)
toast.success('API key copied to clipboard')
setTimeout(() => {
setCopied(false)
}, 2000)
} catch (err) {
console.error('Failed to copy:', err)
toast.error('Failed to copy API key')
}
}, [apiKey])
return {
apiKey,
isLoading,
copied,
copyToClipboard
}
}

View file

@ -0,0 +1,117 @@
// Types for connector API
export interface ConnectorConfig {
[key: string]: string;
}
export interface Connector {
id: number;
name: string;
connector_type: string;
config: ConnectorConfig;
created_at: string;
user_id: string;
}
export interface CreateConnectorRequest {
name: string;
connector_type: string;
config: ConnectorConfig;
}
// Get connector type display name
export const getConnectorTypeDisplay = (type: string): string => {
const typeMap: Record<string, string> = {
"SERPER_API": "Serper API",
"TAVILY_API": "Tavily API",
// Add other connector types here as needed
};
return typeMap[type] || type;
};
// API service for connectors
export const ConnectorService = {
// Create a new connector
async createConnector(data: CreateConnectorRequest): Promise<Connector> {
const response = await fetch(`${process.env.NEXT_PUBLIC_FASTAPI_BACKEND_URL}/api/v1/search-source-connectors/`, {
method: "POST",
headers: {
"Content-Type": "application/json",
"Authorization": `Bearer ${localStorage.getItem("surfsense_bearer_token")}`,
},
body: JSON.stringify(data),
});
if (!response.ok) {
const errorData = await response.json();
throw new Error(errorData.detail || "Failed to create connector");
}
return response.json();
},
// Get all connectors
async getConnectors(skip = 0, limit = 100): Promise<Connector[]> {
const response = await fetch(`${process.env.NEXT_PUBLIC_FASTAPI_BACKEND_URL}/api/v1/search-source-connectors/?skip=${skip}&limit=${limit}`, {
headers: {
"Authorization": `Bearer ${localStorage.getItem("surfsense_bearer_token")}`,
},
});
if (!response.ok) {
const errorData = await response.json();
throw new Error(errorData.detail || "Failed to fetch connectors");
}
return response.json();
},
// Get a specific connector
async getConnector(connectorId: number): Promise<Connector> {
const response = await fetch(`${process.env.NEXT_PUBLIC_FASTAPI_BACKEND_URL}/api/v1/search-source-connectors/${connectorId}`, {
headers: {
"Authorization": `Bearer ${localStorage.getItem("surfsense_bearer_token")}`,
},
});
if (!response.ok) {
const errorData = await response.json();
throw new Error(errorData.detail || "Failed to fetch connector");
}
return response.json();
},
// Update a connector
async updateConnector(connectorId: number, data: CreateConnectorRequest): Promise<Connector> {
const response = await fetch(`${process.env.NEXT_PUBLIC_FASTAPI_BACKEND_URL}/api/v1/search-source-connectors/${connectorId}`, {
method: "PUT",
headers: {
"Content-Type": "application/json",
"Authorization": `Bearer ${localStorage.getItem("surfsense_bearer_token")}`,
},
body: JSON.stringify(data),
});
if (!response.ok) {
const errorData = await response.json();
throw new Error(errorData.detail || "Failed to update connector");
}
return response.json();
},
// Delete a connector
async deleteConnector(connectorId: number): Promise<void> {
const response = await fetch(`${process.env.NEXT_PUBLIC_FASTAPI_BACKEND_URL}/api/v1/search-source-connectors/${connectorId}`, {
method: "DELETE",
headers: {
"Authorization": `Bearer ${localStorage.getItem("surfsense_bearer_token")}`,
},
});
if (!response.ok) {
const errorData = await response.json();
throw new Error(errorData.detail || "Failed to delete connector");
}
},
};

View file

@ -0,0 +1,115 @@
"use client"
import { useState, useEffect } from 'react';
import { toast } from 'sonner';
export interface Document {
id: number;
title: string;
document_type: "EXTENSION" | "CRAWLED_URL" | "SLACK_CONNECTOR" | "NOTION_CONNECTOR" | "FILE";
document_metadata: any;
content: string;
created_at: string;
search_space_id: number;
}
export function useDocuments(searchSpaceId: number) {
const [documents, setDocuments] = useState<Document[]>([]);
const [loading, setLoading] = useState(true);
const [error, setError] = useState<string | null>(null);
useEffect(() => {
const fetchDocuments = async () => {
try {
setLoading(true);
const response = await fetch(
`${process.env.NEXT_PUBLIC_FASTAPI_BACKEND_URL}/api/v1/documents`,
{
headers: {
Authorization: `Bearer ${localStorage.getItem('surfsense_bearer_token')}`,
},
method: "GET",
}
);
if (!response.ok) {
toast.error("Failed to fetch documents");
throw new Error("Failed to fetch documents");
}
const data = await response.json();
setDocuments(data);
setError(null);
} catch (err: any) {
setError(err.message || 'Failed to fetch documents');
console.error('Error fetching documents:', err);
} finally {
setLoading(false);
}
};
if (searchSpaceId) {
fetchDocuments();
}
}, [searchSpaceId]);
// Function to refresh the documents list
const refreshDocuments = async () => {
setLoading(true);
try {
const response = await fetch(
`${process.env.NEXT_PUBLIC_FASTAPI_BACKEND_URL}/api/v1/documents`,
{
headers: {
Authorization: `Bearer ${localStorage.getItem('surfsense_bearer_token')}`,
},
method: "GET",
}
);
if (!response.ok) {
toast.error("Failed to fetch documents");
throw new Error("Failed to fetch documents");
}
const data = await response.json();
setDocuments(data);
setError(null);
} catch (err: any) {
setError(err.message || 'Failed to fetch documents');
console.error('Error fetching documents:', err);
} finally {
setLoading(false);
}
};
// Function to delete a document
const deleteDocument = async (documentId: number) => {
try {
const response = await fetch(
`${process.env.NEXT_PUBLIC_FASTAPI_BACKEND_URL}/api/v1/documents/${documentId}`,
{
headers: {
Authorization: `Bearer ${localStorage.getItem('surfsense_bearer_token')}`,
},
method: "DELETE",
}
);
if (!response.ok) {
toast.error("Failed to delete document");
throw new Error("Failed to delete document");
}
toast.success("Document deleted successfully");
// Update the local state after successful deletion
setDocuments(documents.filter(doc => doc.id !== documentId));
return true;
} catch (err: any) {
toast.error(err.message || 'Failed to delete document');
console.error('Error deleting document:', err);
return false;
}
};
return { documents, loading, error, refreshDocuments, deleteDocument };
}

View file

@ -0,0 +1,19 @@
import * as React from "react"
const MOBILE_BREAKPOINT = 768
export function useIsMobile() {
const [isMobile, setIsMobile] = React.useState<boolean | undefined>(undefined)
React.useEffect(() => {
const mql = window.matchMedia(`(max-width: ${MOBILE_BREAKPOINT - 1}px)`)
const onChange = () => {
setIsMobile(window.innerWidth < MOBILE_BREAKPOINT)
}
mql.addEventListener("change", onChange)
setIsMobile(window.innerWidth < MOBILE_BREAKPOINT)
return () => mql.removeEventListener("change", onChange)
}, [])
return !!isMobile
}

View file

@ -0,0 +1,75 @@
"use client"
import { useState, useEffect } from 'react';
import { toast } from 'sonner';
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('surfsense_bearer_token')}`,
},
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('surfsense_bearer_token')}`,
},
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 };
}

View file

@ -0,0 +1,302 @@
import { useState, useEffect, useCallback } from 'react';
export interface SearchSourceConnector {
id: number;
name: string;
connector_type: string;
is_indexable: boolean;
last_indexed_at: string | null;
config: Record<string, any>;
user_id?: string;
created_at?: string;
}
export interface ConnectorSourceItem {
id: number;
name: string;
type: string;
sources: any[];
}
/**
* Hook to fetch search source connectors from the API
*/
export const useSearchSourceConnectors = () => {
const [connectors, setConnectors] = useState<SearchSourceConnector[]>([]);
const [isLoading, setIsLoading] = useState(true);
const [error, setError] = useState<Error | null>(null);
const [connectorSourceItems, setConnectorSourceItems] = useState<ConnectorSourceItem[]>([
{
id: 1,
name: "Crawled URL",
type: "CRAWLED_URL",
sources: [],
},
{
id: 2,
name: "File",
type: "FILE",
sources: [],
},
{
id: 3,
name: "Extension",
type: "EXTENSION",
sources: [],
}
]);
useEffect(() => {
const fetchConnectors = async () => {
try {
setIsLoading(true);
const token = localStorage.getItem('surfsense_bearer_token');
if (!token) {
throw new Error('No authentication token found');
}
const response = await fetch(
`${process.env.NEXT_PUBLIC_FASTAPI_BACKEND_URL}/api/v1/search-source-connectors/`,
{
method: 'GET',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${token}`
}
}
);
if (!response.ok) {
throw new Error(`Failed to fetch connectors: ${response.statusText}`);
}
const data = await response.json();
setConnectors(data);
// Update connector source items when connectors change
updateConnectorSourceItems(data);
} catch (err) {
setError(err instanceof Error ? err : new Error('An unknown error occurred'));
console.error('Error fetching search source connectors:', err);
} finally {
setIsLoading(false);
}
};
fetchConnectors();
}, []);
// Update connector source items when connectors change
const updateConnectorSourceItems = (currentConnectors: SearchSourceConnector[]) => {
// Start with the default hardcoded connectors
const defaultConnectors: ConnectorSourceItem[] = [
{
id: 1,
name: "Crawled URL",
type: "CRAWLED_URL",
sources: [],
},
{
id: 2,
name: "File",
type: "FILE",
sources: [],
},
{
id: 3,
name: "Extension",
type: "EXTENSION",
sources: [],
}
];
// Add the API connectors
const apiConnectors: ConnectorSourceItem[] = currentConnectors.map((connector, index) => ({
id: 1000 + index, // Use a high ID to avoid conflicts with hardcoded IDs
name: connector.name,
type: connector.connector_type,
sources: [],
}));
setConnectorSourceItems([...defaultConnectors, ...apiConnectors]);
};
/**
* Create a new search source connector
*/
const createConnector = async (connectorData: Omit<SearchSourceConnector, 'id' | 'user_id' | 'created_at'>) => {
try {
const token = localStorage.getItem('surfsense_bearer_token');
if (!token) {
throw new Error('No authentication token found');
}
const response = await fetch(
`${process.env.NEXT_PUBLIC_FASTAPI_BACKEND_URL}/api/v1/search-source-connectors/`,
{
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${token}`
},
body: JSON.stringify(connectorData)
}
);
if (!response.ok) {
throw new Error(`Failed to create connector: ${response.statusText}`);
}
const newConnector = await response.json();
const updatedConnectors = [...connectors, newConnector];
setConnectors(updatedConnectors);
updateConnectorSourceItems(updatedConnectors);
return newConnector;
} catch (err) {
console.error('Error creating search source connector:', err);
throw err;
}
};
/**
* Update an existing search source connector
*/
const updateConnector = async (
connectorId: number,
connectorData: Partial<Omit<SearchSourceConnector, 'id' | 'user_id' | 'created_at'>>
) => {
try {
const token = localStorage.getItem('surfsense_bearer_token');
if (!token) {
throw new Error('No authentication token found');
}
const response = await fetch(
`${process.env.NEXT_PUBLIC_FASTAPI_BACKEND_URL}/api/v1/search-source-connectors/${connectorId}`,
{
method: 'PUT',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${token}`
},
body: JSON.stringify(connectorData)
}
);
if (!response.ok) {
throw new Error(`Failed to update connector: ${response.statusText}`);
}
const updatedConnector = await response.json();
const updatedConnectors = connectors.map(connector =>
connector.id === connectorId ? updatedConnector : connector
);
setConnectors(updatedConnectors);
updateConnectorSourceItems(updatedConnectors);
return updatedConnector;
} catch (err) {
console.error('Error updating search source connector:', err);
throw err;
}
};
/**
* Delete a search source connector
*/
const deleteConnector = async (connectorId: number) => {
try {
const token = localStorage.getItem('surfsense_bearer_token');
if (!token) {
throw new Error('No authentication token found');
}
const response = await fetch(
`${process.env.NEXT_PUBLIC_FASTAPI_BACKEND_URL}/api/v1/search-source-connectors/${connectorId}`,
{
method: 'DELETE',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${token}`
}
}
);
if (!response.ok) {
throw new Error(`Failed to delete connector: ${response.statusText}`);
}
const updatedConnectors = connectors.filter(connector => connector.id !== connectorId);
setConnectors(updatedConnectors);
updateConnectorSourceItems(updatedConnectors);
} catch (err) {
console.error('Error deleting search source connector:', err);
throw err;
}
};
/**
* Index content from a connector to a search space
*/
const indexConnector = async (connectorId: number, searchSpaceId: string | number) => {
try {
const token = localStorage.getItem('surfsense_bearer_token');
if (!token) {
throw new Error('No authentication token found');
}
const response = await fetch(
`${process.env.NEXT_PUBLIC_FASTAPI_BACKEND_URL}/api/v1/search-source-connectors/${connectorId}/index?search_space_id=${searchSpaceId}`,
{
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${token}`
}
}
);
if (!response.ok) {
throw new Error(`Failed to index connector content: ${response.statusText}`);
}
const result = await response.json();
// Update the connector's last_indexed_at timestamp
const updatedConnectors = connectors.map(connector =>
connector.id === connectorId
? { ...connector, last_indexed_at: new Date().toISOString() }
: connector
);
setConnectors(updatedConnectors);
return result;
} catch (err) {
console.error('Error indexing connector content:', err);
throw err;
}
};
/**
* Get connector source items - memoized to prevent unnecessary re-renders
*/
const getConnectorSourceItems = useCallback(() => {
return connectorSourceItems;
}, [connectorSourceItems]);
return {
connectors,
isLoading,
error,
createConnector,
updateConnector,
deleteConnector,
indexConnector,
getConnectorSourceItems,
connectorSourceItems
};
};