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
|
import { difference, differenceWith } from "lodash";
import { Dispatch } from "react";
import { isEpisode, isMovie, isSeries } from "./validate";
export function updateAsyncState<T>(
promise: Promise<T>,
setter: (state: AsyncState<T>) => void,
defaultVal: T
) {
setter({
updating: true,
data: defaultVal,
});
promise
.then((data) => {
setter({
updating: false,
data: data,
});
})
.catch((err) => {
setter({
updating: false,
error: err,
data: defaultVal,
});
});
}
export function getBaseUrl(slash: boolean = false) {
let url: string = "/";
if (process.env.NODE_ENV !== "development") {
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(item: any): 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 buildOrderList<T>(state: OrderIdState<T>): T[] {
const { order, items } = state;
return buildOrderListFrom(items, order);
}
export function buildOrderListFrom<T>(
items: IdState<T>,
order: (number | null)[]
): T[] {
return order.flatMap((v) => {
if (v !== null && v in items) {
const item = items[v];
return [item];
}
return [];
});
}
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[]
): 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 "./hooks";
export * from "./validate";
|