All files / mf VaccineSchedule.tsx

97.05% Statements 33/34
87.5% Branches 14/16
100% Functions 12/12
100% Lines 29/29

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160                                                                                            27x 8x   7x 7x 7x 7x                                                 27x 27x 27x   27x   27x 9x 9x 9x   9x 8x     1x 1x     9x     27x 26x       27x   27x 17x             10x 2x                     8x                 23x       1x                                        
import { useCallback, useEffect, useMemo, useRef, useState } from 'react'
 
import {
  APP_ORIGIN,
  DEFAULT_API_ORIGIN,
  fetchPublicSchedule,
  formatAge,
  type ScheduleItem,
} from '../src/shared/public-schedule'
 
import { STYLE_ELEMENT_ID, STYLES } from './styles'
 
export interface VaccineScheduleProps {
  /** Origin serving the Cygnus API. The default is absolute because this renders on someone else's origin. */
  apiOrigin?: string
  /** Cap on rows. The full PNI schedule is 28 doses; a panel in a host page rarely wants all of them. */
  limit?: number
  /**
   * Called when a visitor activates a row.
   *
   * The remote never navigates the host — same rule the embed follows, and it matters more here, not
   * less: this component runs *inside* the host's React tree, so it could call the host's router
   * directly if it went looking for one. A callback keeps the host in charge of what a click means.
   */
  onSelect?: (item: ScheduleItem) => void
}
 
type LoadState =
  | { status: 'loading' }
  | { status: 'ready'; schedule: ScheduleItem[] }
  | { status: 'failed'; reason: string }
 
/**
 * Styles are injected once into the host document rather than shipped as a CSS asset.
 *
 * Federated CSS is genuinely unsolved: the container hands the host a JavaScript module, and a
 * stylesheet emitted alongside it is the host's problem to discover and load — which is exactly the
 * kind of implicit contract that breaks a month later when the host changes bundler. Every class
 * here is `cygnus-mf-` prefixed for the same reason: this markup lands in a document whose CSS this
 * project has never seen.
 *
 * The embed solves the same problem with a shadow root, which is stronger. It is not available here
 * on purpose — a shadow root would also cut the component off from the host's React tree, and being
 * part of that tree is the entire point of federating rather than embedding.
 */
function useInjectedStyles(): void {
  useEffect(() => {
    if (document.getElementById(STYLE_ELEMENT_ID)) return
 
    const style = document.createElement('style')
    style.id = STYLE_ELEMENT_ID
    style.textContent = STYLES
    document.head.appendChild(style)
 
    // Not removed on unmount: a second instance mounting while the first unmounts would otherwise
    // leave the survivor unstyled. The element is idempotent and ~1 kB — leaking it is the cheaper
    // of the two failures.
  }, [])
}
 
/**
 * The Brazilian PNI immunization schedule, exposed as a Module Federation remote.
 *
 * This is a real component from a real application, not a demo: it fetches over the network, holds
 * state, cancels in flight, and renders three distinct outcomes. That is deliberate — a federated
 * `<div>Hello</div>` proves the plumbing and nothing about whether the plumbing is usable.
 *
 * It is also the only screen in Cygnus that *can* be federated to an anonymous visitor. The other
 * six features are scoped to a signed-in guardian and a specific baby; the PNI schedule is published
 * policy, identical for everyone, and reachable through the single unauthenticated endpoint the API
 * exposes.
 */
export default function VaccineSchedule({
  apiOrigin = DEFAULT_API_ORIGIN,
  limit = 8,
  onSelect,
}: VaccineScheduleProps) {
  const [state, setState] = useState<LoadState>({ status: 'loading' })
  const [attempt, setAttempt] = useState(0)
  const abortRef = useRef<AbortController | null>(null)
 
  useInjectedStyles()
 
  useEffect(() => {
    const controller = new AbortController()
    abortRef.current = controller
    setState({ status: 'loading' })
 
    fetchPublicSchedule(apiOrigin, controller.signal)
      .then((schedule) => setState({ status: 'ready', schedule }))
      .catch((error: unknown) => {
        // An abort is this component being unmounted or retried, not a failure to report.
        Iif (controller.signal.aborted) return
        setState({ status: 'failed', reason: error instanceof Error ? error.message : 'unknown' })
      })
 
    return () => controller.abort()
  }, [apiOrigin, attempt])
 
  const visible = useMemo(
    () => (state.status === 'ready' ? state.schedule.slice(0, limit) : []),
    [state, limit],
  )
 
  const retry = useCallback(() => setAttempt((value) => value + 1), [])
 
  if (state.status === 'loading') {
    return (
      <div className="cygnus-mf-card" aria-busy="true">
        <p className="cygnus-mf-state">Carregando o calendário vacinal…</p>
      </div>
    )
  }
 
  if (state.status === 'failed') {
    return (
      <div className="cygnus-mf-card" role="alert">
        <p className="cygnus-mf-state cygnus-mf-error">Não foi possível carregar o calendário vacinal.</p>
        <p className="cygnus-mf-reason">{state.reason}</p>
        <button type="button" className="cygnus-mf-retry" onClick={retry}>
          Tentar de novo
        </button>
      </div>
    )
  }
 
  return (
    <div className="cygnus-mf-card">
      <p className="cygnus-mf-title">Calendário vacinal</p>
      <p className="cygnus-mf-subtitle">
        Programa Nacional de Imunizações — primeiras {visible.length} doses
      </p>
 
      <ul className="cygnus-mf-list">
        {visible.map((item) => (
          <li key={item.id} className="cygnus-mf-row">
            <span className="cygnus-mf-age">{formatAge(item.recommendedAgeInMonths)}</span>
            <div className="cygnus-mf-detail">
              {onSelect ? (
                <button type="button" className="cygnus-mf-name-button" onClick={() => onSelect(item)}>
                  {item.name}
                </button>
              ) : (
                <p className="cygnus-mf-name">{item.name}</p>
              )}
              <p className="cygnus-mf-dose">{item.doseNumber}ª dose</p>
            </div>
          </li>
        ))}
      </ul>
 
      <p className="cygnus-mf-footer">
        <a href={APP_ORIGIN} target="_blank" rel="noreferrer noopener">
          Acompanhar as vacinas do seu bebê →
        </a>
      </p>
    </div>
  )
}