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

export function getBaseUrl(slash: boolean = false) {
  let url: string = "/";
  if (process.env.NODE_ENV === "production") {
    url = window.Bazarr.baseUrl;
  }

  const endsWithSlash = url.endsWith("/");
  if (slash && !endsWithSlash) {
    return `${url}/`;
  } else if (!slash && endsWithSlash) {
    return url.slice(0, -1);
  }
  return url;
}

export function copyToClipboard(s: string) {
  let field = document.createElement("textarea");
  field.innerText = s;
  document.body.appendChild(field);
  field.select();
  field.setSelectionRange(0, 9999);
  document.execCommand("copy");
  field.remove();
}

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

export function submodProcessColor(s: string) {
  return `color(name=${s})`;
}

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

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

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

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

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 * from "./async";
export * from "./entity";
export * from "./hooks";
export * from "./validate";