Untitled

mail@pastecode.io avatar
unknown
tsx
a year ago
3.0 kB
4
Indexable
Never
import { useState, useEffect, useContext } from "react";
import { killuaContext } from "@/wrapper/killuaRoot";
import { ThunderType } from "@/types/thunder.type";

interface IArgs {
  key: string;
  default: any;
  encrypt: boolean;
}

function useKillua<T>(args: IArgs): [T, (value: T) => void] {
  // context for all thunders
  const { killuaState, setKilluaState } = useContext<any>(killuaContext);

  // for get thunder for current key
  const [getThunder, setGetThunder] = useState<any>(undefined);
  
  // for set thunder for current key
  const setThunderHandler = (value: any) => {
    const updatedKillua = killuaState.reduce(
      (acc: ThunderType[], curr: ThunderType) => {
        if (curr.key === args.key) {
          return [
            ...acc,
            {
              value: value,
              key: args.key,
              encrypt: false,
            },
          ];
        } else {
          return [...acc, curr];
        }
      },
      []
    );
    if (updatedKillua.length === killuaState.length) {
      updatedKillua.push({
        key: args.key,
        value: value,
        encrypt: false,
      });
    }
    setKilluaState(updatedKillua);
  };

  /* 
    tip: call after change args.key (arg for other thunders) and first render
    if (is thunder key in local storage) {
      if(isCorrect data in localStorage) {
        if(args.encrypt === true) {
          decrypt and set state data
        } else {
          set state data
        }
      } else {
        remove local storage data for current key
      }
    } esle {
      add thunder with default value to killua state
    }
  */
  useEffect(() => {
    const localStorageValue = localStorage.getItem(args.key);
    if (localStorageValue) {
      try {
        setThunderHandler(
          args.encrypt
            ? JSON.parse(window.atob(localStorageValue))
            : localStorageValue
        );
      } catch {
        localStorage.removeItem(args.key);
      }
    } else {
      setThunderHandler(args.default);
    }
  }, [args.key]);

  /*
    tip: call after update killua context and first render
    if(is current thunder in killua context) {
      set thunder state with finded value
      set thunder is ready true
      if(args.encrypt === true) {
        encrypt and set to local storage thunder value
      } else {
        set to local storage thunder value
      }
    }
  */
  useEffect(() => {
    if (killuaState.some((item: ThunderType) => item.key === args.key)) {
      const updatedThunderValue = killuaState.filter(
        (item: ThunderType) => item.key === args.key
      )[0]?.value;
      setGetThunder(updatedThunderValue);
      localStorage.setItem(
        args.key,
        args.encrypt
          ? window.btoa(JSON.stringify(updatedThunderValue))
          : updatedThunderValue
      );
    }
  }, [killuaState]);

  return [getThunder, setThunderHandler];
}

export default useKillua;