Skip to content

⭐️⭐️⭐️ Open-source, self-hosted alternative to Linear and Plane. Project management and issue tracking where teams and AI agents work side by side to plan and ship products.

2.5 KB
Raw Blame
import { useInfiniteQuery, useMutation, useQuery, useQueryClient } from '@tanstack/react-query';
import { useTranslations } from 'next-intl';
import { toast } from 'sonner';
import {
  listWebhooks,
  listWebhookDeliveries,
  createWebhook,
  updateWebhook,
  rotateWebhookSecret,
  deleteWebhook,
} from '@/lib/api/endpoints/webhooks';
import { qk } from '@/services/queryKeys';

export function useWebhooksQuery(projectKey: string | null) {
  return useQuery({
    queryKey: qk.webhooks(projectKey ?? ''),
    queryFn: () => listWebhooks(projectKey!),
    enabled: projectKey != null,
  });
}

// A webhook's deliveries for the history sidebar, paginated 25 at a time. Only
// fetched when webhookId is set, so the query runs when the sidebar opens.
export function useWebhookDeliveries(webhookId: number | null) {
  return useInfiniteQuery({
    queryKey: qk.webhookDeliveries(webhookId ?? 0),
    queryFn: ({ pageParam }) => listWebhookDeliveries(webhookId!, pageParam),
    initialPageParam: undefined as number | undefined,
    getNextPageParam: (last) => last.nextCursor ?? undefined,
    enabled: webhookId != null,
  });
}

export function useCreateWebhook(projectKey: string | null) {
  const qc = useQueryClient();
  return useMutation({
    mutationFn: ({ input }: { input: Parameters<typeof createWebhook>[1] }) =>
      createWebhook(projectKey!, input),
    onSuccess: () => {
      if (projectKey) void qc.invalidateQueries({ queryKey: qk.webhooks(projectKey) });
    },
  });
}

export function useUpdateWebhook(projectKey: string | null) {
  const qc = useQueryClient();
  return useMutation({
    mutationFn: ({ id, input }: { id: number; input: Parameters<typeof updateWebhook>[1] }) =>
      updateWebhook(id, input),
    onSuccess: () => {
      if (projectKey) void qc.invalidateQueries({ queryKey: qk.webhooks(projectKey) });
    },
  });
}

export function useRotateWebhookSecret(projectKey: string | null) {
  const t = useTranslations('settings.webhooks');
  const qc = useQueryClient();
  return useMutation({
    mutationFn: (id: number) => rotateWebhookSecret(id),
    onSuccess: () => {
      toast.success(t('secretRotated'));
      if (projectKey) void qc.invalidateQueries({ queryKey: qk.webhooks(projectKey) });
    },
  });
}

export function useDeleteWebhook(projectKey: string | null) {
  const qc = useQueryClient();
  return useMutation({
    mutationFn: (id: number) => deleteWebhook(id),
    onSuccess: () => {
      if (projectKey) void qc.invalidateQueries({ queryKey: qk.webhooks(projectKey) });
    },
  });
}