SurfSense/surfsense_web/components/chat/SourceUtils.tsx

70 lines
1.7 KiB
TypeScript
Raw Normal View History

2025-07-27 10:41:15 -07:00
import type { Connector, Source } from "./types";
2025-04-07 23:47:06 -07:00
/**
* Function to get sources for the main view
*/
export const getMainViewSources = (connector: Connector, initialSourcesDisplay: number) => {
2025-07-27 10:05:37 -07:00
return connector.sources?.slice(0, initialSourcesDisplay);
2025-04-07 23:47:06 -07:00
};
/**
* Function to get filtered sources for the dialog
*/
export const getFilteredSources = (connector: Connector, sourceFilter: string) => {
2025-07-27 10:05:37 -07:00
if (!sourceFilter.trim()) {
return connector.sources;
}
const filter = sourceFilter.toLowerCase().trim();
return connector.sources?.filter(
(source) =>
source.title.toLowerCase().includes(filter) ||
source.description.toLowerCase().includes(filter)
);
2025-04-07 23:47:06 -07:00
};
/**
* Function to get paginated and filtered sources for the dialog
*/
export const getPaginatedDialogSources = (
2025-07-27 10:05:37 -07:00
connector: Connector,
sourceFilter: string,
expandedSources: boolean,
sourcesPage: number,
sourcesPerPage: number
2025-04-07 23:47:06 -07:00
) => {
2025-07-27 10:05:37 -07:00
const filteredSources = getFilteredSources(connector, sourceFilter);
if (expandedSources) {
return filteredSources;
}
return filteredSources?.slice(0, sourcesPage * sourcesPerPage);
2025-04-07 23:47:06 -07:00
};
/**
* Function to get the count of sources for a connector type
*/
export const getSourcesCount = (connectorSources: Connector[], connectorType: string) => {
2025-07-27 10:05:37 -07:00
const connector = connectorSources.find((c) => c.type === connectorType);
return connector?.sources?.length || 0;
2025-04-07 23:47:06 -07:00
};
/**
* Function to get a citation source by ID
*/
export const getCitationSource = (
2025-07-27 10:05:37 -07:00
citationId: number,
connectorSources: Connector[]
2025-04-07 23:47:06 -07:00
): Source | null => {
2025-07-27 10:05:37 -07:00
for (const connector of connectorSources) {
const source = connector.sources?.find((s) => s.id === citationId);
if (source) {
return {
...source,
connectorType: connector.type,
};
}
}
return null;
};