All files / src/features/babies/components GuardiansSection.tsx

94.87% Statements 37/39
89.74% Branches 35/39
100% Functions 13/13
97.14% Lines 34/35

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 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249                                                                      29x 29x 92x 29x   29x 29x   29x                   2x                                 24x                                               27x 27x           27x 3x 3x 2x             1x       27x                                                                                                                                   41x 41x 41x   41x   41x 2x 2x 1x   1x       41x 1x 1x 1x 1x           41x                           1x                           34x                            
import { useState } from 'react'
import { useTranslation } from 'react-i18next'
import { toast } from 'sonner'
 
import {
  AlertDialog,
  AlertDialogAction,
  AlertDialogCancel,
  AlertDialogContent,
  AlertDialogDescription,
  AlertDialogFooter,
  AlertDialogHeader,
  AlertDialogTitle,
  AlertDialogTrigger,
} from '@/components/ui/alert-dialog'
import { Button } from '@/components/ui/button'
import { Input } from '@/components/ui/input'
import { Label } from '@/components/ui/label'
import { cn } from '@/lib/utils'
import { LinkIcon } from '@/shared/icons/link-icon'
import { PlusIcon } from '@/shared/icons/plus-icon'
import { TrashIcon } from '@/shared/icons/trash-icon'
import { UsersIcon } from '@/shared/icons/users-icon'
import { useAuthIdentityStore } from '@/shared/stores/authIdentity.store'
 
import { useBabyGuardians, useCreateBabyInvite, useRemoveBabyGuardian } from '../api/babies.hooks'
import type { Guardian } from '../api/babies.schemas'
 
interface GuardiansSectionProps {
  babyId: string
  babyName: string
  className?: string
}
 
export function GuardiansSection({ babyId, babyName, className }: GuardiansSectionProps) {
  const { t } = useTranslation()
  const guardians = useBabyGuardians(babyId)
  const identity = useAuthIdentityStore((state) => state.identity)
  const [isInviteOpen, setIsInviteOpen] = useState(false)
 
  const currentUserRole = guardians.data?.find((guardian) => guardian.userId === identity?.id)?.role
  const isCurrentUserOwner = currentUserRole === 'OWNER'
 
  return (
    <div className={cn('rounded-2xl border border-border bg-muted/50 p-5', className)}>
      <div className="mb-4 flex items-start justify-between gap-3">
        <div className="flex items-start gap-2">
          <UsersIcon className="text-primary mt-0.5 h-4 w-4 flex-shrink-0" />
          <div>
            <h3 className="font-display text-sm font-extrabold text-ink">{t('babies.guardians.sectionTitle')}</h3>
            <p className="mt-0.5 text-xs text-ink-muted">{t('babies.guardians.sectionDescription')}</p>
          </div>
        </div>
        <Button type="button" variant="outline" size="sm" onClick={() => setIsInviteOpen((open) => !open)}>
          <PlusIcon className="h-3.5 w-3.5" />
          {t('babies.guardians.inviteAction')}
        </Button>
      </div>
 
      {isInviteOpen && <InvitePanel babyId={babyId} />}
 
      {guardians.isPending ? (
        <p className="text-xs text-ink-muted">{t('common.loading')}</p>
      ) : guardians.isError ? (
        <p role="alert" className="text-destructive text-xs">
          {t('babies.guardians.loadError')}
        </p>
      ) : (
        <ul className="flex flex-col gap-2">
          {guardians.data.map((guardian) => (
            <GuardianRow
              key={guardian.userId}
              guardian={guardian}
              babyId={babyId}
              babyName={babyName}
              canRemove={isCurrentUserOwner && guardian.role !== 'OWNER'}
              canLeave={guardian.role !== 'OWNER' && guardian.userId === identity?.id}
            />
          ))}
        </ul>
      )}
    </div>
  )
}
 
interface GuardianRowProps {
  guardian: Guardian
  babyId: string
  babyName: string
  canRemove: boolean
  canLeave: boolean
}
 
function GuardianRow({ guardian, babyId, babyName, canRemove, canLeave }: GuardianRowProps) {
  const { t } = useTranslation()
  const removeGuardian = useRemoveBabyGuardian(babyId)
 
  // Radix's AlertDialogAction always closes the alert dialog immediately on
  // click, before this async handler settles — so a failure can't be shown
  // inline inside the (already-unmounted) AlertDialogContent; a toast is the
  // only reliable way to surface it. Mirrors AdhocVaccineList/MilestoneTimeline.
  const handleRemove = async () => {
    try {
      await removeGuardian.mutateAsync(guardian.userId)
      toast.success(canLeave ? t('babies.guardians.leaveSuccessToast') : t('babies.guardians.removeSuccessToast'))
    } catch {
      // Ramifica como o sucesso logo acima. Sem isto a falha era a única
      // mensagem do app que não dizia o que tinha falhado — "não foi possível
      // concluir a ação" — enquanto todas as outras nomeiam a coisa. E aqui
      // pesa mais: o AlertDialog já fechou, então este toast é tudo o que a
      // pessoa vê.
      toast.error(t(canLeave ? 'babies.guardians.leaveGenericError' : 'babies.guardians.removeGenericError'))
    }
  }
 
  return (
    <li className="flex items-center gap-3 rounded-xl bg-card p-3 shadow-[0_1px_6px_rgba(0,0,0,0.03)]">
      <div className="min-w-0 flex-1">
        <p className="truncate text-sm font-bold text-ink">{guardian.name}</p>
        <p className="truncate text-xs text-ink-muted">{guardian.email}</p>
      </div>
      <span
        className={cn(
          'flex-shrink-0 rounded-full px-2.5 py-1 text-[11px] font-semibold',
          guardian.role === 'OWNER' ? 'bg-emerald-50 dark:bg-emerald-950/40 text-emerald-700 dark:text-emerald-300' : 'bg-violet-50 dark:bg-violet-950/40 text-violet-600 dark:text-violet-300',
        )}
      >
        {guardian.role === 'OWNER' ? t('babies.guardians.roleOwner') : t('babies.guardians.roleGuardian')}
      </span>
 
      {(canRemove || canLeave) && (
        <AlertDialog>
          {/* asChild has to land on the button itself. Wrapping it in a <span>
              hands Radix's trigger props — type, aria-haspopup, aria-expanded —
              to an element whose implicit role accepts none of them, which axe
              reports as aria-allowed-attr, WCAG 2 A. The wrapper bought nothing:
              the -inset expander below positions against the button, which is
              already relative. */}
          <AlertDialogTrigger asChild>
            <button
              type="button"
              aria-label={
                canLeave
                  ? t('babies.guardians.leaveAction')
                  : t('babies.guardians.removeAction', { name: guardian.name })
              }
              className="text-destructive relative inline-flex flex-shrink-0 rounded-lg p-1.5 opacity-60 transition-opacity hover:bg-rose-50 dark:hover:bg-rose-950/40 hover:opacity-100 focus-visible:opacity-100"
            >
              <span className="absolute -inset-2.5" aria-hidden="true" />
              <TrashIcon className="h-3.5 w-3.5" />
            </button>
          </AlertDialogTrigger>
          <AlertDialogContent>
            <AlertDialogHeader>
              <AlertDialogTitle>
                {canLeave ? t('babies.guardians.leaveConfirmTitle') : t('babies.guardians.removeConfirmTitle')}
              </AlertDialogTitle>
              <AlertDialogDescription>
                {canLeave
                  ? t('babies.guardians.leaveConfirmDescription', { babyName })
                  : t('babies.guardians.removeConfirmDescription', { name: guardian.name, babyName })}
              </AlertDialogDescription>
            </AlertDialogHeader>
            <AlertDialogFooter>
              <AlertDialogCancel>{t('babies.guardians.confirmDismiss')}</AlertDialogCancel>
              <AlertDialogAction variant="destructive" onClick={handleRemove} disabled={removeGuardian.isPending}>
                {canLeave ? t('babies.guardians.leaveConfirmAction') : t('babies.guardians.removeConfirmAction')}
              </AlertDialogAction>
            </AlertDialogFooter>
          </AlertDialogContent>
        </AlertDialog>
      )}
    </li>
  )
}
 
interface InvitePanelProps {
  babyId: string
}
 
function InvitePanel({ babyId }: InvitePanelProps) {
  const { t } = useTranslation()
  const [email, setEmail] = useState('')
  const createInvite = useCreateBabyInvite(babyId)
 
  const inviteLink = createInvite.data ? `${window.location.origin}/invites/${createInvite.data.code}` : null
 
  const handleGenerate = async () => {
    try {
      await createInvite.mutateAsync(email.trim() || undefined)
      toast.success(t('babies.guardians.invite.successToast'))
    } catch {
      toast.error(t('babies.guardians.invite.genericError'))
    }
  }
 
  const handleCopy = async () => {
    Iif (!inviteLink) return
    try {
      await navigator.clipboard.writeText(inviteLink)
      toast.success(t('babies.guardians.invite.copiedToast'))
    } catch {
      toast.error(t('babies.guardians.invite.copyError'))
    }
  }
 
  return (
    <div className="mb-4 rounded-xl bg-card p-4 shadow-[0_1px_6px_rgba(0,0,0,0.03)]">
      {inviteLink ? (
        <div>
          <Label htmlFor="invite-link">{t('babies.guardians.invite.linkLabel')}</Label>
          <div className="mt-2 flex items-center gap-2">
            <Input id="invite-link" type="text" readOnly value={inviteLink} className="flex-1" />
            <Button type="button" variant="outline" size="sm" onClick={handleCopy}>
              <LinkIcon className="h-3.5 w-3.5" />
              {t('babies.guardians.invite.copyAction')}
            </Button>
          </div>
          <button
            type="button"
            onClick={() => createInvite.reset()}
            className="text-primary mt-3 text-xs font-semibold hover:underline"
          >
            {t('babies.guardians.invite.newInviteAction')}
          </button>
        </div>
      ) : (
        <div>
          <Label htmlFor="invite-email">{t('babies.guardians.invite.emailLabel')}</Label>
          <Input
            id="invite-email"
            type="email"
            placeholder={t('babies.guardians.invite.emailPlaceholder')}
            value={email}
            onChange={(event) => setEmail(event.target.value)}
            className="mt-2"
          />
          <p className="mt-1 text-xs text-ink-muted">{t('babies.guardians.invite.emailHint')}</p>
          <div className="mt-3 flex justify-end">
            <Button type="button" size="sm" onClick={handleGenerate} disabled={createInvite.isPending}>
              {createInvite.isPending ? t('common.saving') : t('babies.guardians.invite.generateAction')}
            </Button>
          </div>
        </div>
      )}
    </div>
  )
}