refactor(react-hooks): enable stricter effect rules
Turn on react-hooks/set-state-in-effect and react-hooks/incompatible-library, then remove effect-driven local state sync patterns across affected pages. Keep behavior stable by deriving values from source state, remounting tab state by role key, and replacing useForm watch with useWatch.
This commit is contained in:
@@ -4,7 +4,7 @@ import { standardSchemaResolver } from "@hookform/resolvers/standard-schema"
|
||||
import { ArrowLeft, ImagePlus, X } from "lucide-react"
|
||||
import Link from "next/link"
|
||||
import { useRouter } from "next/navigation"
|
||||
import { useEffect, useState } from "react"
|
||||
import { useState } from "react"
|
||||
import { useForm } from "react-hook-form"
|
||||
import { z } from "zod"
|
||||
import { Badge } from "@/components/ui/badge"
|
||||
@@ -46,6 +46,8 @@ export default function NewPostPage() {
|
||||
const [imageCount, setImageCount] = useState(0)
|
||||
const [selectedQuotePostId, setSelectedQuotePostId] = useState<string | undefined>(undefined)
|
||||
const [selectedOrderId, setSelectedOrderId] = useState<string | undefined>(undefined)
|
||||
const canShowOrder = currentRole === "consumer"
|
||||
const effectivePostType = canShowOrder || postType !== "show_order" ? postType : "normal"
|
||||
|
||||
const {
|
||||
register,
|
||||
@@ -61,12 +63,6 @@ export default function NewPostPage() {
|
||||
)
|
||||
}
|
||||
|
||||
useEffect(() => {
|
||||
if (currentRole !== "consumer" && postType === "show_order") {
|
||||
setPostType("normal")
|
||||
}
|
||||
}, [currentRole, postType])
|
||||
|
||||
const availableOrders = orders.filter(
|
||||
(order) => order.status === "completed" && order.consumerId === userId,
|
||||
)
|
||||
@@ -87,8 +83,8 @@ export default function NewPostPage() {
|
||||
content: data.content,
|
||||
images: Array.from({ length: imageCount }).map(() => "/posts/p1-1.jpg"),
|
||||
tags: selectedTags,
|
||||
linkedOrderId: postType === "show_order" ? selectedOrderId : undefined,
|
||||
quotedPostId: postType === "quote" ? selectedQuotePostId : undefined,
|
||||
linkedOrderId: effectivePostType === "show_order" ? selectedOrderId : undefined,
|
||||
quotedPostId: effectivePostType === "quote" ? selectedQuotePostId : undefined,
|
||||
})
|
||||
|
||||
router.push("/community")
|
||||
@@ -113,19 +109,19 @@ export default function NewPostPage() {
|
||||
<form onSubmit={handleSubmit(onSubmit)} className="space-y-6">
|
||||
<div className="space-y-2">
|
||||
<Label>帖子类型</Label>
|
||||
<Select value={postType} onValueChange={setPostType}>
|
||||
<Select value={effectivePostType} onValueChange={setPostType}>
|
||||
<SelectTrigger>
|
||||
<SelectValue />
|
||||
</SelectTrigger>
|
||||
<SelectContent>
|
||||
<SelectItem value="normal">普通帖</SelectItem>
|
||||
{currentRole === "consumer" && <SelectItem value="show_order">秀单帖</SelectItem>}
|
||||
{canShowOrder && <SelectItem value="show_order">秀单帖</SelectItem>}
|
||||
<SelectItem value="quote">引用帖</SelectItem>
|
||||
</SelectContent>
|
||||
</Select>
|
||||
</div>
|
||||
|
||||
{postType === "show_order" && (
|
||||
{effectivePostType === "show_order" && (
|
||||
<div className="space-y-2">
|
||||
<Label>关联订单</Label>
|
||||
<Select value={selectedOrderId} onValueChange={setSelectedOrderId}>
|
||||
@@ -143,7 +139,7 @@ export default function NewPostPage() {
|
||||
</div>
|
||||
)}
|
||||
|
||||
{postType === "quote" && (
|
||||
{effectivePostType === "quote" && (
|
||||
<div className="space-y-2">
|
||||
<Label>引用帖子</Label>
|
||||
<Select value={selectedQuotePostId} onValueChange={setSelectedQuotePostId}>
|
||||
|
||||
Reference in New Issue
Block a user