File size: 626 Bytes
f0743f4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { isJson } from '~/utils/json';

export default function useMultipleKeys(setUserKey: React.Dispatch<React.SetStateAction<string>>) {
  function getMultiKey(name: string, userKey: string) {
    if (isJson(userKey)) {
      const newKey = JSON.parse(userKey);
      return newKey[name];
    } else {
      return '';
    }
  }

  function setMultiKey(name: string, value: number | string | boolean, userKey: string) {
    let newKey = {};
    if (isJson(userKey)) {
      newKey = JSON.parse(userKey);
    }
    newKey[name] = value;

    setUserKey(JSON.stringify(newKey));
  }

  return { getMultiKey, setMultiKey };
}