Skip to content

Commit

Permalink
add message if inconsistent root config
Browse files Browse the repository at this point in the history
  • Loading branch information
sinamics committed Jan 13, 2024
1 parent 079795b commit ae62a53
Show file tree
Hide file tree
Showing 3 changed files with 127 additions and 47 deletions.
93 changes: 93 additions & 0 deletions src/components/adminPage/controller/createPlanet.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import React from "react";
import RootForm from "./rootForm";
import { useTranslations } from "next-intl";
import { Planet } from "@prisma/client";

interface IProps {
getPlanet: Planet & { error?: Error };
open: boolean;
setOpen: (open: boolean) => void;
resetWorld: () => void;
handleFileChange: (event: React.ChangeEvent<HTMLInputElement>) => void;
triggerFileInput: () => void;
closeForm: () => void;
}
const CreatePlanet = ({
getPlanet,
open,
setOpen,
resetWorld,
handleFileChange,
triggerFileInput,
closeForm,
}: IProps) => {
const t = useTranslations("admin");

if (getPlanet?.error) {
return (
<div role="alert" className="alert alert-error mt-10">
<svg
xmlns="http://www.w3.org/2000/svg"
className="stroke-current shrink-0 h-6 w-6"
fill="none"
viewBox="0 0 24 24"
>
<path
strokeLinecap="round"
strokeLinejoin="round"
strokeWidth="2"
d="M12 9v2m0 4h.01m-6.938 4h13.856c1.54 0 2.502-1.667 1.732-3L13.732 4c-.77-1.333-2.694-1.333-3.464 0L3.34 16c-.77 1.333.192 3 1.732 3z"
/>
</svg>
<span>{getPlanet?.error?.message}</span>
<div>
<button onClick={() => resetWorld()} className="btn btn-sm">
Reset
</button>
</div>
</div>
);
}
return (
<div>
<div className="flex w-full justify-between">
<div className={"cursor-pointer"}>
<div className="flex font-medium">
<span>{t("controller.generatePlanet.generatePrivateRootLabel")}</span>
</div>
<div>
<p className="m-0 p-0 text-xs text-gray-500">
{t("controller.generatePlanet.generatePrivateRootPlaceholder")}
</p>
</div>
</div>
<div className="flex gap-3">
<button
onClick={() => setOpen(!open)}
data-testid="view-form"
className="btn btn-sm"
>
{t("controller.generatePlanet.buttons.createPlanet")}
</button>
<input
type="file"
id="fileInput"
style={{ display: "none" }}
onChange={handleFileChange}
accept=".zip"
/>
<button
data-testid="view-form"
className="btn btn-sm"
onClick={triggerFileInput}
>
{t("controller.generatePlanet.buttons.uploadConfig")}
</button>
</div>
</div>
{open ? <RootForm onClose={closeForm} /> : null}
</div>
);
};

export default CreatePlanet;
52 changes: 11 additions & 41 deletions src/components/adminPage/controller/privateRoot.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { api } from "~/utils/api";
import { useModalStore } from "~/utils/store";
import RootForm from "./rootForm";
import Link from "next/link";
import CreatePlanet from "./createPlanet";

const PrivateRoot = () => {
const t = useTranslations("admin");
Expand All @@ -30,7 +31,6 @@ const PrivateRoot = () => {
const a = document.createElement("a");
a.style.display = "none";
a.href = url;
// the filename you want
a.download = "ztnet-world.zip";
document.body.appendChild(a);
a.click();
Expand Down Expand Up @@ -103,7 +103,6 @@ const PrivateRoot = () => {
<p className="text-sm text-gray-500">
{t("controller.generatePlanet.updatePlanetWarning")}
</p>

{getPlanet?.rootNodes?.length > 0 ? (
<>
<div className="space-y-4">
Expand All @@ -123,7 +122,7 @@ const PrivateRoot = () => {
</svg>
<span>{t("controller.generatePlanet.customPlanetInUse")}</span>
</div>
<div className=" space-y-5">
<div className="space-y-5">
{getPlanet?.rootNodes?.map((node, i) => (
<div key={node.id} className="border border-primary rounded p-4 my-4">
<p className="tracking-wide font-medium">Root #{i + 1}</p>
Expand Down Expand Up @@ -184,44 +183,15 @@ const PrivateRoot = () => {
</div>
</>
) : (
<>
<div className="flex w-full justify-between">
<div className={"cursor-pointer"}>
<div className="flex font-medium">
<span>{t("controller.generatePlanet.generatePrivateRootLabel")}</span>
</div>
<div>
<p className="m-0 p-0 text-xs text-gray-500">
{t("controller.generatePlanet.generatePrivateRootPlaceholder")}
</p>
</div>
</div>
<div className="flex gap-3">
<button
onClick={() => setOpen(!open)}
data-testid="view-form"
className="btn btn-sm"
>
{t("controller.generatePlanet.buttons.createPlanet")}
</button>
<input
type="file"
id="fileInput"
style={{ display: "none" }}
onChange={handleFileChange}
accept=".zip"
/>
<button
data-testid="view-form"
className="btn btn-sm"
onClick={triggerFileInput}
>
{t("controller.generatePlanet.buttons.uploadConfig")}
</button>
</div>
</div>
{open ? <RootForm onClose={closeForm} /> : null}
</>
<CreatePlanet
getPlanet={getPlanet}
resetWorld={resetWorld}
open={open}
setOpen={setOpen}
handleFileChange={handleFileChange}
triggerFileInput={triggerFileInput}
closeForm={closeForm}
/>
)}
</div>
</div>
Expand Down
29 changes: 23 additions & 6 deletions src/server/api/routers/adminRoute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -831,6 +831,12 @@ export const adminRouter = createTRPCRouter({
return { ip, identity };
}),
getPlanet: adminRoleProtectedRoute.query(async ({ ctx }) => {
const paths = {
backupDir: `${ZT_FOLDER}/planet_backup`,
planetPath: `${ZT_FOLDER}/planet`,
mkworldDir: `${ZT_FOLDER}/zt-mkworld`,
};

const options = await ctx.prisma.globalOptions.findFirst({
where: {
id: 1,
Expand All @@ -847,6 +853,20 @@ export const adminRouter = createTRPCRouter({
},
},
});
// Check if the backup directory exists
const backupExists = fs.existsSync(paths.backupDir);

// If backup exists and no planet data is found in the database
if (backupExists && !options?.planet) {
return {
error: new Error(
"Inconsistent configuration: Planet backup exists but no planet data in the database.",
),
rootNodes: [], // Assuming rootNodes as an empty array in case of error
...options?.planet,
};
}

return options?.planet;
}),
makeWorld: adminRoleProtectedRoute
Expand Down Expand Up @@ -1069,18 +1089,15 @@ export const adminRouter = createTRPCRouter({
.filter((file) => file.startsWith("planet.bak."))
.sort();

if (backups.length === 0) {
throw new Error("No backup files found.");
}

// Restore from the latest backup
const latestBackup = backups.at(-1);
fs.copyFileSync(`${paths.backupDir}/${latestBackup}`, paths.planetPath);
if (latestBackup) {
fs.copyFileSync(`${paths.backupDir}/${latestBackup}`, paths.planetPath);
}

// Clean up backup and mkworld directories
fs.rmSync(paths.backupDir, { recursive: true, force: true });
fs.rmSync(paths.mkworldDir, { recursive: true, force: true });

/*
*
* Reset local.conf with default port number
Expand Down

0 comments on commit ae62a53

Please sign in to comment.