summaryrefslogtreecommitdiffhomepage
path: root/frontend/src/Settings/components/hooks.ts
blob: bdb98b5a0b3a142520bbfe2d47dd9ae049f6bc6a (plain)
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import { isArray, uniqBy } from "lodash";
import { useCallback, useContext, useMemo } from "react";
import { useSystemSettings } from "../../@redux/hooks";
import { log } from "../../utilities/logger";
import { StagedChangesContext } from "./provider";

export function useStagedValues(): LooseObject {
  const [values] = useContext(StagedChangesContext);
  return values;
}

export function useSingleUpdate() {
  const [, update] = useContext(StagedChangesContext);
  return useCallback(
    (v: any, key: string) => {
      update((staged) => {
        const changes = { ...staged };
        changes[key] = v;

        log("info", "stage settings", changes);

        return changes;
      });
    },
    [update]
  );
}

export function useMultiUpdate() {
  const [, update] = useContext(StagedChangesContext);
  return useCallback(
    (obj: LooseObject) => {
      update((staged) => {
        const changes = { ...staged, ...obj };

        log("info", "stage settings", changes);

        return changes;
      });
    },
    [update]
  );
}

type ValidateFuncType<T> = (v: any) => v is T;

export type OverrideFuncType<T> = (settings: Settings) => T;

export function useExtract<T>(
  key: string,
  validate: ValidateFuncType<T>,
  override?: OverrideFuncType<T>
): Readonly<Nullable<T>> {
  const settings = useSystemSettings().content!;

  const extractValue = useMemo(() => {
    let value: Nullable<T> = null;

    if (settings === undefined) {
      return value;
    }

    let path = key.split("-");

    if (path[0] !== "settings") {
      return null;
    } else {
      path = path.slice(0);
    }

    let item: LooseObject = settings;
    for (const key of path) {
      if (key in item) {
        item = item[key];
      }

      if (validate(item)) {
        value = item;
        break;
      }
    }

    return value;
  }, [key, settings, validate]);

  if (override && settings !== undefined) {
    // TODO: Temporarily override
    return override(settings);
  } else {
    return extractValue;
  }
}

export function useLatest<T>(
  key: string,
  validate: ValidateFuncType<T>,
  override?: OverrideFuncType<T>
): Readonly<Nullable<T>> {
  const extractValue = useExtract<T>(key, validate, override);
  const stagedValue = useStagedValues();
  if (key in stagedValue) {
    return stagedValue[key] as T;
  } else {
    return extractValue;
  }
}

export function useLatestArray<T>(
  key: string,
  compare: keyof T,
  override?: OverrideFuncType<T[]>
): Readonly<Nullable<T[]>> {
  const extractValue = useExtract<T[]>(key, isArray, override);
  const stagedValue = useStagedValues();

  let staged: T[] | undefined = undefined;
  if (key in stagedValue) {
    staged = stagedValue[key];
  }

  return useMemo(() => {
    if (staged !== undefined && extractValue) {
      return uniqBy([...staged, ...extractValue], compare);
    } else {
      return extractValue;
    }
  }, [extractValue, staged, compare]);
}

export function useUpdateArray<T>(key: string, compare: keyof T) {
  const update = useSingleUpdate();
  const stagedValue = useStagedValues();

  const staged: T[] = useMemo(() => {
    if (key in stagedValue) {
      return stagedValue[key];
    } else {
      return [];
    }
  }, [key, stagedValue]);

  return useCallback(
    (v: T) => {
      const newArray = uniqBy([v, ...staged], compare);
      update(newArray, key);
    },
    [compare, staged, key, update]
  );
}