Add slack requests

This commit is contained in:
Nate Kelley 2025-07-09 12:01:07 -06:00
parent 821ed90083
commit 25c235c809
No known key found for this signature in database
GPG Key ID: FD90372AB8D98B4F
5 changed files with 155 additions and 1 deletions

View File

@ -0,0 +1,2 @@
export * from './request';
export * from './queryRequests';

View File

@ -0,0 +1,98 @@
import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query';
import { queryKeys } from '@/api/query_keys';
import { useBusterNotifications } from '@/context/BusterNotifications';
import { useMemoizedFn } from '@/hooks';
import type {
InitiateOAuthRequest,
UpdateIntegrationRequest
} from '@buster/server-shared/slack';
import {
initiateSlackOAuth,
getSlackIntegration,
removeSlackIntegration,
updateSlackIntegration,
getSlackChannels
} from './request';
// GET /api/v2/slack/integration
export const useGetSlackIntegration = (enabled = true) => {
return useQuery({
...queryKeys.slackGetIntegration,
queryFn: getSlackIntegration,
enabled
});
};
// GET /api/v2/slack/channels
export const useGetSlackChannels = (enabled = true) => {
return useQuery({
...queryKeys.slackGetChannels,
queryFn: getSlackChannels,
enabled
});
};
// POST /api/v2/slack/auth/init
export const useInitiateSlackOAuth = () => {
return useMutation({
mutationFn: initiateSlackOAuth
});
};
// PUT /api/v2/slack/integration
export const useUpdateSlackIntegration = () => {
const queryClient = useQueryClient();
return useMutation({
mutationFn: updateSlackIntegration,
onSuccess: () => {
// Invalidate the integration query to refetch the updated data
queryClient.invalidateQueries({
queryKey: queryKeys.slackGetIntegration.queryKey,
refetchType: 'all'
});
}
});
};
// DELETE /api/v2/slack/integration
export const useRemoveSlackIntegration = () => {
const queryClient = useQueryClient();
const { openConfirmModal } = useBusterNotifications();
const mutationFn = useMemoizedFn(
async ({ ignoreConfirm = false }: { ignoreConfirm?: boolean } = {}) => {
const method = async () => {
return await removeSlackIntegration();
};
if (ignoreConfirm) {
return method();
}
return openConfirmModal({
title: 'Remove Slack Integration',
content: 'Are you sure you want to remove the Slack integration? This will disconnect your workspace from Slack.',
primaryButtonProps: {
text: 'Remove'
},
onOk: method
});
}
);
return useMutation({
mutationFn,
onSuccess: () => {
// Invalidate both integration and channels queries
queryClient.invalidateQueries({
queryKey: queryKeys.slackGetIntegration.queryKey,
refetchType: 'all'
});
queryClient.invalidateQueries({
queryKey: queryKeys.slackGetChannels.queryKey,
refetchType: 'all'
});
}
});
};

View File

@ -1 +1,35 @@
import { mainApiV2 } from '../instances'; import { mainApiV2 } from '../instances';
import type {
InitiateOAuthRequest,
UpdateIntegrationRequest,
InitiateOAuthResponse,
GetIntegrationResponse,
RemoveIntegrationResponse,
UpdateIntegrationResponse,
GetChannelsResponse
} from '@buster/server-shared/slack';
// POST /api/v2/slack/auth/init
export const initiateSlackOAuth = async (data: InitiateOAuthRequest) => {
return mainApiV2.post<InitiateOAuthResponse>('/slack/auth/init', data).then((res) => res.data);
};
// GET /api/v2/slack/integration
export const getSlackIntegration = async () => {
return mainApiV2.get<GetIntegrationResponse>('/slack/integration').then((res) => res.data);
};
// DELETE /api/v2/slack/integration
export const removeSlackIntegration = async () => {
return mainApiV2.delete<RemoveIntegrationResponse>('/slack/integration').then((res) => res.data);
};
// PUT /api/v2/slack/integration
export const updateSlackIntegration = async (data: UpdateIntegrationRequest) => {
return mainApiV2.put<UpdateIntegrationResponse>('/slack/integration', data).then((res) => res.data);
};
// GET /api/v2/slack/channels
export const getSlackChannels = async () => {
return mainApiV2.get<GetChannelsResponse>('/slack/channels').then((res) => res.data);
};

View File

@ -11,6 +11,7 @@ import { searchQueryKeys } from './search';
import { termsQueryKeys } from './terms'; import { termsQueryKeys } from './terms';
import { userQueryKeys } from './users'; import { userQueryKeys } from './users';
import { securityQueryKeys } from './security'; import { securityQueryKeys } from './security';
import { slackQueryKeys } from './slack';
export const queryKeys = { export const queryKeys = {
...datasetQueryKeys, ...datasetQueryKeys,
@ -25,5 +26,6 @@ export const queryKeys = {
...datasetGroupQueryKeys, ...datasetGroupQueryKeys,
...permissionGroupQueryKeys, ...permissionGroupQueryKeys,
...currencyQueryKeys, ...currencyQueryKeys,
...securityQueryKeys ...securityQueryKeys,
...slackQueryKeys
}; };

View File

@ -0,0 +1,18 @@
import type {
GetIntegrationResponse,
GetChannelsResponse
} from '@buster/server-shared/slack';
import { queryOptions } from '@tanstack/react-query';
export const slackGetIntegration = queryOptions<GetIntegrationResponse>({
queryKey: ['slack', 'integration']
});
export const slackGetChannels = queryOptions<GetChannelsResponse>({
queryKey: ['slack', 'channels']
});
export const slackQueryKeys = {
slackGetIntegration,
slackGetChannels
};