import { Button } from '@/components/ui/button'; import { DialogContent, DialogDescription, DialogHeader, DialogTitle, } from '@/components/ui/dialog'; import { Spinner } from '@/components/ui/spinner'; import { SYNC_SUBSCRIPTION_FEEDS_FULL, SYNC_SUBSCRIPTION_FEEDS_INCREMENTAL, SYNC_SUBSCRIPTION_SOURCES, } from '@/domains/recorder/schema/subscriptions'; import type { SyncSubscriptionFeedsFullMutation, SyncSubscriptionFeedsFullMutationVariables, SyncSubscriptionFeedsIncrementalMutation, SyncSubscriptionFeedsIncrementalMutationVariables, SyncSubscriptionSourcesMutation, SyncSubscriptionSourcesMutationVariables, } from '@/infra/graphql/gql/graphql'; import { useMutation } from '@apollo/client'; import { useNavigate } from '@tanstack/react-router'; import { RefreshCcwIcon } from 'lucide-react'; import { memo, useCallback } from 'react'; import { toast } from 'sonner'; export type SubscriptionSyncViewCompletePayload = { id: string; }; export interface SubscriptionSyncViewProps { id: number; onComplete: (payload: SubscriptionSyncViewCompletePayload) => void; } export const SubscriptionSyncView = memo( ({ id, onComplete }: SubscriptionSyncViewProps) => { const [syncSubscriptionFeedsIncremental, { loading: loadingIncremental }] = useMutation< SyncSubscriptionFeedsIncrementalMutation, SyncSubscriptionFeedsIncrementalMutationVariables >(SYNC_SUBSCRIPTION_FEEDS_INCREMENTAL, { onCompleted: (data) => { toast.success('Sync completed'); onComplete(data.subscriptionsSyncOneFeedsIncremental); }, onError: (error) => { toast.error('Failed to sync subscription', { description: error.message, }); }, }); const [syncSubscriptionFeedsFull, { loading: loadingFull }] = useMutation< SyncSubscriptionFeedsFullMutation, SyncSubscriptionFeedsFullMutationVariables >(SYNC_SUBSCRIPTION_FEEDS_FULL, { onCompleted: (data) => { toast.success('Sync completed'); onComplete(data.subscriptionsSyncOneFeedsFull); }, onError: (error) => { toast.error('Failed to sync subscription', { description: error.message, }); }, }); const [syncSubscriptionSources, { loading: loadingSources }] = useMutation< SyncSubscriptionSourcesMutation, SyncSubscriptionSourcesMutationVariables >(SYNC_SUBSCRIPTION_SOURCES, { onCompleted: (data) => { toast.success('Sync completed'); onComplete(data.subscriptionsSyncOneSources); }, onError: (error) => { toast.error('Failed to sync subscription', { description: error.message, }); }, }); const loading = loadingIncremental || loadingFull || loadingSources; return (
{loading && (
Syncing...
)}
); } ); export interface SubscriptionSyncDialogContentProps { id: number; onCancel?: VoidFunction; } export const SubscriptionSyncDialogContent = memo( ({ id, onCancel }: SubscriptionSyncDialogContentProps) => { const navigate = useNavigate(); const handleSyncComplete = useCallback( (payload: SubscriptionSyncViewCompletePayload) => { navigate({ to: '/tasks/detail/$id', params: { id: `${payload.id}`, }, }); }, [navigate] ); return ( Sync Subscription Sync the subscription with sources and feeds. ); } );