use-exposed-state.ts 733 B

12345678910111213141516171819
  1. import { type Dispatch, type SetStateAction, useState } from 'react'
  2. import { useUnstableStoreSync } from '@/shared/hooks/use-unstable-store-sync'
  3. /**
  4. * Creates a state variable that is exposed via window.overleaf.unstable.store,
  5. * which is used by Writefull (and only Writefull). Once Writefull is integrated
  6. * into our codebase, it should be able to hook directly into our React
  7. * contexts and we would then be able to remove this hook, replacing it with
  8. * useState.
  9. */
  10. export default function useExposedState<T = any>(
  11. initialState: T | (() => T),
  12. path: string
  13. ): [T, Dispatch<SetStateAction<T>>] {
  14. const [value, setValue] = useState<T>(initialState)
  15. useUnstableStoreSync(path, value)
  16. return [value, setValue]
  17. }