feat: improve requester combobox and admin cleanup flows
This commit is contained in:
parent
788f6928a1
commit
37c32149a6
13 changed files with 923 additions and 180 deletions
|
|
@ -76,12 +76,16 @@ type CompanyOption = {
|
|||
name: string
|
||||
}
|
||||
|
||||
type AdminUsersTab = "team" | "users" | "invites"
|
||||
|
||||
type Props = {
|
||||
initialUsers: AdminUser[]
|
||||
initialInvites: AdminInvite[]
|
||||
roleOptions: readonly AdminRole[]
|
||||
defaultTenantId: string
|
||||
viewerRole: string
|
||||
visibleTabs?: readonly AdminUsersTab[]
|
||||
defaultTab?: AdminUsersTab
|
||||
}
|
||||
|
||||
const ROLE_LABELS: Record<string, string> = {
|
||||
|
|
@ -111,6 +115,9 @@ function machinePersonaBadgeVariant(persona: string | null | undefined) {
|
|||
return "outline" as const
|
||||
}
|
||||
|
||||
const ALL_TABS: AdminUsersTab[] = ["team", "users", "invites"]
|
||||
const DEFAULT_KEEP_EMAILS = ["renan.pac@paulicon.com.br"]
|
||||
|
||||
// Tenant removido da UI (sem exibição)
|
||||
|
||||
function formatDate(dateIso: string) {
|
||||
|
|
@ -170,8 +177,16 @@ function isRestrictedRole(role?: string | null) {
|
|||
return normalized === "admin" || normalized === "agent"
|
||||
}
|
||||
|
||||
export function AdminUsersManager({ initialUsers, initialInvites, roleOptions, defaultTenantId, viewerRole }: Props) {
|
||||
const { convexUserId } = useAuth()
|
||||
export function AdminUsersManager({
|
||||
initialUsers,
|
||||
initialInvites,
|
||||
roleOptions,
|
||||
defaultTenantId,
|
||||
viewerRole,
|
||||
visibleTabs,
|
||||
defaultTab,
|
||||
}: Props) {
|
||||
const { convexUserId, session } = useAuth()
|
||||
const [users, setUsers] = useState<AdminUser[]>(initialUsers)
|
||||
const [invites, setInvites] = useState<AdminInvite[]>(initialInvites)
|
||||
const [companies, setCompanies] = useState<CompanyOption[]>([])
|
||||
|
|
@ -215,6 +230,33 @@ export function AdminUsersManager({ initialUsers, initialInvites, roleOptions, d
|
|||
)
|
||||
const viewerRoleNormalized = viewerRole?.toLowerCase?.() ?? "agent"
|
||||
const viewerIsAdmin = viewerRoleNormalized === "admin"
|
||||
const viewerEmail = session?.user?.email?.toLowerCase() ?? null
|
||||
|
||||
const tabs = useMemo<AdminUsersTab[]>(() => {
|
||||
const source = visibleTabs && visibleTabs.length > 0 ? Array.from(new Set(visibleTabs)) : ALL_TABS
|
||||
return source.filter((value): value is AdminUsersTab => ALL_TABS.includes(value))
|
||||
}, [visibleTabs])
|
||||
|
||||
const initialTabValue = useMemo<AdminUsersTab>(() => {
|
||||
if (tabs.length === 0) {
|
||||
return "team"
|
||||
}
|
||||
if (defaultTab && tabs.includes(defaultTab)) {
|
||||
return defaultTab
|
||||
}
|
||||
if (tabs.includes("team")) {
|
||||
return "team"
|
||||
}
|
||||
return tabs[0]
|
||||
}, [tabs, defaultTab])
|
||||
|
||||
const [tab, setTab] = useState<AdminUsersTab>(initialTabValue)
|
||||
|
||||
useEffect(() => {
|
||||
if (!tabs.includes(tab)) {
|
||||
setTab(tabs[0] ?? "team")
|
||||
}
|
||||
}, [tabs, tab])
|
||||
const canManageUser = useCallback((role?: string | null) => viewerIsAdmin || !isRestrictedRole(role), [viewerIsAdmin])
|
||||
const canManageInvite = useCallback((role: RoleOption) => viewerIsAdmin || !["admin", "agent"].includes(role), [viewerIsAdmin])
|
||||
|
||||
|
|
@ -230,6 +272,22 @@ export function AdminUsersManager({ initialUsers, initialInvites, roleOptions, d
|
|||
})
|
||||
return Array.from(unique)
|
||||
}, [normalizedRoles, viewerIsAdmin])
|
||||
|
||||
const buildKeepEmailSet = useCallback(() => {
|
||||
const keep = new Set<string>()
|
||||
DEFAULT_KEEP_EMAILS.forEach((email) => keep.add(email.toLowerCase()))
|
||||
if (viewerEmail) {
|
||||
keep.add(viewerEmail)
|
||||
}
|
||||
cleanupKeepEmails
|
||||
.split(",")
|
||||
.map((entry) => entry.trim().toLowerCase())
|
||||
.filter(Boolean)
|
||||
.forEach((email) => keep.add(email))
|
||||
return keep
|
||||
}, [cleanupKeepEmails, viewerEmail])
|
||||
|
||||
const cleanupPreview = useMemo(() => Array.from(buildKeepEmailSet()).join(", "), [buildKeepEmailSet])
|
||||
// Split users: team (admin/agent) and people (manager/collaborator); exclude machines
|
||||
const teamUsers = useMemo(
|
||||
() => users.filter((user) => user.role !== "machine" && ["admin", "agent"].includes(coerceRole(user.role))),
|
||||
|
|
@ -269,6 +327,9 @@ export function AdminUsersManager({ initialUsers, initialInvites, roleOptions, d
|
|||
})
|
||||
const [isCreatingUser, setIsCreatingUser] = useState(false)
|
||||
const [createPassword, setCreatePassword] = useState<string | null>(null)
|
||||
const [cleanupDialogOpen, setCleanupDialogOpen] = useState(false)
|
||||
const [cleanupKeepEmails, setCleanupKeepEmails] = useState(DEFAULT_KEEP_EMAILS.join(", "))
|
||||
const [cleanupPending, setCleanupPending] = useState(false)
|
||||
|
||||
// Máquinas (para listar vínculos por usuário)
|
||||
type MachinesListItem = {
|
||||
|
|
@ -619,6 +680,46 @@ export function AdminUsersManager({ initialUsers, initialInvites, roleOptions, d
|
|||
}
|
||||
}
|
||||
|
||||
const handleCleanupConfirm = useCallback(async () => {
|
||||
if (cleanupPending) return
|
||||
const keepSet = buildKeepEmailSet()
|
||||
setCleanupPending(true)
|
||||
toast.loading("Limpando dados antigos...", { id: "cleanup-users" })
|
||||
try {
|
||||
const response = await fetch("/api/admin/users/cleanup", {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ keepEmails: Array.from(keepSet) }),
|
||||
})
|
||||
if (!response.ok) {
|
||||
const data = await response.json().catch(() => ({}))
|
||||
throw new Error(data.error ?? "Não foi possível remover os dados antigos.")
|
||||
}
|
||||
const summary = (await response.json()) as {
|
||||
removedPortalUserIds: string[]
|
||||
removedPortalEmails: string[]
|
||||
removedConvexUserIds: string[]
|
||||
removedTicketIds: string[]
|
||||
keepEmails: string[]
|
||||
}
|
||||
setUsers((previous) => previous.filter((user) => !summary.removedPortalUserIds.includes(user.id)))
|
||||
setUsersSelection((previous) => {
|
||||
if (previous.size === 0) return previous
|
||||
const next = new Set(previous)
|
||||
summary.removedPortalUserIds.forEach((id) => next.delete(id))
|
||||
return next
|
||||
})
|
||||
setCleanupKeepEmails(Array.from(keepSet).join(", "))
|
||||
toast.success("Dados de teste removidos.", { id: "cleanup-users" })
|
||||
setCleanupDialogOpen(false)
|
||||
} catch (error) {
|
||||
const message = error instanceof Error ? error.message : "Não foi possível remover os dados antigos."
|
||||
toast.error(message, { id: "cleanup-users" })
|
||||
} finally {
|
||||
setCleanupPending(false)
|
||||
}
|
||||
}, [buildKeepEmailSet, cleanupPending, setUsersSelection])
|
||||
|
||||
async function handleAssignCompany(event: React.FormEvent<HTMLFormElement>) {
|
||||
event.preventDefault()
|
||||
const normalizedEmail = linkEmail.trim().toLowerCase()
|
||||
|
|
@ -1002,23 +1103,48 @@ async function handleDeleteUser() {
|
|||
|
||||
return (
|
||||
<>
|
||||
<Tabs defaultValue="team" className="w-full">
|
||||
<Tabs value={tab} onValueChange={(value) => setTab(value as AdminUsersTab)} className="w-full">
|
||||
<TabsList className="h-12 w-full justify-start rounded-xl bg-slate-100 p-1">
|
||||
<TabsTrigger value="team" className="rounded-lg">Equipe</TabsTrigger>
|
||||
<TabsTrigger value="users" className="rounded-lg">Usuários</TabsTrigger>
|
||||
<TabsTrigger value="invites" className="rounded-lg">Convites</TabsTrigger>
|
||||
{tabs.includes("team") ? (
|
||||
<TabsTrigger value="team" className="rounded-lg">
|
||||
Equipe
|
||||
</TabsTrigger>
|
||||
) : null}
|
||||
{tabs.includes("users") ? (
|
||||
<TabsTrigger value="users" className="rounded-lg">
|
||||
Usuários
|
||||
</TabsTrigger>
|
||||
) : null}
|
||||
{tabs.includes("invites") ? (
|
||||
<TabsTrigger value="invites" className="rounded-lg">
|
||||
Convites
|
||||
</TabsTrigger>
|
||||
) : null}
|
||||
</TabsList>
|
||||
|
||||
{tabs.includes("team") ? (
|
||||
<TabsContent value="team" className="mt-6 space-y-6">
|
||||
<div className="flex flex-col gap-4 rounded-xl border border-slate-200 bg-white p-4 shadow-sm sm:flex-row sm:items-center sm:justify-between">
|
||||
<div className="space-y-1">
|
||||
<p className="text-sm font-semibold text-neutral-900">Equipe cadastrada</p>
|
||||
<p className="text-xs text-neutral-500">{filteredTeamUsers.length} {filteredTeamUsers.length === 1 ? "membro" : "membros"}</p>
|
||||
</div>
|
||||
<Button onClick={handleOpenCreateUser} size="sm" className="gap-2 self-start sm:self-auto">
|
||||
<IconUserPlus className="size-4" />
|
||||
Novo usuário
|
||||
</Button>
|
||||
<div className="flex flex-col gap-2 sm:flex-row sm:items-center sm:gap-3">
|
||||
<Button onClick={handleOpenCreateUser} size="sm" className="gap-2 self-start sm:self-auto">
|
||||
<IconUserPlus className="size-4" />
|
||||
Novo usuário
|
||||
</Button>
|
||||
<Button
|
||||
type="button"
|
||||
variant="ghost"
|
||||
size="sm"
|
||||
className="gap-2 self-start text-amber-600 hover:bg-amber-50 hover:text-amber-700 sm:self-auto"
|
||||
onClick={() => setCleanupDialogOpen(true)}
|
||||
>
|
||||
<IconAlertTriangle className="size-4" />
|
||||
Limpar dados antigos
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
<div className="rounded-xl border border-slate-200 bg-white p-4 shadow-sm md:grid md:grid-cols-[minmax(0,1fr)_auto_auto_auto_auto] md:items-center md:gap-3">
|
||||
<div className="relative w-full md:max-w-sm">
|
||||
|
|
@ -1291,7 +1417,9 @@ async function handleDeleteUser() {
|
|||
</CardContent>
|
||||
</Card>
|
||||
</TabsContent>
|
||||
) : null}
|
||||
|
||||
{tabs.includes("users") ? (
|
||||
<TabsContent value="users" className="mt-6 space-y-6">
|
||||
<div className="flex flex-col gap-4 rounded-xl border border-slate-200 bg-white p-4 shadow-sm sm:flex-row sm:items-center sm:justify-between">
|
||||
<div className="space-y-1">
|
||||
|
|
@ -1556,7 +1684,9 @@ async function handleDeleteUser() {
|
|||
</CardContent>
|
||||
</Card>
|
||||
</TabsContent>
|
||||
) : null}
|
||||
|
||||
{tabs.includes("invites") ? (
|
||||
<TabsContent value="invites" className="mt-6 space-y-6">
|
||||
<Card>
|
||||
<CardHeader>
|
||||
|
|
@ -1813,7 +1943,51 @@ async function handleDeleteUser() {
|
|||
</CardContent>
|
||||
</Card>
|
||||
</TabsContent>
|
||||
) : null}
|
||||
</Tabs>
|
||||
<Dialog
|
||||
open={cleanupDialogOpen}
|
||||
onOpenChange={(open) => {
|
||||
if (!open && cleanupPending) return
|
||||
setCleanupDialogOpen(open)
|
||||
}}
|
||||
>
|
||||
<DialogContent>
|
||||
<DialogHeader>
|
||||
<DialogTitle>Remover dados de teste</DialogTitle>
|
||||
<DialogDescription>
|
||||
Remove usuários, tickets e acessos que não estiverem na lista de e-mails preservada. Esta ação não pode ser desfeita.
|
||||
</DialogDescription>
|
||||
</DialogHeader>
|
||||
<div className="space-y-3">
|
||||
<div className="space-y-1">
|
||||
<Label htmlFor="cleanup-keep-emails">E-mails a preservar</Label>
|
||||
<Input
|
||||
id="cleanup-keep-emails"
|
||||
value={cleanupKeepEmails}
|
||||
onChange={(event) => setCleanupKeepEmails(event.target.value)}
|
||||
placeholder="email@empresa.com, outro@dominio.com"
|
||||
/>
|
||||
<p className="text-xs text-muted-foreground">
|
||||
Sempre preservamos automaticamente: {viewerEmail ?? "seu e-mail atual"} e{" "}
|
||||
{DEFAULT_KEEP_EMAILS.join(", ")}.
|
||||
</p>
|
||||
</div>
|
||||
<div className="rounded-lg border border-dashed border-slate-200 bg-slate-50 px-3 py-2">
|
||||
<p className="text-xs font-semibold text-neutral-700">Lista final preservada</p>
|
||||
<p className="text-xs text-neutral-500 break-all">{cleanupPreview}</p>
|
||||
</div>
|
||||
</div>
|
||||
<DialogFooter>
|
||||
<Button variant="outline" onClick={() => setCleanupDialogOpen(false)} disabled={cleanupPending}>
|
||||
Cancelar
|
||||
</Button>
|
||||
<Button variant="destructive" onClick={handleCleanupConfirm} disabled={cleanupPending}>
|
||||
{cleanupPending ? "Removendo..." : "Remover dados"}
|
||||
</Button>
|
||||
</DialogFooter>
|
||||
</DialogContent>
|
||||
</Dialog>
|
||||
<Dialog
|
||||
open={createDialogOpen}
|
||||
onOpenChange={(open) => {
|
||||
|
|
|
|||
|
|
@ -30,7 +30,6 @@ import {
|
|||
COMPANY_CONTRACT_TYPES,
|
||||
COMPANY_LOCATION_TYPES,
|
||||
COMPANY_STATE_REGISTRATION_TYPES,
|
||||
SLA_SEVERITY_LEVELS,
|
||||
companyFormSchema,
|
||||
type CompanyBusinessHours,
|
||||
type CompanyContract,
|
||||
|
|
@ -119,14 +118,6 @@ const DAY_OPTIONS = [
|
|||
] as const
|
||||
|
||||
const EMPTY_SELECT_VALUE = "__empty__"
|
||||
|
||||
const SLA_LEVEL_LABEL: Record<(typeof SLA_SEVERITY_LEVELS)[number], string> = {
|
||||
P1: "P1",
|
||||
P2: "P2",
|
||||
P3: "P3",
|
||||
P4: "P4",
|
||||
}
|
||||
|
||||
function createId(prefix: string) {
|
||||
if (typeof crypto !== "undefined" && typeof crypto.randomUUID === "function") {
|
||||
return `${prefix}-${crypto.randomUUID()}`
|
||||
|
|
@ -763,7 +754,6 @@ function TableView({ companies, machineCountsBySlug, onEdit, onDelete }: TableVi
|
|||
<TableHead>Empresa</TableHead>
|
||||
<TableHead>Contratos ativos</TableHead>
|
||||
<TableHead>Contatos</TableHead>
|
||||
<TableHead>SLA</TableHead>
|
||||
<TableHead>Máquinas</TableHead>
|
||||
<TableHead className="text-right">Ações</TableHead>
|
||||
</TableRow>
|
||||
|
|
@ -771,7 +761,7 @@ function TableView({ companies, machineCountsBySlug, onEdit, onDelete }: TableVi
|
|||
<TableBody>
|
||||
{companies.length === 0 ? (
|
||||
<TableRow>
|
||||
<TableCell colSpan={6}>
|
||||
<TableCell colSpan={5}>
|
||||
<div className="flex flex-col items-center gap-2 py-6 text-sm text-muted-foreground">
|
||||
<IconBuildingSkyscraper className="size-6 text-border" />
|
||||
<p>Nenhuma empresa encontrada com os filtros atuais.</p>
|
||||
|
|
@ -852,27 +842,6 @@ function TableView({ companies, machineCountsBySlug, onEdit, onDelete }: TableVi
|
|||
</ul>
|
||||
)}
|
||||
</TableCell>
|
||||
<TableCell className="align-middle text-xs">
|
||||
{company.sla ? (
|
||||
<div className="space-y-1 rounded-md border border-border/60 px-2 py-1">
|
||||
<p className="font-semibold uppercase text-muted-foreground">
|
||||
{company.sla.calendar.toUpperCase()}
|
||||
</p>
|
||||
<ul className="space-y-1">
|
||||
{company.sla.severities.map((severity) => (
|
||||
<li key={severity.level} className="flex items-center justify-between text-[11px]">
|
||||
<span>{SLA_LEVEL_LABEL[severity.level]}</span>
|
||||
<span>
|
||||
{severity.responseMinutes}m · {severity.resolutionMinutes}m
|
||||
</span>
|
||||
</li>
|
||||
))}
|
||||
</ul>
|
||||
</div>
|
||||
) : (
|
||||
<p className="text-muted-foreground">Sem SLA cadastrado.</p>
|
||||
)}
|
||||
</TableCell>
|
||||
<TableCell className="align-middle text-sm">
|
||||
<Badge variant="outline">{machineCount}</Badge>
|
||||
</TableCell>
|
||||
|
|
|
|||
|
|
@ -266,65 +266,67 @@ function AccountsTable({ initialAccounts }: { initialAccounts: AdminAccount[] })
|
|||
</div>
|
||||
|
||||
<div className="overflow-x-auto">
|
||||
<Table className="min-w-[64rem]">
|
||||
<TableHeader>
|
||||
<TableRow>
|
||||
<TableHead>Usuário</TableHead>
|
||||
<TableHead>Empresa</TableHead>
|
||||
<TableHead>Papel</TableHead>
|
||||
<TableHead>Último acesso</TableHead>
|
||||
<TableHead className="text-right">Selecionar</TableHead>
|
||||
</TableRow>
|
||||
</TableHeader>
|
||||
<TableBody>
|
||||
{filteredAccounts.length === 0 ? (
|
||||
<div className="min-w-[64rem] overflow-hidden rounded-lg border">
|
||||
<Table className="w-full table-fixed text-sm">
|
||||
<TableHeader className="bg-muted">
|
||||
<TableRow>
|
||||
<TableCell colSpan={5} className="text-center text-sm text-muted-foreground">
|
||||
Nenhum usuário encontrado.
|
||||
</TableCell>
|
||||
<TableHead>Usuário</TableHead>
|
||||
<TableHead>Empresa</TableHead>
|
||||
<TableHead>Papel</TableHead>
|
||||
<TableHead>Último acesso</TableHead>
|
||||
<TableHead className="text-right">Selecionar</TableHead>
|
||||
</TableRow>
|
||||
) : (
|
||||
filteredAccounts.map((account) => {
|
||||
const initials = account.name
|
||||
.split(" ")
|
||||
.filter(Boolean)
|
||||
.slice(0, 2)
|
||||
.map((part) => part.charAt(0).toUpperCase())
|
||||
.join("")
|
||||
return (
|
||||
<TableRow key={account.id} className="hover:bg-muted/40">
|
||||
<TableCell>
|
||||
<div className="flex items-center gap-3">
|
||||
<Avatar className="size-9 border border-border/60">
|
||||
<AvatarFallback>{initials || account.email.charAt(0).toUpperCase()}</AvatarFallback>
|
||||
</Avatar>
|
||||
<div className="min-w-0 space-y-1">
|
||||
<p className="font-semibold text-foreground">{account.name}</p>
|
||||
<p className="text-xs text-muted-foreground">{account.email}</p>
|
||||
</TableHeader>
|
||||
<TableBody>
|
||||
{filteredAccounts.length === 0 ? (
|
||||
<TableRow>
|
||||
<TableCell colSpan={5} className="text-center text-sm text-muted-foreground">
|
||||
Nenhum usuário encontrado.
|
||||
</TableCell>
|
||||
</TableRow>
|
||||
) : (
|
||||
filteredAccounts.map((account) => {
|
||||
const initials = account.name
|
||||
.split(" ")
|
||||
.filter(Boolean)
|
||||
.slice(0, 2)
|
||||
.map((part) => part.charAt(0).toUpperCase())
|
||||
.join("")
|
||||
return (
|
||||
<TableRow key={account.id} className="hover:bg-muted/40">
|
||||
<TableCell>
|
||||
<div className="flex items-center gap-3">
|
||||
<Avatar className="size-9 border border-border/60">
|
||||
<AvatarFallback>{initials || account.email.charAt(0).toUpperCase()}</AvatarFallback>
|
||||
</Avatar>
|
||||
<div className="min-w-0 space-y-1">
|
||||
<p className="font-semibold text-foreground">{account.name}</p>
|
||||
<p className="text-xs text-muted-foreground">{account.email}</p>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</TableCell>
|
||||
<TableCell className="text-sm text-muted-foreground">
|
||||
{account.companyName ?? <span className="italic text-muted-foreground/70">Sem empresa</span>}
|
||||
</TableCell>
|
||||
<TableCell className="text-sm">
|
||||
<Badge variant="secondary">{ROLE_LABEL[account.role]}</Badge>
|
||||
</TableCell>
|
||||
<TableCell className="text-xs text-muted-foreground">{formatDate(account.lastSeenAt)}</TableCell>
|
||||
<TableCell className="text-right">
|
||||
<Checkbox
|
||||
checked={rowSelection[account.id] ?? false}
|
||||
onCheckedChange={(checked) =>
|
||||
setRowSelection((prev) => ({ ...prev, [account.id]: Boolean(checked) }))
|
||||
}
|
||||
/>
|
||||
</TableCell>
|
||||
</TableRow>
|
||||
)
|
||||
})
|
||||
)}
|
||||
</TableBody>
|
||||
</Table>
|
||||
</TableCell>
|
||||
<TableCell className="text-sm text-muted-foreground">
|
||||
{account.companyName ?? <span className="italic text-muted-foreground/70">Sem empresa</span>}
|
||||
</TableCell>
|
||||
<TableCell className="text-sm">
|
||||
<Badge variant="secondary">{ROLE_LABEL[account.role]}</Badge>
|
||||
</TableCell>
|
||||
<TableCell className="text-xs text-muted-foreground">{formatDate(account.lastSeenAt)}</TableCell>
|
||||
<TableCell className="text-right">
|
||||
<Checkbox
|
||||
checked={rowSelection[account.id] ?? false}
|
||||
onCheckedChange={(checked) =>
|
||||
setRowSelection((prev) => ({ ...prev, [account.id]: Boolean(checked) }))
|
||||
}
|
||||
/>
|
||||
</TableCell>
|
||||
</TableRow>
|
||||
)
|
||||
})
|
||||
)}
|
||||
</TableBody>
|
||||
</Table>
|
||||
</div>
|
||||
</div>
|
||||
</CardContent>
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue