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:
zetaloop
2026-02-22 10:03:00 +08:00
parent c9dbf5037e
commit 519fb92c34
9 changed files with 36 additions and 59 deletions
+9 -13
View File
@@ -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}>
+4 -8
View File
@@ -377,19 +377,15 @@ function SearchPageContent() {
const game = searchParams.get("game")
return game ? [game] : []
})
const [priceRange, setPriceRange] = useState<{ min: string; max: string }>({ min: "", max: "" })
const [priceRange, setPriceRange] = useState<{ min: string; max: string }>({
min: "",
max: "",
})
const [onlyOnline, setOnlyOnline] = useState(false)
const [minRating, setMinRating] = useState("0")
const [sortBy, setSortBy] = useState("composite")
const [visibleCount, setVisibleCount] = useState(12)
useEffect(() => {
const q = searchParams.get("q")
if (q !== null && q !== searchQuery) {
setSearchQuery(q)
}
}, [searchParams, searchQuery])
useEffect(() => {
const timer = setTimeout(() => {
const params = new URLSearchParams(searchParams.toString())