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
+214
@@ -0,0 +1,214 @@
|
||||
import {
|
||||
type ActionFunctionArgs,
|
||||
type LoaderFunctionArgs,
|
||||
type MetaFunction,
|
||||
redirect,
|
||||
useActionData,
|
||||
useLoaderData,
|
||||
useNavigate,
|
||||
useNavigation,
|
||||
useRevalidator,
|
||||
useSubmit,
|
||||
} from "react-router";
|
||||
import { CatalogError } from "@hivegrid/foundation";
|
||||
import type { ChatChannelSettingsMemberRole, ChatChannelSettingsVM } from "@hivegrid/view-models";
|
||||
|
||||
import { ChatChannelSettingsPage } from "~/components/screens/chat/ChatChannelSettingsPage";
|
||||
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,
|
||||
chatChannelSettingsPath,
|
||||
chatIndexPath,
|
||||
} from "~/modules/chat/routes/chatRoutePaths";
|
||||
import {
|
||||
addChatChannelMember,
|
||||
archiveChatChannel,
|
||||
changeChatChannelMemberRole,
|
||||
changeChatChannelPrivacy,
|
||||
leaveChatChannel,
|
||||
loadChatChannelSettingsPage,
|
||||
removeChatChannelMember,
|
||||
} from "~/modules/chat/services/ChatChannelSettingsService.server";
|
||||
import { useRootData } from "~/utils/data/useRootData";
|
||||
import { validateCSRFToken } from "~/utils/session.server";
|
||||
|
||||
type LoaderData = Readonly<{
|
||||
title: string;
|
||||
tenantSlug: string;
|
||||
chatId: string;
|
||||
requestId: string;
|
||||
vm: ChatChannelSettingsVM;
|
||||
}>;
|
||||
|
||||
type ActionData = Readonly<{ errorKey: string }>;
|
||||
type SettingsIntent = "add_member" | "set_role" | "remove_member" | "set_privacy" | "leave" | "archive";
|
||||
|
||||
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 chatId = params.id ?? "";
|
||||
const vm = await loadChatChannelSettingsPage({
|
||||
context,
|
||||
tenantSlug,
|
||||
chatId,
|
||||
labels: {
|
||||
directMessage: t("chat.channel.fallback.directMessage"),
|
||||
unnamedGroup: t("chat.channel.fallback.group"),
|
||||
unnamedProject: t("chat.channel.fallback.project"),
|
||||
unknownProjectStage: t("chat.channel.fallback.projectStage"),
|
||||
},
|
||||
});
|
||||
|
||||
return Response.json({
|
||||
title: `${t("chat.channelSettings.title")} | ${process.env.APP_NAME}`,
|
||||
tenantSlug,
|
||||
chatId,
|
||||
requestId: crypto.randomUUID(),
|
||||
vm,
|
||||
} satisfies LoaderData);
|
||||
}
|
||||
|
||||
export async function action({ request, params }: ActionFunctionArgs): Promise<Response> {
|
||||
const context = await loadChatContext({ request, params });
|
||||
const tenantSlug = params.tenant ?? "";
|
||||
const chatId = params.id ?? "";
|
||||
const formData = await request.formData();
|
||||
await validateCSRFToken(request, { formData });
|
||||
|
||||
const intent = parseIntent(formData.get("intent"));
|
||||
const requestId = parseIdentifier(formData.get("requestId"));
|
||||
if (!intent || !requestId) return invalidInput();
|
||||
const base = { context, chatId, requestId };
|
||||
|
||||
try {
|
||||
if (intent === "add_member") {
|
||||
const memberUserId = parseIdentifier(formData.get("memberUserId"));
|
||||
if (!memberUserId) return invalidInput();
|
||||
await addChatChannelMember({ ...base, memberUserId });
|
||||
} else if (intent === "set_role") {
|
||||
const memberUserId = parseIdentifier(formData.get("memberUserId"));
|
||||
const roleInChat = parseRole(formData.get("roleInChat"));
|
||||
if (!memberUserId || !roleInChat) return invalidInput();
|
||||
await changeChatChannelMemberRole({ ...base, memberUserId, roleInChat });
|
||||
} else if (intent === "remove_member") {
|
||||
const memberUserId = parseIdentifier(formData.get("memberUserId"));
|
||||
if (!memberUserId) return invalidInput();
|
||||
await removeChatChannelMember({ ...base, memberUserId });
|
||||
} else if (intent === "set_privacy") {
|
||||
const privacy = parsePrivacy(formData.get("privacy"));
|
||||
if (!privacy) return invalidInput();
|
||||
await changeChatChannelPrivacy({ ...base, privacy });
|
||||
} else if (intent === "leave") {
|
||||
await leaveChatChannel(base);
|
||||
return redirect(withToast(chatIndexPath(tenantSlug), "chat.toast.channelLeft"));
|
||||
} else {
|
||||
await archiveChatChannel(base);
|
||||
return redirect(withToast(chatIndexPath(tenantSlug), "chat.toast.channelArchived"));
|
||||
}
|
||||
|
||||
return redirect(withToast(chatChannelSettingsPath(tenantSlug, chatId), settingsSuccessKey(intent, formData)));
|
||||
} 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 ChatChannelSettingsRoute(): React.JSX.Element {
|
||||
const { tenantSlug, chatId, requestId, vm } = useLoaderData<LoaderData>();
|
||||
const actionData = useActionData<ActionData>();
|
||||
const navigate = useNavigate();
|
||||
const navigation = useNavigation();
|
||||
const revalidator = useRevalidator();
|
||||
const submit = useSubmit();
|
||||
const { csrf } = useRootData();
|
||||
const busy = navigation.state === "submitting";
|
||||
|
||||
function submitIntent(intent: SettingsIntent, values: Readonly<Record<string, string>> = {}): void {
|
||||
const formData = new FormData();
|
||||
formData.set("csrf", csrf ?? "");
|
||||
formData.set("intent", intent);
|
||||
formData.set("requestId", mutationRequestId(requestId, intent));
|
||||
for (const [key, value] of Object.entries(values)) formData.set(key, value);
|
||||
void submit(formData, { method: "post" });
|
||||
}
|
||||
|
||||
return (
|
||||
<ChatChannelSettingsPage
|
||||
vm={vm}
|
||||
busy={busy}
|
||||
actionErrorKey={actionData?.errorKey}
|
||||
actions={{
|
||||
onBack: () => void navigate(chatChannelPath(tenantSlug, chatId)),
|
||||
onRetry: () => revalidator.revalidate(),
|
||||
onAddMember: (memberUserId) => submitIntent("add_member", { memberUserId }),
|
||||
onChangeRole: (memberUserId, roleInChat) => submitIntent("set_role", { memberUserId, roleInChat }),
|
||||
onRemoveMember: (memberUserId) => submitIntent("remove_member", { memberUserId }),
|
||||
onPrivacyChange: (privacy) => submitIntent("set_privacy", { privacy }),
|
||||
onLeave: () => submitIntent("leave"),
|
||||
onArchive: () => submitIntent("archive"),
|
||||
}}
|
||||
/>
|
||||
);
|
||||
}
|
||||
|
||||
function mutationRequestId(base: string, intent: SettingsIntent): string {
|
||||
return `${base}:${intent}`;
|
||||
}
|
||||
|
||||
function parseIntent(value: FormDataEntryValue | null): SettingsIntent | null {
|
||||
const intent = value?.toString() ?? "";
|
||||
return ["add_member", "set_role", "remove_member", "set_privacy", "leave", "archive"].includes(intent)
|
||||
? (intent as SettingsIntent)
|
||||
: null;
|
||||
}
|
||||
|
||||
function parseIdentifier(value: FormDataEntryValue | null): string | null {
|
||||
const normalized = value?.toString().trim() ?? "";
|
||||
return normalized && normalized.length <= 191 && !normalized.includes("\0") ? normalized : null;
|
||||
}
|
||||
|
||||
function parseRole(value: FormDataEntryValue | null): ChatChannelSettingsMemberRole | null {
|
||||
const role = value?.toString() ?? "";
|
||||
return role === "admin" || role === "member" ? role : null;
|
||||
}
|
||||
|
||||
function parsePrivacy(value: FormDataEntryValue | null): "public" | "private" | null {
|
||||
const privacy = value?.toString() ?? "";
|
||||
return privacy === "public" || privacy === "private" ? privacy : null;
|
||||
}
|
||||
|
||||
function invalidInput(): Response {
|
||||
return Response.json({ errorKey: "chat.errors.invalidInput" } satisfies ActionData, { status: 400 });
|
||||
}
|
||||
|
||||
function settingsSuccessKey(intent: SettingsIntent, formData: FormData): string {
|
||||
if (intent === "add_member") return "chat.toast.memberAdded";
|
||||
if (intent === "set_role") return "chat.toast.memberRoleChanged";
|
||||
if (intent === "remove_member") return "chat.toast.memberRemoved";
|
||||
if (intent === "leave") return "chat.toast.channelLeft";
|
||||
if (intent === "archive") return "chat.toast.channelArchived";
|
||||
return formData.get("privacy") === "public"
|
||||
? "chat.toast.channelMadePublic"
|
||||
: "chat.toast.channelMadePrivate";
|
||||
}
|
||||
|
||||
function withToast(path: string, messageKey: string): string {
|
||||
const separator = path.includes("?") ? "&" : "?";
|
||||
return `${path}${separator}toast=${encodeURIComponent(messageKey)}`;
|
||||
}
|
||||
|
||||
export function ErrorBoundary(): React.JSX.Element {
|
||||
return <ServerError />;
|
||||
}
|
||||
|
||||
export const handle = {
|
||||
moduleKey: CHAT_MODULE_KEY,
|
||||
breadcrumbKey: "chat.channelSettings.title",
|
||||
};
|
||||
Reference in new issue
Block a user