12284290cc
- orders: remove name fields from creation, keep dispatchMode logic - chat: remove readonly/senderName/senderAvatar references - reviews: remove fromUserAvatar/toUserId, simplify participant check - disputes: remove initiatorId/initiatorName from creation - posts: remove authorRole/quotedPostId, keep linkedOrderId as number - comments: remove postId from creation - wallet: use string amounts
46 lines
1.2 KiB
TypeScript
46 lines
1.2 KiB
TypeScript
import { generateId } from "@/lib/id"
|
|
import type { Comment, User } from "@/lib/types"
|
|
import { create } from "zustand"
|
|
|
|
interface CommentState {
|
|
comments: Comment[]
|
|
addComment: (postId: string, author: User, content: string) => Comment | null
|
|
toggleCommentLike: (commentId: string) => void
|
|
}
|
|
|
|
export const useCommentStore = create<CommentState>((set) => ({
|
|
comments: [],
|
|
addComment: (postId, author, content) => {
|
|
const normalizedContent = content.trim()
|
|
if (!normalizedContent) return null
|
|
|
|
const comment: Comment = {
|
|
id: generateId("comment"),
|
|
author,
|
|
content: normalizedContent,
|
|
likeCount: 0,
|
|
liked: false,
|
|
createdAt: new Date().toISOString(),
|
|
}
|
|
|
|
set((state) => ({
|
|
comments: [...state.comments, comment],
|
|
}))
|
|
|
|
return comment
|
|
},
|
|
toggleCommentLike: (commentId) => {
|
|
set((state) => ({
|
|
comments: state.comments.map((comment) => {
|
|
if (comment.id !== commentId) return comment
|
|
const liked = !comment.liked
|
|
return {
|
|
...comment,
|
|
liked,
|
|
likeCount: liked ? comment.likeCount + 1 : Math.max(0, comment.likeCount - 1),
|
|
}
|
|
}),
|
|
}))
|
|
},
|
|
}))
|