diff --git a/src/components/adminPage/controller/createPlanet.tsx b/src/components/adminPage/controller/createPlanet.tsx new file mode 100644 index 00000000..9d10af36 --- /dev/null +++ b/src/components/adminPage/controller/createPlanet.tsx @@ -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) => void; + triggerFileInput: () => void; + closeForm: () => void; +} +const CreatePlanet = ({ + getPlanet, + open, + setOpen, + resetWorld, + handleFileChange, + triggerFileInput, + closeForm, +}: IProps) => { + const t = useTranslations("admin"); + + if (getPlanet?.error) { + return ( +
+ + + + {getPlanet?.error?.message} +
+ +
+
+ ); + } + return ( +
+
+
+
+ {t("controller.generatePlanet.generatePrivateRootLabel")} +
+
+

+ {t("controller.generatePlanet.generatePrivateRootPlaceholder")} +

+
+
+
+ + + +
+
+ {open ? : null} +
+ ); +}; + +export default CreatePlanet; diff --git a/src/components/adminPage/controller/privateRoot.tsx b/src/components/adminPage/controller/privateRoot.tsx index 40d844be..8c4e4063 100644 --- a/src/components/adminPage/controller/privateRoot.tsx +++ b/src/components/adminPage/controller/privateRoot.tsx @@ -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"); @@ -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(); @@ -103,7 +103,6 @@ const PrivateRoot = () => {

{t("controller.generatePlanet.updatePlanetWarning")}

- {getPlanet?.rootNodes?.length > 0 ? ( <>
@@ -123,7 +122,7 @@ const PrivateRoot = () => { {t("controller.generatePlanet.customPlanetInUse")}
-
+
{getPlanet?.rootNodes?.map((node, i) => (

Root #{i + 1}

@@ -184,44 +183,15 @@ const PrivateRoot = () => {
) : ( - <> -
-
-
- {t("controller.generatePlanet.generatePrivateRootLabel")} -
-
-

- {t("controller.generatePlanet.generatePrivateRootPlaceholder")} -

-
-
-
- - - -
-
- {open ? : null} - + )}
diff --git a/src/server/api/routers/adminRoute.ts b/src/server/api/routers/adminRoute.ts index 4b69b4b3..55f560b6 100644 --- a/src/server/api/routers/adminRoute.ts +++ b/src/server/api/routers/adminRoute.ts @@ -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, @@ -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 @@ -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