{"version":3,"file":"getContext.cjs","names":["useGlobalContext"],"sources":["../../src/GlobalContext/getContext.ts"],"sourcesContent":["import { useEffect, useState } from 'react';\nimport { effect } from '@pastweb/tools';\nimport { useGlobalContext } from './useGlobalContext';\n\n/**\n * Reads one key from the active {@link GlobalContext} and re-renders when that\n * key changes.\n *\n * The value is backed by the reactive context object from `@pastweb/tools`.\n *\n * @typeParam T - Expected value type.\n * @param key - Context key to read.\n * @returns The current value for `key`.\n *\n * @example\n * ```tsx\n * function UserName() {\n *   const user = getContext<{ name: string }>('user');\n *   return <span>{user.name}</span>;\n * }\n * ```\n */\nexport function getContext<T = any>(key: string): T {\n  const globalContext = useGlobalContext();\n\n  const [value, setValue] = useState<T>(globalContext[key]);\n\n  useEffect(() => {\n    effect(\n      () => { setValue(globalContext[key]); },\n      () => globalContext[key],\n    );\n  }, []);\n\n  return value as T;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;AAsBA,SAAgB,WAAoB,KAAgB;CAClD,MAAM,gBAAgBA,uCAAAA,iBAAiB;CAEvC,MAAM,CAAC,OAAO,aAAA,GAAA,MAAA,SAAA,CAAwB,cAAc,IAAI;CAExD,CAAA,GAAA,MAAA,UAAA,OAAgB;EACd,CAAA,GAAA,eAAA,OAAA,OACQ;GAAE,SAAS,cAAc,IAAI;EAAG,SAChC,cAAc,IACtB;CACF,GAAG,CAAC,CAAC;CAEL,OAAO;AACT"}