Merge pull request #5 from fblgit/fix/slack-channel-discovery

Fix: Use live API data for Slack channel discovery
This commit is contained in:
FBLGit 2025-05-29 11:41:58 +07:00 committed by GitHub
commit 6bd595b83b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
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 [];
}
}