implement install-commands button (#10)

* fix: type conversion

* implement install-commands button
This commit is contained in:
UUBulb
2024-11-30 13:44:17 +08:00
committed by GitHub
parent 35436e69bb
commit 7d672fa8c5
8 changed files with 149 additions and 12 deletions

View File

@@ -6,19 +6,40 @@ import {
} from "@/components/ui/dropdown-menu"
import { Button, ButtonProps } from "@/components/ui/button"
import { forwardRef, useState } from "react"
import { useConfig } from "@/hooks/useConfig"
import { ConfigEssential } from "@/types"
import { Check, Clipboard } from "lucide-react"
import { t } from "i18next"
import { toast } from "sonner"
import { useTranslation } from "react-i18next"
enum OSTypes {
Linux = 1,
macOS,
Windows
}
export const InstallCommandsMenu = forwardRef<HTMLButtonElement, ButtonProps>((props, ref) => {
const [copy, setCopy] = useState(false);
const { config } = useConfig();
const { t } = useTranslation();
const switchState = async () => {
const switchState = async (type: number) => {
if (!copy) {
setCopy(true);
await navigator.clipboard.writeText("stub");
setTimeout(() => {
setCopy(false);
}, 1000);
try {
setCopy(true);
if (config)
await navigator.clipboard.writeText(generateCommand(type, config));
} catch (e) {
console.error(e);
toast(t("Error"), {
description: t("Results.UnExpectedError"),
})
} finally {
setTimeout(() => {
setCopy(false);
}, 2 * 1000);
}
}
}
@@ -31,10 +52,30 @@ export const InstallCommandsMenu = forwardRef<HTMLButtonElement, ButtonProps>((p
</Button>
</DropdownMenuTrigger>
<DropdownMenuContent>
<DropdownMenuItem onClick={switchState}>Linux</DropdownMenuItem>
<DropdownMenuItem onClick={switchState}>macOS</DropdownMenuItem>
<DropdownMenuItem onClick={switchState}>Windows</DropdownMenuItem>
<DropdownMenuItem onClick={async () => { switchState(OSTypes.Linux) }}>Linux</DropdownMenuItem>
<DropdownMenuItem onClick={async () => { switchState(OSTypes.macOS) }}>macOS</DropdownMenuItem>
<DropdownMenuItem onClick={async () => { switchState(OSTypes.Windows) }}>Windows</DropdownMenuItem>
</DropdownMenuContent>
</DropdownMenu>
);
})
const generateCommand = (type: number, { agent_secret_key, install_host, listen_port, tls }: ConfigEssential) => {
if (!install_host)
throw new Error("You have not specify the installed host.");
const env = `NZ_SERVER=${install_host}:${listen_port} NZ_TLS=${tls || false} NZ_CLIENT_SECRET=${agent_secret_key}`;
switch (type) {
case OSTypes.Linux:
case OSTypes.macOS: {
return `curl -L https://raw.githubusercontent.com/nezhahq/scripts/main/agent/install.sh -o nezha.sh && chmod +x nezha.sh && env ${env} ./nezha.sh`
}
case OSTypes.Windows: {
return `${env} [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Ssl3 -bor [Net.SecurityProtocolType]::Tls -bor [Net.SecurityProtocolType]::Tls11 -bor [Net.SecurityProtocolType]::Tls12;set-ExecutionPolicy RemoteSigned;Invoke-WebRequest https://raw.githubusercontent.com/nezhahq/scripts/main/agent/install.ps1 -OutFile C:\install.ps1;powershell.exe C:\install.ps1`
}
default: {
throw new Error(`Unknown OS: ${type}`);
}
}
}