All files / src/hooks useSectionVisibility.ts

100% Statements 21/21
83.33% Branches 5/6
100% Functions 6/6
100% Lines 20/20

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    2x     37x 37x 37x 2x 2x         35x       4x       74x   74x 4x 4x 4x 1x   3x   4x 4x       74x 592x       74x    
import { useState, useCallback } from 'react'
 
const STORAGE_KEY = 'laputa-hidden-sections'
 
function loadHiddenSections(): Set<string> {
  try {
    const raw = localStorage.getItem(STORAGE_KEY)
    if (raw) {
      const arr = JSON.parse(raw)
      Eif (Array.isArray(arr)) return new Set(arr)
    }
  } catch {
    // ignore corrupt data
  }
  return new Set()
}
 
function saveHiddenSections(hidden: Set<string>) {
  localStorage.setItem(STORAGE_KEY, JSON.stringify([...hidden]))
}
 
export function useSectionVisibility() {
  const [hiddenSections, setHiddenSections] = useState<Set<string>>(loadHiddenSections)
 
  const toggleSection = useCallback((type: string) => {
    setHiddenSections((prev) => {
      const next = new Set(prev)
      if (next.has(type)) {
        next.delete(type)
      } else {
        next.add(type)
      }
      saveHiddenSections(next)
      return next
    })
  }, [])
 
  const isSectionVisible = useCallback(
    (type: string) => !hiddenSections.has(type),
    [hiddenSections],
  )
 
  return { hiddenSections, toggleSection, isSectionVisible }
}