All files / web/src/components/practice RelationshipBadge.tsx

0% Statements 0/334
0% Branches 0/1
0% Functions 0/1
0% Lines 0/334

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 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             
'use client'

import { useMemo } from 'react'
import { useTheme } from '@/contexts/ThemeContext'
import type { StudentRelationship, ViewerRelationType } from '@/types/student'
import { css, cx } from '../../../styled-system/css'
import { Tooltip } from '../ui/Tooltip'

// =============================================================================
// Types
// =============================================================================

interface RelationshipBadgeProps {
  /**
   * The relationship data - can be the simple StudentRelationship or
   * a ViewerRelationType string for when you already know the type
   */
  relationship: StudentRelationship | ViewerRelationType | null
  /**
   * Optional count of other stakeholders (e.g., "2 other parents")
   * Shown in tooltip when provided
   */
  otherStakeholderCount?: {
    parents: number
    teachers: number
  }
  /** Size variant */
  size?: 'sm' | 'md'
  /** Additional class name */
  className?: string
  /** Whether to show tooltip on hover (default: true) */
  showTooltip?: boolean
}

// =============================================================================
// Configuration
// =============================================================================

interface RelationshipConfig {
  icon: string
  label: string
  description: string
  color: {
    light: { bg: string; border: string; icon: string }
    dark: { bg: string; border: string; icon: string }
  }
}

const RELATIONSHIP_CONFIGS: Record<ViewerRelationType, RelationshipConfig> = {
  parent: {
    icon: '🏠', // house emoji
    label: 'Your Child',
    description: 'You have full access to this student',
    color: {
      light: { bg: 'blue.100', border: 'blue.300', icon: 'blue.600' },
      dark: { bg: 'blue.900/60', border: 'blue.700', icon: 'blue.400' },
    },
  },
  teacher: {
    icon: '📚', // books emoji
    label: 'Your Student',
    description: 'Enrolled in your classroom',
    color: {
      light: { bg: 'purple.100', border: 'purple.300', icon: 'purple.600' },
      dark: { bg: 'purple.900/60', border: 'purple.700', icon: 'purple.400' },
    },
  },
  observer: {
    icon: '📍', // location pin emoji
    label: 'Visiting',
    description: 'Present in your classroom',
    color: {
      light: { bg: 'emerald.100', border: 'emerald.300', icon: 'emerald.600' },
      dark: {
        bg: 'emerald.900/60',
        border: 'emerald.700',
        icon: 'emerald.400',
      },
    },
  },
  none: {
    icon: '👤', // person silhouette emoji
    label: 'Student',
    description: 'No direct relationship',
    color: {
      light: { bg: 'gray.100', border: 'gray.300', icon: 'gray.500' },
      dark: { bg: 'gray.800', border: 'gray.700', icon: 'gray.500' },
    },
  },
}

// =============================================================================
// Helper Functions
// =============================================================================

/**
 * Convert StudentRelationship to ViewerRelationType
 * Priority: parent > teacher > observer > none
 */
function getRelationType(
  relationship: StudentRelationship | ViewerRelationType | null
): ViewerRelationType {
  if (!relationship) return 'none'

  // If already a string type, return it
  if (typeof relationship === 'string') {
    return relationship
  }

  // Convert StudentRelationship to type
  if (relationship.isMyChild) return 'parent'
  if (relationship.isEnrolled) return 'teacher'
  if (relationship.isPresent) return 'observer'
  return 'none'
}

/**
 * Build tooltip content with optional stakeholder counts
 */
function buildTooltipContent(
  config: RelationshipConfig,
  otherStakeholders?: { parents: number; teachers: number }
): string {
  const lines = [config.label]

  if (otherStakeholders) {
    const parts: string[] = []
    if (otherStakeholders.parents > 0) {
      parts.push(
        `${otherStakeholders.parents} other parent${otherStakeholders.parents > 1 ? 's' : ''}`
      )
    }
    if (otherStakeholders.teachers > 0) {
      parts.push(
        `${otherStakeholders.teachers} teacher${otherStakeholders.teachers > 1 ? 's' : ''}`
      )
    }
    if (parts.length > 0) {
      lines.push(parts.join(' • '))
    }
  }

  return lines.join('\n')
}

// =============================================================================
// Component
// =============================================================================

/**
 * RelationshipBadge - Compact icon showing viewer's relationship to a student
 *
 * Used on student tiles to quickly identify relationship at a glance.
 * Shows colored icon with optional tooltip for more details.
 *
 * Colors:
 * - Blue (🏠): Parent - this is your child
 * - Purple (📚): Teacher - enrolled in your classroom
 * - Emerald (📍): Observer - visiting your classroom
 * - Gray (👤): None - no direct relationship
 */
export function RelationshipBadge({
  relationship,
  otherStakeholderCount,
  size = 'md',
  className,
  showTooltip = true,
}: RelationshipBadgeProps) {
  const { resolvedTheme } = useTheme()
  const isDark = resolvedTheme === 'dark'

  const relationType = useMemo(() => getRelationType(relationship), [relationship])
  const config = RELATIONSHIP_CONFIGS[relationType]
  const colors = isDark ? config.color.dark : config.color.light

  // Don't render anything for "none" relationship (or show a subtle indicator)
  if (relationType === 'none') {
    return null
  }

  const badgeSize = size === 'sm' ? '22px' : '26px'
  const fontSize = size === 'sm' ? '0.75rem' : '0.875rem'

  const tooltipContent = useMemo(
    () => buildTooltipContent(config, otherStakeholderCount),
    [config, otherStakeholderCount]
  )

  const badge = (
    <div
      data-component="relationship-badge"
      data-type={relationType}
      className={cx(
        css({
          display: 'flex',
          alignItems: 'center',
          justifyContent: 'center',
          borderRadius: '6px',
          border: '1px solid',
          flexShrink: 0,
          transition: 'transform 0.15s ease, box-shadow 0.15s ease',
          cursor: showTooltip ? 'help' : 'default',
          _hover: showTooltip
            ? {
                transform: 'scale(1.1)',
                boxShadow: '0 2px 8px rgba(0, 0, 0, 0.15)',
              }
            : {},
        }),
        className
      )}
      style={{
        width: badgeSize,
        height: badgeSize,
        fontSize,
        backgroundColor: `var(--colors-${colors.bg.replace(/[./]/g, '-')})`,
        borderColor: `var(--colors-${colors.border.replace(/[./]/g, '-')})`,
      }}
    >
      <span
        style={{
          filter: 'drop-shadow(0 1px 1px rgba(0,0,0,0.1))',
        }}
      >
        {config.icon}
      </span>
    </div>
  )

  if (showTooltip) {
    return (
      <Tooltip content={tooltipContent} side="right" delayDuration={200}>
        {badge}
      </Tooltip>
    )
  }

  return badge
}

// =============================================================================
// Companion: Relationship Summary Line
// =============================================================================

interface RelationshipSummaryProps {
  /** Relationship type */
  type: ViewerRelationType
  /** Optional: classroom name for teacher/observer */
  classroomName?: string
  /** Optional: counts of other stakeholders */
  otherStakeholders?: {
    parents: number
    teachers: number
  }
  /** Additional class name */
  className?: string
}

/**
 * RelationshipSummary - One-line text summary of relationship
 *
 * Used in card headers and modal subtitles.
 * Example outputs:
 * - "Your Child"
 * - "Your Child • 1 other parent"
 * - "Your Student • Math 101"
 * - "Visiting • 2 teachers"
 */
export function RelationshipSummary({
  type,
  classroomName,
  otherStakeholders,
  className,
}: RelationshipSummaryProps) {
  const { resolvedTheme } = useTheme()
  const isDark = resolvedTheme === 'dark'
  const config = RELATIONSHIP_CONFIGS[type]

  const parts: string[] = [config.label]

  // Add classroom name for teachers/observers
  if (classroomName && (type === 'teacher' || type === 'observer')) {
    parts.push(classroomName)
  }

  // Add other stakeholder counts
  if (otherStakeholders) {
    const stakeholderParts: string[] = []
    if (otherStakeholders.parents > 0) {
      stakeholderParts.push(
        `${otherStakeholders.parents} other parent${otherStakeholders.parents > 1 ? 's' : ''}`
      )
    }
    if (otherStakeholders.teachers > 0) {
      stakeholderParts.push(
        `${otherStakeholders.teachers} teacher${otherStakeholders.teachers > 1 ? 's' : ''}`
      )
    }
    if (stakeholderParts.length > 0) {
      parts.push(stakeholderParts.join(', '))
    }
  }

  if (type === 'none') {
    return null
  }

  return (
    <span
      data-component="relationship-summary"
      className={cx(
        css({
          fontSize: '0.75rem',
          fontWeight: 'medium',
        }),
        className
      )}
      style={{
        color: isDark
          ? `var(--colors-${config.color.dark.icon.replace(/[./]/g, '-')})`
          : `var(--colors-${config.color.light.icon.replace(/[./]/g, '-')})`,
      }}
    >
      {parts.join(' • ')}
    </span>
  )
}

// =============================================================================
// Exports
// =============================================================================

export { RELATIONSHIP_CONFIGS, getRelationType }
export type { RelationshipBadgeProps, RelationshipSummaryProps, RelationshipConfig }