aboutsummaryrefslogtreecommitdiffhomepage
path: root/frontend/src/pages/Settings/components/index.tsx
blob: 99d1658bc5d913a7b8553929a426f457e06856bd (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
import api from "@/apis/raw";
import { Button } from "@mantine/core";
import { FunctionComponent, useCallback, useEffect, useState } from "react";
import { useSettingValue } from "../utilities/hooks";

export const URLTestButton: FunctionComponent<{
  category: "sonarr" | "radarr";
}> = ({ category }) => {
  const [title, setTitle] = useState("Test");
  const [color, setVar] = useState("primary");

  const address = useSettingValue<string>(`settings-${category}-ip`);
  const port = useSettingValue<number>(`settings-${category}-port`);
  const url = useSettingValue<string>(`settings-${category}-base_url`);
  const apikey = useSettingValue<string>(`settings-${category}-apikey`);
  const ssl = useSettingValue<boolean>(`settings-${category}-ssl`);

  const click = useCallback(() => {
    if (address && apikey && ssl !== null) {
      let testUrl: string;

      let baseUrl = url;
      if (baseUrl && baseUrl.startsWith("/") === false) {
        baseUrl = "/" + baseUrl;
      }

      if (port) {
        testUrl = `${address}:${port}${baseUrl ?? ""}`;
      } else {
        testUrl = `${address}${baseUrl ?? ""}`;
      }
      const request = {
        protocol: ssl ? "https" : "http",
        url: testUrl,
        params: {
          apikey,
        },
      };

      if (!request.url.endsWith("/")) {
        request.url += "/";
      }

      api.utils
        .urlTest(request.protocol, request.url, request.params)
        .then((result) => {
          if (result.status) {
            setTitle(`Version: ${result.version}`);
            setVar("success");
          } else {
            setTitle(result.error);
            setVar("danger");
          }
        });
    }
  }, [address, port, url, apikey, ssl]);

  return (
    <Button onClick={click} color={color} title={title}>
      {title}
    </Button>
  );
};

export const ProviderTestButton: FunctionComponent<{
  category: string;
}> = ({ category }) => {
  const testConnection = "Test Connection";
  const [title, setTitle] = useState(testConnection);
  const [color, setVar] = useState("primary");

  const testUrl = useSettingValue<string>(`settings-${category}-endpoint`);

  const click = useCallback(() => {
    if (testUrl !== null) {
      const urlWithoutProtocol = new URL(testUrl).host;
      const request = {
        protocol: "http",
        url: urlWithoutProtocol,
      };
      if (!request.url.endsWith("/")) {
        request.url += "/";
      }

      api.utils
        .providerUrlTest(request.protocol, request.url)
        .then((result) => {
          if (result.status) {
            setTitle(`${result.version}`);
            setVar("success");
          } else {
            setVar("danger");
            if (result.code === 404) {
              setTitle(
                "Connected but no version found (possibly whisper-asr?)",
              );
            } else {
              setTitle(result.error);
            }
          }
        });
    }
  }, [testUrl]);

  useEffect(() => {
    setTitle(testConnection);
  }, [testUrl]);

  return (
    <Button onClick={click} color={color} title={title}>
      {title}
    </Button>
  );
};

export * from "./Card";
export * from "./Layout";
export { default as Layout } from "./Layout";
export { default as LayoutModal } from "./LayoutModal";
export * from "./Message";
export * from "./Section";
export * from "./collapse";
export { default as CollapseBox } from "./collapse";
export * from "./forms";
export * from "./pathMapper";