Use tryMatchPort

This commit is contained in:
ajnart
2022-07-22 16:19:07 +02:00
parent b5f1491fbb
commit 3b0658fee2
2 changed files with 6 additions and 22 deletions

View File

@@ -22,7 +22,7 @@ import { IconApps as Apps } from '@tabler/icons';
import { useEffect, useState } from 'react'; import { useEffect, useState } from 'react';
import { v4 as uuidv4 } from 'uuid'; import { v4 as uuidv4 } from 'uuid';
import { useConfig } from '../../tools/state'; import { useConfig } from '../../tools/state';
import { ServiceTypeList, StatusCodes } from '../../tools/types'; import { tryMatchPort, ServiceTypeList, StatusCodes } from '../../tools/types';
import Tip from '../layout/Tip'; import Tip from '../layout/Tip';
export function AddItemShelfButton(props: any) { export function AddItemShelfButton(props: any) {
@@ -77,24 +77,6 @@ function MatchService(name: string, form: any) {
} }
} }
function MatchPort(name: string, form: any) {
const portmap = [
{ name: 'qbittorrent', value: '8080' },
{ name: 'sonarr', value: '8989' },
{ name: 'radarr', value: '7878' },
{ name: 'lidarr', value: '8686' },
{ name: 'readarr', value: '8787' },
{ name: 'deluge', value: '8112' },
{ name: 'transmission', value: '9091' },
{ name: 'dash.', value: '3001' },
];
// Match name with portmap key
const port = portmap.find((p) => p.name === name.toLowerCase());
if (port) {
form.setFieldValue('url', `http://localhost:${port.value}`);
}
}
const DEFAULT_ICON = '/favicon.svg'; const DEFAULT_ICON = '/favicon.svg';
export function AddAppShelfItemForm(props: { setOpened: (b: boolean) => void } & any) { export function AddAppShelfItemForm(props: { setOpened: (b: boolean) => void } & any) {
@@ -154,7 +136,7 @@ export function AddAppShelfItemForm(props: { setOpened: (b: boolean) => void } &
if (form.values.name !== debounced || form.values.icon !== DEFAULT_ICON) return; if (form.values.name !== debounced || form.values.icon !== DEFAULT_ICON) return;
MatchIcon(form.values.name, form); MatchIcon(form.values.name, form);
MatchService(form.values.name, form); MatchService(form.values.name, form);
MatchPort(form.values.name, form); tryMatchPort(form.values.name, form);
}, [debounced]); }, [debounced]);
// Try to set const hostname to new URL(form.values.url).hostname) // Try to set const hostname to new URL(form.values.url).hostname)

View File

@@ -1,5 +1,5 @@
import Dockerode from 'dockerode'; import Dockerode from 'dockerode';
import { Config, MatchingImages, ServiceType } from './types'; import { Config, MatchingImages, ServiceType, tryMatchPort } from './types';
async function MatchIcon(name: string) { async function MatchIcon(name: string) {
const res = await fetch( const res = await fetch(
@@ -23,11 +23,13 @@ function tryMatchType(imageName: string): ServiceType {
export function tryMatchService(container: Dockerode.ContainerInfo | undefined) { export function tryMatchService(container: Dockerode.ContainerInfo | undefined) {
if (container === undefined) return {}; if (container === undefined) return {};
const name = container.Names[0].substring(1); const name = container.Names[0].substring(1);
const type = tryMatchType(container.Image);
const port = tryMatchPort(type.toLowerCase());
return { return {
name, name,
id: container.Id, id: container.Id,
type: tryMatchType(container.Image), type: tryMatchType(container.Image),
url: `${container.Ports.at(0)?.IP}:${container.Ports.at(0)?.PublicPort}`, url: `localhost${port ? `:${port.value}` : ''}`,
icon: `https://cdn.jsdelivr.net/gh/walkxhub/dashboard-icons/png/${name icon: `https://cdn.jsdelivr.net/gh/walkxhub/dashboard-icons/png/${name
.replace(/\s+/g, '-') .replace(/\s+/g, '-')
.toLowerCase()}.png`, .toLowerCase()}.png`,