import { useState, useEffect } from 'react'

const useViewMode = (componentName, defaultMode = 'list') => {
  const getInitialViewMode = () => {
    const stored = sessionStorage.getItem(`${componentName}_viewMode`)
    return stored ? stored : defaultMode
  }

  const [viewMode, setViewMode] = useState(getInitialViewMode)

  useEffect(() => {
    sessionStorage.setItem(`${componentName}_viewMode`, viewMode)
  }, [viewMode, componentName])

  return [viewMode, setViewMode]
}

export default useViewMode
