aboutsummaryrefslogtreecommitdiff
path: root/lib/hooks/useWatchStorage.tsx
blob: ee24a392eb2638d636454582d78a810f19c18611 (plain) (blame)
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
26
27
28
import { UserData } from "@/components/watch/new-player/player";
import { useState } from "react";

function useWatchStorage() {
  // Get initial value from local storage or empty object
  const [settings, setSettings] = useState(() => {
    const storedSettings = localStorage?.getItem("artplayer_settings");
    return storedSettings ? JSON.parse(storedSettings) : {};
  });

  const getSettings = (id: string): UserData | undefined => {
    return settings[id];
  };

  // Function to update settings
  const updateSettings = (id: string, data?: any) => {
    // Update state
    const updatedSettings = { ...settings, [id]: data };
    setSettings(updatedSettings);

    // Update local storage
    localStorage.setItem("artplayer_settings", JSON.stringify(updatedSettings));
  };

  return [getSettings, updateSettings];
}

export default useWatchStorage;