diff --git a/surfsense_web/app/dashboard/[search_space_id]/connectors/[connector_id]/edit/page.tsx b/surfsense_web/app/dashboard/[search_space_id]/connectors/[connector_id]/edit/page.tsx index 899d5f260..52b26779b 100644 --- a/surfsense_web/app/dashboard/[search_space_id]/connectors/[connector_id]/edit/page.tsx +++ b/surfsense_web/app/dashboard/[search_space_id]/connectors/[connector_id]/edit/page.tsx @@ -74,9 +74,8 @@ export default function EditConnectorPage() { isFetchingRepos, handleFetchRepositories, handleRepoSelectionChange, - // Placeholder functions that would ideally come from the hook - // discoverSlackChannels: hookDiscoverSlackChannels, - // triggerSlackReindex: hookTriggerSlackReindex, + discoverSlackChannelsAPI, // Added the actual API function from the hook + // triggerSlackReindex: hookTriggerSlackReindex, // Placeholder for reindex } = useConnectorEditPage(connectorId, searchSpaceId); // State for Slack Channel Management @@ -110,22 +109,18 @@ export default function EditConnectorPage() { const handleDiscoverChannels = async () => { setIsDiscoveringChannels(true); toast.info("Discovering Slack channels..."); - // Replace with actual API call: - // const channels = await hookDiscoverSlackChannels(connectorId); - // For now, using mock data: - await new Promise(resolve => setTimeout(resolve, 1500)); // Simulate API delay - const mockChannels: SlackChannelInfo[] = [ - { id: "C123", name: "general", is_private: false, is_member: true }, - { id: "C456", name: "random", is_private: false, is_member: true }, - { id: "C789", name: "dev-team-private", is_private: true, is_member: true }, - { id: "CABC", name: "marketing", is_private: false, is_member: false }, // Bot not member - ]; - const memberChannels = mockChannels.filter(ch => ch.is_member); - setDiscoveredChannels(memberChannels); - if (memberChannels.length > 0) { - toast.success(`Discovered ${memberChannels.length} channels where bot is a member.`); + + // Call the actual API function from the hook + const actualChannels = await discoverSlackChannelsAPI(connectorId); + + setDiscoveredChannels(actualChannels); + + if (actualChannels.length > 0) { + toast.success(`Discovered ${actualChannels.length} channels where the bot is a member.`); } else { - toast.warning("No channels found where the bot is a member, or discovery failed."); + // The API function itself should show an error toast on failure. + // This warning can be for cases where the API call succeeded but returned no channels. + toast.warning("No channels found where the bot is a member, or discovery returned no channels."); } setIsDiscoveringChannels(false); }; diff --git a/surfsense_web/hooks/useConnectorEditPage.ts b/surfsense_web/hooks/useConnectorEditPage.ts index 7e81c5524..3dc384dec 100644 --- a/surfsense_web/hooks/useConnectorEditPage.ts +++ b/surfsense_web/hooks/useConnectorEditPage.ts @@ -13,6 +13,14 @@ import { EditConnectorFormValues } from '@/components/editConnector/types'; +// Define SlackChannelInfo interface as it might not be globally available +export interface SlackChannelInfo { + id: string; + name: string; + is_private: boolean; + is_member: boolean; +} + export function useConnectorEditPage(connectorId: number, searchSpaceId: string) { const router = useRouter(); const { connectors, updateConnector, isLoading: connectorsLoading } = useSearchSourceConnectors(); @@ -245,5 +253,54 @@ export function useConnectorEditPage(connectorId: number, searchSpaceId: string) isFetchingRepos, handleFetchRepositories, handleRepoSelectionChange, + discoverSlackChannelsAPI, // Add the new function here }; -} +} + +// Implementation of discoverSlackChannelsAPI +async function discoverSlackChannelsAPI(connectorId: number): Promise { + const token = localStorage.getItem('surfsense_bearer_token'); + if (!token) { + toast.error('Authentication token not found. Please log in again.'); + return []; + } + + try { + const response = await fetch( + `${process.env.NEXT_PUBLIC_FASTAPI_BACKEND_URL}/api/search-source-connectors/slack/${connectorId}/discover-channels`, + { + method: 'GET', + headers: { + 'Content-Type': 'application/json', + 'Authorization': `Bearer ${token}`, + }, + } + ); + + if (!response.ok) { + let errorMsg = 'Failed to discover channels.'; + try { + const errorData = await response.json(); + errorMsg = errorData.detail || errorMsg; + } catch (e) { + // Ignore if error response is not JSON + } + toast.error(`Failed to discover channels: ${errorMsg}`); + return []; + } + + const data = await response.json(); + if (data && Array.isArray(data.channels)) { + // Optional: Add a success toast if needed, e.g.: + // toast.success(`Discovered ${data.channels.length} channels.`); + return data.channels as SlackChannelInfo[]; + } else { + toast.error('Invalid response format from server when discovering channels.'); + return []; + } + } catch (error) { + console.error("Error discovering Slack channels:", error); + toast.error(error instanceof Error ? `Error discovering channels: ${error.message}` : "An unknown error occurred while discovering channels."); + return []; + } +}