import { useEffect, useRef, useState } from 'react';

export const useRealtimeState = <T extends any>(initialState?: T) => {
  const [state, setState] = useState<T>(() => {
    return initialState as T;
  });
  const stateRef = useRef<T>(initialState);

  const set = (value: T) => {
    stateRef.current = value;
    setState(value);
  };

  const get = () => stateRef.current;

  // useEffect(() => {
  //   stateRef.current = state;
  // }, [state]);

  return [state, stateRef, set, get] as const;
};