mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-12-04 06:54:28 +00:00
fix(webui): persist project sorting and prevent full page flash (#276)
This commit is contained in:
@@ -28,11 +28,12 @@ import { Input } from '@/ui/components/ui/input'
|
|||||||
import { Switch } from '@/ui/components/ui/switch'
|
import { Switch } from '@/ui/components/ui/switch'
|
||||||
import { formatDateAgo } from '@/ui/lib/time'
|
import { formatDateAgo } from '@/ui/lib/time'
|
||||||
import { cn } from '@/ui/lib/utils'
|
import { cn } from '@/ui/lib/utils'
|
||||||
import { useMutation, useQuery, useQueryClient, useSuspenseQuery } from '@tanstack/react-query'
|
import { useUserMetadataStore } from '@/ui/stores/user-metadata'
|
||||||
|
import { keepPreviousData, useMutation, useQuery, useQueryClient } from '@tanstack/react-query'
|
||||||
import { useNavigate } from '@tanstack/react-router'
|
import { useNavigate } from '@tanstack/react-router'
|
||||||
import type { InferRequestType, InferResponseType } from 'hono/client'
|
import type { InferRequestType, InferResponseType } from 'hono/client'
|
||||||
import { MoreHorizontal, PlusIcon } from 'lucide-react'
|
import { MoreHorizontal, PlusIcon } from 'lucide-react'
|
||||||
import { useState } from 'react'
|
import { useEffect, useState } from 'react'
|
||||||
import { toast } from 'sonner'
|
import { toast } from 'sonner'
|
||||||
|
|
||||||
function TeamPage() {
|
function TeamPage() {
|
||||||
@@ -57,12 +58,29 @@ function TeamPage() {
|
|||||||
// State for Members Dialog
|
// State for Members Dialog
|
||||||
const [isMembersDialogOpen, setIsMembersDialogOpen] = useState(false)
|
const [isMembersDialogOpen, setIsMembersDialogOpen] = useState(false)
|
||||||
|
|
||||||
// Sort State
|
// Sort State & Preference
|
||||||
const [sortBy, setSortBy] = useState<string>('name')
|
const { metadata, setMetadata: setUserMetadata } = useUserMetadataStore()
|
||||||
const [sortDirection, setSortDirection] = useState<'asc' | 'desc'>('asc')
|
const sortKey = `team:${teamId}:projects:sort`
|
||||||
|
const savedSort = metadata[sortKey] as
|
||||||
|
| { sortBy?: string; sortDirection?: 'asc' | 'desc' }
|
||||||
|
| undefined
|
||||||
|
|
||||||
|
const [sortBy, setSortBy] = useState<string>(() => savedSort?.sortBy || 'name')
|
||||||
|
const [sortDirection, setSortDirection] = useState<'asc' | 'desc'>(
|
||||||
|
() => savedSort?.sortDirection || 'asc',
|
||||||
|
)
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (savedSort?.sortBy && savedSort.sortBy !== sortBy) {
|
||||||
|
setSortBy(savedSort.sortBy)
|
||||||
|
}
|
||||||
|
if (savedSort?.sortDirection && savedSort.sortDirection !== sortDirection) {
|
||||||
|
setSortDirection(savedSort.sortDirection)
|
||||||
|
}
|
||||||
|
}, [savedSort])
|
||||||
|
|
||||||
const $getProjects = client.api.teams[':teamId'].projects.$get
|
const $getProjects = client.api.teams[':teamId'].projects.$get
|
||||||
const { data: projects } = useSuspenseQuery({
|
const { data: projects } = useQuery({
|
||||||
queryKey: ['teams', teamId, 'projects', sortBy, sortDirection],
|
queryKey: ['teams', teamId, 'projects', sortBy, sortDirection],
|
||||||
queryFn: async () => {
|
queryFn: async () => {
|
||||||
const res = await $getProjects({
|
const res = await $getProjects({
|
||||||
@@ -72,6 +90,7 @@ function TeamPage() {
|
|||||||
if (!res.ok) throw new Error('Failed to fetch projects')
|
if (!res.ok) throw new Error('Failed to fetch projects')
|
||||||
return await res.json()
|
return await res.json()
|
||||||
},
|
},
|
||||||
|
placeholderData: keepPreviousData,
|
||||||
})
|
})
|
||||||
|
|
||||||
const { data: members } = useQuery({
|
const { data: members } = useQuery({
|
||||||
@@ -249,6 +268,7 @@ function TeamPage() {
|
|||||||
const handleSortChange = (newSortBy: string, newSortDirection: 'asc' | 'desc') => {
|
const handleSortChange = (newSortBy: string, newSortDirection: 'asc' | 'desc') => {
|
||||||
setSortBy(newSortBy)
|
setSortBy(newSortBy)
|
||||||
setSortDirection(newSortDirection)
|
setSortDirection(newSortDirection)
|
||||||
|
setUserMetadata(teamId, sortKey, { sortBy: newSortBy, sortDirection: newSortDirection })
|
||||||
}
|
}
|
||||||
|
|
||||||
const safeMembers = Array.isArray(members) ? members : []
|
const safeMembers = Array.isArray(members) ? members : []
|
||||||
@@ -294,7 +314,7 @@ function TeamPage() {
|
|||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<div className="grid grid-cols-1 sm:grid-cols-2 md:grid-cols-3 lg:grid-cols-4 xl:grid-cols-6 gap-4">
|
<div className="grid grid-cols-1 sm:grid-cols-2 md:grid-cols-3 lg:grid-cols-4 xl:grid-cols-6 gap-4">
|
||||||
{projects.data?.map((project: ProjectInfo) => (
|
{projects?.data?.map((project: ProjectInfo) => (
|
||||||
<div
|
<div
|
||||||
key={project.id}
|
key={project.id}
|
||||||
className={cn(
|
className={cn(
|
||||||
|
|||||||
Reference in New Issue
Block a user