All files / web/src/components/toys/number-line/constants/demos usePhiCenteringMode.ts

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

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                                                                                                                                                                                                                                                                                                                                                                                                                                                                     
import { useState, useRef, useCallback, useEffect } from 'react'
import type { AlignmentConfig } from './renderPhiExploreImage'

const ALL_SUBJECTS = [
  'fiddlehead',
  'galaxy',
  'hurricane',
  'nautilus',
  'pinecone',
  'rams-horn',
  'romanesco',
  'sunflower',
  'wave',
] as const

const DEFAULT_ALIGNMENT: AlignmentConfig = {
  scale: 1,
  rotation: 0,
  offsetX: 0,
  offsetY: 0,
}

type ThemeVariant = 'light' | 'dark'

interface AlignmentEntry {
  light?: AlignmentConfig
  dark?: AlignmentConfig
  // legacy top-level fields
  scale?: number
  rotation?: number
  offsetX?: number
  offsetY?: number
}

type AlignmentJson = Record<string, AlignmentEntry>

export interface PhiCenteringMode {
  enabled: boolean
  subjectId: string
  theme: ThemeVariant
  alignment: AlignmentConfig
  image: HTMLImageElement | null
  allSubjects: readonly string[]
  dirty: boolean
  saving: boolean

  setEnabled: (v: boolean) => void
  setSubject: (id: string) => void
  setTheme: (t: ThemeVariant) => void
  updateAlignment: (partial: Partial<AlignmentConfig>) => void
  resetAlignment: () => void
  nextSubject: () => void
  prevSubject: () => void
}

export function usePhiCenteringMode(resolvedTheme: string | undefined): PhiCenteringMode {
  const [enabled, setEnabled] = useState(false)
  const [subjectId, setSubjectIdRaw] = useState<string>(ALL_SUBJECTS[0])
  const [theme, setThemeRaw] = useState<ThemeVariant>(resolvedTheme === 'dark' ? 'dark' : 'light')
  const [alignment, setAlignment] = useState<AlignmentConfig>({ ...DEFAULT_ALIGNMENT })
  const [image, setImage] = useState<HTMLImageElement | null>(null)
  const [dirty, setDirty] = useState(false)
  const [saving, setSaving] = useState(false)

  const alignmentDataRef = useRef<AlignmentJson | null>(null)
  const loadIdRef = useRef(0)
  const saveTimerRef = useRef<ReturnType<typeof setTimeout> | null>(null)
  const latestAlignmentRef = useRef(alignment)
  latestAlignmentRef.current = alignment

  // Load alignment.json once
  useEffect(() => {
    if (!enabled) return
    if (alignmentDataRef.current) return // already loaded

    let cancelled = false
    async function load() {
      try {
        const res = await fetch(`/images/constants/phi-explore/alignment.json?t=${Date.now()}`)
        if (!res.ok) return
        const data = (await res.json()) as AlignmentJson
        if (cancelled) return
        alignmentDataRef.current = data
        // Apply alignment for current subject+theme
        const entry = data[subjectId]
        if (entry?.[theme]) {
          setAlignment({ ...entry[theme]! })
        }
      } catch {
        // ignore
      }
    }
    load()
    return () => {
      cancelled = true
    }
    // Only load once when enabled
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, [enabled])

  // Load image when subject or theme changes
  useEffect(() => {
    if (!enabled) return
    const id = ++loadIdRef.current
    setImage(null)

    const img = new Image()
    img.crossOrigin = 'anonymous'
    img.onload = () => {
      if (id !== loadIdRef.current) return
      setImage(img)
    }
    img.onerror = () => {
      // ignore
    }
    img.src = `/images/constants/phi-explore/${subjectId}-${theme}.png`

    // Also update alignment from cached data
    const data = alignmentDataRef.current
    if (data) {
      const entry = data[subjectId]
      if (entry?.[theme]) {
        setAlignment({ ...entry[theme]! })
      } else {
        setAlignment({ ...DEFAULT_ALIGNMENT })
      }
    }
    setDirty(false)

    return () => {
      loadIdRef.current++
    }
  }, [enabled, subjectId, theme])

  // Debounced auto-save
  const scheduleSave = useCallback(() => {
    if (saveTimerRef.current) clearTimeout(saveTimerRef.current)
    saveTimerRef.current = setTimeout(async () => {
      const al = latestAlignmentRef.current
      setSaving(true)
      try {
        const res = await fetch('/api/admin/constant-images/phi-explore/alignment', {
          method: 'POST',
          headers: { 'Content-Type': 'application/json' },
          body: JSON.stringify({
            subjectId,
            theme,
            alignment: al,
          }),
        })
        if (res.ok) {
          const data = (await res.json()) as AlignmentJson
          alignmentDataRef.current = data
          setDirty(false)
        }
      } catch {
        // ignore
      } finally {
        setSaving(false)
      }
    }, 500)
  }, [subjectId, theme])

  // Cleanup save timer
  useEffect(() => {
    return () => {
      if (saveTimerRef.current) clearTimeout(saveTimerRef.current)
    }
  }, [])

  const updateAlignment = useCallback(
    (partial: Partial<AlignmentConfig>) => {
      setAlignment((prev) => {
        const next = { ...prev, ...partial }
        return next
      })
      setDirty(true)
      scheduleSave()
    },
    [scheduleSave]
  )

  const resetAlignment = useCallback(() => {
    setAlignment({ ...DEFAULT_ALIGNMENT })
    setDirty(true)
    scheduleSave()
  }, [scheduleSave])

  const setSubject = useCallback((id: string) => {
    setSubjectIdRaw(id)
  }, [])

  const setTheme = useCallback((t: ThemeVariant) => {
    setThemeRaw(t)
  }, [])

  const nextSubject = useCallback(() => {
    const idx = ALL_SUBJECTS.indexOf(subjectId as (typeof ALL_SUBJECTS)[number])
    const next = ALL_SUBJECTS[(idx + 1) % ALL_SUBJECTS.length]
    setSubjectIdRaw(next)
  }, [subjectId])

  const prevSubject = useCallback(() => {
    const idx = ALL_SUBJECTS.indexOf(subjectId as (typeof ALL_SUBJECTS)[number])
    const prev = ALL_SUBJECTS[(idx - 1 + ALL_SUBJECTS.length) % ALL_SUBJECTS.length]
    setSubjectIdRaw(prev)
  }, [subjectId])

  return {
    enabled,
    subjectId,
    theme,
    alignment,
    image,
    allSubjects: ALL_SUBJECTS,
    dirty,
    saving,
    setEnabled,
    setSubject,
    setTheme,
    updateAlignment,
    resetAlignment,
    nextSubject,
    prevSubject,
  }
}