/** * The persisted proposals of passive discovery (D994), shown inside the scan * modal — the same source as the scan itself. * * `useDiscoveryProposals` backs the pill on the page's Scan button: `count` is * `null` until the query has ANSWERED (an unanswered query is not "0"), and `0` * means no pill. `DiscoveredProposalsSection` lists the proposals * (`deviceManager.listDiscoveryProposals` already hides what was dismissed, * adopted or set up as an integration) and renders nothing for an empty answer. * * Adopt reuses the scan modal's flows: a `setupIntegration` proposal opens the * integration form, any other goes through `deviceManager.adoptDevice`. */ import { Badge, Button, TEXT_SECTION_LABEL, useDeviceManagerAdoptDevice, useDeviceManagerDismissDiscoveryProposal, useDeviceManagerListDiscoveryProposals, } from '@camstack/ui-library' import type { DiscoveryProposal } from '@camstack/types' import { Check, Loader2 } from 'lucide-react' import { type ReactElement, useCallback, useState } from 'react' import { useTranslation } from 'react-i18next' import { integrationSetupSeed, type IntegrationFormSeed } from './integration-setup-seed' /** The list re-reads on this clock; the listener and the sweep fill it in the background. */ const PROPOSALS_REFRESH_MS = 60_000 type Proposal = DiscoveryProposal interface DiscoveryProposalsState { /** Background-discovered proposals, or `null` while the query has not answered. */ readonly count: number | null /** Re-read the list (the section and the pill share one cache entry). */ readonly refresh: () => void } export function useDiscoveryProposals(): DiscoveryProposalsState { const query = useDeviceManagerListDiscoveryProposals( {}, { refetchInterval: PROPOSALS_REFRESH_MS }, ) const { refetch } = query const refresh = useCallback(() => void refetch(), [refetch]) return { count: query.data === undefined ? null : query.data.proposals.length, refresh } } /** The count pill on the Scan button; nothing for an unanswered query or zero. */ export function DiscoveryProposalPill({ count, }: { readonly count: number | null }): ReactElement | null { if (count === null || count === 0) return null return ( {count} ) } interface DiscoveredProposalsSectionProps { readonly onSetupIntegration: (seed: IntegrationFormSeed) => void } export function DiscoveredProposalsSection({ onSetupIntegration, }: DiscoveredProposalsSectionProps): ReactElement | null { const { t } = useTranslation() const query = useDeviceManagerListDiscoveryProposals( {}, { refetchInterval: PROPOSALS_REFRESH_MS }, ) const adopt = useDeviceManagerAdoptDevice() const dismiss = useDeviceManagerDismissDiscoveryProposal() const [busy, setBusy] = useState>(new Set()) const { refetch } = query const withBusy = useCallback( async (id: string, run: () => Promise) => { setBusy((prev) => new Set(prev).add(id)) try { await run() void refetch() } catch { // the row stays in the list, still actionable } finally { setBusy((prev) => { const next = new Set(prev) next.delete(id) return next }) } }, [refetch], ) const onAdopt = useCallback( async (proposal: Proposal) => { const seed = integrationSetupSeed(proposal.addonId, proposal.candidate) if (seed !== null) { onSetupIntegration(seed) return } await withBusy(proposal.id, () => adopt.mutateAsync({ addonId: proposal.addonId, candidate: proposal.candidate }), ) }, [adopt, onSetupIntegration, withBusy], ) const onDismiss = useCallback( (proposal: Proposal) => withBusy(proposal.id, () => dismiss.mutateAsync({ id: proposal.id })), [dismiss, withBusy], ) const proposals = query.data?.proposals ?? [] if (proposals.length === 0) return null return ( {t('integrations.discoveryProposals.title', 'Found in the background')} ({proposals.length}) {proposals.map((proposal) => ( {proposal.candidate.suggestedName} {proposal.addonId} {busy.has(proposal.id) ? ( ) : ( <> void onDismiss(proposal)}> {t('integrations.discoveryProposals.dismiss', 'Dismiss')} void onAdopt(proposal)}> {t('integrations.discoveryProposals.adopt', 'Adopt')} > )} ))} ) }
{proposal.candidate.suggestedName}
{proposal.addonId}