mirror of
https://github.com/MODSetter/SurfSense.git
synced 2026-07-12 22:42:13 +02:00
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:
parent
16c7dedba0
commit
9311e12c87
2 changed files with 71 additions and 19 deletions
|
|
@ -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 [];
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue