aboutsummaryrefslogtreecommitdiffhomepage
path: root/frontend/src/utilities/index.ts
blob: cde66d8943ba18b77db04cdde2780ad33468139e (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
import { Dispatch } from "react";
import { difference, differenceWith } from "lodash";
import { isEpisode, isMovie, isSeries } from "./validate";

export function toggleState(
  dispatch: Dispatch<boolean>,
  wait: number,
  start = false,
) {
  dispatch(!start);
  setTimeout(() => dispatch(start), wait);
}

export function GetItemId<T extends object>(item: T): number | undefined {
  if (isMovie(item)) {
    return item.radarrId;
  } else if (isEpisode(item)) {
    return item.sonarrEpisodeId;
  } else if (isSeries(item)) {
    return item.sonarrSeriesId;
  } else {
    return undefined;
  }
}

export function BuildKey(...args: unknown[]) {
  return args.join("-");
}

export function Reload() {
  window.location.reload();
}

export function ScrollToTop() {
  window.scrollTo(0, 0);
}

const pathReplaceReg = new RegExp("/{1,}", "g");
export function pathJoin(...parts: string[]) {
  const separator = "/";
  return parts.join(separator).replace(pathReplaceReg, separator);
}

export function filterSubtitleBy(
  subtitles: Subtitle[],
  languages: Language.Info[],
): Subtitle[] {
  if (languages.length === 0) {
    return subtitles.filter((subtitle) => {
      return subtitle.path !== null;
    });
  } else {
    const result = differenceWith(
      subtitles,
      languages,
      (a, b) => a.code2 === b.code2 || a.path !== null || a.code2 === undefined,
    );
    return difference(subtitles, result);
  }
}

export function fromPython(value: PythonBoolean | undefined): boolean {
  return value === "True";
}

export function toPython(value: boolean): PythonBoolean {
  return value ? "True" : "False";
}

export * from "./env";
export * from "./hooks";
export * from "./validate";