Track HiveGrid Cloud planning reference
This commit is contained in:
1 parent
b3d5ea6764
commit
e341c5938a
5784 files changed
+1104929
-3
No files matched your search
@@ -0,0 +1,223 @@
|
||||
import { LoaderFunction, LoaderFunctionArgs, useLoaderData } from "react-router";
|
||||
import { useNavigate, useSearchParams } from "react-router";
|
||||
import clsx from "clsx";
|
||||
import { useEffect, useState } from "react";
|
||||
import { useTranslation } from "react-i18next";
|
||||
import { Colors } from "~/application/enums/shared/Colors";
|
||||
import ColorBadge from "~/components/ui/badges/ColorBadge";
|
||||
import ButtonSecondary from "~/components/ui/buttons/ButtonSecondary";
|
||||
import InputSelect from "~/components/ui/input/InputSelect";
|
||||
import InputText from "~/components/ui/input/InputText";
|
||||
import CodeGeneratorService, { CodeGeneratorFileDto, CodeGeneratorOptions } from "~/modules/codeGenerator/service/CodeGeneratorService";
|
||||
import { useRootData } from "~/utils/data/useRootData";
|
||||
import { EntityWithDetails, getEntityByName } from "~/utils/db/entities/entities.db.server";
|
||||
import { verifyUserHasPermission } from "~/utils/helpers/.server/PermissionsService";
|
||||
|
||||
type LoaderData = {
|
||||
entity: EntityWithDetails;
|
||||
};
|
||||
export let loader = async ({ request, params }: LoaderFunctionArgs) => {
|
||||
await verifyUserHasPermission(request, "admin.entities.view");
|
||||
const entity = await getEntityByName({ tenantId: null, name: params.entity! });
|
||||
return Response.json({ entity });
|
||||
};
|
||||
|
||||
export default function CodeGeneratorFiles() {
|
||||
const { t } = useTranslation();
|
||||
const { debug } = useRootData();
|
||||
const data = useLoaderData<LoaderData>();
|
||||
const navigate = useNavigate();
|
||||
|
||||
const [showEditor] = useState<boolean>(true);
|
||||
const [searchInput, setSearchInput] = useState<string>("");
|
||||
const [searchParams, setSearchParams] = useSearchParams();
|
||||
|
||||
const [options, setOptions] = useState<CodeGeneratorOptions>(getDefaultOptions(data.entity, searchParams.get("type")?.toString()));
|
||||
|
||||
const [files, setFiles] = useState<CodeGeneratorFileDto[]>([]);
|
||||
|
||||
useEffect(() => {
|
||||
const options: CodeGeneratorOptions = getDefaultOptions(data.entity, searchParams.get("type")?.toString());
|
||||
setOptions(options);
|
||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||
}, [data.entity, searchParams]);
|
||||
|
||||
useEffect(() => {
|
||||
if (options) {
|
||||
const moduleFiles = CodeGeneratorService.getModuleFiles(options);
|
||||
const routeFiles = CodeGeneratorService.getRouteFiles(options);
|
||||
setFiles([...moduleFiles, ...routeFiles]);
|
||||
} else {
|
||||
setFiles([]);
|
||||
}
|
||||
}, [options]);
|
||||
|
||||
useEffect(() => {
|
||||
const file = searchParams.get("file");
|
||||
if (!file && files.length > 0) {
|
||||
searchParams.set("file", files[0].file);
|
||||
setSearchParams(searchParams);
|
||||
}
|
||||
}, [files, searchParams, setSearchParams]);
|
||||
|
||||
function getDefaultOptions(entity: EntityWithDetails, type?: string) {
|
||||
const options: CodeGeneratorOptions = {
|
||||
entity,
|
||||
type: type ?? "dynamic",
|
||||
moduleDirectory: CodeGeneratorService.moduleDirectoryOptions(data.entity)[0].value,
|
||||
routesDirectory: CodeGeneratorService.routeDirectoryOptions(data.entity)[0].value,
|
||||
deleteFilesOnFinish: false,
|
||||
// generateZip: false,
|
||||
};
|
||||
return options;
|
||||
}
|
||||
|
||||
function getSelectedFileCode() {
|
||||
const file = searchParams.get("file");
|
||||
const selectedFile = files.find((e) => e.file === file);
|
||||
if (selectedFile) {
|
||||
return selectedFile.content;
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
function getSelectedRoute() {
|
||||
if (options?.routesDirectory.includes("/app.$tenant")) {
|
||||
return "/";
|
||||
} else {
|
||||
return options?.routesDirectory.replace("./apps/web/app/routes", "") ?? "";
|
||||
}
|
||||
}
|
||||
function onTry() {
|
||||
navigate(getSelectedRoute());
|
||||
}
|
||||
function filteredItems() {
|
||||
return files.filter(
|
||||
(e) =>
|
||||
e.file.toLowerCase().includes(searchInput.toLowerCase()) ||
|
||||
// e.content.toLowerCase().includes(searchInput.toLowerCase()) ||
|
||||
e.type.toLowerCase().includes(searchInput.toLowerCase())
|
||||
);
|
||||
}
|
||||
return (
|
||||
<div className="space-y-3 p-4">
|
||||
<div className="flex items-center justify-between space-x-2">
|
||||
<div className="text-foreground text-lg font-bold">{t("models.entity.codeGenerator.title")}</div>
|
||||
</div>
|
||||
<div className="space-y-2">
|
||||
<div className="grid grid-cols-3 gap-2">
|
||||
<div>
|
||||
<InputSelect
|
||||
className="col-span-3"
|
||||
title={t("models.entity.type")}
|
||||
value={options?.type}
|
||||
setValue={(e) => {
|
||||
setOptions({ ...options, type: e?.toString() ?? "" });
|
||||
searchParams.set("type", e?.toString() ?? "");
|
||||
searchParams.delete("file");
|
||||
setSearchParams(searchParams);
|
||||
}}
|
||||
options={[
|
||||
{ name: t("models.entity.codeGenerator.types.dynamic"), value: "dynamic" },
|
||||
{ name: t("models.entity.codeGenerator.types.customModel"), value: "custom", disabled: true },
|
||||
]}
|
||||
/>
|
||||
</div>
|
||||
<div>
|
||||
<InputSelect
|
||||
title={t("models.entity.codeGenerator.moduleDirectory")}
|
||||
value={options?.moduleDirectory}
|
||||
setValue={(e) => setOptions({ ...options, moduleDirectory: e?.toString() ?? "" })}
|
||||
options={CodeGeneratorService.moduleDirectoryOptions(data.entity)}
|
||||
/>
|
||||
</div>
|
||||
<div>
|
||||
<InputSelect
|
||||
title={t("models.entity.codeGenerator.routesDirectory")}
|
||||
value={options?.routesDirectory}
|
||||
setValue={(e) => setOptions({ ...options, routesDirectory: e?.toString() ?? "" })}
|
||||
options={CodeGeneratorService.routeDirectoryOptions(data.entity)}
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<label className="text-muted-foreground mb-1 flex justify-between space-x-2 truncate text-xs font-medium">{t("models.entity.codeGenerator.files")}</label>
|
||||
<div className="border-border bg-background my-1 h-[calc(100vh-270px)] overflow-hidden rounded-md border p-2">
|
||||
<div className="flex flex-col space-y-2 md:flex-row md:space-x-2 md:space-y-0">
|
||||
<div className="md:w-5/12">
|
||||
<div className="hidden space-y-2 overflow-y-auto p-1 md:block">
|
||||
<InputText placeholder={t("models.entity.codeGenerator.searchFiles", { count: files.length })} value={searchInput} setValue={(e) => setSearchInput(e.toString() ?? "")} />
|
||||
<ul className="bg-background border-border divide-border h-[calc(100vh-325px)] divide-y overflow-y-scroll rounded-md border">
|
||||
{filteredItems().map((file, idx) => (
|
||||
<li key={idx}>
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => {
|
||||
searchParams.set("file", file.file);
|
||||
setSearchParams(searchParams);
|
||||
}}
|
||||
className={clsx(
|
||||
"w-full cursor-pointer truncate rounded-sm border-2 border-dashed p-2 text-left text-sm",
|
||||
searchParams.get("file") === file.file
|
||||
? "border-border text-foreground bg-secondary/90 "
|
||||
: "hover:bg-secondary/90 text-foreground/80 border-transparent"
|
||||
)}
|
||||
>
|
||||
<div className="flex items-center justify-between space-x-3">
|
||||
<div className="flex flex-col">
|
||||
<div className="truncate">
|
||||
{file.file} <span className="text-muted-foreground text-xs">({t("models.entity.codeGenerator.lineCount", { count: file.content.split("\n").length })})</span>
|
||||
</div>
|
||||
{file.directory && <span className="text-muted-foreground text-xs font-medium">{file.directory}/</span>}
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<ColorBadge color={file.type === "module" ? Colors.BLUE : file.type === "route" ? Colors.RED : Colors.VIOLET} />
|
||||
</div>
|
||||
</div>
|
||||
</button>
|
||||
</li>
|
||||
))}
|
||||
</ul>
|
||||
</div>
|
||||
<InputSelect
|
||||
className="md:hidden"
|
||||
options={files.map((file, idx) => {
|
||||
return { name: file.file, value: idx };
|
||||
})}
|
||||
title={t("models.entity.codeGenerator.selectFile")}
|
||||
value="test"
|
||||
setValue={(i) => {
|
||||
searchParams.set("file", files[i as number].file);
|
||||
setSearchParams(searchParams);
|
||||
}}
|
||||
/>
|
||||
</div>
|
||||
{showEditor && (
|
||||
<div className="overflow-y-auto md:w-7/12">
|
||||
<div>
|
||||
<InputText
|
||||
editor="monaco"
|
||||
editorLanguage="typescript"
|
||||
withLabel={false}
|
||||
value={getSelectedFileCode()}
|
||||
setValue={() => {}}
|
||||
editorSize="screen"
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="flex justify-end space-x-1">
|
||||
<ButtonSecondary disabled={!debug} onClick={onTry}>
|
||||
{t("shared.try")}
|
||||
</ButtonSecondary>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
import { LoaderFunction } from "react-router";
|
||||
import { Link, useLoaderData } from "react-router";
|
||||
import { useTranslation } from "react-i18next";
|
||||
import { EntityWithDetails, getAllEntities } from "~/utils/db/entities/entities.db.server";
|
||||
import { verifyUserHasPermission } from "~/utils/helpers/.server/PermissionsService";
|
||||
import { adminPath } from "~/utils/app/CanonicalUrlUtils";
|
||||
|
||||
type LoaderData = {
|
||||
entities: EntityWithDetails[];
|
||||
};
|
||||
export let loader: LoaderFunction = async ({ request }) => {
|
||||
await verifyUserHasPermission(request, "admin.entities.view");
|
||||
const data: LoaderData = {
|
||||
entities: await getAllEntities({ tenantId: null }),
|
||||
};
|
||||
return data;
|
||||
};
|
||||
|
||||
export default () => {
|
||||
const { t } = useTranslation();
|
||||
const data = useLoaderData<LoaderData>();
|
||||
return (
|
||||
<div className="space-y-4 overflow-y-auto p-4 sm:px-8 sm:py-7">
|
||||
{data.entities.length === 0 ? (
|
||||
<Link
|
||||
to={adminPath("entities/new", { mode: "host-aware" })}
|
||||
className="border-border focus:bg-background hover:border-border relative flex w-full flex-col justify-center space-y-2 rounded-lg border-2 border-dashed p-6 text-center focus:border-2 focus:border-gray-600 focus:outline-hidden"
|
||||
>
|
||||
<div className="text-foreground block text-sm font-medium">{t("models.entity.actions.create")}</div>
|
||||
</Link>
|
||||
) : (
|
||||
<>
|
||||
<div className="text-foreground text-lg font-bold">{t("shared.generate")}</div>
|
||||
<div className="grid grid-cols-3 gap-3">
|
||||
{data.entities.map((item) => {
|
||||
return (
|
||||
<Link
|
||||
key={item.name}
|
||||
to={`files/${item.name}`}
|
||||
reloadDocument
|
||||
className="border-border focus:bg-background hover:border-border relative flex w-full flex-col justify-center space-y-2 rounded-lg border-2 border-dashed p-3 text-center focus:border-2 focus:border-gray-600 focus:outline-hidden"
|
||||
>
|
||||
<div className="text-foreground block text-sm font-medium">{t(item.titlePlural)}</div>
|
||||
</Link>
|
||||
);
|
||||
})}
|
||||
</div>
|
||||
</>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
};
|
||||
Reference in new issue
Block a user