Files
kotosoft-id/kotosoft-id-frontend/app/me/items/positions.tsx
T
2026-05-04 11:14:30 +03:00

73 lines
2.3 KiB
TypeScript

"use client"
import { useEffect, useState } from "react"
import { useLocale, useTranslations } from "next-intl"
import type { Position } from "@/types"
import { Item, ItemContent, ItemDescription, ItemTitle } from "@/components/ui/item"
import { Badge } from "@/components/ui/badge"
import { Spinner } from "@/components/ui/spinner"
import { fetchMyPositions } from "@/actions/positions"
const positionsCache: Record<string, Position[]> = {}
export default function Positions() {
const t = useTranslations("MePage.items.positions")
const locale = useLocale()
const [positions, setPositions] = useState<Position[] | null>(
() => positionsCache[locale] ?? null
)
const [loading, setLoading] = useState(!positionsCache[locale])
useEffect(() => {
if (positionsCache[locale]) return
setLoading(true)
fetchMyPositions(locale)
.then((data) => {
positionsCache[locale] = data
setPositions(data)
})
.finally(() => setLoading(false))
}, [locale])
return (
<div className="w-full lg:max-w-sm flex flex-col gap-4">
<h1 className="text-xl">{t("title")}</h1>
{loading && (
<div className="flex items-center gap-2 text-sm text-muted-foreground">
<Spinner />
<span>{t("loading")}</span>
</div>
)}
{!loading && (
<div className="space-y-4">
{positions && positions.length > 0 ? (
positions.map((position) => (
<Item key={position.code}>
<ItemContent>
<ItemTitle>{position.name}</ItemTitle>
{position.description && (
<ItemDescription>{position.description}</ItemDescription>
)}
{position.permissions.length > 0 && (
<div className="flex flex-wrap gap-1 mt-2">
{position.permissions.map((perm) => (
<Badge key={perm.code} variant="secondary" className="text-xs">
{perm.description}
</Badge>
))}
</div>
)}
</ItemContent>
</Item>
))
) : (
<p className="text-sm text-muted-foreground">{t("empty")}</p>
)}
</div>
)}
</div>
)
}