feat(reports): adiciona opcao Todas as empresas no relatorio por empresa
- Frontend: usa usePersistentCompanyFilter para persistir selecao - Frontend: adiciona opcao "Todas as empresas" como primeira opcao - Backend: torna companyId opcional na query companyOverview - Backend: usa resolveScopedCompanyId para scoping de gestores 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
parent
14480df9f3
commit
8a237a820d
2 changed files with 59 additions and 39 deletions
|
|
@ -1,12 +1,13 @@
|
|||
"use client"
|
||||
|
||||
import { useEffect, useMemo, useState } from "react"
|
||||
import { useMemo, useState } from "react"
|
||||
import Link from "next/link"
|
||||
import { useQuery } from "convex/react"
|
||||
import { api } from "@/convex/_generated/api"
|
||||
import type { Id } from "@/convex/_generated/dataModel"
|
||||
import { DEFAULT_TENANT_ID } from "@/lib/constants"
|
||||
import { useAuth } from "@/lib/auth-client"
|
||||
import { usePersistentCompanyFilter } from "@/lib/use-company-filter"
|
||||
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card"
|
||||
import { Skeleton } from "@/components/ui/skeleton"
|
||||
import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/components/ui/select"
|
||||
|
|
@ -59,7 +60,7 @@ const MACHINE_STATUS_CONFIG = {
|
|||
export function CompanyReport() {
|
||||
const { session, convexUserId, isStaff } = useAuth()
|
||||
const tenantId = session?.user.tenantId ?? DEFAULT_TENANT_ID
|
||||
const [selectedCompany, setSelectedCompany] = useState<string>("")
|
||||
const [selectedCompany, setSelectedCompany] = usePersistentCompanyFilter("all")
|
||||
const [timeRange, setTimeRange] = useState<"7d" | "30d" | "90d">("30d")
|
||||
|
||||
const companies = useQuery(
|
||||
|
|
@ -67,28 +68,28 @@ export function CompanyReport() {
|
|||
isStaff && convexUserId ? { tenantId, viewerId: convexUserId as Id<"users"> } : "skip"
|
||||
) as CompanyRecord[] | undefined
|
||||
|
||||
const companyOptions = useMemo<SearchableComboboxOption[]>(
|
||||
() =>
|
||||
(companies ?? []).map((company) => ({
|
||||
const companyOptions = useMemo<SearchableComboboxOption[]>(() => {
|
||||
const base: SearchableComboboxOption[] = [{ value: "all", label: "Todas as empresas" }]
|
||||
if (!companies || companies.length === 0) {
|
||||
return base
|
||||
}
|
||||
const sorted = [...companies].sort((a, b) => a.name.localeCompare(b.name, "pt-BR"))
|
||||
return [
|
||||
base[0],
|
||||
...sorted.map((company) => ({
|
||||
value: company.id as string,
|
||||
label: company.name,
|
||||
})),
|
||||
[companies]
|
||||
)
|
||||
|
||||
useEffect(() => {
|
||||
if (!selectedCompany && companyOptions.length > 0) {
|
||||
setSelectedCompany(companyOptions[0]?.value ?? "")
|
||||
}
|
||||
}, [companyOptions, selectedCompany])
|
||||
]
|
||||
}, [companies])
|
||||
|
||||
const report = useQuery(
|
||||
api.reports.companyOverview,
|
||||
selectedCompany && convexUserId && isStaff
|
||||
convexUserId && isStaff
|
||||
? {
|
||||
tenantId,
|
||||
viewerId: convexUserId as Id<"users">,
|
||||
companyId: selectedCompany as Id<"companies">,
|
||||
companyId: selectedCompany === "all" ? undefined : (selectedCompany as Id<"companies">),
|
||||
range: timeRange,
|
||||
}
|
||||
: "skip"
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue