Skip to content

Commit cd99ed6

Browse files
committed
Refactor account page and comments section for improved functionality
- Reordered async operations in AccountPage to ensure proper execution flow. - Introduced a utility function to check for comment images in CommentsSection. - Updated saveReplyEdit and saveEdit functions to handle empty content and image attachments more effectively. - Adjusted button click handlers to pass entire comment objects instead of just IDs for better data management.
1 parent 24ea979 commit cd99ed6

2 files changed

Lines changed: 33 additions & 11 deletions

File tree

app/account/page.tsx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -447,14 +447,15 @@ export default function AccountPage() {
447447
setError("");
448448
setMessage("");
449449
try {
450-
const savedHandle = await claimPublicHandle(user, normalizedHandle);
451-
await updateProfile(user, { displayName: savedHandle });
452-
await syncPublicUser(user);
453450
const syncedRisk = await syncCurrentDevice();
454451
if (syncedRisk.blocked) {
455452
await signOut(auth).catch(() => undefined);
456453
throw new Error(syncedRisk.reason || "This browser installation is blocked.");
457454
}
455+
const savedHandle = await claimPublicHandle(user, normalizedHandle);
456+
await updateProfile(user, { displayName: savedHandle });
457+
await syncPublicUser(user);
458+
await syncCurrentDevice();
458459
await refreshProfile();
459460
setHandle(savedHandle);
460461
setPendingPhotoURL("");

components/CommentsSection.tsx

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,19 @@ type ReplyThread = {
127127
};
128128

129129
type CommentSort = "recent" | "oldest" | "liked";
130+
131+
function hasCommentImages(item: {
132+
images?: CommentImageAttachment[];
133+
imageStoragePath?: string;
134+
imageUrl?: string;
135+
}) {
136+
return Boolean(
137+
(item.images && item.images.length > 0) ||
138+
item.imageStoragePath ||
139+
item.imageUrl,
140+
);
141+
}
142+
130143
type CommentReaction = "like" | "dislike";
131144

132145
type CommentsSectionProps = {
@@ -1357,9 +1370,11 @@ export default function CommentsSection({
13571370
}
13581371
};
13591372

1360-
const saveReplyEdit = async (commentId: string, replyId: string) => {
1373+
const saveReplyEdit = async (commentId: string, reply: ReplyRecord) => {
13611374
const trimmedContent = editingReplyContent.trim();
1362-
if (!trimmedContent || trimmedContent.length > MAX_COMMENT_LENGTH) return;
1375+
if (trimmedContent.length > MAX_COMMENT_LENGTH) return;
1376+
if (!trimmedContent && !hasCommentImages(reply)) return;
1377+
const replyId = reply.id;
13631378
setReplyBusyId(replyId);
13641379
setError("");
13651380
try {
@@ -1470,9 +1485,11 @@ export default function CommentsSection({
14701485
}
14711486
};
14721487

1473-
const saveEdit = async (commentId: string) => {
1488+
const saveEdit = async (comment: CommentRecord) => {
14741489
const trimmedContent = editingContent.trim();
1475-
if (!trimmedContent || trimmedContent.length > MAX_COMMENT_LENGTH) return;
1490+
if (trimmedContent.length > MAX_COMMENT_LENGTH) return;
1491+
if (!trimmedContent && !hasCommentImages(comment)) return;
1492+
const commentId = comment.id;
14761493
setBusyId(commentId);
14771494
setError("");
14781495
try {
@@ -2093,8 +2110,11 @@ export default function CommentsSection({
20932110
</button>
20942111
<button
20952112
type="button"
2096-
onClick={() => saveEdit(comment.id)}
2097-
disabled={busyId === comment.id || !editingContent.trim()}
2113+
onClick={() => saveEdit(comment)}
2114+
disabled={
2115+
busyId === comment.id ||
2116+
(!editingContent.trim() && !hasCommentImages(comment))
2117+
}
20982118
className="group inline-flex items-center gap-2 text-sm font-semibold uppercase transition-colors duration-300 hover:text-[#8a2ae3] focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-4 focus-visible:outline-[#8a2ae3] disabled:opacity-40"
20992119
>
21002120
Save <RiArrowRightLine className="text-xl" />
@@ -2556,11 +2576,12 @@ export default function CommentsSection({
25562576
<button
25572577
type="button"
25582578
onClick={() =>
2559-
saveReplyEdit(comment.id, reply.id)
2579+
saveReplyEdit(comment.id, reply)
25602580
}
25612581
disabled={
25622582
replyBusyId === reply.id ||
2563-
!editingReplyContent.trim()
2583+
(!editingReplyContent.trim() &&
2584+
!hasCommentImages(reply))
25642585
}
25652586
className="text-[#8a2ae3] hover:text-white disabled:opacity-35"
25662587
>

0 commit comments

Comments
 (0)