Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 | 1x 1x 1x 1x 1x 1x 1x 1x 4x 4x 4x 4x 4x 4x 4x 4x 4x 1x 4x 4x 4x 4x 1x 5x 5x 5x 5x 5x 5x 5x 5x 1x 1x 4x 4x 5x 2x 2x 2x 1x 1x 1x 1x 1x 1x 1x 2x 5x 5x 1x 1x 1x 1x 1x 1x 1x 1x 6x 1x 1x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 10x 10x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 10x 10x 6x 6x 3x 6x 3x 3x 3x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 10x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 5x 4x 4x 4x 4x 5x 1x 1x 1x 1x 3x 5x 5x 5x 3x 3x 3x 3x 3x 3x 5x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 5x 1x 1x 1x 5x 4x 4x 4x 5x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x | import { useCallback } from 'react';
import { useQueryClient, type InfiniteData } from '@tanstack/react-query';
import { toast } from 'sonner';
// Constants
import { ERROR_MESSAGES } from '@/constants/messages';
// Services
import { lunchMenusService } from '@/services/lunchMenus';
// Types
import {
MenuSocketUpdate,
CommentChangeAction,
CommentChangePayload,
} from '@/types/meal';
import {
Sentiment,
CommentListResponse,
CommentListItemResponse,
} from '@/types';
// Utils
import {
getAuthToken,
applyDailyCommentCounts,
toCompactDateString,
getAffectedMaps,
} from '@/utils';
type CommentsInfinite = InfiniteData<CommentListResponse | null>;
/** Apply `fn` to each page's data array, skipping null/error pages. */
const mapCommentPages = (
data: CommentsInfinite | undefined,
fn: (comments: CommentListItemResponse[]) => CommentListItemResponse[],
): CommentsInfinite | undefined =>
data
? {
...data,
pages: data.pages.map((page) =>
page ? { ...page, data: fn(page.data) } : page,
),
}
: data;
const commentMatchesFilter = (
comment: CommentListItemResponse,
filter: { sentiment?: Sentiment; isRead?: boolean },
): boolean =>
(filter.sentiment === undefined || comment.sentiment === filter.sentiment) &&
(filter.isRead === undefined || comment.isRead === filter.isRead);
const applyCommentChangeToCache = (
data: CommentsInfinite | undefined,
action: CommentChangeAction,
comment: CommentChangePayload['comment'],
filter: { sentiment?: Sentiment; isRead?: boolean },
): CommentsInfinite | undefined => {
if (!data) return data;
const id = comment.id;
if (action === CommentChangeAction.DELETED) {
return mapCommentPages(data, (cs) => cs.filter((c) => c.id !== id));
}
const item = comment as CommentListItemResponse;
const belongs = commentMatchesFilter(item, filter);
if (action === CommentChangeAction.CREATED) {
if (!belongs) return data;
// Skip if already on any page (duplicate socket delivery).
if (data.pages.some((page) => page?.data.some((c) => c.id === id)))
return data;
// Prepend to the first page; later pages surface on scroll/refetch.
return {
...data,
pages: data.pages.map((page, idx) =>
idx === 0 && page ? { ...page, data: [item, ...page.data] } : page,
),
};
}
// updated / read / classified: patch across all pages; remove if no longer matches filter.
const shown = data.pages.some((page) => page?.data.some((c) => c.id === id));
if (!shown) return data;
if (!belongs) {
return mapCommentPages(data, (cs) => cs.filter((c) => c.id !== id));
}
return mapCommentPages(data, (cs) =>
cs.map((c) => (c.id === id ? { ...c, ...item } : c)),
);
};
interface CommentsDashboardLogicProps {
fromDate: string;
toDate: string;
monthFromDate: string;
monthToDate: string;
filterKey: string;
filterParams: { sentiment?: Sentiment; isRead?: boolean };
firebaseUser: any;
setDayMap: (fn: (prev: any) => any) => void;
setCalendarMap: (fn: (prev: any) => any) => void;
commentToMenuId: Record<string, string>;
allComments: CommentListItemResponse[];
setLoadingIds: (fn: (prev: Set<string>) => Set<string>) => void;
}
const QUERY_KEYS = {
comments: (fromDate: string, toDate: string, filterKey: string) =>
['lunchMenus', 'comments', fromDate, toDate, filterKey] as const,
};
export const useCommentsOperations = ({
fromDate,
toDate,
monthFromDate,
monthToDate,
filterKey,
filterParams,
firebaseUser,
setDayMap,
setCalendarMap,
commentToMenuId,
allComments,
setLoadingIds,
}: CommentsDashboardLogicProps) => {
const queryClient = useQueryClient();
const handleRealtimeUpdate = useCallback(
(update: MenuSocketUpdate) => {
const dc = update.dayCounts;
if (dc) {
const compact = toCompactDateString(dc.date);
if (compact) {
const { updateRange, updateMonth } = getAffectedMaps(
compact,
fromDate,
toDate,
monthFromDate,
monthToDate,
);
if (updateRange)
setDayMap((prev) => applyDailyCommentCounts(prev, dc));
if (updateMonth)
setCalendarMap((prev) => applyDailyCommentCounts(prev, dc));
}
}
// Handle commentChange (dashboard only, for admins viewing all comments)
const change = update.commentChange;
if (change) {
// For CREATED events, skip if the comment's menu date is outside the
// currently applied range — it would vanish on reload anyway.
if (
change.action === CommentChangeAction.CREATED &&
'date' in change.comment
) {
const commentDate = toCompactDateString(change.comment.date);
if (commentDate < fromDate || commentDate > toDate) return;
}
queryClient.setQueryData<CommentsInfinite>(
QUERY_KEYS.comments(fromDate, toDate, filterKey),
(prev) =>
applyCommentChangeToCache(
prev,
change.action,
change.comment,
filterParams,
),
);
}
},
[
fromDate,
toDate,
monthFromDate,
monthToDate,
filterKey,
queryClient,
filterParams,
setDayMap,
setCalendarMap,
],
);
const handleToggleSelect = useCallback(
async (commentId: string) => {
if (!firebaseUser) return;
setLoadingIds((prev) => {
const next = new Set(prev);
if (next.has(commentId)) return prev;
next.add(commentId);
return next;
});
try {
const lunchMenuId = commentToMenuId[commentId];
if (!lunchMenuId) {
setLoadingIds((prev) => {
const next = new Set(prev);
next.delete(commentId);
return next;
});
return;
}
const currentIsRead =
allComments.find((c) => c.id === commentId)?.isRead ?? false;
const newIsRead = !currentIsRead;
const token = await getAuthToken();
const { data, error } = await lunchMenusService.setReadStatus(
lunchMenuId,
commentId,
newIsRead,
token,
);
if (error?.message) {
toast.error(error.message);
return;
}
if (data) {
// Optimistic: flip the checkbox in the active tab across all loaded pages.
queryClient.setQueryData<CommentsInfinite>(
QUERY_KEYS.comments(fromDate, toDate, filterKey),
(prev) =>
mapCommentPages(prev, (comments) =>
comments.map((c) =>
c.id === commentId ? { ...c, isRead: newIsRead } : c,
),
),
);
}
} catch (err) {
toast.error(
err instanceof Error ? err.message : ERROR_MESSAGES.DEFAULT,
);
} finally {
setLoadingIds((prev) => {
const next = new Set(prev);
next.delete(commentId);
return next;
});
}
},
[
firebaseUser,
commentToMenuId,
allComments,
queryClient,
fromDate,
toDate,
filterKey,
setLoadingIds,
],
);
return {
handleRealtimeUpdate,
handleToggleSelect,
};
};
|