Reorganize repo structure and standards references
This commit is contained in:
1 parent
3cb93a81be
commit
be2054274d
5885 files changed
+2588
-195
No files matched your search
+106
@@ -0,0 +1,106 @@
|
||||
import { redirect, useLoaderData, useLocation, useNavigate } from "react-router";
|
||||
import type { LoaderFunctionArgs, MetaFunction } from "react-router";
|
||||
import { useTranslation } from "react-i18next";
|
||||
import ButtonTertiary from "~/components/ui/buttons/ButtonTertiary";
|
||||
import { ErrorPage } from "~/components/ui/errors/ErrorPage";
|
||||
import RefreshIcon from "~/components/ui/icons/RefreshIcon";
|
||||
import { absoluteAppRootPath } from "~/utils/app/CrossHostUrls";
|
||||
import { adminPath } from "~/utils/app/CanonicalUrlUtils";
|
||||
import { permissionDisplayDescription, permissionDisplayName } from "~/components/core/roles/roleDisplay";
|
||||
import { getDomainRoutingConfig } from "@toolctrl/config";
|
||||
import { getTranslations } from "~/locale/i18next.server";
|
||||
import { getPermissionName } from "~/utils/db/permissions/permissions.db.server";
|
||||
import { getUser } from "~/utils/db/users.db.server";
|
||||
import { getUserPermission } from "~/utils/helpers/.server/PermissionsService";
|
||||
import { getUserInfo } from "~/utils/session.server";
|
||||
import { safeRedirectPathFromRequest } from "~/utils/app/RedirectSafety.server";
|
||||
|
||||
type LoaderData = {
|
||||
title: string;
|
||||
heading: string;
|
||||
permission: { name: string; description: string };
|
||||
user: { admin?: unknown } | null;
|
||||
appFallbackPath: string;
|
||||
};
|
||||
export const loader = async ({ request, params }: LoaderFunctionArgs) => {
|
||||
const { t } = await getTranslations(request);
|
||||
const permission = await getPermissionName(params.permission ?? "");
|
||||
const searchParams = new URLSearchParams(new URL(request.url).search);
|
||||
const redirectTo = safeRedirectPathFromRequest(request, searchParams.get("redirect")?.toString());
|
||||
const userInfo = await getUserInfo(request);
|
||||
if (searchParams.has("redirect")) {
|
||||
if (!permission) {
|
||||
throw redirect(redirectTo);
|
||||
}
|
||||
const userInfo = await getUserInfo(request);
|
||||
const { userPermission } = await getUserPermission({ userId: userInfo.userId, permissionName: permission.name });
|
||||
if (userPermission) {
|
||||
// return redirect(redirectTo);
|
||||
}
|
||||
} else if (!permission) {
|
||||
throw redirect("/404?error=permission-not-found");
|
||||
}
|
||||
|
||||
const data: LoaderData = {
|
||||
title: `${t("shared.unauthorized")} | ${process.env.APP_NAME}`,
|
||||
heading: t("shared.unauthorized"),
|
||||
permission,
|
||||
user: await getUser(userInfo.userId),
|
||||
appFallbackPath: absoluteAppRootPath(getDomainRoutingConfig()),
|
||||
};
|
||||
return data;
|
||||
};
|
||||
|
||||
export const meta: MetaFunction<typeof loader> = ({ data }) => [{ title: data?.title }];
|
||||
|
||||
function PermissionDetails({ permission }: { permission: LoaderData["permission"] }) {
|
||||
const { t } = useTranslation();
|
||||
return (
|
||||
<div className="text-muted-foreground mx-auto w-full max-w-md text-left text-base">
|
||||
<div className="border-border bg-secondary flex justify-start space-y-2 border border-dashed p-4 dark:border-gray-700 dark:bg-gray-900">
|
||||
<div className="space-y-2">
|
||||
<div className="font-bold">{permissionDisplayDescription(permission, t) ?? permissionDisplayName(permission, t)}</div>
|
||||
<div>
|
||||
<span>{t("pages.unauthorized.permission")} →</span> <span className="font-light italic">{permission.name}</span>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
export default function Page401() {
|
||||
const { t } = useTranslation();
|
||||
const location = useLocation();
|
||||
const navigate = useNavigate();
|
||||
const data = useLoaderData<LoaderData>();
|
||||
const { permission, user } = data;
|
||||
|
||||
return (
|
||||
<ErrorPage
|
||||
status={401}
|
||||
title={data.heading}
|
||||
withGoBack={false}
|
||||
diagnosticChildren={<PermissionDetails permission={permission} />}
|
||||
actionSlot={
|
||||
<div className="mx-auto mt-4 flex max-w-md flex-wrap items-center justify-center gap-2">
|
||||
{user?.admin ? (
|
||||
<ButtonTertiary to={adminPath("dashboard", { mode: "host-aware" })}>
|
||||
<div> ←</div>
|
||||
<div>{t("pages.unauthorized.goBackToAdmin")}</div>
|
||||
</ButtonTertiary>
|
||||
) : (
|
||||
<ButtonTertiary to={data.appFallbackPath}>
|
||||
<div> ←</div>
|
||||
<div>{t("pages.unauthorized.goBackToApp")}</div>
|
||||
</ButtonTertiary>
|
||||
)}
|
||||
<ButtonTertiary type="button" onClick={() => navigate(location)}>
|
||||
<div>{t("pages.unauthorized.recheckPermission")}</div>
|
||||
<RefreshIcon className="h-4 w-4" />
|
||||
</ButtonTertiary>
|
||||
</div>
|
||||
}
|
||||
/>
|
||||
);
|
||||
}
|
||||
Reference in new issue
Block a user