Skip to content

Commit

Permalink
chore: integration events hook (#7641)
Browse files Browse the repository at this point in the history
https://linear.app/unleash/issue/2-2440/create-new-integration-event-hooks

Adds a frontend hook for integration events, allowing us to easily fetch
paginated integration events for a specific integration configuration
id.
  • Loading branch information
nunogois authored Jul 23, 2024
1 parent 1d6dc9b commit cf4435c
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import { useContext } from 'react';
import useSWRInfinite, {
type SWRInfiniteConfiguration,
type SWRInfiniteKeyLoader,
} from 'swr/infinite';
import { formatApiPath } from 'utils/formatPath';
import type { IntegrationEvents } from 'interfaces/integrationEvent';
import { useUiFlag } from 'hooks/useUiFlag';
import AccessContext from 'contexts/AccessContext';
import handleErrorResponses from '../httpErrorResponseHandler';

const fetcher = async (url: string) => {
const response = await fetch(url);
await handleErrorResponses('Integration events')(response);
return response.json();
};

export const useIntegrationEvents = (
integrationId?: number,
limit = 50,
options: SWRInfiniteConfiguration = {},
) => {
const { isAdmin } = useContext(AccessContext);
const integrationEventsEnabled = useUiFlag('integrationEvents');

const getKey: SWRInfiniteKeyLoader = (
pageIndex: number,
previousPageData: IntegrationEvents,
) => {
// Does not meet conditions
if (!integrationId || !isAdmin || !integrationEventsEnabled)
return null;

// Reached the end
if (previousPageData && !previousPageData.integrationEvents.length)
return null;

return formatApiPath(
`api/admin/addons/${integrationId}/events?limit=${limit}&offset=${
pageIndex * limit
}`,
);
};

const { data, error, size, setSize, mutate } =
useSWRInfinite<IntegrationEvents>(getKey, fetcher, {
...options,
revalidateAll: true,
});

const integrationEvents = data
? data.flatMap(({ integrationEvents }) => integrationEvents)
: [];

const isLoadingInitialData = !data && !error;
const isLoadingMore = size > 0 && !data?.[size - 1];
const loading = isLoadingInitialData || isLoadingMore;

const hasMore = data?.[size - 1]?.integrationEvents.length === limit;

const loadMore = () => {
if (loading || !hasMore) return;
setSize(size + 1);
};

return {
integrationEvents,
hasMore,
loadMore,
loading,
refetch: () => mutate(),
error,
};
};
15 changes: 15 additions & 0 deletions frontend/src/interfaces/integrationEvent.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import type { IEvent } from './event';

export type IntegrationEvent = {
id: string;
integrationId: number;
createdAt: string;
state: 'success' | 'failed' | 'successWithErrors';
stateDetails: string;
event: IEvent;
details: Record<string, unknown>;
};

export type IntegrationEvents = {
integrationEvents: IntegrationEvent[];
};
1 change: 1 addition & 0 deletions frontend/src/interfaces/uiConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ export type UiFlags = {
resourceLimits?: boolean;
insightsV2?: boolean;
featureCollaborators?: boolean;
integrationEvents?: boolean;
};

export interface IVersionInfo {
Expand Down

0 comments on commit cf4435c

Please sign in to comment.