sync me
This commit is contained in:
parent
58edc3040e
commit
b499ab853f
291
app/admin/dashboard/area.tsx
Normal file
291
app/admin/dashboard/area.tsx
Normal file
@ -0,0 +1,291 @@
|
|||||||
|
"use client"
|
||||||
|
|
||||||
|
import * as React from "react"
|
||||||
|
import { Area, AreaChart, CartesianGrid, XAxis } from "recharts"
|
||||||
|
|
||||||
|
import { useIsMobile } from "@/hooks/use-mobile"
|
||||||
|
import {
|
||||||
|
Card,
|
||||||
|
CardAction,
|
||||||
|
CardContent,
|
||||||
|
CardDescription,
|
||||||
|
CardHeader,
|
||||||
|
CardTitle,
|
||||||
|
} from "@/components/ui/card"
|
||||||
|
import {
|
||||||
|
ChartConfig,
|
||||||
|
ChartContainer,
|
||||||
|
ChartTooltip,
|
||||||
|
ChartTooltipContent,
|
||||||
|
} from "@/components/ui/chart"
|
||||||
|
import {
|
||||||
|
Select,
|
||||||
|
SelectContent,
|
||||||
|
SelectItem,
|
||||||
|
SelectTrigger,
|
||||||
|
SelectValue,
|
||||||
|
} from "@/components/ui/select"
|
||||||
|
import {
|
||||||
|
ToggleGroup,
|
||||||
|
ToggleGroupItem,
|
||||||
|
} from "@/components/ui/toggle-group"
|
||||||
|
|
||||||
|
export const description = "An interactive area chart"
|
||||||
|
|
||||||
|
const chartData = [
|
||||||
|
{ date: "2024-04-01", desktop: 222, mobile: 150 },
|
||||||
|
{ date: "2024-04-02", desktop: 97, mobile: 180 },
|
||||||
|
{ date: "2024-04-03", desktop: 167, mobile: 120 },
|
||||||
|
{ date: "2024-04-04", desktop: 242, mobile: 260 },
|
||||||
|
{ date: "2024-04-05", desktop: 373, mobile: 290 },
|
||||||
|
{ date: "2024-04-06", desktop: 301, mobile: 340 },
|
||||||
|
{ date: "2024-04-07", desktop: 245, mobile: 180 },
|
||||||
|
{ date: "2024-04-08", desktop: 409, mobile: 320 },
|
||||||
|
{ date: "2024-04-09", desktop: 59, mobile: 110 },
|
||||||
|
{ date: "2024-04-10", desktop: 261, mobile: 190 },
|
||||||
|
{ date: "2024-04-11", desktop: 327, mobile: 350 },
|
||||||
|
{ date: "2024-04-12", desktop: 292, mobile: 210 },
|
||||||
|
{ date: "2024-04-13", desktop: 342, mobile: 380 },
|
||||||
|
{ date: "2024-04-14", desktop: 137, mobile: 220 },
|
||||||
|
{ date: "2024-04-15", desktop: 120, mobile: 170 },
|
||||||
|
{ date: "2024-04-16", desktop: 138, mobile: 190 },
|
||||||
|
{ date: "2024-04-17", desktop: 446, mobile: 360 },
|
||||||
|
{ date: "2024-04-18", desktop: 364, mobile: 410 },
|
||||||
|
{ date: "2024-04-19", desktop: 243, mobile: 180 },
|
||||||
|
{ date: "2024-04-20", desktop: 89, mobile: 150 },
|
||||||
|
{ date: "2024-04-21", desktop: 137, mobile: 200 },
|
||||||
|
{ date: "2024-04-22", desktop: 224, mobile: 170 },
|
||||||
|
{ date: "2024-04-23", desktop: 138, mobile: 230 },
|
||||||
|
{ date: "2024-04-24", desktop: 387, mobile: 290 },
|
||||||
|
{ date: "2024-04-25", desktop: 215, mobile: 250 },
|
||||||
|
{ date: "2024-04-26", desktop: 75, mobile: 130 },
|
||||||
|
{ date: "2024-04-27", desktop: 383, mobile: 420 },
|
||||||
|
{ date: "2024-04-28", desktop: 122, mobile: 180 },
|
||||||
|
{ date: "2024-04-29", desktop: 315, mobile: 240 },
|
||||||
|
{ date: "2024-04-30", desktop: 454, mobile: 380 },
|
||||||
|
{ date: "2024-05-01", desktop: 165, mobile: 220 },
|
||||||
|
{ date: "2024-05-02", desktop: 293, mobile: 310 },
|
||||||
|
{ date: "2024-05-03", desktop: 247, mobile: 190 },
|
||||||
|
{ date: "2024-05-04", desktop: 385, mobile: 420 },
|
||||||
|
{ date: "2024-05-05", desktop: 481, mobile: 390 },
|
||||||
|
{ date: "2024-05-06", desktop: 498, mobile: 520 },
|
||||||
|
{ date: "2024-05-07", desktop: 388, mobile: 300 },
|
||||||
|
{ date: "2024-05-08", desktop: 149, mobile: 210 },
|
||||||
|
{ date: "2024-05-09", desktop: 227, mobile: 180 },
|
||||||
|
{ date: "2024-05-10", desktop: 293, mobile: 330 },
|
||||||
|
{ date: "2024-05-11", desktop: 335, mobile: 270 },
|
||||||
|
{ date: "2024-05-12", desktop: 197, mobile: 240 },
|
||||||
|
{ date: "2024-05-13", desktop: 197, mobile: 160 },
|
||||||
|
{ date: "2024-05-14", desktop: 448, mobile: 490 },
|
||||||
|
{ date: "2024-05-15", desktop: 473, mobile: 380 },
|
||||||
|
{ date: "2024-05-16", desktop: 338, mobile: 400 },
|
||||||
|
{ date: "2024-05-17", desktop: 499, mobile: 420 },
|
||||||
|
{ date: "2024-05-18", desktop: 315, mobile: 350 },
|
||||||
|
{ date: "2024-05-19", desktop: 235, mobile: 180 },
|
||||||
|
{ date: "2024-05-20", desktop: 177, mobile: 230 },
|
||||||
|
{ date: "2024-05-21", desktop: 82, mobile: 140 },
|
||||||
|
{ date: "2024-05-22", desktop: 81, mobile: 120 },
|
||||||
|
{ date: "2024-05-23", desktop: 252, mobile: 290 },
|
||||||
|
{ date: "2024-05-24", desktop: 294, mobile: 220 },
|
||||||
|
{ date: "2024-05-25", desktop: 201, mobile: 250 },
|
||||||
|
{ date: "2024-05-26", desktop: 213, mobile: 170 },
|
||||||
|
{ date: "2024-05-27", desktop: 420, mobile: 460 },
|
||||||
|
{ date: "2024-05-28", desktop: 233, mobile: 190 },
|
||||||
|
{ date: "2024-05-29", desktop: 78, mobile: 130 },
|
||||||
|
{ date: "2024-05-30", desktop: 340, mobile: 280 },
|
||||||
|
{ date: "2024-05-31", desktop: 178, mobile: 230 },
|
||||||
|
{ date: "2024-06-01", desktop: 178, mobile: 200 },
|
||||||
|
{ date: "2024-06-02", desktop: 470, mobile: 410 },
|
||||||
|
{ date: "2024-06-03", desktop: 103, mobile: 160 },
|
||||||
|
{ date: "2024-06-04", desktop: 439, mobile: 380 },
|
||||||
|
{ date: "2024-06-05", desktop: 88, mobile: 140 },
|
||||||
|
{ date: "2024-06-06", desktop: 294, mobile: 250 },
|
||||||
|
{ date: "2024-06-07", desktop: 323, mobile: 370 },
|
||||||
|
{ date: "2024-06-08", desktop: 385, mobile: 320 },
|
||||||
|
{ date: "2024-06-09", desktop: 438, mobile: 480 },
|
||||||
|
{ date: "2024-06-10", desktop: 155, mobile: 200 },
|
||||||
|
{ date: "2024-06-11", desktop: 92, mobile: 150 },
|
||||||
|
{ date: "2024-06-12", desktop: 492, mobile: 420 },
|
||||||
|
{ date: "2024-06-13", desktop: 81, mobile: 130 },
|
||||||
|
{ date: "2024-06-14", desktop: 426, mobile: 380 },
|
||||||
|
{ date: "2024-06-15", desktop: 307, mobile: 350 },
|
||||||
|
{ date: "2024-06-16", desktop: 371, mobile: 310 },
|
||||||
|
{ date: "2024-06-17", desktop: 475, mobile: 520 },
|
||||||
|
{ date: "2024-06-18", desktop: 107, mobile: 170 },
|
||||||
|
{ date: "2024-06-19", desktop: 341, mobile: 290 },
|
||||||
|
{ date: "2024-06-20", desktop: 408, mobile: 450 },
|
||||||
|
{ date: "2024-06-21", desktop: 169, mobile: 210 },
|
||||||
|
{ date: "2024-06-22", desktop: 317, mobile: 270 },
|
||||||
|
{ date: "2024-06-23", desktop: 480, mobile: 530 },
|
||||||
|
{ date: "2024-06-24", desktop: 132, mobile: 180 },
|
||||||
|
{ date: "2024-06-25", desktop: 141, mobile: 190 },
|
||||||
|
{ date: "2024-06-26", desktop: 434, mobile: 380 },
|
||||||
|
{ date: "2024-06-27", desktop: 448, mobile: 490 },
|
||||||
|
{ date: "2024-06-28", desktop: 149, mobile: 200 },
|
||||||
|
{ date: "2024-06-29", desktop: 103, mobile: 160 },
|
||||||
|
{ date: "2024-06-30", desktop: 446, mobile: 400 },
|
||||||
|
]
|
||||||
|
|
||||||
|
const chartConfig = {
|
||||||
|
visitors: {
|
||||||
|
label: "Visitors",
|
||||||
|
},
|
||||||
|
desktop: {
|
||||||
|
label: "Desktop",
|
||||||
|
color: "var(--primary)",
|
||||||
|
},
|
||||||
|
mobile: {
|
||||||
|
label: "Mobile",
|
||||||
|
color: "var(--primary)",
|
||||||
|
},
|
||||||
|
} satisfies ChartConfig
|
||||||
|
|
||||||
|
export function ChartAreaInteractive() {
|
||||||
|
const isMobile = useIsMobile()
|
||||||
|
const [timeRange, setTimeRange] = React.useState("90d")
|
||||||
|
|
||||||
|
React.useEffect(() => {
|
||||||
|
if (isMobile) {
|
||||||
|
setTimeRange("7d")
|
||||||
|
}
|
||||||
|
}, [isMobile])
|
||||||
|
|
||||||
|
const filteredData = chartData.filter((item) => {
|
||||||
|
const date = new Date(item.date)
|
||||||
|
const referenceDate = new Date("2024-06-30")
|
||||||
|
let daysToSubtract = 90
|
||||||
|
if (timeRange === "30d") {
|
||||||
|
daysToSubtract = 30
|
||||||
|
} else if (timeRange === "7d") {
|
||||||
|
daysToSubtract = 7
|
||||||
|
}
|
||||||
|
const startDate = new Date(referenceDate)
|
||||||
|
startDate.setDate(startDate.getDate() - daysToSubtract)
|
||||||
|
return date >= startDate
|
||||||
|
})
|
||||||
|
|
||||||
|
return (
|
||||||
|
<Card className="@container/card">
|
||||||
|
<CardHeader>
|
||||||
|
<CardTitle>Total Visitors</CardTitle>
|
||||||
|
<CardDescription>
|
||||||
|
<span className="hidden @[540px]/card:block">
|
||||||
|
Total for the last 3 months
|
||||||
|
</span>
|
||||||
|
<span className="@[540px]/card:hidden">Last 3 months</span>
|
||||||
|
</CardDescription>
|
||||||
|
<CardAction>
|
||||||
|
<ToggleGroup
|
||||||
|
type="single"
|
||||||
|
value={timeRange}
|
||||||
|
onValueChange={setTimeRange}
|
||||||
|
variant="outline"
|
||||||
|
className="hidden *:data-[slot=toggle-group-item]:!px-4 @[767px]/card:flex"
|
||||||
|
>
|
||||||
|
<ToggleGroupItem value="90d">Last 3 months</ToggleGroupItem>
|
||||||
|
<ToggleGroupItem value="30d">Last 30 days</ToggleGroupItem>
|
||||||
|
<ToggleGroupItem value="7d">Last 7 days</ToggleGroupItem>
|
||||||
|
</ToggleGroup>
|
||||||
|
<Select value={timeRange} onValueChange={setTimeRange}>
|
||||||
|
<SelectTrigger
|
||||||
|
className="flex w-40 **:data-[slot=select-value]:block **:data-[slot=select-value]:truncate @[767px]/card:hidden"
|
||||||
|
size="sm"
|
||||||
|
aria-label="Select a value"
|
||||||
|
>
|
||||||
|
<SelectValue placeholder="Last 3 months" />
|
||||||
|
</SelectTrigger>
|
||||||
|
<SelectContent className="rounded-xl">
|
||||||
|
<SelectItem value="90d" className="rounded-lg">
|
||||||
|
Last 3 months
|
||||||
|
</SelectItem>
|
||||||
|
<SelectItem value="30d" className="rounded-lg">
|
||||||
|
Last 30 days
|
||||||
|
</SelectItem>
|
||||||
|
<SelectItem value="7d" className="rounded-lg">
|
||||||
|
Last 7 days
|
||||||
|
</SelectItem>
|
||||||
|
</SelectContent>
|
||||||
|
</Select>
|
||||||
|
</CardAction>
|
||||||
|
</CardHeader>
|
||||||
|
<CardContent className="px-2 pt-4 sm:px-6 sm:pt-6">
|
||||||
|
<ChartContainer
|
||||||
|
config={chartConfig}
|
||||||
|
className="aspect-auto h-[250px] w-full"
|
||||||
|
>
|
||||||
|
<AreaChart data={filteredData}>
|
||||||
|
<defs>
|
||||||
|
<linearGradient id="fillDesktop" x1="0" y1="0" x2="0" y2="1">
|
||||||
|
<stop
|
||||||
|
offset="5%"
|
||||||
|
stopColor="var(--color-desktop)"
|
||||||
|
stopOpacity={1.0}
|
||||||
|
/>
|
||||||
|
<stop
|
||||||
|
offset="95%"
|
||||||
|
stopColor="var(--color-desktop)"
|
||||||
|
stopOpacity={0.1}
|
||||||
|
/>
|
||||||
|
</linearGradient>
|
||||||
|
<linearGradient id="fillMobile" x1="0" y1="0" x2="0" y2="1">
|
||||||
|
<stop
|
||||||
|
offset="5%"
|
||||||
|
stopColor="var(--color-mobile)"
|
||||||
|
stopOpacity={0.8}
|
||||||
|
/>
|
||||||
|
<stop
|
||||||
|
offset="95%"
|
||||||
|
stopColor="var(--color-mobile)"
|
||||||
|
stopOpacity={0.1}
|
||||||
|
/>
|
||||||
|
</linearGradient>
|
||||||
|
</defs>
|
||||||
|
<CartesianGrid vertical={false} />
|
||||||
|
<XAxis
|
||||||
|
dataKey="date"
|
||||||
|
tickLine={false}
|
||||||
|
axisLine={false}
|
||||||
|
tickMargin={8}
|
||||||
|
minTickGap={32}
|
||||||
|
tickFormatter={(value) => {
|
||||||
|
const date = new Date(value)
|
||||||
|
return date.toLocaleDateString("en-US", {
|
||||||
|
month: "short",
|
||||||
|
day: "numeric",
|
||||||
|
})
|
||||||
|
}}
|
||||||
|
/>
|
||||||
|
<ChartTooltip
|
||||||
|
cursor={false}
|
||||||
|
content={
|
||||||
|
<ChartTooltipContent
|
||||||
|
labelFormatter={(value) => {
|
||||||
|
return new Date(value).toLocaleDateString("en-US", {
|
||||||
|
month: "short",
|
||||||
|
day: "numeric",
|
||||||
|
})
|
||||||
|
}}
|
||||||
|
indicator="dot"
|
||||||
|
/>
|
||||||
|
}
|
||||||
|
/>
|
||||||
|
<Area
|
||||||
|
dataKey="mobile"
|
||||||
|
type="natural"
|
||||||
|
fill="url(#fillMobile)"
|
||||||
|
stroke="var(--color-mobile)"
|
||||||
|
stackId="a"
|
||||||
|
/>
|
||||||
|
<Area
|
||||||
|
dataKey="desktop"
|
||||||
|
type="natural"
|
||||||
|
fill="url(#fillDesktop)"
|
||||||
|
stroke="var(--color-desktop)"
|
||||||
|
stackId="a"
|
||||||
|
/>
|
||||||
|
</AreaChart>
|
||||||
|
</ChartContainer>
|
||||||
|
</CardContent>
|
||||||
|
</Card>
|
||||||
|
)
|
||||||
|
}
|
||||||
@ -1,14 +1,23 @@
|
|||||||
"use client"
|
"use client"
|
||||||
|
|
||||||
|
import { SectionCards } from "./sections"
|
||||||
|
import { ChartAreaInteractive } from "./area"
|
||||||
|
|
||||||
|
|
||||||
export default function Page() {
|
export default function Page() {
|
||||||
return (
|
return (
|
||||||
|
|
||||||
<div className="flex flex-1 flex-col">
|
<div className="flex flex-1 flex-col">
|
||||||
<div className="@container/main flex flex-1 flex-col gap-2">
|
<div className="@container/main flex flex-1 flex-col gap-2">
|
||||||
<div className="flex flex-col gap-4 py-4 md:gap-6 md:py-6">
|
<div className="flex flex-col gap-4 py-4 md:gap-6 md:py-6">
|
||||||
|
<SectionCards />
|
||||||
|
<div className="px-4 lg:px-6">
|
||||||
|
<ChartAreaInteractive />
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
102
app/admin/dashboard/sections.tsx
Normal file
102
app/admin/dashboard/sections.tsx
Normal file
@ -0,0 +1,102 @@
|
|||||||
|
import { IconTrendingDown, IconTrendingUp } from "@tabler/icons-react"
|
||||||
|
|
||||||
|
import { Badge } from "@/components/ui/badge"
|
||||||
|
import {
|
||||||
|
Card,
|
||||||
|
CardAction,
|
||||||
|
CardDescription,
|
||||||
|
CardFooter,
|
||||||
|
CardHeader,
|
||||||
|
CardTitle,
|
||||||
|
} from "@/components/ui/card"
|
||||||
|
|
||||||
|
export function SectionCards() {
|
||||||
|
return (
|
||||||
|
<div className="*:data-[slot=card]:from-primary/5 *:data-[slot=card]:to-card dark:*:data-[slot=card]:bg-card grid grid-cols-1 gap-4 px-4 *:data-[slot=card]:bg-gradient-to-t *:data-[slot=card]:shadow-xs lg:px-6 @xl/main:grid-cols-2 @5xl/main:grid-cols-4">
|
||||||
|
<Card className="@container/card">
|
||||||
|
<CardHeader>
|
||||||
|
<CardDescription>Total Revenue</CardDescription>
|
||||||
|
<CardTitle className="text-2xl font-semibold tabular-nums @[250px]/card:text-3xl">
|
||||||
|
$1,250.00
|
||||||
|
</CardTitle>
|
||||||
|
<CardAction>
|
||||||
|
<Badge variant="outline">
|
||||||
|
<IconTrendingUp />
|
||||||
|
+12.5%
|
||||||
|
</Badge>
|
||||||
|
</CardAction>
|
||||||
|
</CardHeader>
|
||||||
|
<CardFooter className="flex-col items-start gap-1.5 text-sm">
|
||||||
|
<div className="line-clamp-1 flex gap-2 font-medium">
|
||||||
|
Trending up this month <IconTrendingUp className="size-4" />
|
||||||
|
</div>
|
||||||
|
<div className="text-muted-foreground">
|
||||||
|
Visitors for the last 6 months
|
||||||
|
</div>
|
||||||
|
</CardFooter>
|
||||||
|
</Card>
|
||||||
|
<Card className="@container/card">
|
||||||
|
<CardHeader>
|
||||||
|
<CardDescription>New Customers</CardDescription>
|
||||||
|
<CardTitle className="text-2xl font-semibold tabular-nums @[250px]/card:text-3xl">
|
||||||
|
1,234
|
||||||
|
</CardTitle>
|
||||||
|
<CardAction>
|
||||||
|
<Badge variant="outline">
|
||||||
|
<IconTrendingDown />
|
||||||
|
-20%
|
||||||
|
</Badge>
|
||||||
|
</CardAction>
|
||||||
|
</CardHeader>
|
||||||
|
<CardFooter className="flex-col items-start gap-1.5 text-sm">
|
||||||
|
<div className="line-clamp-1 flex gap-2 font-medium">
|
||||||
|
Down 20% this period <IconTrendingDown className="size-4" />
|
||||||
|
</div>
|
||||||
|
<div className="text-muted-foreground">
|
||||||
|
Acquisition needs attention
|
||||||
|
</div>
|
||||||
|
</CardFooter>
|
||||||
|
</Card>
|
||||||
|
<Card className="@container/card">
|
||||||
|
<CardHeader>
|
||||||
|
<CardDescription>Active Accounts</CardDescription>
|
||||||
|
<CardTitle className="text-2xl font-semibold tabular-nums @[250px]/card:text-3xl">
|
||||||
|
45,678
|
||||||
|
</CardTitle>
|
||||||
|
<CardAction>
|
||||||
|
<Badge variant="outline">
|
||||||
|
<IconTrendingUp />
|
||||||
|
+12.5%
|
||||||
|
</Badge>
|
||||||
|
</CardAction>
|
||||||
|
</CardHeader>
|
||||||
|
<CardFooter className="flex-col items-start gap-1.5 text-sm">
|
||||||
|
<div className="line-clamp-1 flex gap-2 font-medium">
|
||||||
|
Strong user retention <IconTrendingUp className="size-4" />
|
||||||
|
</div>
|
||||||
|
<div className="text-muted-foreground">Engagement exceed targets</div>
|
||||||
|
</CardFooter>
|
||||||
|
</Card>
|
||||||
|
<Card className="@container/card">
|
||||||
|
<CardHeader>
|
||||||
|
<CardDescription>Growth Rate</CardDescription>
|
||||||
|
<CardTitle className="text-2xl font-semibold tabular-nums @[250px]/card:text-3xl">
|
||||||
|
4.5%
|
||||||
|
</CardTitle>
|
||||||
|
<CardAction>
|
||||||
|
<Badge variant="outline">
|
||||||
|
<IconTrendingUp />
|
||||||
|
+4.5%
|
||||||
|
</Badge>
|
||||||
|
</CardAction>
|
||||||
|
</CardHeader>
|
||||||
|
<CardFooter className="flex-col items-start gap-1.5 text-sm">
|
||||||
|
<div className="line-clamp-1 flex gap-2 font-medium">
|
||||||
|
Steady performance increase <IconTrendingUp className="size-4" />
|
||||||
|
</div>
|
||||||
|
<div className="text-muted-foreground">Meets growth projections</div>
|
||||||
|
</CardFooter>
|
||||||
|
</Card>
|
||||||
|
</div>
|
||||||
|
)
|
||||||
|
}
|
||||||
@ -20,6 +20,6 @@ export default function Dashboard() {
|
|||||||
}
|
}
|
||||||
}, [isAuthenticated, isLoading, router, user]);
|
}, [isAuthenticated, isLoading, router, user]);
|
||||||
|
|
||||||
return (<></>)
|
redirect('/admin/dashboard')
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,7 +1,7 @@
|
|||||||
"use client"
|
"use client"
|
||||||
|
|
||||||
import * as React from "react"
|
import * as React from "react"
|
||||||
import { useQuery, gql, useMutation } from '@apollo/client';
|
import { useQuery, gql } from '@apollo/client';
|
||||||
import {
|
import {
|
||||||
Dialog,
|
Dialog,
|
||||||
DialogClose,
|
DialogClose,
|
||||||
@ -117,9 +117,10 @@ import {
|
|||||||
TabsTrigger,
|
TabsTrigger,
|
||||||
} from "@/components/ui/tabs"
|
} from "@/components/ui/tabs"
|
||||||
import CreateUserForm from "./create-user-form";
|
import CreateUserForm from "./create-user-form";
|
||||||
|
import { useUser } from "@/app/user-context";
|
||||||
|
|
||||||
export const schema = z.object({
|
export const schema = z.object({
|
||||||
id: z.number(),
|
id: z.string(),
|
||||||
username: z.string(),
|
username: z.string(),
|
||||||
email: z.string(),
|
email: z.string(),
|
||||||
role: z.string(),
|
role: z.string(),
|
||||||
@ -128,7 +129,7 @@ export const schema = z.object({
|
|||||||
})
|
})
|
||||||
|
|
||||||
// Create a separate component for the drag handle
|
// Create a separate component for the drag handle
|
||||||
function DragHandle({ id }: { id: number }) {
|
function DragHandle({ id }: { id: string }) {
|
||||||
const { attributes, listeners } = useSortable({
|
const { attributes, listeners } = useSortable({
|
||||||
id,
|
id,
|
||||||
})
|
})
|
||||||
@ -211,7 +212,21 @@ const columns: ColumnDef<z.infer<typeof schema>>[] = [
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
accessorKey: "createdAt",
|
accessorKey: "createdAt",
|
||||||
header: "Created At",
|
header: ({ column }) => {
|
||||||
|
return (
|
||||||
|
<Button
|
||||||
|
variant="ghost"
|
||||||
|
onClick={() => column.toggleSorting(column.getIsSorted() === "asc")}
|
||||||
|
className="px-0 hover:bg-transparent"
|
||||||
|
>
|
||||||
|
Created At
|
||||||
|
<IconChevronDown
|
||||||
|
className={`ml-2 h-4 w-4 transition-transform ${column.getIsSorted() === "asc" ? "rotate-180" : ""
|
||||||
|
}`}
|
||||||
|
/>
|
||||||
|
</Button>
|
||||||
|
)
|
||||||
|
},
|
||||||
cell: ({ row }) => {
|
cell: ({ row }) => {
|
||||||
const date = new Date(row.original.createdAt);
|
const date = new Date(row.original.createdAt);
|
||||||
const now = new Date();
|
const now = new Date();
|
||||||
@ -252,10 +267,31 @@ const columns: ColumnDef<z.infer<typeof schema>>[] = [
|
|||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
},
|
},
|
||||||
|
enableSorting: true,
|
||||||
|
sortingFn: (rowA, rowB, columnId) => {
|
||||||
|
const dateA = new Date(rowA.original.createdAt);
|
||||||
|
const dateB = new Date(rowB.original.createdAt);
|
||||||
|
return dateA.getTime() - dateB.getTime();
|
||||||
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
accessorKey: "lastLogin",
|
id: "lastLogin",
|
||||||
header: "Last Login",
|
accessorFn: (row) => row.updatedAt,
|
||||||
|
header: ({ column }) => {
|
||||||
|
return (
|
||||||
|
<Button
|
||||||
|
variant="ghost"
|
||||||
|
onClick={() => column.toggleSorting(column.getIsSorted() === "asc")}
|
||||||
|
className="px-0 hover:bg-transparent"
|
||||||
|
>
|
||||||
|
Last Login
|
||||||
|
<IconChevronDown
|
||||||
|
className={`ml-2 h-4 w-4 transition-transform ${column.getIsSorted() === "asc" ? "rotate-180" : ""
|
||||||
|
}`}
|
||||||
|
/>
|
||||||
|
</Button>
|
||||||
|
)
|
||||||
|
},
|
||||||
cell: ({ row }) => {
|
cell: ({ row }) => {
|
||||||
const date = new Date(row.original.updatedAt);
|
const date = new Date(row.original.updatedAt);
|
||||||
const now = new Date();
|
const now = new Date();
|
||||||
@ -292,6 +328,12 @@ const columns: ColumnDef<z.infer<typeof schema>>[] = [
|
|||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
},
|
},
|
||||||
|
enableSorting: true,
|
||||||
|
sortingFn: (rowA, rowB, columnId) => {
|
||||||
|
const dateA = new Date(rowA.original.updatedAt);
|
||||||
|
const dateB = new Date(rowB.original.updatedAt);
|
||||||
|
return dateA.getTime() - dateB.getTime();
|
||||||
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
id: "actions",
|
id: "actions",
|
||||||
@ -345,8 +387,8 @@ function DraggableRow({ row }: { row: Row<z.infer<typeof schema>> }) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const GET_USERS = gql`
|
const GET_USERS = gql`
|
||||||
query GetUsers($offset: Int, $limit: Int) {
|
query GetUsers($offset: Int, $limit: Int, $sort_by: String, $sort_order: String, $filter: String) {
|
||||||
users(offset: $offset, limit: $limit) {
|
users(offset: $offset, limit: $limit, sortBy: $sort_by, sortOrder: $sort_order, filter: $filter) {
|
||||||
id
|
id
|
||||||
username
|
username
|
||||||
email
|
email
|
||||||
@ -358,8 +400,8 @@ const GET_USERS = gql`
|
|||||||
`
|
`
|
||||||
|
|
||||||
const USERS_INFO = gql`
|
const USERS_INFO = gql`
|
||||||
query UsersInfo($offset: Int, $limit: Int) {
|
query UsersInfo($offset: Int, $limit: Int, $sort_by: String, $sort_order: String, $filter: String) {
|
||||||
usersInfo(offset: $offset, limit: $limit) {
|
usersInfo(offset: $offset, limit: $limit, sortBy: $sort_by, sortOrder: $sort_order, filter: $filter) {
|
||||||
totalUsers
|
totalUsers
|
||||||
totalActiveUsers
|
totalActiveUsers
|
||||||
totalAdminUsers
|
totalAdminUsers
|
||||||
@ -378,7 +420,7 @@ const USERS_INFO = gql`
|
|||||||
|
|
||||||
export function UserTable() {
|
export function UserTable() {
|
||||||
|
|
||||||
const { data, loading, error: usersInfoError, refetch } = useQuery(USERS_INFO)
|
const { data, loading, error, refetch } = useQuery(USERS_INFO)
|
||||||
|
|
||||||
const [localData, setLocalData] = React.useState<any[]>([])
|
const [localData, setLocalData] = React.useState<any[]>([])
|
||||||
|
|
||||||
@ -407,60 +449,20 @@ export function UserTable() {
|
|||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<Tabel data={localData} refetch={refetch} info={data?.usersInfo} isLoading={loading} handleDragEnd={handleDragEnd} />
|
{error && <div className="text-red-500">{error.message}</div>}
|
||||||
|
<UserTabs
|
||||||
|
initialData={localData}
|
||||||
|
initialLoading={loading}
|
||||||
|
info={data?.usersInfo}
|
||||||
|
handleDragEnd={handleDragEnd}
|
||||||
|
refetch={refetch}
|
||||||
|
/>
|
||||||
</>
|
</>
|
||||||
)
|
)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function Tabel({ data, refetch, info, isLoading, handleDragEnd }: { data: any, refetch: any, info: any, isLoading: boolean, handleDragEnd: (event: DragEndEvent) => void }) {
|
function UserTabs({ initialData, initialLoading, info, handleDragEnd, refetch }: { initialData: any[], initialLoading: boolean, info: any, handleDragEnd: (event: DragEndEvent) => void, refetch: any }) {
|
||||||
|
|
||||||
const [rowSelection, setRowSelection] = React.useState({})
|
|
||||||
const [columnVisibility, setColumnVisibility] =
|
|
||||||
React.useState<VisibilityState>({})
|
|
||||||
const [columnFilters, setColumnFilters] = React.useState<ColumnFiltersState>(
|
|
||||||
[]
|
|
||||||
)
|
|
||||||
const [sorting, setSorting] = React.useState<SortingState>([])
|
|
||||||
const [pagination, setPagination] = React.useState({
|
|
||||||
pageIndex: 0,
|
|
||||||
pageSize: 10,
|
|
||||||
})
|
|
||||||
const sortableId = React.useId()
|
|
||||||
const sensors = useSensors(
|
|
||||||
useSensor(MouseSensor, {}),
|
|
||||||
useSensor(TouchSensor, {}),
|
|
||||||
useSensor(KeyboardSensor, {})
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
const table = useReactTable({
|
|
||||||
data: data || [],
|
|
||||||
columns,
|
|
||||||
state: {
|
|
||||||
sorting,
|
|
||||||
columnVisibility,
|
|
||||||
rowSelection,
|
|
||||||
columnFilters,
|
|
||||||
pagination,
|
|
||||||
},
|
|
||||||
getRowId: (row) => row.id.toString(),
|
|
||||||
enableRowSelection: true,
|
|
||||||
onRowSelectionChange: setRowSelection,
|
|
||||||
onSortingChange: setSorting,
|
|
||||||
onColumnFiltersChange: setColumnFilters,
|
|
||||||
onColumnVisibilityChange: setColumnVisibility,
|
|
||||||
onPaginationChange: setPagination,
|
|
||||||
getCoreRowModel: getCoreRowModel(),
|
|
||||||
getFilteredRowModel: getFilteredRowModel(),
|
|
||||||
getPaginationRowModel: getPaginationRowModel(),
|
|
||||||
getSortedRowModel: getSortedRowModel(),
|
|
||||||
getFacetedRowModel: getFacetedRowModel(),
|
|
||||||
getFacetedUniqueValues: getFacetedUniqueValues(),
|
|
||||||
manualPagination: true,
|
|
||||||
})
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Dialog>
|
<Dialog>
|
||||||
@ -508,6 +510,177 @@ function Tabel({ data, refetch, info, isLoading, handleDragEnd }: { data: any, r
|
|||||||
<TabsTrigger value="inactive_users">Inactive Users</TabsTrigger>
|
<TabsTrigger value="inactive_users">Inactive Users</TabsTrigger>
|
||||||
</TabsList>
|
</TabsList>
|
||||||
<div className="flex items-center gap-2">
|
<div className="flex items-center gap-2">
|
||||||
|
<DialogTrigger asChild>
|
||||||
|
<Button variant="outline" size="sm">
|
||||||
|
<IconPlus />
|
||||||
|
<span className="hidden lg:inline">Add User</span>
|
||||||
|
</Button>
|
||||||
|
</DialogTrigger>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<TabsContent value="all_users" className="relative flex flex-col gap-4 overflow-auto px-4 lg:px-6">
|
||||||
|
<UserDataTable
|
||||||
|
data={initialData}
|
||||||
|
isLoading={initialLoading}
|
||||||
|
handleDragEnd={handleDragEnd}
|
||||||
|
useInitialData={true}
|
||||||
|
enableServerSideSort={true}
|
||||||
|
refetchFn={refetch}
|
||||||
|
/>
|
||||||
|
</TabsContent>
|
||||||
|
|
||||||
|
<TabsContent value="active_users" className="relative flex flex-col gap-4 overflow-auto px-4 lg:px-6">
|
||||||
|
<UserDataTable
|
||||||
|
filter="Users"
|
||||||
|
handleDragEnd={handleDragEnd}
|
||||||
|
useInitialData={false}
|
||||||
|
/>
|
||||||
|
</TabsContent>
|
||||||
|
|
||||||
|
<TabsContent value="admin_users" className="relative flex flex-col gap-4 overflow-auto px-4 lg:px-6">
|
||||||
|
<UserDataTable
|
||||||
|
filter="Admin"
|
||||||
|
handleDragEnd={handleDragEnd}
|
||||||
|
useInitialData={false}
|
||||||
|
/>
|
||||||
|
</TabsContent>
|
||||||
|
|
||||||
|
<TabsContent value="inactive_users" className="relative flex flex-col gap-4 overflow-auto px-4 lg:px-6">
|
||||||
|
<UserDataTable
|
||||||
|
filter="Users"
|
||||||
|
handleDragEnd={handleDragEnd}
|
||||||
|
useInitialData={false}
|
||||||
|
/>
|
||||||
|
</TabsContent>
|
||||||
|
</Tabs>
|
||||||
|
</Dialog>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 可重用的数据表格组件
|
||||||
|
function UserDataTable({
|
||||||
|
data: propData,
|
||||||
|
isLoading: propIsLoading = false,
|
||||||
|
handleDragEnd,
|
||||||
|
filter,
|
||||||
|
useInitialData = false,
|
||||||
|
enableServerSideSort = false,
|
||||||
|
refetchFn
|
||||||
|
}: {
|
||||||
|
data?: any[]
|
||||||
|
isLoading?: boolean
|
||||||
|
handleDragEnd: (event: DragEndEvent) => void
|
||||||
|
filter?: string
|
||||||
|
useInitialData?: boolean
|
||||||
|
enableServerSideSort?: boolean
|
||||||
|
refetchFn?: any
|
||||||
|
}) {
|
||||||
|
const [localData, setLocalData] = React.useState<any[]>([])
|
||||||
|
const [rowSelection, setRowSelection] = React.useState({})
|
||||||
|
const [columnVisibility, setColumnVisibility] = React.useState<VisibilityState>({})
|
||||||
|
const [columnFilters, setColumnFilters] = React.useState<ColumnFiltersState>([])
|
||||||
|
const [sorting, setSorting] = React.useState<SortingState>([])
|
||||||
|
const [pagination, setPagination] = React.useState({
|
||||||
|
pageIndex: 0,
|
||||||
|
pageSize: 10,
|
||||||
|
})
|
||||||
|
|
||||||
|
// 为非初始数据的tab查询数据
|
||||||
|
const { data: queryData, loading: queryLoading, refetch } = useQuery(GET_USERS, {
|
||||||
|
variables: {
|
||||||
|
offset: pagination.pageIndex * pagination.pageSize,
|
||||||
|
limit: pagination.pageSize,
|
||||||
|
sortBy: sorting[0]?.id || "createdAt",
|
||||||
|
sortOrder: sorting[0]?.desc ? "DESC" : "ASC",
|
||||||
|
filter: filter
|
||||||
|
},
|
||||||
|
skip: useInitialData,
|
||||||
|
fetchPolicy: 'cache-and-network'
|
||||||
|
})
|
||||||
|
|
||||||
|
const data = useInitialData ? propData : queryData?.users
|
||||||
|
const isLoading = useInitialData ? propIsLoading : queryLoading
|
||||||
|
|
||||||
|
// 同步数据到本地状态
|
||||||
|
React.useEffect(() => {
|
||||||
|
if (data && Array.isArray(data)) {
|
||||||
|
setLocalData(data)
|
||||||
|
}
|
||||||
|
}, [data])
|
||||||
|
|
||||||
|
// 当筛选条件变化时重置分页
|
||||||
|
React.useEffect(() => {
|
||||||
|
setPagination({ pageIndex: 0, pageSize: 10 })
|
||||||
|
}, [filter])
|
||||||
|
|
||||||
|
// 当排序或分页变化时,重新查询数据
|
||||||
|
React.useEffect(() => {
|
||||||
|
const refetchFunc = refetchFn || refetch
|
||||||
|
if ((!useInitialData || enableServerSideSort) && refetchFunc) {
|
||||||
|
refetchFunc({
|
||||||
|
offset: pagination.pageIndex * pagination.pageSize,
|
||||||
|
limit: pagination.pageSize,
|
||||||
|
sortBy: sorting[0]?.id || "createdAt",
|
||||||
|
sortOrder: sorting[0]?.desc ? "DESC" : "ASC",
|
||||||
|
filter: filter
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}, [sorting, pagination, filter, useInitialData, enableServerSideSort, refetch, refetchFn])
|
||||||
|
|
||||||
|
function handleLocalDragEnd(event: DragEndEvent) {
|
||||||
|
const { active, over } = event
|
||||||
|
if (active && over && active.id !== over.id) {
|
||||||
|
setLocalData((currentData) => {
|
||||||
|
const oldIndex = currentData.findIndex((item) => item.id === active.id)
|
||||||
|
const newIndex = currentData.findIndex((item) => item.id === over.id)
|
||||||
|
|
||||||
|
if (oldIndex === -1 || newIndex === -1) return currentData
|
||||||
|
|
||||||
|
// 只做本地排序,不保存到数据库
|
||||||
|
return arrayMove(currentData, oldIndex, newIndex)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
// 也调用父组件的handleDragEnd
|
||||||
|
handleDragEnd(event)
|
||||||
|
}
|
||||||
|
|
||||||
|
const sortableId = React.useId()
|
||||||
|
const sensors = useSensors(
|
||||||
|
useSensor(MouseSensor, {}),
|
||||||
|
useSensor(TouchSensor, {}),
|
||||||
|
useSensor(KeyboardSensor, {})
|
||||||
|
)
|
||||||
|
|
||||||
|
const table = useReactTable({
|
||||||
|
data: localData || [],
|
||||||
|
columns,
|
||||||
|
state: {
|
||||||
|
sorting,
|
||||||
|
columnVisibility,
|
||||||
|
rowSelection,
|
||||||
|
columnFilters,
|
||||||
|
pagination,
|
||||||
|
},
|
||||||
|
getRowId: (row) => row.id.toString(),
|
||||||
|
enableRowSelection: true,
|
||||||
|
onRowSelectionChange: setRowSelection,
|
||||||
|
onSortingChange: setSorting,
|
||||||
|
onColumnFiltersChange: setColumnFilters,
|
||||||
|
onColumnVisibilityChange: setColumnVisibility,
|
||||||
|
onPaginationChange: setPagination,
|
||||||
|
getCoreRowModel: getCoreRowModel(),
|
||||||
|
getFilteredRowModel: getFilteredRowModel(),
|
||||||
|
getPaginationRowModel: getPaginationRowModel(),
|
||||||
|
getSortedRowModel: getSortedRowModel(),
|
||||||
|
getFacetedRowModel: getFacetedRowModel(),
|
||||||
|
getFacetedUniqueValues: getFacetedUniqueValues(),
|
||||||
|
manualPagination: true,
|
||||||
|
})
|
||||||
|
|
||||||
|
return (
|
||||||
|
<>
|
||||||
|
{/* <div className="flex items-center gap-2 mb-4">
|
||||||
<DropdownMenu>
|
<DropdownMenu>
|
||||||
<DropdownMenuTrigger asChild>
|
<DropdownMenuTrigger asChild>
|
||||||
<Button variant="outline" size="sm">
|
<Button variant="outline" size="sm">
|
||||||
@ -541,24 +714,13 @@ function Tabel({ data, refetch, info, isLoading, handleDragEnd }: { data: any, r
|
|||||||
})}
|
})}
|
||||||
</DropdownMenuContent>
|
</DropdownMenuContent>
|
||||||
</DropdownMenu>
|
</DropdownMenu>
|
||||||
<DialogTrigger asChild>
|
</div> */}
|
||||||
<Button variant="outline" size="sm">
|
|
||||||
<IconPlus />
|
|
||||||
<span className="hidden lg:inline">Add User</span>
|
|
||||||
</Button>
|
|
||||||
</DialogTrigger>
|
|
||||||
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
<TabsContent
|
|
||||||
value="all_users"
|
|
||||||
className="relative flex flex-col gap-4 overflow-auto px-4 lg:px-6"
|
|
||||||
>
|
|
||||||
<div className="overflow-hidden rounded-lg border">
|
<div className="overflow-hidden rounded-lg border">
|
||||||
<DndContext
|
<DndContext
|
||||||
collisionDetection={closestCenter}
|
collisionDetection={closestCenter}
|
||||||
modifiers={[restrictToVerticalAxis]}
|
modifiers={[restrictToVerticalAxis]}
|
||||||
onDragEnd={handleDragEnd}
|
onDragEnd={handleLocalDragEnd}
|
||||||
sensors={sensors}
|
sensors={sensors}
|
||||||
id={sortableId}
|
id={sortableId}
|
||||||
>
|
>
|
||||||
@ -582,15 +744,18 @@ function Tabel({ data, refetch, info, isLoading, handleDragEnd }: { data: any, r
|
|||||||
))}
|
))}
|
||||||
</TableHeader>
|
</TableHeader>
|
||||||
<TableBody className="**:data-[slot=table-cell]:first:w-8">
|
<TableBody className="**:data-[slot=table-cell]:first:w-8">
|
||||||
{isLoading ? (<>
|
{isLoading ? (
|
||||||
<TableRow>
|
<TableRow>
|
||||||
<TableCell colSpan={columns.length} className="h-24 w-full flex items-center justify-center">
|
<TableCell
|
||||||
<IconLoader className="size-4 animate-spin" />
|
colSpan={columns.length}
|
||||||
|
className="h-24 text-center">
|
||||||
|
Loading...
|
||||||
|
{/* <IconLoader className="size-4 animate-spin" /> */}
|
||||||
</TableCell>
|
</TableCell>
|
||||||
</TableRow>
|
</TableRow>
|
||||||
</>) : table.getRowModel().rows?.length ? (
|
) : table.getRowModel().rows?.length ? (
|
||||||
<SortableContext
|
<SortableContext
|
||||||
items={data?.map((user: any) => user.id) || []}
|
items={localData?.map((user: any) => user.id) || []}
|
||||||
strategy={verticalListSortingStrategy}
|
strategy={verticalListSortingStrategy}
|
||||||
>
|
>
|
||||||
{table.getRowModel().rows.map((row) => (
|
{table.getRowModel().rows.map((row) => (
|
||||||
@ -688,26 +853,8 @@ function Tabel({ data, refetch, info, isLoading, handleDragEnd }: { data: any, r
|
|||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</TabsContent>
|
</>
|
||||||
<TabsContent
|
|
||||||
value="active_users"
|
|
||||||
className="flex flex-col px-4 lg:px-6"
|
|
||||||
>
|
|
||||||
<div className="aspect-video w-full flex-1 rounded-lg border border-dashed"></div>
|
|
||||||
</TabsContent>
|
|
||||||
<TabsContent value="admin_users" className="flex flex-col px-4 lg:px-6">
|
|
||||||
<div className="aspect-video w-full flex-1 rounded-lg border border-dashed"></div>
|
|
||||||
</TabsContent>
|
|
||||||
<TabsContent
|
|
||||||
value="inactive_users"
|
|
||||||
className="flex flex-col px-4 lg:px-6"
|
|
||||||
>
|
|
||||||
<div className="aspect-video w-full flex-1 rounded-lg border border-dashed"></div>
|
|
||||||
</TabsContent>
|
|
||||||
</Tabs>
|
|
||||||
</Dialog>
|
|
||||||
)
|
)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const chartData = [
|
const chartData = [
|
||||||
@ -732,14 +879,26 @@ const chartConfig = {
|
|||||||
|
|
||||||
function TableCellViewer({ item }: { item: z.infer<typeof schema> }) {
|
function TableCellViewer({ item }: { item: z.infer<typeof schema> }) {
|
||||||
const isMobile = useIsMobile()
|
const isMobile = useIsMobile()
|
||||||
|
const { user } = useUser()
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Drawer direction={isMobile ? "bottom" : "right"}>
|
<Drawer direction={isMobile ? "bottom" : "right"}>
|
||||||
<DrawerTrigger asChild>
|
<DrawerTrigger asChild>
|
||||||
|
<div className="flex items-center gap-2">
|
||||||
<Button variant="link" className="text-foreground w-fit px-0 text-left">
|
<Button variant="link" className="text-foreground w-fit px-0 text-left">
|
||||||
{item.username}
|
{item.username}
|
||||||
</Button>
|
</Button>
|
||||||
|
{
|
||||||
|
item.id === user?.id ? (
|
||||||
|
<Badge variant="secondary" className="text-[10px] w-fit">
|
||||||
|
Me
|
||||||
|
</Badge>
|
||||||
|
) : <></>
|
||||||
|
}
|
||||||
|
|
||||||
|
</div>
|
||||||
</DrawerTrigger>
|
</DrawerTrigger>
|
||||||
|
|
||||||
<DrawerContent>
|
<DrawerContent>
|
||||||
<DrawerHeader className="gap-1">
|
<DrawerHeader className="gap-1">
|
||||||
<DrawerTitle>{item.username}</DrawerTitle>
|
<DrawerTitle>{item.username}</DrawerTitle>
|
||||||
|
|||||||
@ -54,12 +54,8 @@ export function AppSidebar({ ...props }: React.ComponentProps<typeof Sidebar>) {
|
|||||||
return (
|
return (
|
||||||
<Sidebar {...props}>
|
<Sidebar {...props}>
|
||||||
<SidebarHeader className="h-16 border-b border-sidebar-border">
|
<SidebarHeader className="h-16 border-b border-sidebar-border">
|
||||||
{
|
|
||||||
user ? <NavUser user={user} /> : null
|
|
||||||
}
|
|
||||||
</SidebarHeader>
|
</SidebarHeader>
|
||||||
<SidebarContent>
|
<SidebarContent>
|
||||||
{/* <DatePicker /> */}
|
|
||||||
<SidebarGroup className="p-2 gap-2 px-4">
|
<SidebarGroup className="p-2 gap-2 px-4">
|
||||||
<Label htmlFor="date">Location</Label>
|
<Label htmlFor="date">Location</Label>
|
||||||
<Select defaultValue={currentLocation} onValueChange={(value) => {
|
<Select defaultValue={currentLocation} onValueChange={(value) => {
|
||||||
@ -88,19 +84,12 @@ export function AppSidebar({ ...props }: React.ComponentProps<typeof Sidebar>) {
|
|||||||
<span className="text-sm text-muted-foreground bg-muted rounded-md px-1 py-1 text-center w-10 inline-block">{mapState.zoomLevel.toFixed(1)}</span>
|
<span className="text-sm text-muted-foreground bg-muted rounded-md px-1 py-1 text-center w-10 inline-block">{mapState.zoomLevel.toFixed(1)}</span>
|
||||||
</div>
|
</div>
|
||||||
</SidebarGroup>
|
</SidebarGroup>
|
||||||
|
|
||||||
<SidebarSeparator className="mx-0" />
|
<SidebarSeparator className="mx-0" />
|
||||||
<Calendars calendars={data.calendars} />
|
|
||||||
</SidebarContent>
|
</SidebarContent>
|
||||||
<SidebarFooter>
|
<SidebarFooter>
|
||||||
<SidebarMenu>
|
{
|
||||||
<SidebarMenuItem>
|
user ? <NavUser user={user} /> : null
|
||||||
<SidebarMenuButton>
|
}
|
||||||
<Plus />
|
|
||||||
<span>New Calendar</span>
|
|
||||||
</SidebarMenuButton>
|
|
||||||
</SidebarMenuItem>
|
|
||||||
</SidebarMenu>
|
|
||||||
</SidebarFooter>
|
</SidebarFooter>
|
||||||
<SidebarRail />
|
<SidebarRail />
|
||||||
</Sidebar>
|
</Sidebar>
|
||||||
|
|||||||
@ -2,6 +2,7 @@ import type { Metadata } from "next";
|
|||||||
import { Geist, Geist_Mono } from "next/font/google";
|
import { Geist, Geist_Mono } from "next/font/google";
|
||||||
import "./globals.css";
|
import "./globals.css";
|
||||||
import { ClientProviders } from "./client-provider";
|
import { ClientProviders } from "./client-provider";
|
||||||
|
import { Toaster } from "@/components/ui/sonner"
|
||||||
|
|
||||||
const geistSans = Geist({
|
const geistSans = Geist({
|
||||||
variable: "--font-geist-sans",
|
variable: "--font-geist-sans",
|
||||||
@ -32,6 +33,7 @@ export default function RootLayout({
|
|||||||
<ClientProviders>
|
<ClientProviders>
|
||||||
{children}
|
{children}
|
||||||
</ClientProviders>
|
</ClientProviders>
|
||||||
|
<Toaster />
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
);
|
);
|
||||||
|
|||||||
@ -31,6 +31,9 @@ import {
|
|||||||
} from '@/components/ui/sidebar'
|
} from '@/components/ui/sidebar'
|
||||||
import { User } from "@/types/user"
|
import { User } from "@/types/user"
|
||||||
import { useUser } from "./user-context"
|
import { useUser } from "./user-context"
|
||||||
|
import Link from "next/link"
|
||||||
|
import { Badge } from "@/components/ui/badge"
|
||||||
|
import { IconDashboard } from "@tabler/icons-react"
|
||||||
|
|
||||||
export function NavUser({ user }: { user: User }) {
|
export function NavUser({ user }: { user: User }) {
|
||||||
const { isMobile } = useSidebar()
|
const { isMobile } = useSidebar()
|
||||||
@ -50,9 +53,19 @@ export function NavUser({ user }: { user: User }) {
|
|||||||
<AvatarFallback className="rounded-lg">CN</AvatarFallback>
|
<AvatarFallback className="rounded-lg">CN</AvatarFallback>
|
||||||
</Avatar>
|
</Avatar>
|
||||||
<div className="grid flex-1 text-left text-sm leading-tight">
|
<div className="grid flex-1 text-left text-sm leading-tight">
|
||||||
|
|
||||||
|
<div className="flex items-start gap-1">
|
||||||
<span className="truncate font-semibold">{user.name}</span>
|
<span className="truncate font-semibold">{user.name}</span>
|
||||||
<span className="truncate text-xs">{user.email}</span>
|
<Badge variant="secondary" className="text-[10px] w-fit">
|
||||||
|
{user.role}
|
||||||
|
</Badge>
|
||||||
</div>
|
</div>
|
||||||
|
<span className="text-muted-foreground truncate text-xs">
|
||||||
|
{user.email}
|
||||||
|
</span>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
|
||||||
<ChevronsUpDown className="ml-auto size-4" />
|
<ChevronsUpDown className="ml-auto size-4" />
|
||||||
</SidebarMenuButton>
|
</SidebarMenuButton>
|
||||||
</DropdownMenuTrigger>
|
</DropdownMenuTrigger>
|
||||||
@ -83,14 +96,25 @@ export function NavUser({ user }: { user: User }) {
|
|||||||
</DropdownMenuGroup>
|
</DropdownMenuGroup>
|
||||||
<DropdownMenuSeparator />
|
<DropdownMenuSeparator />
|
||||||
<DropdownMenuGroup>
|
<DropdownMenuGroup>
|
||||||
|
<Link href="/me">
|
||||||
<DropdownMenuItem>
|
<DropdownMenuItem>
|
||||||
<BadgeCheck />
|
<BadgeCheck />
|
||||||
Account
|
Account
|
||||||
</DropdownMenuItem>
|
</DropdownMenuItem>
|
||||||
|
</Link>
|
||||||
<DropdownMenuItem>
|
<DropdownMenuItem>
|
||||||
<CreditCard />
|
<CreditCard />
|
||||||
Billing
|
Billing
|
||||||
</DropdownMenuItem>
|
</DropdownMenuItem>
|
||||||
|
{user.role === 'ADMIN' ?
|
||||||
|
(
|
||||||
|
<Link href="/admin">
|
||||||
|
<DropdownMenuItem>
|
||||||
|
<IconDashboard />
|
||||||
|
Dashboard
|
||||||
|
</DropdownMenuItem>
|
||||||
|
</Link>
|
||||||
|
) : <></>}
|
||||||
<DropdownMenuItem>
|
<DropdownMenuItem>
|
||||||
<Bell />
|
<Bell />
|
||||||
Notifications
|
Notifications
|
||||||
|
|||||||
13
app/page.tsx
13
app/page.tsx
@ -35,17 +35,10 @@ export default function Page() {
|
|||||||
<AppSidebar />
|
<AppSidebar />
|
||||||
<SidebarInset>
|
<SidebarInset>
|
||||||
<header className="sticky top-0 flex h-16 shrink-0 items-center gap-2 border-b bg-background px-4">
|
<header className="sticky top-0 flex h-16 shrink-0 items-center gap-2 border-b bg-background px-4">
|
||||||
<SidebarTrigger className="-ml-1" />
|
{/* <SidebarTrigger className="-ml-1" /> */}
|
||||||
<Separator orientation="vertical" className="mr-2 h-4" />
|
{/* <Separator orientation="vertical" className="mr-2 h-4" /> */}
|
||||||
<div className="flex items-center gap-2 justify-between w-full">
|
<div className="flex items-center gap-2 justify-between w-full">
|
||||||
<Breadcrumb>
|
{/* <ThemeToggle /> */}
|
||||||
<BreadcrumbList>
|
|
||||||
<BreadcrumbItem>
|
|
||||||
<BreadcrumbPage>October 2024</BreadcrumbPage>
|
|
||||||
</BreadcrumbItem>
|
|
||||||
</BreadcrumbList>
|
|
||||||
</Breadcrumb>
|
|
||||||
<ThemeToggle />
|
|
||||||
</div>
|
</div>
|
||||||
</header>
|
</header>
|
||||||
<div className="relative h-full w-full flex flex-col">
|
<div className="relative h-full w-full flex flex-col">
|
||||||
|
|||||||
@ -26,6 +26,7 @@ const GET_USER_QUERY = gql`
|
|||||||
id
|
id
|
||||||
username
|
username
|
||||||
email
|
email
|
||||||
|
role
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
`
|
`
|
||||||
@ -108,7 +109,6 @@ export function UserProvider({ children }: UserProviderProps) {
|
|||||||
try {
|
try {
|
||||||
const token = localStorage.getItem(TOKEN_KEY)
|
const token = localStorage.getItem(TOKEN_KEY)
|
||||||
|
|
||||||
debugger
|
|
||||||
|
|
||||||
if (token && isTokenValid(token)) {
|
if (token && isTokenValid(token)) {
|
||||||
const payload = parseJWT(token)
|
const payload = parseJWT(token)
|
||||||
@ -126,6 +126,7 @@ export function UserProvider({ children }: UserProviderProps) {
|
|||||||
isAuthenticated: true,
|
isAuthenticated: true,
|
||||||
isLoading: false
|
isLoading: false
|
||||||
})
|
})
|
||||||
|
document.cookie = 'is_logged_in=true; path=/; max-age=3600'
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -137,6 +138,7 @@ export function UserProvider({ children }: UserProviderProps) {
|
|||||||
isAuthenticated: false,
|
isAuthenticated: false,
|
||||||
isLoading: false
|
isLoading: false
|
||||||
})
|
})
|
||||||
|
document.cookie = 'is_logged_in=false; path=/; max-age=3600'
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Auth initialization error:', error)
|
console.error('Auth initialization error:', error)
|
||||||
setAuthState({
|
setAuthState({
|
||||||
@ -145,6 +147,7 @@ export function UserProvider({ children }: UserProviderProps) {
|
|||||||
isAuthenticated: false,
|
isAuthenticated: false,
|
||||||
isLoading: false
|
isLoading: false
|
||||||
})
|
})
|
||||||
|
document.cookie = 'is_logged_in=false; path=/; max-age=3600'
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -152,7 +155,6 @@ export function UserProvider({ children }: UserProviderProps) {
|
|||||||
try {
|
try {
|
||||||
setAuthState(prev => ({ ...prev, isLoading: true }))
|
setAuthState(prev => ({ ...prev, isLoading: true }))
|
||||||
|
|
||||||
debugger
|
|
||||||
|
|
||||||
const response = await loginMutation({ variables: credentials })
|
const response = await loginMutation({ variables: credentials })
|
||||||
|
|
||||||
@ -166,7 +168,6 @@ export function UserProvider({ children }: UserProviderProps) {
|
|||||||
throw new Error('Invalid token received')
|
throw new Error('Invalid token received')
|
||||||
}
|
}
|
||||||
|
|
||||||
debugger
|
|
||||||
|
|
||||||
const payload = parseJWT(token)
|
const payload = parseJWT(token)
|
||||||
if (!payload) {
|
if (!payload) {
|
||||||
@ -307,6 +308,7 @@ export function UserProvider({ children }: UserProviderProps) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
localStorage.setItem(TOKEN_KEY, token)
|
localStorage.setItem(TOKEN_KEY, token)
|
||||||
|
document.cookie = 'is_logged_in=true; path=/; max-age=3600'
|
||||||
setAuthState({
|
setAuthState({
|
||||||
user,
|
user,
|
||||||
token,
|
token,
|
||||||
@ -318,6 +320,7 @@ export function UserProvider({ children }: UserProviderProps) {
|
|||||||
resetApolloCache()
|
resetApolloCache()
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Token refresh error:', error)
|
console.error('Token refresh error:', error)
|
||||||
|
document.cookie = 'is_logged_in=false; path=/; max-age=3600'
|
||||||
logout()
|
logout()
|
||||||
throw error
|
throw error
|
||||||
}
|
}
|
||||||
@ -329,7 +332,7 @@ export function UserProvider({ children }: UserProviderProps) {
|
|||||||
const updatedUser: User = {
|
const updatedUser: User = {
|
||||||
id: userData.currentUser.id,
|
id: userData.currentUser.id,
|
||||||
email: userData.currentUser.email,
|
email: userData.currentUser.email,
|
||||||
name: userData.currentUser.name,
|
name: userData.currentUser.username,
|
||||||
avatar: userData.currentUser.avatar,
|
avatar: userData.currentUser.avatar,
|
||||||
role: userData.currentUser.role
|
role: userData.currentUser.role
|
||||||
}
|
}
|
||||||
@ -353,6 +356,7 @@ export function UserProvider({ children }: UserProviderProps) {
|
|||||||
// 监听用户数据变化,定期更新用户信息
|
// 监听用户数据变化,定期更新用户信息
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (userData && authState.isAuthenticated) {
|
if (userData && authState.isAuthenticated) {
|
||||||
|
console.log('userData', userData)
|
||||||
updateUserInfo(userData)
|
updateUserInfo(userData)
|
||||||
}
|
}
|
||||||
}, [userData, authState.isAuthenticated])
|
}, [userData, authState.isAuthenticated])
|
||||||
|
|||||||
25
components/ui/sonner.tsx
Normal file
25
components/ui/sonner.tsx
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
"use client"
|
||||||
|
|
||||||
|
import { useTheme } from "next-themes"
|
||||||
|
import { Toaster as Sonner, ToasterProps } from "sonner"
|
||||||
|
|
||||||
|
const Toaster = ({ ...props }: ToasterProps) => {
|
||||||
|
const { theme = "system" } = useTheme()
|
||||||
|
|
||||||
|
return (
|
||||||
|
<Sonner
|
||||||
|
theme={theme as ToasterProps["theme"]}
|
||||||
|
className="toaster group"
|
||||||
|
style={
|
||||||
|
{
|
||||||
|
"--normal-bg": "var(--popover)",
|
||||||
|
"--normal-text": "var(--popover-foreground)",
|
||||||
|
"--normal-border": "var(--border)",
|
||||||
|
} as React.CSSProperties
|
||||||
|
}
|
||||||
|
{...props}
|
||||||
|
/>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
export { Toaster }
|
||||||
21
package-lock.json
generated
21
package-lock.json
generated
@ -43,6 +43,7 @@
|
|||||||
"lucide-react": "^0.525.0",
|
"lucide-react": "^0.525.0",
|
||||||
"maplibre-gl": "^5.6.1",
|
"maplibre-gl": "^5.6.1",
|
||||||
"next": "15.4.1",
|
"next": "15.4.1",
|
||||||
|
"next-themes": "^0.4.6",
|
||||||
"ol": "^10.6.1",
|
"ol": "^10.6.1",
|
||||||
"ol-mapbox-style": "^13.0.1",
|
"ol-mapbox-style": "^13.0.1",
|
||||||
"react": "19.1.0",
|
"react": "19.1.0",
|
||||||
@ -4253,6 +4254,16 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"node_modules/next-themes": {
|
||||||
|
"version": "0.4.6",
|
||||||
|
"resolved": "https://registry.npmjs.org/next-themes/-/next-themes-0.4.6.tgz",
|
||||||
|
"integrity": "sha512-pZvgD5L0IEvX5/9GWyHMf3m8BKiVQwsCMHfoFosXtXBMnaS0ZnIJ9ST4b4NqLVKDEm8QBxoNNGNaBv2JNF6XNA==",
|
||||||
|
"license": "MIT",
|
||||||
|
"peerDependencies": {
|
||||||
|
"react": "^16.8 || ^17 || ^18 || ^19 || ^19.0.0-rc",
|
||||||
|
"react-dom": "^16.8 || ^17 || ^18 || ^19 || ^19.0.0-rc"
|
||||||
|
}
|
||||||
|
},
|
||||||
"node_modules/next/node_modules/postcss": {
|
"node_modules/next/node_modules/postcss": {
|
||||||
"version": "8.4.31",
|
"version": "8.4.31",
|
||||||
"resolved": "https://registry.npmjs.org/postcss/-/postcss-8.4.31.tgz",
|
"resolved": "https://registry.npmjs.org/postcss/-/postcss-8.4.31.tgz",
|
||||||
@ -4851,7 +4862,7 @@
|
|||||||
},
|
},
|
||||||
"node_modules/sonner": {
|
"node_modules/sonner": {
|
||||||
"version": "2.0.6",
|
"version": "2.0.6",
|
||||||
"resolved": "http://mirrors.cloud.tencent.com/npm/sonner/-/sonner-2.0.6.tgz",
|
"resolved": "https://registry.npmjs.org/sonner/-/sonner-2.0.6.tgz",
|
||||||
"integrity": "sha512-yHFhk8T/DK3YxjFQXIrcHT1rGEeTLliVzWbO0xN8GberVun2RiBnxAjXAYpZrqwEVHBG9asI/Li8TAAhN9m59Q==",
|
"integrity": "sha512-yHFhk8T/DK3YxjFQXIrcHT1rGEeTLliVzWbO0xN8GberVun2RiBnxAjXAYpZrqwEVHBG9asI/Li8TAAhN9m59Q==",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"peerDependencies": {
|
"peerDependencies": {
|
||||||
@ -8107,6 +8118,12 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"next-themes": {
|
||||||
|
"version": "0.4.6",
|
||||||
|
"resolved": "https://registry.npmjs.org/next-themes/-/next-themes-0.4.6.tgz",
|
||||||
|
"integrity": "sha512-pZvgD5L0IEvX5/9GWyHMf3m8BKiVQwsCMHfoFosXtXBMnaS0ZnIJ9ST4b4NqLVKDEm8QBxoNNGNaBv2JNF6XNA==",
|
||||||
|
"requires": {}
|
||||||
|
},
|
||||||
"node-releases": {
|
"node-releases": {
|
||||||
"version": "2.0.19",
|
"version": "2.0.19",
|
||||||
"resolved": "https://registry.npmjs.org/node-releases/-/node-releases-2.0.19.tgz",
|
"resolved": "https://registry.npmjs.org/node-releases/-/node-releases-2.0.19.tgz",
|
||||||
@ -8489,7 +8506,7 @@
|
|||||||
},
|
},
|
||||||
"sonner": {
|
"sonner": {
|
||||||
"version": "2.0.6",
|
"version": "2.0.6",
|
||||||
"resolved": "http://mirrors.cloud.tencent.com/npm/sonner/-/sonner-2.0.6.tgz",
|
"resolved": "https://registry.npmjs.org/sonner/-/sonner-2.0.6.tgz",
|
||||||
"integrity": "sha512-yHFhk8T/DK3YxjFQXIrcHT1rGEeTLliVzWbO0xN8GberVun2RiBnxAjXAYpZrqwEVHBG9asI/Li8TAAhN9m59Q==",
|
"integrity": "sha512-yHFhk8T/DK3YxjFQXIrcHT1rGEeTLliVzWbO0xN8GberVun2RiBnxAjXAYpZrqwEVHBG9asI/Li8TAAhN9m59Q==",
|
||||||
"requires": {}
|
"requires": {}
|
||||||
},
|
},
|
||||||
|
|||||||
@ -44,6 +44,7 @@
|
|||||||
"lucide-react": "^0.525.0",
|
"lucide-react": "^0.525.0",
|
||||||
"maplibre-gl": "^5.6.1",
|
"maplibre-gl": "^5.6.1",
|
||||||
"next": "15.4.1",
|
"next": "15.4.1",
|
||||||
|
"next-themes": "^0.4.6",
|
||||||
"ol": "^10.6.1",
|
"ol": "^10.6.1",
|
||||||
"ol-mapbox-style": "^13.0.1",
|
"ol-mapbox-style": "^13.0.1",
|
||||||
"react": "19.1.0",
|
"react": "19.1.0",
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user