69 lines
1.7 KiB
TypeScript
69 lines
1.7 KiB
TypeScript
import { generateId } from "@/lib/id"
|
|
import { mockPosts } from "@/lib/mock"
|
|
import type { Post, User, UserRole } from "@/lib/types"
|
|
import { create } from "zustand"
|
|
|
|
interface CreatePostInput {
|
|
author: User
|
|
authorRole: UserRole
|
|
title: string
|
|
content: string
|
|
images: string[]
|
|
tags: string[]
|
|
linkedOrderId?: string
|
|
quotedPostId?: string
|
|
}
|
|
|
|
interface PostState {
|
|
posts: Post[]
|
|
createPost: (input: CreatePostInput) => Post
|
|
togglePostLike: (postId: string) => void
|
|
incrementCommentCount: (postId: string) => void
|
|
}
|
|
|
|
export const usePostStore = create<PostState>((set) => ({
|
|
posts: mockPosts,
|
|
createPost: (input) => {
|
|
const post: Post = {
|
|
id: generateId("post"),
|
|
author: input.author,
|
|
authorRole: input.authorRole,
|
|
title: input.title.trim(),
|
|
content: input.content.trim(),
|
|
images: input.images,
|
|
tags: input.tags,
|
|
linkedOrderId: input.linkedOrderId,
|
|
quotedPostId: input.quotedPostId,
|
|
likeCount: 0,
|
|
commentCount: 0,
|
|
liked: false,
|
|
pinned: 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,
|
|
),
|
|
})),
|
|
}))
|