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
64 lines
1.6 KiB
TypeScript
64 lines
1.6 KiB
TypeScript
import { generateId } from "@/lib/id"
|
|
import type { Post, User } from "@/lib/types"
|
|
import { create } from "zustand"
|
|
|
|
interface CreatePostInput {
|
|
author: User
|
|
title: string
|
|
content: string
|
|
images: string[]
|
|
tags: string[]
|
|
linkedOrderId?: string
|
|
}
|
|
|
|
interface PostState {
|
|
posts: Post[]
|
|
createPost: (input: CreatePostInput) => Post
|
|
togglePostLike: (postId: string) => void
|
|
incrementCommentCount: (postId: string) => void
|
|
}
|
|
|
|
export const usePostStore = create<PostState>((set) => ({
|
|
posts: [],
|
|
createPost: (input) => {
|
|
const post: Post = {
|
|
id: generateId("post"),
|
|
author: input.author,
|
|
title: input.title.trim(),
|
|
content: input.content.trim(),
|
|
images: input.images,
|
|
tags: input.tags,
|
|
linkedOrderId: input.linkedOrderId ? Number(input.linkedOrderId) : undefined,
|
|
pinned: false,
|
|
likeCount: 0,
|
|
commentCount: 0,
|
|
liked: false,
|
|
createdAt: new Date().toISOString(),
|
|
}
|
|
|
|
set((state) => ({
|
|
posts: [post, ...state.posts],
|
|
}))
|
|
|
|
return post
|
|
},
|
|
togglePostLike: (postId) =>
|
|
set((state) => ({
|
|
posts: state.posts.map((post) => {
|
|
if (post.id !== postId) return post
|
|
const liked = !post.liked
|
|
return {
|
|
...post,
|
|
liked,
|
|
likeCount: liked ? post.likeCount + 1 : Math.max(0, post.likeCount - 1),
|
|
}
|
|
}),
|
|
})),
|
|
incrementCommentCount: (postId) =>
|
|
set((state) => ({
|
|
posts: state.posts.map((post) =>
|
|
post.id === postId ? { ...post, commentCount: post.commentCount + 1 } : post,
|
|
),
|
|
})),
|
|
}))
|