Skip to content

A modern and easy to use dashboard. 80+ integrations. 20K+ icons built in. Authentication out of the box. No YAML, drag and drop configuration.

7.0 KB
Raw Blame
import type { ReactNode } from "react";
import { Alert, Badge, Checkbox, Group, PasswordInput, Stack, Text, TextInput } from "@mantine/core";
import { IconCheck, IconKey, IconServer, IconX } from "@tabler/icons-react";

import type {
  CustomWidgetSecretKind,
  CustomWidgetLegacySecretKind,
  CustomWidgetSourceSetup,
  CustomWidgetSourceUrlIssue,
} from "../core";
import { getCustomWidgetSourceSetupIssue, isCustomWidgetHeaderSecretKind } from "../core";

export interface CustomWidgetSourceSetupValue {
  baseUrl: string;
  integrationId?: string;
  urlConfirmed: boolean;
  secrets: Partial<Record<CustomWidgetSecretKind, string>>;
}

export interface CustomWidgetSourceSetupMessages {
  title: string;
  description: string;
  suggestedUrl: string;
  baseUrl: string;
  authentication: string;
  confirmUrl: string;
  ready: string;
  needsUrl: string;
  credentialsMissing: string;
  credentialsOptional: string;
  configured: string;
  secret(kind: CustomWidgetLegacySecretKind): string;
  urlError(issue: CustomWidgetSourceUrlIssue): string;
}

export interface CustomWidgetSourceSetupPanelProps {
  setups: CustomWidgetSourceSetup[];
  values: Record<string, CustomWidgetSourceSetupValue>;
  messages: CustomWidgetSourceSetupMessages;
  onChange(sourceId: string, value: CustomWidgetSourceSetupValue): void;
  renderIntegrationSource?(setup: CustomWidgetSourceSetup, value: CustomWidgetSourceSetupValue): ReactNode;
}

export function createCustomWidgetSourceSetupValues(
  setups: readonly CustomWidgetSourceSetup[],
): Record<string, CustomWidgetSourceSetupValue> {
  return Object.fromEntries(
    setups.map((setup) => [
      setup.sourceId,
      {
        baseUrl: setup.baseUrl,
        integrationId: setup.integrationId,
        urlConfirmed: !setup.requiresUrlConfirmation,
        secrets: {} as Partial<Record<CustomWidgetSecretKind, string>>,
      } satisfies CustomWidgetSourceSetupValue,
    ]),
  );
}

export function isCustomWidgetSourceSetupReady(
  setups: readonly CustomWidgetSourceSetup[],
  values: Record<string, CustomWidgetSourceSetupValue>,
) {
  return setups.every((setup) => {
    const value = values[setup.sourceId];
    if (setup.integrationKind) return Boolean(value?.integrationId);
    return Boolean(value && !getCustomWidgetSourceSetupIssue(value) && value.urlConfirmed);
  });
}

export function CustomWidgetSourceSetupPanel({
  setups,
  values,
  messages,
  onChange,
  renderIntegrationSource,
}: CustomWidgetSourceSetupPanelProps) {
  if (setups.length === 0) return null;
  return (
    <Stack gap="sm">
      <Alert icon={<IconServer size={16} />}>
        <Text fw={600} size="sm">
          {messages.title}
        </Text>
        <Text size="sm">{messages.description}</Text>
      </Alert>
      {setups.map((setup) => {
        const value = values[setup.sourceId] ?? createCustomWidgetSourceSetupValues([setup])[setup.sourceId];
        if (!value) return null;
        if (setup.integrationKind) {
          return (
            <Stack
              key={setup.sourceId}
              gap="sm"
              p="md"
              style={{
                border: "1px solid var(--mantine-color-default-border)",
                borderRadius: "var(--mantine-radius-md)",
              }}
            >
              <Text fw={600}>{setup.sourceName}</Text>
              {renderIntegrationSource?.(setup, value)}
            </Stack>
          );
        }
        const issue = getCustomWidgetSourceSetupIssue(value);
        const missingCredentials = setup.credentialFields.filter(
          (field) => !field.configured && !value.secrets[field.kind]?.trim(),
        );
        const urlReady = !issue && value.urlConfirmed;
        return (
          <Stack
            key={setup.sourceId}
            gap="sm"
            p="md"
            style={{
              border: "1px solid var(--mantine-color-default-border)",
              borderRadius: "var(--mantine-radius-md)",
            }}
          >
            <Group justify="space-between" align="flex-start">
              <div>
                <Text fw={600}>{setup.sourceName}</Text>
                <Text size="xs" c="dimmed">
                  {setup.sourceId}
                </Text>
              </div>
              <Group gap="xs">
                <Badge
                  variant="light"
                  color={urlReady ? "green" : "red"}
                  leftSection={urlReady ? <IconCheck size={11} /> : <IconX size={11} />}
                >
                  {urlReady ? messages.ready : messages.needsUrl}
                </Badge>
                {missingCredentials.length > 0 && (
                  <Badge variant="light" color="yellow" leftSection={<IconKey size={11} />}>
                    {messages.credentialsMissing}
                  </Badge>
                )}
              </Group>
            </Group>
            <Text size="xs" c="dimmed">
              {messages.suggestedUrl}: {setup.suggestedBaseUrl}
            </Text>
            <TextInput
              label={messages.baseUrl}
              type="url"
              value={value.baseUrl}
              error={issue ? messages.urlError(issue) : undefined}
              onChange={(event) =>
                onChange(setup.sourceId, { ...value, baseUrl: event.currentTarget.value, urlConfirmed: true })
              }
            />
            <Group grow align="start">
              <TextInput label={messages.authentication} value={setup.authType} readOnly />
            </Group>
            {setup.requiresUrlConfirmation && value.baseUrl === setup.suggestedBaseUrl && (
              <Checkbox
                checked={value.urlConfirmed}
                label={messages.confirmUrl}
                onChange={(event) => onChange(setup.sourceId, { ...value, urlConfirmed: event.currentTarget.checked })}
              />
            )}
            {setup.credentialFields.map((field) => {
              const Input = field.kind === "username" ? TextInput : PasswordInput;
              let label: string;
              if (isCustomWidgetHeaderSecretKind(field.kind)) label = field.destination ?? field.kind.slice(7);
              else label = messages.secret(field.kind);
              return (
                <Input
                  key={field.kind}
                  label={label}
                  description={field.destination}
                  value={value.secrets[field.kind] ?? ""}
                  placeholder={field.configured ? messages.configured : undefined}
                  leftSection={<IconKey size={15} />}
                  autoComplete="off"
                  onChange={(event) =>
                    onChange(setup.sourceId, {
                      ...value,
                      secrets: { ...value.secrets, [field.kind]: event.currentTarget.value },
                    })
                  }
                />
              );
            })}
            {missingCredentials.length > 0 && (
              <Text size="xs" c="yellow">
                {messages.credentialsOptional}
              </Text>
            )}
          </Stack>
        );
      })}
    </Stack>
  );
}