import classNames from 'classnames' import { useDeferredValue, useMemo, useState } from 'react' import type { CompatiblePartner, PartnerAsset, PartnerAssetKind, } from 'lib/seam/ecosystem/types.js' import { useCompatiblePartners } from 'lib/seam/ecosystem/use-compatible-partners.js' import { getEcosystemErrorMessage } from 'lib/ui/ecosystem/errors.js' import { SearchField } from 'lib/ui/ecosystem/SearchField.js' import { StateMessage } from 'lib/ui/ecosystem/StateMessage.js' import { type EcosystemTheme, getEcosystemThemeClassName, } from 'lib/ui/ecosystem/theme.js' export interface CompatibleAppsProps { /** Device category, e.g., `smartlock`, `thermostat`, `sensor`, `intercom`. */ deviceType: string /** DeviceDB manufacturer slug. Lists the apps that support this brand. */ manufacturer?: string | undefined /** DeviceDB provider slug, i.e., the cloud the devices connect through. */ provider?: string | undefined /** DeviceDB capability, e.g., `can_provision_mobile_keys`. Shows per-app notes. */ capability?: string | undefined /** Industry slug to restrict the apps, e.g., `short-term-rental`. */ industry?: string | undefined /** Apps shown before the show more button. */ maxVisible?: number | undefined hideSearch?: boolean | undefined hideTitle?: boolean | undefined /** Color theme. `auto` follows the visitor's color scheme. */ theme?: EcosystemTheme | undefined className?: string | undefined } const defaultMaxVisible = 24 /** * The live apps (Seam partners) that work with a device category, * manufacturer, provider or capability through Seam. */ export function CompatibleApps({ deviceType, manufacturer = '', provider = '', capability = '', industry = '', maxVisible = defaultMaxVisible, hideSearch = false, hideTitle = false, theme = 'light', className, }: CompatibleAppsProps): JSX.Element { const { partners, isPending, isError, error, refetch } = useCompatiblePartners({ deviceType: deviceType ?? '', manufacturer, provider, capability, industry, }) const [searchValue, setSearchValue] = useState('') const [showAll, setShowAll] = useState(false) const query = useDeferredValue(searchValue.toLowerCase().trim()) const sortedPartners = useMemo( () => [...(partners ?? [])].sort(comparePartners), [partners] ) const filteredPartners = useMemo( () => query.length === 0 ? sortedPartners : sortedPartners.filter((partner) => matchesSearch(partner, query)), [sortedPartners, query] ) const visibleCount = getVisibleCount(maxVisible) const visiblePartners = showAll ? filteredPartners : filteredPartners.slice(0, visibleCount) const hiddenCount = filteredPartners.length - visiblePartners.length const showNotes = capability.length > 0 || provider.length > 0 const isMissingDeviceType = (deviceType ?? '').trim().length === 0 const content = (() => { if (isMissingDeviceType) { return {t.missingDeviceType} } if (isError) { const { message, canRetry } = getEcosystemErrorMessage(error, t.error) return ( { void refetch() }, } : {})} > {message} ) } if (isPending) { return } if (sortedPartners.length === 0) { return {t.noApps} } if (filteredPartners.length === 0) { return ( { setSearchValue('') }} > {t.noSearchResults(searchValue.trim())} ) } return ( <> {hiddenCount > 0 && (
)} ) })() const showHeader = !hideTitle || !hideSearch return (
{showHeader && (
{!hideTitle &&

{t.title}

} {!hideSearch && ( )}
)} {content}
) } function PartnerCard({ partner, showNote, }: { partner: CompatiblePartner showNote: boolean }): JSX.Element { const logo = getPartnerLogo(partner, lightLogoKinds) const darkLogo = getPartnerLogo(partner, darkLogoKinds) const hasDarkLogo = darkLogo != null && darkLogo.url !== logo?.url const note = showNote ? partner.support.note : null const subtitle = partner.industries.map((i) => i.display_name).join(' ยท ') const body = ( <>
{logo == null ? ( {partner.display_name.slice(0, 1)} ) : ( <> {t.logoAlt(partner.display_name)} {hasDarkLogo && ( )} )}
{partner.display_name} {subtitle.length > 0 && ( {subtitle} )} {note != null && note.length > 0 && ( {note} )}
) return (
  • {partner.website == null ? (
    {body}
    ) : ( {body} )}
  • ) } function LoadingGrid({ count }: { count: number }): JSX.Element { return ( ) } const lightLogoKinds: PartnerAssetKind[] = [ 'square_logo', 'horizontal_logo', 'square_logo_mono', 'horizontal_logo_mono', ] const darkLogoKinds: PartnerAssetKind[] = [ 'square_logo_mono', 'square_logo', 'horizontal_logo_mono', 'horizontal_logo', ] const getPartnerLogo = ( partner: CompatiblePartner, kinds: PartnerAssetKind[] ): PartnerAsset | null => { for (const kind of kinds) { const asset = partner.assets.find((a) => a.kind === kind) if (asset != null) return asset } return null } const comparePartners = ( a: CompatiblePartner, b: CompatiblePartner ): number => { if (a.is_featured !== b.is_featured) return a.is_featured ? -1 : 1 return a.display_name.localeCompare(b.display_name) } const matchesSearch = (partner: CompatiblePartner, query: string): boolean => partner.display_name.toLowerCase().includes(query) || partner.industries.some((i) => i.display_name.toLowerCase().includes(query) ) || partner.solutions.some((s) => s.display_name.toLowerCase().includes(query)) const getVisibleCount = (maxVisible: number): number => Number.isFinite(maxVisible) && maxVisible > 0 ? Math.floor(maxVisible) : defaultMaxVisible const t = { title: 'Works with these apps', search: 'Search apps', error: 'An error occurred while loading compatible apps.', retry: 'Retry', noApps: 'No apps are listed for this selection yet.', noSearchResults: (query: string) => `No apps found for "${query}".`, clearSearch: 'Clear search', showMore: (count: number) => `Show ${count} more ${count === 1 ? 'app' : 'apps'}`, logoAlt: (name: string) => `${name} logo`, missingDeviceType: 'Set a device type to list compatible apps.', }