All files / src/features/specialists/components SpecialistAccessFields.tsx

95% Statements 19/20
80.76% Branches 21/26
92.3% Functions 12/13
100% Lines 19/19

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                                    3x 4x 3x 166x     119x 119x 119x 119x 10x       119x                                               114x   114x 114x           3x                                                                                 51x   52x               1x                                                         3x 3x                
import { Controller, useWatch, type Control } from 'react-hook-form'
import { useTranslation } from 'react-i18next'
import { Button } from '@/components/ui/button'
import { babyAvatarAppearance, babyInitials } from '@/shared/utils/babyAvatarColor'
import type { SpecialistFormInput } from '../api/specialists.schemas'
import type { SpecialistEditorProps } from './SpecialistEditor'
 
type Props = Pick<
  SpecialistEditorProps,
  | 'babies'
  | 'guardians'
  | 'babiesLoading'
  | 'babiesError'
  | 'guardiansLoading'
  | 'guardiansError'
  | 'retryBabies'
  | 'retryGuardians'
> & { control: Control<SpecialistFormInput> }
const toggle = (list: string[], id: string) =>
  list.includes(id) ? list.filter((item) => item !== id) : [...list, id]
const selectionClass = (selected: boolean) =>
  `flex min-w-0 cursor-pointer items-center gap-3 rounded-xl border p-3 text-sm transition-colors ${selected ? 'border-primary bg-primary/5' : 'border-border hover:bg-muted'}`
 
export function SpecialistAccessFields(props: Props) {
  const { t } = useTranslation()
  const { babies, guardians, control } = props
  const [babyIds, sharedIds] = useWatch({ control, name: ['babyIds', 'sharedWithUserIds'] })
  const retry = (callback: () => void) => (
    <Button type="button" variant="outline" size="sm" onClick={callback}>
      {t('specialists.editor.retry')}
    </Button>
  )
  return (
    <section aria-labelledby="specialist-links" className="space-y-4 border-t border-border pt-5">
      <h3 id="specialist-links" className="font-semibold">
        {t('specialists.editor.links')}
      </h3>
      <fieldset>
        <legend className="text-sm font-medium">{t('specialists.babiesLabel')}</legend>
        <p className="mt-1 text-sm text-ink-muted">{t('specialists.editor.babiesHint')}</p>
        {props.babiesLoading ? (
          <p role="status" className="mt-3 text-sm">
            {t('common.loading')}
          </p>
        ) : props.babiesError ? (
          <div role="alert" className="mt-3 space-y-2 text-sm">
            <p>{t('specialists.editor.babiesError')}</p>
            {retry(props.retryBabies)}
          </div>
        ) : babies.length === 0 ? (
          <p className="mt-3 text-sm text-ink-muted">{t('specialists.editor.noChildren')}</p>
        ) : (
          <Controller
            control={control}
            name="babyIds"
            render={({ field }) => (
              <div className="mt-3 grid gap-2 md:grid-cols-2">
                {babies.map((baby) => {
                  const avatar = babyAvatarAppearance(baby.id, baby.avatarColor)
                  return (
                    <label key={baby.id} className={selectionClass(field.value.includes(baby.id))}>
                      <input
                        type="checkbox"
                        className="size-4 shrink-0 accent-primary"
                        checked={field.value.includes(baby.id)}
                        onChange={() => field.onChange(toggle(field.value, baby.id))}
                      />
                      <span
                        aria-hidden="true"
                        className={`flex size-9 shrink-0 items-center justify-center overflow-hidden rounded-full font-bold ${avatar.className}`}
                        style={avatar.style}
                      >
                        {baby.avatarUrl ? (
                          <img src={baby.avatarUrl} alt="" className="size-full object-cover" />
                        ) : (
                          babyInitials(baby.name)
                        )}
                      </span>
                      <span className="min-w-0 break-words font-medium">{baby.name}</span>
                    </label>
                  )
                })}
              </div>
            )}
          />
        )}
      </fieldset>
      <fieldset>
        <legend className="text-sm font-medium">{t('specialists.editor.additionalShare')}</legend>
        <p className="mt-1 text-sm text-ink-muted">{t('specialists.editor.shareHint')}</p>
        {props.guardiansLoading ? (
          <p role="status" className="mt-3 text-sm">
            {t('common.loading')}
          </p>
        ) : props.guardiansError ? (
          <div role="alert" className="mt-3 space-y-2 text-sm">
            <p>{t('specialists.editor.guardiansError')}</p>
            {retry(props.retryGuardians)}
          </div>
        ) : guardians.length === 0 ? (
          <p className="mt-3 text-sm text-ink-muted">{t('specialists.editor.noGuardians')}</p>
        ) : (
          <Controller
            control={control}
            name="sharedWithUserIds"
            render={({ field }) => (
              <div className="mt-3 space-y-2">
                {guardians.map((guardian) => (
                  <label
                    key={guardian.userId}
                    className={selectionClass(field.value.includes(guardian.userId))}
                  >
                    <input
                      type="checkbox"
                      className="size-4 shrink-0 accent-primary"
                      checked={field.value.includes(guardian.userId)}
                      onChange={() => field.onChange(toggle(field.value, guardian.userId))}
                    />
                    <span className="min-w-0 break-words">
                      <span className="font-medium">{guardian.name}</span>
                      <span className="block text-xs text-ink-muted">{guardian.email}</span>
                    </span>
                  </label>
                ))}
              </div>
            )}
          />
        )}
      </fieldset>
      <div className="rounded-xl border border-border bg-muted/40 p-4 text-sm" aria-live="polite">
        <p className="font-semibold">{t('specialists.editor.visibility')}</p>
        <p className="mt-1 text-ink-muted">
          {t(
            babyIds.length
              ? 'specialists.editor.childAccess'
              : sharedIds.length
                ? 'specialists.editor.sharedAccess'
                : 'specialists.editor.privateAccess',
          )}
        </p>
        {sharedIds.length > 0 && (
          <p className="mt-2 break-words text-ink-muted">
            {t('specialists.editor.directAccess', { count: sharedIds.length })}
            {guardians.length > 0 &&
              ` ${guardians
                .filter((person) => sharedIds.includes(person.userId))
                .map((person) => person.name)
                .join(', ')}`}
          </p>
        )}
      </div>
    </section>
  )
}