aboutsummaryrefslogtreecommitdiffhomepage
path: root/frontend/src/components/inputs/FileBrowser.tsx
blob: ce57a49384607b3954700b0e77d4635d8082fe00 (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
import { useFileSystem } from "@/apis/hooks";
import { faFolder } from "@fortawesome/free-regular-svg-icons";
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
import { Autocomplete, AutocompleteProps } from "@mantine/core";
import { FunctionComponent, useEffect, useMemo, useRef, useState } from "react";

// TODO: use fortawesome icons
const backKey = "⏎ Back";

function getLastSeparator(path: string): number {
  let idx = path.lastIndexOf("/");
  if (idx === -1) {
    idx = path.lastIndexOf("\\");
  }
  return idx;
}

function extractPath(raw: string) {
  if (raw.endsWith("/") || raw.endsWith("\\")) {
    return raw;
  } else {
    const idx = getLastSeparator(raw);
    return raw.slice(0, idx + 1);
  }
}

export type FileBrowserProps = Omit<AutocompleteProps, "data"> & {
  type: "sonarr" | "radarr" | "bazarr";
};

type FileTreeItem = {
  value: string;
  item?: FileTree;
};

export const FileBrowser: FunctionComponent<FileBrowserProps> = ({
  defaultValue,
  type,
  onChange,
  ...props
}) => {
  const [isShow, setIsShow] = useState(false);
  const [value, setValue] = useState(defaultValue ?? "");
  const [path, setPath] = useState(() => extractPath(value));

  const { data: tree } = useFileSystem(type, path, isShow);

  const data = useMemo<FileTreeItem[]>(
    () => [
      { value: backKey },
      ...(tree?.map((v) => ({
        value: v.path,
        item: v,
      })) ?? []),
    ],
    [tree],
  );

  const parent = useMemo(() => {
    const idx = getLastSeparator(path.slice(0, -1));
    return path.slice(0, idx + 1);
  }, [path]);

  useEffect(() => {
    if (value === path) {
      return;
    }

    const newPath = extractPath(value);
    if (newPath !== path) {
      setPath(newPath);
      onChange && onChange(newPath);
    }
  }, [path, value, onChange]);

  const ref = useRef<HTMLInputElement>(null);

  return (
    <Autocomplete
      {...props}
      ref={ref}
      icon={<FontAwesomeIcon icon={faFolder}></FontAwesomeIcon>}
      placeholder="Click to start"
      data={data}
      value={value}
      // Temporary solution of infinite dropdown items, fix later
      limit={NaN}
      maxDropdownHeight={240}
      filter={(value, item) => {
        if (item.value === backKey) {
          return true;
        } else {
          return item.value.includes(value);
        }
      }}
      onChange={(val) => {
        if (val !== backKey) {
          setValue(val);
        } else {
          setValue(parent);
        }
      }}
      onFocus={() => setIsShow(true)}
      onBlur={() => setIsShow(false)}
    ></Autocomplete>
  );
};