All files / src/shared/components BrandSignature.tsx

92.85% Statements 13/14
100% Branches 12/12
100% Functions 4/4
92.3% Lines 12/13

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                    4x                         29x 14x 14x           29x 14x 5x 5x           29x         29x 29x 29x                                
import { useEffect, useState } from 'react'
import { useTranslation } from 'react-i18next'
import { cn } from '@/lib/utils'
import { BrandMark } from './BrandMark'
 
interface BrandSignatureProps {
  inverse?: boolean
  compact?: boolean
}
 
const FOLDED_THIS_VISIT_KEY = 'cygnus-mark-folded'
 
/**
 * Whether the mark should fold on this screen: yes for the first one of the
 * visit (the browser tab's session), no after. Folding on every page would
 * turn a greeting into a tic.
 *
 * Read in the state initializer and recorded after mount, so StrictMode's
 * double render and two signatures on one screen (the sign-in panel and its
 * phone-width twin) agree on the answer. Without session storage the answer
 * is no: the mark simply appears.
 */
function useFoldOncePerVisit(): boolean {
  const [fold] = useState(() => {
    try {
      return window.sessionStorage.getItem(FOLDED_THIS_VISIT_KEY) === null
    } catch {
      return false
    }
  })
 
  useEffect(() => {
    if (!fold) return
    try {
      window.sessionStorage.setItem(FOLDED_THIS_VISIT_KEY, '1')
    } catch {
      // Storage refused: the next screen may fold again, which is harmless.
    }
  }, [fold])
 
  return fold
}
 
/** The mark carries its own night background, so the same drawing works on light and dark surfaces. */
export function BrandSignature({ inverse = false, compact = false }: BrandSignatureProps) {
  const { t } = useTranslation()
  const fold = useFoldOncePerVisit()
  return (
    <span className="inline-flex items-center gap-3">
      <BrandMark
        fold={fold}
        className={cn(
          compact ? 'size-10' : 'size-12',
          // On the dark green auth panel the navy tile would sink into the background without an edge.
          inverse && 'rounded-[22.5%] ring-1 ring-white/20',
        )}
      />
      <span className={cn('font-display font-extrabold tracking-tight', compact ? 'text-xl' : 'text-2xl', inverse ? 'text-white' : 'text-ink')}>
        {t('common.appName')}
      </span>
    </span>
  )
}