261001_01_ENH: 문자 템플릿 JPG 첨부 및 MMS 이미지 미리보기 추가
This commit is contained in:
@@ -74,6 +74,9 @@ export const qk = {
|
||||
templateList: (chnnlTy: ChannelType, itrntPrjctCd: string) =>
|
||||
["message-template", "list", chnnlTy, itrntPrjctCd] as const,
|
||||
template: (tmplatIdx: number) => ["message-template", "detail", tmplatIdx] as const,
|
||||
templateAttachments: (tmplatIdx: number) => ["message-template", "attachment", tmplatIdx] as const,
|
||||
templateAttachment: (tmplatIdx: number, fileName: string) =>
|
||||
["message-template", "attachment", tmplatIdx, fileName] as const,
|
||||
|
||||
bulkSendList: (sendTy: number, reqstSvc: string, prjctCd: string) =>
|
||||
["message-report", "bulk-list", sendTy, reqstSvc, prjctCd] as const,
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
"use client";
|
||||
|
||||
import { keepPreviousData, useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
|
||||
import { keepPreviousData, useMutation, useQueries, useQuery, useQueryClient } from "@tanstack/react-query";
|
||||
import { nextApi } from "@/lib/http-client";
|
||||
import { qk } from "@/query/key/keys";
|
||||
import type {
|
||||
@@ -19,7 +19,7 @@ import type {
|
||||
SendResultInfo,
|
||||
SendSingleResponse,
|
||||
} from "@/types/message";
|
||||
import type { TemplateSummaryWithPurpose, TemplateWithPurpose } from "@/types/message";
|
||||
import type { TemplateAttachmentImage, TemplateSummaryWithPurpose, TemplateWithPurpose } from "@/types/message";
|
||||
|
||||
export function useTemplateListQuery(chnnlTy: ChannelType, itrntPrjctCd: string, enabled: boolean) {
|
||||
return useQuery({
|
||||
@@ -79,6 +79,8 @@ export function useSaveTemplateMutation(chnnlTy: ChannelType, itrntPrjctCd: stri
|
||||
onSuccess: (res) => {
|
||||
if (res.tmplatIdx) {
|
||||
qc.setQueryData(qk.template(res.tmplatIdx), res);
|
||||
// 같은 파일명으로 교체될 수 있으므로 첨부 이미지 미리보기 캐시를 비운다.
|
||||
qc.removeQueries({ queryKey: qk.templateAttachments(res.tmplatIdx) });
|
||||
}
|
||||
qc.invalidateQueries({ queryKey: qk.templateList(chnnlTy, itrntPrjctCd) });
|
||||
},
|
||||
@@ -97,6 +99,38 @@ export function useDownloadTemplateAttachmentMutation(tmplatIdx: number) {
|
||||
});
|
||||
}
|
||||
|
||||
const readBlobAsDataUrl = (blob: Blob) =>
|
||||
new Promise<string>((resolve, reject) => {
|
||||
const reader = new FileReader();
|
||||
reader.onload = () => resolve(reader.result as string);
|
||||
reader.onerror = () => reject(reader.error);
|
||||
reader.readAsDataURL(blob);
|
||||
});
|
||||
|
||||
/**
|
||||
* 문자 템플릿 첨부 JPG 미리보기 (파일별 캐시, 불러온 이미지만 fileNames 순서대로 반환)
|
||||
* data URL로 캐시하므로 Object URL 해제가 필요 없다.
|
||||
*/
|
||||
export function useTemplateAttachmentImagesQuery(tmplatIdx: number, fileNames: string[], enabled: boolean) {
|
||||
return useQueries({
|
||||
queries: fileNames.map((fileName) => ({
|
||||
queryKey: qk.templateAttachment(tmplatIdx, fileName),
|
||||
queryFn: async () => {
|
||||
const buffer = await nextApi.get<ArrayBuffer>(
|
||||
`/message-template/${tmplatIdx}/attachment?fileName=${encodeURIComponent(fileName)}`,
|
||||
{ expectBinary: true },
|
||||
);
|
||||
return readBlobAsDataUrl(new Blob([buffer], { type: "image/jpeg" }));
|
||||
},
|
||||
enabled: enabled && tmplatIdx > 0,
|
||||
staleTime: 60_000,
|
||||
retry: 1,
|
||||
})),
|
||||
combine: (results): TemplateAttachmentImage[] =>
|
||||
results.flatMap((result, i) => (result.data ? [{ fileName: fileNames[i], src: result.data }] : [])),
|
||||
});
|
||||
}
|
||||
|
||||
export function useUpdateMainTemplateMutation(prjctIdx: number, chnnlTy: ChannelType) {
|
||||
const qc = useQueryClient();
|
||||
const key = qk.mainTemplate(prjctIdx, chnnlTy);
|
||||
|
||||
Reference in New Issue
Block a user