From 6e71047570dc0c4a5c60bd1711af3be2e949236c Mon Sep 17 00:00:00 2001
From: Bram Suurd <78373894+BramSuurdje@users.noreply.github.com>
Date: Tue, 19 Nov 2024 22:21:00 +0100
Subject: [PATCH] Refactor Buttons component to use a ButtonLink for cleaner
code, simplifying the source URL generation and layout (#371)
* Refactor Buttons component to use a ButtonLink for cleaner code, simplifying the source URL generation and layout
* Refactor DefaultPassword component to simplify credential handling and enhance code readability with map function
* Refactor DefaultSettings component to improve resource display logic and enhance readability using a new ResourceDisplay subcomponent
---
.../_components/ScriptItems/Buttons.tsx | 76 ++++++++++---------
.../ScriptItems/DefaultPassword.tsx | 76 ++++++++-----------
.../ScriptItems/DefaultSettings.tsx | 70 ++++++++---------
3 files changed, 104 insertions(+), 118 deletions(-)
diff --git a/frontend/src/app/scripts/_components/ScriptItems/Buttons.tsx b/frontend/src/app/scripts/_components/ScriptItems/Buttons.tsx
index 00a667827..cb46e437b 100644
--- a/frontend/src/app/scripts/_components/ScriptItems/Buttons.tsx
+++ b/frontend/src/app/scripts/_components/ScriptItems/Buttons.tsx
@@ -5,45 +5,53 @@ import { BookOpenText, Code, Globe } from "lucide-react";
import Link from "next/link";
const generateSourceUrl = (slug: string, type: string) => {
- if (type === "ct") {
- return `https://raw.githubusercontent.com/community-scripts/${basePath}/main/install/${slug}-install.sh`;
- } else {
- return `https://raw.githubusercontent.com/community-scripts/${basePath}/main/${type}/${slug}.sh`;
- }
+ const baseUrl = `https://raw.githubusercontent.com/community-scripts/${basePath}/main`;
+ return type === "ct"
+ ? `${baseUrl}/install/${slug}-install.sh`
+ : `${baseUrl}/${type}/${slug}.sh`;
};
+interface ButtonLinkProps {
+ href: string;
+ icon: React.ReactNode;
+ text: string;
+}
+
+const ButtonLink = ({ href, icon, text }: ButtonLinkProps) => (
+
+);
+
export default function Buttons({ item }: { item: Script }) {
+ const buttons = [
+ item.website && {
+ href: item.website,
+ icon: ,
+ text: "Website",
+ },
+ item.documentation && {
+ href: item.documentation,
+ icon: ,
+ text: "Documentation",
+ },
+ {
+ href: generateSourceUrl(item.slug, item.type),
+ icon:
,
+ text: "Source Code",
+ },
+ ].filter(Boolean) as ButtonLinkProps[];
+
return (
- {item.website && (
-
- )}
- {item.documentation && (
-
- )}
- {
-
- }
+ {buttons.map((props, index) => (
+
+ ))}
);
}
diff --git a/frontend/src/app/scripts/_components/ScriptItems/DefaultPassword.tsx b/frontend/src/app/scripts/_components/ScriptItems/DefaultPassword.tsx
index 4bd555a10..15a962318 100644
--- a/frontend/src/app/scripts/_components/ScriptItems/DefaultPassword.tsx
+++ b/frontend/src/app/scripts/_components/ScriptItems/DefaultPassword.tsx
@@ -4,55 +4,39 @@ import { Separator } from "@/components/ui/separator";
import { Script } from "@/lib/types";
export default function DefaultPassword({ item }: { item: Script }) {
- const hasDefaultLogin =
- item.default_credentials.username && item.default_credentials.password;
+ const { username, password } = item.default_credentials;
+ const hasDefaultLogin = username && password;
+
+ if (!hasDefaultLogin) return null;
+
+ const copyCredential = (type: "username" | "password") => {
+ handleCopy(type, item.default_credentials[type] ?? "");
+ };
return (
-
- {hasDefaultLogin && (
-
-
-
Default Login Credentials
+
+
+
Default Login Credentials
+
+
+
+
+ You can use the following credentials to login to the {item.name}{" "}
+ {item.type}.
+
+ {["username", "password"].map((type) => (
+
+ {type.charAt(0).toUpperCase() + type.slice(1)}:{" "}
+
-
-
-
- You can use the following credentials to login to the {""}
- {item.name} {item.type}.
-
-
- Username:{" "}
-
-
-
- Password:{" "}
-
-
-
-
- )}
+ ))}
+
);
}
diff --git a/frontend/src/app/scripts/_components/ScriptItems/DefaultSettings.tsx b/frontend/src/app/scripts/_components/ScriptItems/DefaultSettings.tsx
index 1c7d89c78..bfa61759d 100644
--- a/frontend/src/app/scripts/_components/ScriptItems/DefaultSettings.tsx
+++ b/frontend/src/app/scripts/_components/ScriptItems/DefaultSettings.tsx
@@ -1,56 +1,50 @@
import { Script } from "@/lib/types";
export default function DefaultSettings({ item }: { item: Script }) {
+ const getDisplayValueFromRAM = (ram: number) =>
+ ram >= 1024 ? `${Math.floor(ram / 1024)}GB` : `${ram}MB`;
+
+ const ResourceDisplay = ({
+ settings,
+ title,
+ }: {
+ settings: (typeof item.install_methods)[0];
+ title: string;
+ }) => {
+ const { cpu, ram, hdd } = settings.resources;
+ return (
+
+
{title}
+
CPU: {cpu}vCPU
+
+ RAM: {getDisplayValueFromRAM(ram ?? 0)}
+
+
HDD: {hdd}GB
+
+ );
+ };
+
const defaultSettings = item.install_methods.find(
(method) => method.type === "default",
);
-
- const defaultSettingsAvailable =
- defaultSettings?.resources.cpu ||
- defaultSettings?.resources.ram ||
- defaultSettings?.resources.hdd;
-
const defaultAlpineSettings = item.install_methods.find(
(method) => method.type === "alpine",
);
- const getDisplayValueFromRAM = (ram: number) => {
- if (ram >= 1024) {
- return (ram / 1024).toFixed(0) + "GB";
- }
- return ram + "MB";
- };
+ const hasDefaultSettings =
+ defaultSettings?.resources &&
+ Object.values(defaultSettings.resources).some(Boolean);
return (
<>
- {defaultSettingsAvailable && (
-
-
Default settings
-
- CPU: {defaultSettings?.resources.cpu}vCPU
-
-
- RAM: {getDisplayValueFromRAM(defaultSettings?.resources.ram ?? 0)}
-
-
- HDD: {defaultSettings?.resources.hdd}GB
-
-
+ {hasDefaultSettings && (
+
)}
{defaultAlpineSettings && (
-
-
Default Alpine settings
-
- CPU: {defaultAlpineSettings?.resources.cpu}vCPU
-
-
- RAM:{" "}
- {getDisplayValueFromRAM(defaultAlpineSettings?.resources.ram ?? 0)}
-
-
- HDD: {defaultAlpineSettings?.resources.hdd}GB
-
-
+
)}
>
);