Refresh HiveGrid Cloud reference snapshot
This commit is contained in:
1 parent
5e99e14772
commit
7f88026f79
5955 files changed
+1177325
-188362
No files matched your search
+171
@@ -0,0 +1,171 @@
|
||||
import {
|
||||
type ActionFunctionArgs,
|
||||
type LoaderFunctionArgs,
|
||||
type MetaFunction,
|
||||
redirect,
|
||||
useActionData,
|
||||
useLoaderData,
|
||||
useNavigate,
|
||||
useNavigation,
|
||||
useRevalidator,
|
||||
useSubmit,
|
||||
} from "react-router";
|
||||
import { CatalogError } from "@hivegrid/foundation";
|
||||
import {
|
||||
chatChannelCreateSubmissionSchema,
|
||||
type ChatChannelCreateMode,
|
||||
type ChatChannelCreateSubmission,
|
||||
type ChatChannelCreateVM,
|
||||
} from "@hivegrid/view-models";
|
||||
|
||||
import { ChatChannelCreatePage } from "~/components/screens/chat/ChatChannelCreatePage";
|
||||
import ServerError from "~/components/ui/errors/ServerError";
|
||||
import { getTranslations } from "~/locale/i18next.server";
|
||||
import { loadChatContext } from "~/modules/chat/routes/chatAccess.server";
|
||||
import { CHAT_MODULE_KEY, chatChannelPath, chatIndexPath, chatNotFoundResponse } from "~/modules/chat/routes/chatRoutePaths";
|
||||
import { createChatChannel, loadChatChannelCreatePage } from "~/modules/chat/services/ChatChannelCreateService.server";
|
||||
import { useRootData } from "~/utils/data/useRootData";
|
||||
import { validateCSRFToken } from "~/utils/session.server";
|
||||
|
||||
type LoaderData = {
|
||||
readonly title: string;
|
||||
readonly tenantSlug: string;
|
||||
readonly requestId: string;
|
||||
readonly vm: ChatChannelCreateVM;
|
||||
};
|
||||
|
||||
type ActionData = {
|
||||
readonly errorKey: string;
|
||||
};
|
||||
|
||||
export async function loader({ request, params }: LoaderFunctionArgs): Promise<Response> {
|
||||
const [{ t }, context] = await Promise.all([getTranslations(request), loadChatContext({ request, params })]);
|
||||
const tenantSlug = params.tenant ?? "";
|
||||
const vm = await loadChatChannelCreatePage({
|
||||
context,
|
||||
requestedMode: parseRequestedMode(new URL(request.url).searchParams.get("kind")),
|
||||
});
|
||||
|
||||
if (vm.status === "ready" && vm.data.allowedModes.length === 0) {
|
||||
throw chatNotFoundResponse();
|
||||
}
|
||||
|
||||
return Response.json({
|
||||
title: `${t("chat.channelCreate.title")} | ${process.env.APP_NAME}`,
|
||||
tenantSlug,
|
||||
requestId: crypto.randomUUID(),
|
||||
vm,
|
||||
} satisfies LoaderData);
|
||||
}
|
||||
|
||||
export async function action({ request, params }: ActionFunctionArgs): Promise<Response> {
|
||||
const context = await loadChatContext({ request, params });
|
||||
const formData = await request.formData();
|
||||
await validateCSRFToken(request, { formData });
|
||||
|
||||
const parsed = chatChannelCreateSubmissionSchema.safeParse(readSubmission(formData));
|
||||
if (!parsed.success) {
|
||||
return Response.json({ errorKey: parsed.error.issues[0]?.message ?? "chat.errors.invalidInput" } satisfies ActionData, { status: 400 });
|
||||
}
|
||||
const requestId = formData.get("requestId")?.toString().trim() ?? "";
|
||||
if (!requestId || requestId.length > 191 || requestId.includes("\0")) {
|
||||
return Response.json({ errorKey: "chat.errors.invalidInput" } satisfies ActionData, { status: 400 });
|
||||
}
|
||||
|
||||
try {
|
||||
const result = await createChatChannel({
|
||||
context,
|
||||
requestId,
|
||||
submission: parsed.data,
|
||||
});
|
||||
const messageKey = parsed.data.mode === "group" ? "chat.toast.groupCreated" : "chat.toast.directOpened";
|
||||
return redirect(`${chatChannelPath(params.tenant ?? "", result.chatId)}?toast=${encodeURIComponent(messageKey)}`);
|
||||
} catch (error) {
|
||||
if (error instanceof CatalogError) {
|
||||
return Response.json({ errorKey: error.i18nKey } satisfies ActionData, { status: error.status });
|
||||
}
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
export const meta: MetaFunction = ({ data }) => [{ title: (data as LoaderData | undefined)?.title }];
|
||||
|
||||
export default function ChatChannelCreateRoute(): React.JSX.Element {
|
||||
const { tenantSlug, requestId, vm } = useLoaderData<LoaderData>();
|
||||
const actionData = useActionData<ActionData>();
|
||||
const navigate = useNavigate();
|
||||
const navigation = useNavigation();
|
||||
const revalidator = useRevalidator();
|
||||
const submit = useSubmit();
|
||||
const { csrf } = useRootData();
|
||||
|
||||
return (
|
||||
<ChatChannelCreatePage
|
||||
vm={vm}
|
||||
busy={navigation.state === "submitting"}
|
||||
actionErrorKey={actionData?.errorKey}
|
||||
actions={{
|
||||
onCancel: () => void navigate(chatIndexPath(tenantSlug)),
|
||||
onRetry: () => revalidator.revalidate(),
|
||||
onCreate: (submission) => {
|
||||
const formData = submissionToFormData(submission, requestId);
|
||||
formData.set("csrf", csrf ?? "");
|
||||
void submit(formData, { method: "post" });
|
||||
},
|
||||
}}
|
||||
/>
|
||||
);
|
||||
}
|
||||
|
||||
function parseRequestedMode(value: string | null): ChatChannelCreateMode | undefined {
|
||||
return value === "group" || value === "dm" ? value : undefined;
|
||||
}
|
||||
|
||||
function readSubmission(formData: FormData): unknown {
|
||||
const mode = formData.get("mode")?.toString() ?? "";
|
||||
if (mode === "group") {
|
||||
const description = formData.get("description")?.toString().trim() ?? "";
|
||||
return {
|
||||
mode,
|
||||
name: formData.get("name")?.toString() ?? "",
|
||||
description: description || null,
|
||||
privacy: formData.get("privacy")?.toString() ?? "",
|
||||
initialUserIds: formData.getAll("initialUserIds").map((value) => value.toString()),
|
||||
initialGroupIds: formData.getAll("initialGroupIds").map((value) => value.toString()),
|
||||
syncGroupMembership: formData.get("syncGroupMembership")?.toString() === "true",
|
||||
};
|
||||
}
|
||||
if (mode === "dm") {
|
||||
return {
|
||||
mode,
|
||||
peerUserId: formData.get("peerUserId")?.toString() ?? "",
|
||||
};
|
||||
}
|
||||
return { mode };
|
||||
}
|
||||
|
||||
function submissionToFormData(submission: ChatChannelCreateSubmission, requestId: string): FormData {
|
||||
const formData = new FormData();
|
||||
formData.set("requestId", requestId);
|
||||
formData.set("mode", submission.mode);
|
||||
if (submission.mode === "group") {
|
||||
formData.set("name", submission.name);
|
||||
formData.set("description", submission.description ?? "");
|
||||
formData.set("privacy", submission.privacy);
|
||||
for (const userId of submission.initialUserIds) formData.append("initialUserIds", userId);
|
||||
for (const groupId of submission.initialGroupIds) formData.append("initialGroupIds", groupId);
|
||||
formData.set("syncGroupMembership", String(submission.syncGroupMembership));
|
||||
} else {
|
||||
formData.set("peerUserId", submission.peerUserId);
|
||||
}
|
||||
return formData;
|
||||
}
|
||||
|
||||
export function ErrorBoundary(): React.JSX.Element {
|
||||
return <ServerError />;
|
||||
}
|
||||
|
||||
export const handle = {
|
||||
moduleKey: CHAT_MODULE_KEY,
|
||||
breadcrumbKey: "chat.channelCreate.title",
|
||||
};
|
||||
Reference in new issue
Block a user