File size: 809 Bytes
a296341
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"use client"

import { useEffect, useState, useTransition } from "react"

import { getDynamicConfig } from "@/app/queries/getDynamicConfig"
import { DynamicConfig } from "@/types"

import { getDefaultDynamicConfig } from "./getDefaultDynamicConfig"

export function useDynamicConfig(): {
  config: DynamicConfig;
  isConfigReady: boolean;
} {
  const [_isPending, startTransition] = useTransition()
  const [config, setConfig] = useState<DynamicConfig>(getDefaultDynamicConfig())
  const [isConfigReady, setConfigReady] = useState(false)

  useEffect(() => {
    startTransition(async () => {
      if (isConfigReady) { return }
      const newConfig = await getDynamicConfig()
      setConfig(newConfig)
      setConfigReady(true)
    })
  }, [isConfigReady])

  return {
    config,
    isConfigReady
  }
}