feat: show invoices on team/org (#27120)
Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
This commit is contained in:
1 parent
29ec2b9cb6
commit
4f0a1d0f9b
9 files changed
+544
-4
No files matched your search
@@ -10,8 +10,8 @@ import { showToast } from "@calcom/ui/components/toast";
|
||||
import { usePathname } from "next/navigation";
|
||||
import { useSession } from "next-auth/react";
|
||||
import { useState } from "react";
|
||||
|
||||
import BillingCredits from "~/settings/billing/components/BillingCredits";
|
||||
import { InvoicesTable } from "~/settings/billing/components/InvoicesTable";
|
||||
|
||||
interface CtaRowProps {
|
||||
title: string;
|
||||
@@ -146,6 +146,7 @@ const BillingView = () => {
|
||||
</div>
|
||||
</div>
|
||||
<BillingCredits />
|
||||
<InvoicesTable />
|
||||
|
||||
<Dialog open={showSkipTrialDialog} onOpenChange={setShowSkipTrialDialog}>
|
||||
<DialogContent>
|
||||
|
||||
@@ -0,0 +1,236 @@
|
||||
"use client";
|
||||
|
||||
import { keepPreviousData } from "@tanstack/react-query";
|
||||
import { usePathname } from "next/navigation";
|
||||
import { useSession } from "next-auth/react";
|
||||
import { useState, useEffect, useRef } from "react";
|
||||
|
||||
import dayjs from "@calcom/dayjs";
|
||||
import {
|
||||
DataTableProvider,
|
||||
ColumnFilterType,
|
||||
useFilterValue,
|
||||
ZDateRangeFilterValue,
|
||||
type FilterableColumn,
|
||||
} from "@calcom/features/data-table";
|
||||
import { useSegments } from "@calcom/features/data-table/hooks/useSegments";
|
||||
import { useLocale } from "@calcom/lib/hooks/useLocale";
|
||||
import { trpc } from "@calcom/trpc/react";
|
||||
import type { FilterType } from "@calcom/types/data-table";
|
||||
import { Badge } from "@calcom/ui/components/badge";
|
||||
import { Button } from "@calcom/ui/components/button";
|
||||
import { PanelCard } from "@calcom/ui/components/card";
|
||||
import { Icon } from "@calcom/ui/components/icon";
|
||||
import {
|
||||
Table,
|
||||
TableBody,
|
||||
TableCell,
|
||||
TableHead,
|
||||
TableHeader,
|
||||
TableRow,
|
||||
} from "@calcom/ui/components/table/TableNew";
|
||||
|
||||
import { DateRangeFilter } from "~/data-table/components";
|
||||
|
||||
import { InvoicesTableSkeleton } from "./InvoicesTableSkeleton";
|
||||
|
||||
const DEFAULT_PAGE_SIZE = 10;
|
||||
|
||||
const createdAtColumn: Extract<FilterableColumn, { type: Extract<FilterType, "dr"> }> = {
|
||||
id: "createdAt",
|
||||
title: "Date",
|
||||
type: ColumnFilterType.DATE_RANGE,
|
||||
};
|
||||
|
||||
function formatCurrency(amount: number, currency: string) {
|
||||
return new Intl.NumberFormat(undefined, {
|
||||
style: "currency",
|
||||
currency: currency.toUpperCase(),
|
||||
}).format(amount / 100);
|
||||
}
|
||||
|
||||
function getStatusBadgeColor(status: string | null): "green" | "orange" | "gray" | "red" {
|
||||
switch (status) {
|
||||
case "paid":
|
||||
return "green";
|
||||
case "open":
|
||||
return "orange";
|
||||
case "draft":
|
||||
return "gray";
|
||||
case "void":
|
||||
case "uncollectible":
|
||||
return "red";
|
||||
default:
|
||||
return "gray";
|
||||
}
|
||||
}
|
||||
|
||||
export function InvoicesTable() {
|
||||
const pathname = usePathname();
|
||||
|
||||
if (!pathname) return null;
|
||||
|
||||
return (
|
||||
<DataTableProvider tableIdentifier={`${pathname}-invoices`} useSegments={useSegments}>
|
||||
<InvoicesTableContent />
|
||||
</DataTableProvider>
|
||||
);
|
||||
}
|
||||
|
||||
function InvoicesTableContent() {
|
||||
const { t } = useLocale();
|
||||
const pathname = usePathname();
|
||||
const session = useSession();
|
||||
const [cursors, setCursors] = useState<(string | null)[]>([null]);
|
||||
const [currentPage, setCurrentPage] = useState(0);
|
||||
|
||||
const dateRangeFilter = useFilterValue("createdAt", ZDateRangeFilterValue)?.data;
|
||||
|
||||
// Derive teamId from context (same logic as billing-view.tsx)
|
||||
const getTeamIdFromContext = () => {
|
||||
if (!pathname) return null;
|
||||
if (pathname.includes("/teams/") && pathname.includes("/billing")) {
|
||||
const teamIdMatch = pathname.match(/\/teams\/(\d+)\/billing/);
|
||||
return teamIdMatch ? parseInt(teamIdMatch[1], 10) : null;
|
||||
}
|
||||
if (pathname.includes("/organizations/billing")) {
|
||||
return session.data?.user?.org?.id ?? null;
|
||||
}
|
||||
return null;
|
||||
};
|
||||
|
||||
const teamId = getTeamIdFromContext();
|
||||
|
||||
const startDate = dateRangeFilter?.startDate ? new Date(dateRangeFilter.startDate) : undefined;
|
||||
const endDate = dateRangeFilter?.endDate ? new Date(dateRangeFilter.endDate) : undefined;
|
||||
|
||||
const { data, isLoading, isFetching } = trpc.viewer.teams.listInvoices.useQuery(
|
||||
{
|
||||
teamId: teamId!,
|
||||
limit: DEFAULT_PAGE_SIZE,
|
||||
cursor: cursors[currentPage],
|
||||
startDate,
|
||||
endDate,
|
||||
},
|
||||
{ enabled: !!teamId, placeholderData: keepPreviousData }
|
||||
);
|
||||
|
||||
// Reset pagination when date filter changes
|
||||
const isFirstRender = useRef(true);
|
||||
useEffect(() => {
|
||||
if (isFirstRender.current) {
|
||||
isFirstRender.current = false;
|
||||
return;
|
||||
}
|
||||
setCursors([null]);
|
||||
setCurrentPage(0);
|
||||
}, [dateRangeFilter?.startDate, dateRangeFilter?.endDate]);
|
||||
|
||||
if (!teamId) return null;
|
||||
if (isLoading) return <InvoicesTableSkeleton />;
|
||||
|
||||
const handleNextPage = () => {
|
||||
if (data?.nextCursor) {
|
||||
setCursors((prev) => [...prev, data.nextCursor]);
|
||||
setCurrentPage((prev) => prev + 1);
|
||||
}
|
||||
};
|
||||
|
||||
const handlePrevPage = () => {
|
||||
if (currentPage > 0) {
|
||||
setCurrentPage((prev) => prev - 1);
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
<PanelCard title={t("invoices")} headerContent={<DateRangeFilter column={createdAtColumn} />} className="mt-5">
|
||||
{!data?.invoices.length && currentPage === 0 ? (
|
||||
<div className="text-subtle p-6 text-center text-sm">{t("no_invoices_in_date_range")}</div>
|
||||
) : (
|
||||
<>
|
||||
<div className="overflow-x-auto">
|
||||
<Table>
|
||||
<TableHeader>
|
||||
<TableRow>
|
||||
<TableHead>{t("date")}</TableHead>
|
||||
<TableHead>{t("invoice_number")}</TableHead>
|
||||
<TableHead>{t("amount")}</TableHead>
|
||||
<TableHead>{t("status")}</TableHead>
|
||||
<TableHead>{t("description")}</TableHead>
|
||||
<TableHead className="text-right">{t("actions")}</TableHead>
|
||||
</TableRow>
|
||||
</TableHeader>
|
||||
<TableBody>
|
||||
{data?.invoices.map((invoice) => (
|
||||
<TableRow key={invoice.id}>
|
||||
<TableCell>{dayjs.unix(invoice.created).format("MMM D, YYYY")}</TableCell>
|
||||
<TableCell className="font-mono text-sm">
|
||||
{invoice.number && invoice.hostedInvoiceUrl ? (
|
||||
<a
|
||||
href={invoice.hostedInvoiceUrl}
|
||||
target="_blank"
|
||||
rel="noopener noreferrer"
|
||||
className="inline-flex items-center gap-1 text-blue-600 hover:text-blue-800 hover:underline">
|
||||
{invoice.number}
|
||||
<Icon name="external-link" className="h-3 w-3" />
|
||||
</a>
|
||||
) : (
|
||||
invoice.number || "-"
|
||||
)}
|
||||
</TableCell>
|
||||
<TableCell>
|
||||
{formatCurrency(invoice.amountPaid || invoice.amountDue, invoice.currency)}
|
||||
</TableCell>
|
||||
<TableCell>
|
||||
<Badge color={getStatusBadgeColor(invoice.status)}>
|
||||
{invoice.status ? t(`invoice_status_${invoice.status}`) : "-"}
|
||||
</Badge>
|
||||
</TableCell>
|
||||
<TableCell className="max-w-[200px] truncate">
|
||||
{invoice.description || invoice.lineItems[0]?.description || "-"}
|
||||
</TableCell>
|
||||
<TableCell className="text-right">
|
||||
{invoice.invoicePdf && (
|
||||
<Button
|
||||
variant="icon"
|
||||
color="minimal"
|
||||
href={invoice.invoicePdf}
|
||||
target="_blank"
|
||||
rel="noopener noreferrer"
|
||||
StartIcon="download"
|
||||
tooltip={t("download_pdf")}
|
||||
/>
|
||||
)}
|
||||
</TableCell>
|
||||
</TableRow>
|
||||
))}
|
||||
</TableBody>
|
||||
</Table>
|
||||
</div>
|
||||
|
||||
{/* Pagination */}
|
||||
{(currentPage > 0 || data?.hasMore) && (
|
||||
<div className="border-muted flex items-center justify-end gap-2 border-t px-4 py-3">
|
||||
<Button
|
||||
color="secondary"
|
||||
size="sm"
|
||||
onClick={handlePrevPage}
|
||||
disabled={currentPage === 0 || isFetching}
|
||||
StartIcon="arrow-left">
|
||||
{t("previous")}
|
||||
</Button>
|
||||
<Button
|
||||
color="secondary"
|
||||
size="sm"
|
||||
onClick={handleNextPage}
|
||||
disabled={!data?.hasMore || isFetching}
|
||||
EndIcon="arrow-right">
|
||||
{t("next")}
|
||||
</Button>
|
||||
</div>
|
||||
)}
|
||||
</>
|
||||
)}
|
||||
</PanelCard>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
"use client";
|
||||
|
||||
import { useLocale } from "@calcom/lib/hooks/useLocale";
|
||||
import { PanelCard } from "@calcom/ui/components/card";
|
||||
import { SkeletonText } from "@calcom/ui/components/skeleton";
|
||||
|
||||
export function InvoicesTableSkeleton() {
|
||||
const { t } = useLocale();
|
||||
|
||||
return (
|
||||
<PanelCard title={t("invoices")} className="mt-5">
|
||||
<div className="p-4">
|
||||
{/* Header row */}
|
||||
<div className="mb-3 flex gap-4 border-b pb-3">
|
||||
<SkeletonText className="h-4 w-20" />
|
||||
<SkeletonText className="h-4 w-24" />
|
||||
<SkeletonText className="h-4 w-16" />
|
||||
<SkeletonText className="h-4 w-16" />
|
||||
<SkeletonText className="h-4 flex-1" />
|
||||
<SkeletonText className="h-4 w-24" />
|
||||
<SkeletonText className="h-4 w-16" />
|
||||
</div>
|
||||
{/* Data rows */}
|
||||
{Array.from({ length: 5 }).map((_, i) => (
|
||||
<div key={i} className="flex gap-4 py-3">
|
||||
<SkeletonText className="h-4 w-20" />
|
||||
<SkeletonText className="h-4 w-24" />
|
||||
<SkeletonText className="h-4 w-16" />
|
||||
<SkeletonText className="h-4 w-16" />
|
||||
<SkeletonText className="h-4 flex-1" />
|
||||
<SkeletonText className="h-4 w-24" />
|
||||
<SkeletonText className="h-4 w-8" />
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
</PanelCard>
|
||||
);
|
||||
}
|
||||
@@ -4536,6 +4536,22 @@
|
||||
"error_enabling_feature": "Error enabling feature. Please try again.",
|
||||
"set_organizer_as_contact_owner": "Set booking organizer as contact owner",
|
||||
"overwrite_existing_contact_owner": "Overwrite existing contact owner",
|
||||
"invoice_number": "Invoice #",
|
||||
"payment_method": "Payment Method",
|
||||
"download_pdf": "Download PDF",
|
||||
"no_invoices_yet": "No invoices yet",
|
||||
"invoice_status_paid": "Paid",
|
||||
"invoice_status_open": "Open",
|
||||
"invoice_status_draft": "Draft",
|
||||
"invoice_status_void": "Void",
|
||||
"invoice_status_uncollectible": "Uncollectible",
|
||||
"no_invoices_in_date_range": "No invoices found in the selected date range",
|
||||
"view_and_download_invoices": "View and download your invoices",
|
||||
"last_7_days": "Last 7 days",
|
||||
"last_30_days": "Last 30 days",
|
||||
"last_3_months": "Last 3 months",
|
||||
"last_6_months": "Last 6 months",
|
||||
"last_12_months": "Last 12 months",
|
||||
"calendar_events_disabled_video_limitation": "When calendar events are disabled, meeting links cannot be generated for apps whose link generation is tied to calendar event creation, for example, Google Meet and Microsoft Teams. Other video apps like Cal Video will continue to work.",
|
||||
"ADD_NEW_STRINGS_ABOVE_THIS_LINE_TO_PREVENT_MERGE_CONFLICTS": "↑↑↑↑↑↑↑↑↑↑↑↑↑ Add your new strings above here ↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑"
|
||||
}
|
||||
Reference in new issue
Block a user