Fix: Use live API data for Slack channel discovery

Replaced mock data in the Slack connector's channel management UI with a real API call to the backend.

- Implemented `discoverSlackChannelsAPI` in the `useConnectorEditPage` hook to fetch channels from `/api/search-source-connectors/slack/:connectorId/discover-channels`.
- Updated the `handleDiscoverChannels` function in `EditConnectorPage` to use this new API function.
- Ensured consistent `SlackChannelInfo` type usage.

This fixes a bug where incorrect Slack channel names were displayed due to the UI using stale mock data instead of actual data reflecting the bot's true channel memberships.
This commit is contained in:
google-labs-jules[bot] 2025-05-29 04:34:14 +00:00
parent 16c7dedba0
commit 9311e12c87
2 changed files with 71 additions and 19 deletions

View file

@ -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);
};

View file

@ -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<SlackChannelInfo[]> {
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 [];
}
}