260624_03_ENH: label 일괄 관리 및 문자 발송 팝업에 사용가능 변수 추가

This commit is contained in:
2026-06-24 10:39:47 +09:00
parent 438e6258dc
commit c4f8e630ea
9 changed files with 224 additions and 111 deletions

View File

@@ -1,6 +1,6 @@
"use client";
import {useEffect, useMemo, useState} from "react";
import {useEffect, useMemo, useRef, useState} from "react";
import {Edit, RotateCcw, Save, Send} from "lucide-react";
import {Button} from "@/components/ui/button";
import {Input} from "@/components/ui/input";
@@ -22,9 +22,12 @@ import {UserCompact} from "@/types/gwcf";
import {getDetailValue} from "@/lib/gwcf/view";
import {useAuth} from "@/hooks/useAuth";
import {buildUserMsgParam} from "@/lib/gwcf/notification";
import {getSmsParamLabel} from "@/lib/gwcf/field-labels";
type TemplateDraft = Pick<Template, "tmplatIdx" | "tmplatNm" | "tmplatTitle" | "tmplatBody" | "sendTel">;
const EMPTY_DENY_PARAM_KEYS: string[] = [];
const toText = (value: string | null | undefined) => value ?? "";
const toDraft = (template: Template, emptyBody = false): TemplateDraft => ({
@@ -43,6 +46,7 @@ export function SmsSendDialog({
newMessageTemplateIdx,
selectedUsers,
addParams = {},
denyParamKeys = EMPTY_DENY_PARAM_KEYS,
onBeforeDefaultTemplateSend,
isBeforeSendPending = false,
onSent,
@@ -54,6 +58,7 @@ export function SmsSendDialog({
newMessageTemplateIdx?: number;
selectedUsers: UserCompact[];
addParams?: Record<string, string>;
denyParamKeys?: string[];
onBeforeDefaultTemplateSend?: () => Promise<void>;
isBeforeSendPending?: boolean;
onSent?: () => void;
@@ -63,6 +68,7 @@ export function SmsSendDialog({
const [isNewMessage, setIsNewMessage] = useState(false);
const [isEditing, setIsEditing] = useState(false);
const [draft, setDraft] = useState<TemplateDraft | null>(null);
const bodyTextareaRef = useRef<HTMLTextAreaElement | null>(null);
const templateQuery = useNotificationTemplateQuery(activeTemplateIdx, open);
const saveTemplateMutation = useSaveNotificationTemplateMutation();
@@ -71,19 +77,27 @@ export function SmsSendDialog({
const isBusy = templateQuery.isLoading || saveTemplateMutation.isPending || sendSmsMutation.isPending || isBeforeSendPending;
const canEdit = isNewMessage || isEditing;
const denyParamKeySet = useMemo(() => new Set(denyParamKeys), [denyParamKeys]);
const destinations = useMemo(() => {
return selectedUsers
.map((user) => ({
sid: user.userId,
destTel: getDetailValue(user.userDetail, "userTel"),
destNm: user.userName || user.userId,
msgParam: {
.map((user) => {
const msgParam = Object.fromEntries(
Object.entries({
...buildUserMsgParam(user),
...addParams,
},
}))
}).filter(([key]) => !denyParamKeySet.has(key))
);
return {
sid: user.userId,
destTel: getDetailValue(user.userDetail, "userTel"),
destNm: user.userName || user.userId,
msgParam,
};
})
.filter((item) => item.destTel);
}, [selectedUsers, addParams]);
}, [selectedUsers, addParams, denyParamKeySet]);
const msgParamKeys = useMemo(() => {
return Array.from(
@@ -111,11 +125,24 @@ export function SmsSendDialog({
setDraft((prev) => prev ? {...prev, [key]: value} : prev);
};
useEffect(() => {
if (!open) return;
const insertMsgParam = (key: string) => {
if (!draft || !canEdit || isBusy) return;
console.log("d\estinations msgParam keys", msgParamKeys, destinations);
}, [msgParamKeys, open]);
const token = `#{${key}}`;
const textarea = bodyTextareaRef.current;
const body = draft.tmplatBody;
const selectionStart = textarea?.selectionStart ?? body.length;
const selectionEnd = textarea?.selectionEnd ?? selectionStart;
const nextBody = `${body.slice(0, selectionStart)}${token}${body.slice(selectionEnd)}`;
const nextCursor = selectionStart + token.length;
updateDraft("tmplatBody", nextBody);
requestAnimationFrame(() => {
textarea?.focus();
textarea?.setSelectionRange(nextCursor, nextCursor);
});
};
const saveTemplate = async () => {
if (!draft) return null;
@@ -134,6 +161,13 @@ export function SmsSendDialog({
return saved;
};
const cancelEdit = () => {
if (templateQuery.data) {
setDraft(toDraft(templateQuery.data, isNewMessage));
}
setIsEditing(false);
};
const switchToNewMessage = () => {
if (!newMessageTemplateIdx) return;
@@ -243,12 +277,32 @@ export function SmsSendDialog({
<label className="space-y-1 text-sm">
<span className="font-medium">내용</span>
<textarea
ref={bodyTextareaRef}
value={draft.tmplatBody}
disabled={!canEdit || isBusy}
onChange={(event) => updateDraft("tmplatBody", event.target.value)}
className="min-h-56 w-full rounded-md border border-input bg-background p-3 text-sm disabled:cursor-not-allowed disabled:opacity-70"
/>
</label>
{msgParamKeys.length > 0 && (
<div className="space-y-1 text-sm">
<div className="font-medium text-muted-foreground">사용 가능 변수값</div>
<div className="flex flex-wrap gap-1.5">
{msgParamKeys.map((key) => (
<Button
key={key}
type="button"
className="h-auto flex-col items-start gap-0 rounded bg-muted px-2 py-1 text-center text-xs text-foreground hover:bg-muted/80 disabled:cursor-not-allowed disabled:opacity-50"
disabled={!canEdit || isBusy}
onClick={() => insertMsgParam(key)}
>
<span className="text-center">{getSmsParamLabel(key)}</span>
<code className="font-mono text-muted-foreground">{`#{${key}}`}</code>
</Button>
))}
</div>
</div>
)}
</div>
)}
</div>
@@ -261,10 +315,16 @@ export function SmsSendDialog({
</Button>
)}
{!isNewMessage && draft && isEditing && (
<Button type="button" variant="outline" onClick={saveTemplate} disabled={isBusy}>
<Save className="h-4 w-4" />
저장
</Button>
<>
<Button type="button" variant="outline" onClick={saveTemplate} disabled={isBusy}>
<Save className="h-4 w-4" />
저장
</Button>
<Button type="button" variant="outline" onClick={cancelEdit} disabled={isBusy}>
<RotateCcw className="h-4 w-4" />
취소
</Button>
</>
)}
<Button type="button" disabled={!draft || isBusy || isEditing || selectedUsers.length === 0} onClick={send}>
<Send className="h-4 w-4" />