diff --git a/web/README.md b/web/README.md new file mode 100644 index 0000000..b58e0af --- /dev/null +++ b/web/README.md @@ -0,0 +1,46 @@ +# Getting Started with Create React App + +This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app). + +## Available Scripts + +In the project directory, you can run: + +### `yarn start` + +Runs the app in the development mode.\ +Open [http://localhost:3000](http://localhost:3000) to view it in the browser. + +The page will reload if you make edits.\ +You will also see any lint errors in the console. + +### `yarn test` + +Launches the test runner in the interactive watch mode.\ +See the section about [running tests](https://facebook.github.io/create-react-app/docs/running-tests) for more information. + +### `yarn build` + +Builds the app for production to the `build` folder.\ +It correctly bundles React in production mode and optimizes the build for the best performance. + +The build is minified and the filenames include the hashes.\ +Your app is ready to be deployed! + +See the section about [deployment](https://facebook.github.io/create-react-app/docs/deployment) for more information. + +### `yarn eject` + +**Note: this is a one-way operation. Once you `eject`, you can’t go back!** + +If you aren’t satisfied with the build tool and configuration choices, you can `eject` at any time. This command will remove the single build dependency from your project. + +Instead, it will copy all the configuration files and the transitive dependencies (webpack, Babel, ESLint, etc) right into your project so you have full control over them. All of the commands except `eject` will still work, but they will point to the copied scripts so you can tweak them. At this point you’re on your own. + +You don’t have to ever use `eject`. The curated feature set is suitable for small and middle deployments, and you shouldn’t feel obligated to use this feature. However we understand that this tool wouldn’t be useful if you couldn’t customize it when you are ready for it. + +## Learn More + +You can learn more in the [Create React App documentation](https://facebook.github.io/create-react-app/docs/getting-started). + +To learn React, check out the [React documentation](https://reactjs.org/). diff --git a/web/build.go b/web/build.go new file mode 100644 index 0000000..0ab34b0 --- /dev/null +++ b/web/build.go @@ -0,0 +1,61 @@ +// Package web web/build.go +package web + +import ( + "embed" + "html/template" + "io" + "io/fs" + "net/http" + "os" + "path" +) + +//go:embed build +var Assets embed.FS + +// fsFunc is short-hand for constructing a http.FileSystem +// implementation +type fsFunc func(name string) (fs.File, error) + +func (f fsFunc) Open(name string) (fs.File, error) { + return f(name) +} + +// AssetHandler returns a http.Handler that will serve files from +// the Assets embed.FS. When locating a file, it will strip the given +// prefix from the request and prepend the root to the filesystem +// lookup: typical prefix might be /web/, and root would be build. +func AssetHandler(prefix, root string) http.Handler { + handler := fsFunc(func(name string) (fs.File, error) { + assetPath := path.Join(root, name) + + // If we can't find the asset, return the default index.html + // content + f, err := Assets.Open(assetPath) + if os.IsNotExist(err) { + return Assets.Open("build/index.html") + } + + // Otherwise, assume this is a legitimate request routed + // correctly + return f, err + }) + + return http.StripPrefix(prefix, http.FileServer(http.FS(handler))) +} + +type IndexParams struct { + Title string + Version string + BaseUrl string +} + +func Index(w io.Writer, p IndexParams) error { + return parseIndex().Execute(w, p) +} + +func parseIndex() *template.Template { + return template.Must( + template.New("index.html").ParseFS(Assets, "build/index.html")) +} diff --git a/web/craco.config.js b/web/craco.config.js new file mode 100644 index 0000000..4b2951f --- /dev/null +++ b/web/craco.config.js @@ -0,0 +1,10 @@ +module.exports = { + style: { + postcss: { + plugins: [ + require('tailwindcss'), + require('autoprefixer'), + ], + }, + }, +} \ No newline at end of file diff --git a/web/package.json b/web/package.json new file mode 100644 index 0000000..8047e67 --- /dev/null +++ b/web/package.json @@ -0,0 +1,64 @@ +{ + "name": "web", + "version": "0.1.0", + "private": true, + "proxy": "http://localhost:8989", + "homepage": ".", + "dependencies": { + "@craco/craco": "^6.1.2", + "@headlessui/react": "^1.2.0", + "@heroicons/react": "^1.0.1", + "@testing-library/jest-dom": "^5.11.4", + "@testing-library/react": "^11.1.0", + "@testing-library/user-event": "^12.1.10", + "@types/jest": "^26.0.15", + "@types/node": "^12.0.0", + "@types/react": "^17.0.0", + "@types/react-dom": "^17.0.0", + "final-form": "^4.20.2", + "final-form-arrays": "^3.0.2", + "react": "^17.0.2", + "react-dom": "^17.0.2", + "react-final-form": "^6.5.3", + "react-final-form-arrays": "^3.1.3", + "react-multi-select-component": "^4.0.2", + "react-query": "^3.18.1", + "react-router-dom": "^5.2.0", + "react-scripts": "4.0.3", + "react-select": "5.0.0-beta.0", + "recoil": "^0.4.0", + "typescript": "^4.1.2", + "web-vitals": "^1.0.1" + }, + "scripts": { + "start": "craco start", + "build": "craco build", + "test": "craco test", + "eject": "react-scripts eject" + }, + "eslintConfig": { + "extends": [ + "react-app", + "react-app/jest" + ] + }, + "browserslist": { + "production": [ + ">0.2%", + "not dead", + "not op_mini all" + ], + "development": [ + "last 1 chrome version", + "last 1 firefox version", + "last 1 safari version" + ] + }, + "devDependencies": { + "@tailwindcss/forms": "^0.3.2", + "@types/react-router-dom": "^5.1.7", + "autoprefixer": "^9", + "postcss": "^7", + "tailwindcss": "npm:@tailwindcss/postcss7-compat" + } +} diff --git a/web/public/favicon.ico b/web/public/favicon.ico new file mode 100644 index 0000000..a11777c Binary files /dev/null and b/web/public/favicon.ico differ diff --git a/web/public/index.html b/web/public/index.html new file mode 100644 index 0000000..42187fb --- /dev/null +++ b/web/public/index.html @@ -0,0 +1,33 @@ + + + + + + + + + + + autobrr + {{if eq .BaseUrl "/" }} + + + {{else}} + + + {{end}} + + + +
+ + diff --git a/web/public/logo192.png b/web/public/logo192.png new file mode 100644 index 0000000..fc44b0a Binary files /dev/null and b/web/public/logo192.png differ diff --git a/web/public/logo512.png b/web/public/logo512.png new file mode 100644 index 0000000..a4e47a6 Binary files /dev/null and b/web/public/logo512.png differ diff --git a/web/public/manifest.json b/web/public/manifest.json new file mode 100644 index 0000000..080d6c7 --- /dev/null +++ b/web/public/manifest.json @@ -0,0 +1,25 @@ +{ + "short_name": "React App", + "name": "Create React App Sample", + "icons": [ + { + "src": "favicon.ico", + "sizes": "64x64 32x32 24x24 16x16", + "type": "image/x-icon" + }, + { + "src": "logo192.png", + "type": "image/png", + "sizes": "192x192" + }, + { + "src": "logo512.png", + "type": "image/png", + "sizes": "512x512" + } + ], + "start_url": ".", + "display": "standalone", + "theme_color": "#000000", + "background_color": "#ffffff" +} diff --git a/web/public/robots.txt b/web/public/robots.txt new file mode 100644 index 0000000..e9e57dc --- /dev/null +++ b/web/public/robots.txt @@ -0,0 +1,3 @@ +# https://www.robotstxt.org/robotstxt.html +User-agent: * +Disallow: diff --git a/web/src/api/APIClient.ts b/web/src/api/APIClient.ts new file mode 100644 index 0000000..812046c --- /dev/null +++ b/web/src/api/APIClient.ts @@ -0,0 +1,110 @@ +import {Action, DownloadClient, Filter, Indexer, Network} from "../domain/interfaces"; + +function baseClient(endpoint: string, method: string, { body, ...customConfig}: any = {}) { + let baseUrl = "" + if (window.APP.baseUrl) { + if (window.APP.baseUrl === '/') { + baseUrl = "/" + } else if (window.APP.baseUrl === `{{.BaseUrl}}`) { + baseUrl = "" + } else if (window.APP.baseUrl === "/autobrr/") { + baseUrl = "/autobrr/" + } else { + baseUrl = window.APP.baseUrl + } + } + + const headers = {'content-type': 'application/json'} + const config = { + method: method, + ...customConfig, + headers: { + ...headers, + ...customConfig.headers, + }, + } + + if (body) { + config.body = JSON.stringify(body) + } + + return window.fetch(`${baseUrl}${endpoint}`, config) + .then(async response => { + if (response.status === 401) { + // unauthorized + // window.location.assign(window.location) + + return + } + + if (response.status === 404) { + return Promise.reject(new Error(response.statusText)) + } + + if (response.status === 201) { + return "" + } + + if (response.status === 204) { + return "" + } + + if (response.ok) { + return await response.json() + } else { + const errorMessage = await response.text() + + return Promise.reject(new Error(errorMessage)) + } + }) +} + +const appClient = { + Get: (endpoint: string) => baseClient(endpoint, "GET"), + Post: (endpoint: string, data: any) => baseClient(endpoint, "POST", { body: data }), + Put: (endpoint: string, data: any) => baseClient(endpoint, "PUT", { body: data }), + Patch: (endpoint: string, data: any) => baseClient(endpoint, "PATCH", { body: data }), + Delete: (endpoint: string) => baseClient(endpoint, "DELETE"), +} + +const APIClient = { + actions: { + create: (action: Action) => appClient.Post("api/actions", action), + update: (action: Action) => appClient.Put(`api/actions/${action.id}`, action), + delete: (id: number) => appClient.Delete(`api/actions/${id}`), + toggleEnable: (id: number) => appClient.Patch(`api/actions/${id}/toggleEnabled`, null), + }, + config: { + get: () => appClient.Get("api/config") + }, + download_clients: { + getAll: () => appClient.Get("api/download_clients"), + create: (dc: DownloadClient) => appClient.Post(`api/download_clients`, dc), + update: (dc: DownloadClient) => appClient.Put(`api/download_clients`, dc), + delete: (id: number) => appClient.Delete(`api/download_clients/${id}`), + test: (dc: DownloadClient) => appClient.Post(`api/download_clients/test`, dc), + }, + filters: { + getAll: () => appClient.Get("api/filters"), + getByID: (id: number) => appClient.Get(`api/filters/${id}`), + create: (filter: Filter) => appClient.Post(`api/filters`, filter), + update: (filter: Filter) => appClient.Put(`api/filters/${filter.id}`, filter), + delete: (id: number) => appClient.Delete(`api/filters/${id}`), + }, + indexers: { + getOptions: () => appClient.Get("api/indexer/options"), + getAll: () => appClient.Get("api/indexer"), + getSchema: () => appClient.Get("api/indexer/schema"), + create: (indexer: Indexer) => appClient.Post(`api/indexer`, indexer), + update: (indexer: Indexer) => appClient.Put(`api/indexer`, indexer), + delete: (id: number) => appClient.Delete(`api/indexer/${id}`), + }, + irc: { + getNetworks: () => appClient.Get("api/irc"), + createNetwork: (network: Network) => appClient.Post(`api/irc`, network), + updateNetwork: (network: Network) => appClient.Put(`api/irc/network/${network.id}`, network), + deleteNetwork: (id: number) => appClient.Delete(`api/irc/network/${id}`), + }, +} + +export default APIClient; \ No newline at end of file diff --git a/web/src/components/EmptyListState.tsx b/web/src/components/EmptyListState.tsx new file mode 100644 index 0000000..9eeb686 --- /dev/null +++ b/web/src/components/EmptyListState.tsx @@ -0,0 +1,24 @@ +import React from "react"; + +interface props { + text: string; + buttonText?: string; + buttonOnClick?: any; +} + +export function EmptyListState({ text, buttonText, buttonOnClick }: props) { + return ( +
+

{text}

+ {buttonText && buttonOnClick && ( + + )} +
+ ) +} \ No newline at end of file diff --git a/web/src/components/FilterActionList.tsx b/web/src/components/FilterActionList.tsx new file mode 100644 index 0000000..1a77230 --- /dev/null +++ b/web/src/components/FilterActionList.tsx @@ -0,0 +1,665 @@ +import {Action, DownloadClient} from "../domain/interfaces"; +import React, {Fragment, useEffect, useRef } from "react"; +import {Dialog, Listbox, Switch, Transition} from '@headlessui/react' +import {classNames} from "../styles/utils"; +import {CheckIcon, ChevronRightIcon, ExclamationIcon, SelectorIcon,} from "@heroicons/react/solid"; +import {useToggle} from "../hooks/hooks"; +import {useMutation} from "react-query"; +import {queryClient} from ".."; +import {Field, Form} from "react-final-form"; +import {TextField} from "./inputs"; +import DEBUG from "./debug"; +import APIClient from "../api/APIClient"; + +interface radioFieldsetOption { + label: string; + value: string; +} + +const actionTypeOptions: radioFieldsetOption[] = [ + {label: "Test", value: "TEST"}, + {label: "Watch dir", value: "WATCH_FOLDER"}, + {label: "Exec", value: "EXEC"}, + {label: "qBittorrent", value: "QBITTORRENT"}, + {label: "Deluge", value: "DELUGE"}, +]; + +interface FilterListProps { + actions: Action[]; + clients: DownloadClient[]; + filterID: number; +} + +export function FilterActionList({actions, clients, filterID}: FilterListProps) { + useEffect(() => { + // console.log("render list") + }, []) + + return ( +
+ +
+ ) +} + +interface ListItemProps { + action: Action; + clients: DownloadClient[]; + filterID: number; + idx: number; +} + +function ListItem({action, clients, filterID, idx}: ListItemProps) { + const [deleteModalIsOpen, toggleDeleteModal] = useToggle(false) + const [edit, toggleEdit] = useToggle(false) + + const deleteMutation = useMutation((actionID: number) => APIClient.actions.delete(actionID), { + onSuccess: () => { + queryClient.invalidateQueries(['filter',filterID]); + toggleDeleteModal() + } + }) + + const enabledMutation = useMutation((actionID: number) => APIClient.actions.toggleEnable(actionID), { + onSuccess: () => { + queryClient.invalidateQueries(['filter',filterID]); + } + }) + + const updateMutation = useMutation((action: Action) => APIClient.actions.update(action), { + onSuccess: () => { + queryClient.invalidateQueries(['filter',filterID]); + } + }) + + const toggleActive = () => { + enabledMutation.mutate(action.id) + } + + useEffect(() => { + }, [action]) + + const cancelButtonRef = useRef(null) + + const deleteAction = () => { + deleteMutation.mutate(action.id) + } + + const onSubmit = (action: Action) => { + // TODO clear data depending on type + updateMutation.mutate(action) + }; + + const TypeForm = (action: Action) => { + switch (action.type) { + case "TEST": + return ( +
+
+
+
+
+
+

Notice

+
+

+ The test action does nothing except to show if the filter works. +

+
+
+
+
+
+ ) + case "EXEC": + return ( +
+
+ + +
+
+ ) + case "WATCH_FOLDER": + return ( +
+ +
+ ) + case "QBITTORRENT": + return ( +
+
+ +
+ ( + + {({open}) => ( + <> + Client +
+ + {input.value ? clients.find(c => c.id === input.value)!.name : "Choose a client"} + {/*Choose a client*/} + + + + + + + {clients.filter((c) => c.type === action.type).map((client: any) => ( + + classNames( + active ? 'text-white bg-indigo-600' : 'text-gray-900', + 'cursor-default select-none relative py-2 pl-3 pr-9' + ) + } + value={client.id} + > + {({selected, active}) => ( + <> + + {client.name} + + + {selected ? ( + + + ) : null} + + )} + + ))} + + +
+ + )} +
+ )}/> +
+ +
+ +
+
+ +
+ + +
+ +
+
+ + + + {({input, meta}) => ( +
+ + {meta.touched && meta.error && + {meta.error}} +
+ )} +
+
+ +
+ + + + {({input, meta}) => ( +
+ + {meta.touched && meta.error && + {meta.error}} +
+ )} +
+
+
+
+ ) + case "DELUGE": + return ( +
+
+
+ ( + + {({open}) => ( + <> + Client +
+ + {input.value ? clients.find(c => c.id === input.value)!.name : "Choose a client"} + {/*Choose a client*/} + + + + + + + {clients.filter((c) => c.type === action.type).map((client: any) => ( + + classNames( + active ? 'text-white bg-indigo-600' : 'text-gray-900', + 'cursor-default select-none relative py-2 pl-3 pr-9' + ) + } + value={client.id} + > + {({selected, active}) => ( + <> + + {client.name} + + + {selected ? ( + + + ) : null} + + )} + + ))} + + +
+ + )} +
+ )}/> + +
+
+ +
+
+ +
+ +
+ +
+
+ + + + {({input, meta}) => ( +
+ + {meta.touched && meta.error && + {meta.error}} +
+ )} +
+
+ +
+ + + + {({input, meta}) => ( +
+ + {meta.touched && meta.error && + {meta.error}} +
+ )} +
+
+
+
+ ) + + default: + return

default

+ } + } + + return ( +
  • +
    + + Use setting + + +
    + + {edit && +
    + + +
    + + + + + {/* This element is to trick the browser into centering the modal contents. */} + + +
    +
    +
    +
    +
    +
    + + Remove filter action + +
    +

    + Are you sure you want to remove this action? + This action cannot be undone. +

    +
    +
    +
    +
    +
    + + +
    +
    +
    +
    +
    +
    + +
    + {({handleSubmit, values}) => { + return ( + + +
    +
    + + ( + + {({open}) => ( + <> + Type +
    + + {input.value ? actionTypeOptions.find(c => c.value === input.value)!.label : "Choose a type"} + + + + + + + {actionTypeOptions.map((opt) => ( + + classNames( + active ? 'text-white bg-indigo-600' : 'text-gray-900', + 'cursor-default select-none relative py-2 pl-3 pr-9' + ) + } + value={opt.value} + > + {({selected, active}) => ( + <> + + {opt.label} + + + {selected ? ( + + + ) : null} + + )} + + ))} + + +
    + + )} +
    + )}/> +
    + + + +
    + + {TypeForm(values)} + +
    +
    + + +
    + + +
    +
    +
    + + + + + ) + }} + +
    + } +
  • + ) +} diff --git a/web/src/components/debug.tsx b/web/src/components/debug.tsx new file mode 100644 index 0000000..1bc059a --- /dev/null +++ b/web/src/components/debug.tsx @@ -0,0 +1,15 @@ +import React from "react"; + +const DEBUG = ({ values }: any) => { + if (process.env.NODE_ENV !== "development") { + return null; + } + + return ( +
    +
    {JSON.stringify(values, 0 as any, 2)}
    +
    + ); +}; + +export default DEBUG; diff --git a/web/src/components/empty/EmptySimple.tsx b/web/src/components/empty/EmptySimple.tsx new file mode 100644 index 0000000..e344034 --- /dev/null +++ b/web/src/components/empty/EmptySimple.tsx @@ -0,0 +1,27 @@ +import {PlusIcon} from "@heroicons/react/solid"; + +interface props { + title: string; + subtitle: string; + buttonText: string; + buttonAction: any; +} + +const EmptySimple = ({ title, subtitle, buttonText, buttonAction}: props) => ( +
    +

    {title}

    +

    {subtitle}

    +
    + +
    +
    +) + +export default EmptySimple; \ No newline at end of file diff --git a/web/src/components/headings/TitleSubtitle.tsx b/web/src/components/headings/TitleSubtitle.tsx new file mode 100644 index 0000000..d29d40c --- /dev/null +++ b/web/src/components/headings/TitleSubtitle.tsx @@ -0,0 +1,15 @@ +import React from "react"; + +interface Props { + title: string; + subtitle: string; +} + +const TitleSubtitle: React.FC = ({ title, subtitle }) => ( +
    +

    {title}

    +

    {subtitle}

    +
    +) + +export default TitleSubtitle; \ No newline at end of file diff --git a/web/src/components/inputs/Error.tsx b/web/src/components/inputs/Error.tsx new file mode 100644 index 0000000..73f4693 --- /dev/null +++ b/web/src/components/inputs/Error.tsx @@ -0,0 +1,20 @@ +import React from "react"; +import { Field } from "react-final-form"; + +interface Props { + name: string; + classNames?: string; + subscribe?: any; +} + +const Error: React.FC = ({ name, classNames }) => ( + + touched && error ? {error} : null + } + /> +); + +export default Error; diff --git a/web/src/components/inputs/MultiSelectField.tsx b/web/src/components/inputs/MultiSelectField.tsx new file mode 100644 index 0000000..a298ec1 --- /dev/null +++ b/web/src/components/inputs/MultiSelectField.tsx @@ -0,0 +1,50 @@ +import React from "react"; +import {Field} from "react-final-form"; +import MultiSelect from "react-multi-select-component"; +import {classNames, COL_WIDTHS} from "../../styles/utils"; + +interface Props { + label?: string; + options?: [] | any; + name: string; + className?: string; + columns?: COL_WIDTHS; +} + +const MultiSelectField: React.FC = ({ + name, + label, + options, + className, + columns + }) => ( +
    + + val && val.map((item: any) => item.value)} + format={val => + val && + val.map((item: any) => options.find((o: any) => o.value === item)) + } + render={({input, meta}) => ( + + )} + /> +
    + ); + +export default MultiSelectField; diff --git a/web/src/components/inputs/RadioFieldset.tsx b/web/src/components/inputs/RadioFieldset.tsx new file mode 100644 index 0000000..46a8598 --- /dev/null +++ b/web/src/components/inputs/RadioFieldset.tsx @@ -0,0 +1,60 @@ +import React from "react"; +import {Field} from "react-final-form"; + +export interface radioFieldsetOption { + label: string; + description: string; + value: string; +} + +interface props { + name: string; + legend: string; + options: radioFieldsetOption[]; +} + +const RadioFieldset: React.FC = ({ name, legend,options }) => ( +
    +
    +
    + {legend} +
    +
    +
    + + {options.map((opt, idx) => ( +
    +
    + ( + + )} + /> +
    +
    + +

    + {opt.description} +

    +
    +
    + ))} + +
    +
    +
    +
    +) + +export default RadioFieldset; diff --git a/web/src/components/inputs/SwitchGroup.tsx b/web/src/components/inputs/SwitchGroup.tsx new file mode 100644 index 0000000..eb57421 --- /dev/null +++ b/web/src/components/inputs/SwitchGroup.tsx @@ -0,0 +1,55 @@ +import React from "react"; +import {Switch} from "@headlessui/react"; +import {Field} from "react-final-form"; +import {classNames} from "../../styles/utils"; + +interface Props { + name: string; + label: string; + description?: string; + className?: string; +} + +const SwitchGroup: React.FC = ({name, label, description}) => ( +
      + +
      + + {label} + + {description && ( + + {description} + + )} +
      + + ( + + Use setting + + )} + /> +
      +
    +) + +export default SwitchGroup; \ No newline at end of file diff --git a/web/src/components/inputs/TextAreaWide.tsx b/web/src/components/inputs/TextAreaWide.tsx new file mode 100644 index 0000000..24402f5 --- /dev/null +++ b/web/src/components/inputs/TextAreaWide.tsx @@ -0,0 +1,40 @@ +import {Field} from "react-final-form"; +import React from "react"; +import Error from "./Error"; +import {classNames} from "../../styles/utils"; + +interface Props { + name: string; + label?: string; + placeholder?: string; + className?: string; + required?: boolean; +} + +const TextAreaWide: React.FC = ({name, label, placeholder, required, className}) => ( +
    +
    + + +
    +
    + ( +