|
| 1 | +import type { BillingAddOns } from "@trigger.dev/billing"; |
| 2 | +import { useEffect, useRef } from "react"; |
| 3 | +import { MetricsLayout } from "~/components/layout/MetricsLayout"; |
| 4 | +import { DateTime } from "~/components/primitives/DateTime"; |
| 5 | +import { Paragraph } from "~/components/primitives/Paragraph"; |
| 6 | +import { Select, SelectItem } from "~/components/primitives/Select"; |
| 7 | +import { Spinner } from "~/components/primitives/Spinner"; |
| 8 | +import { TabButton, TabContainer } from "~/components/primitives/Tabs"; |
| 9 | +import { useOptimisticLocation } from "~/hooks/useOptimisticLocation"; |
| 10 | +import { useSearchParams } from "~/hooks/useSearchParam"; |
| 11 | +import type { BillingUsageResult } from "~/services/billingUsage.server"; |
| 12 | +import { |
| 13 | + usageProjects, |
| 14 | + type MonthlyUsageView, |
| 15 | + type ProjectUsageView, |
| 16 | + type UsageDimension, |
| 17 | +} from "~/utils/billingUsageBreakdown"; |
| 18 | +import { |
| 19 | + ALL_PROJECTS, |
| 20 | + browserGroupingsStore, |
| 21 | + parseUsagePageState, |
| 22 | + storedGroupingsToRestore, |
| 23 | + USAGE_PAGE_PARAMS, |
| 24 | + writeStoredGroupings, |
| 25 | + type GroupingsParam, |
| 26 | + type UsagePageTab, |
| 27 | +} from "~/utils/billingUsagePageState"; |
| 28 | +import { AddOnsTab } from "./AddOnsTab"; |
| 29 | +import { BillingUsageErrorPanel } from "./BillingUsageErrorPanel"; |
| 30 | +import { MonthStepper } from "./MonthStepper"; |
| 31 | +import { ProjectUsageTab } from "./ProjectUsageTab"; |
| 32 | +import { UsageTab, type UsagePlanLimits } from "./UsageTab"; |
| 33 | +import { useUsageFill } from "./useUsageFill"; |
| 34 | + |
| 35 | +const TABS: { value: UsagePageTab; label: string }[] = [ |
| 36 | + { value: "usage", label: "Usage" }, |
| 37 | + { value: "projects", label: "By project" }, |
| 38 | + { value: "addons", label: "Add-ons" }, |
| 39 | +]; |
| 40 | + |
| 41 | +export function BillingUsageView({ |
| 42 | + organizationSlug, |
| 43 | + months, |
| 44 | + month, |
| 45 | + addOns, |
| 46 | + limits, |
| 47 | + ...initial |
| 48 | +}: { |
| 49 | + organizationSlug: string; |
| 50 | + months: string[]; |
| 51 | + month: string; |
| 52 | + usage: BillingUsageResult<MonthlyUsageView>; |
| 53 | + projectUsage: BillingUsageResult<ProjectUsageView>; |
| 54 | + addOns: BillingUsageResult<BillingAddOns>; |
| 55 | + limits: UsagePlanLimits; |
| 56 | +}) { |
| 57 | + const { usage, projectUsage, pendingDays, isFilling } = useUsageFill( |
| 58 | + organizationSlug, |
| 59 | + month, |
| 60 | + initial |
| 61 | + ); |
| 62 | + const location = useOptimisticLocation(); |
| 63 | + const { replace } = useSearchParams(); |
| 64 | + const projects = projectUsage.ok ? usageProjects(projectUsage.value) : []; |
| 65 | + const state = parseUsagePageState( |
| 66 | + new URLSearchParams(location.search), |
| 67 | + projects.map((project) => project.id) |
| 68 | + ); |
| 69 | + |
| 70 | + const hasRestored = useRef(false); |
| 71 | + useEffect(() => { |
| 72 | + if (hasRestored.current) return; |
| 73 | + hasRestored.current = true; |
| 74 | + |
| 75 | + const restore = storedGroupingsToRestore( |
| 76 | + new URLSearchParams(location.search), |
| 77 | + browserGroupingsStore() |
| 78 | + ); |
| 79 | + if (Object.keys(restore).length > 0) replace(restore); |
| 80 | + }, [location.search, replace]); |
| 81 | + |
| 82 | + const changeGroupings = (param: GroupingsParam, groupings: UsageDimension[]) => { |
| 83 | + writeStoredGroupings(browserGroupingsStore(), param, groupings); |
| 84 | + replace({ [USAGE_PAGE_PARAMS[param]]: groupings.join(",") }); |
| 85 | + }; |
| 86 | + |
| 87 | + const active = { usage, projects: projectUsage, addons: addOns }[state.tab]; |
| 88 | + const fetchedAt = active.ok ? active.value.fetchedAt : undefined; |
| 89 | + |
| 90 | + return ( |
| 91 | + <MetricsLayout.Root> |
| 92 | + <MetricsLayout.Filters> |
| 93 | + <TabContainer> |
| 94 | + {TABS.map((tab) => ( |
| 95 | + <TabButton |
| 96 | + key={tab.value} |
| 97 | + layoutId="billing-usage-tabs" |
| 98 | + isActive={state.tab === tab.value} |
| 99 | + onClick={() => replace({ [USAGE_PAGE_PARAMS.tab]: tab.value })} |
| 100 | + > |
| 101 | + {tab.label} |
| 102 | + </TabButton> |
| 103 | + ))} |
| 104 | + </TabContainer> |
| 105 | + <div className="flex items-center gap-2"> |
| 106 | + {state.tab === "projects" && projects.length > 0 && ( |
| 107 | + <Select |
| 108 | + name="project" |
| 109 | + value={state.project} |
| 110 | + setValue={(value) => replace({ [USAGE_PAGE_PARAMS.project]: value })} |
| 111 | + items={[ |
| 112 | + { value: ALL_PROJECTS, label: "All projects" }, |
| 113 | + ...projects.map((project) => ({ value: project.id, label: project.name })), |
| 114 | + ]} |
| 115 | + text={(value) => |
| 116 | + projects.find((project) => project.id === value)?.name ?? "All projects" |
| 117 | + } |
| 118 | + dropdownIcon |
| 119 | + variant="tertiary/small" |
| 120 | + > |
| 121 | + {(items) => |
| 122 | + items.map((item) => ( |
| 123 | + <SelectItem key={item.value} value={item.value}> |
| 124 | + {item.label} |
| 125 | + </SelectItem> |
| 126 | + )) |
| 127 | + } |
| 128 | + </Select> |
| 129 | + )} |
| 130 | + {pendingDays > 0 && state.tab !== "addons" && ( |
| 131 | + <div className="flex items-center gap-1.5 whitespace-nowrap"> |
| 132 | + {isFilling && <Spinner className="size-3.5" color="muted" />} |
| 133 | + <Paragraph variant="extra-small"> |
| 134 | + {isFilling ? "Loading" : "Couldn't load"} {pendingDays} earlier{" "} |
| 135 | + {pendingDays === 1 ? "day" : "days"} |
| 136 | + </Paragraph> |
| 137 | + </div> |
| 138 | + )} |
| 139 | + {fetchedAt && ( |
| 140 | + <Paragraph variant="extra-small" className="whitespace-nowrap"> |
| 141 | + Updated <DateTime date={fetchedAt} /> |
| 142 | + </Paragraph> |
| 143 | + )} |
| 144 | + <MonthStepper |
| 145 | + months={months} |
| 146 | + month={month} |
| 147 | + onChange={(value) => replace({ [USAGE_PAGE_PARAMS.month]: value })} |
| 148 | + /> |
| 149 | + </div> |
| 150 | + </MetricsLayout.Filters> |
| 151 | + {state.tab === "usage" ? ( |
| 152 | + usage.ok ? ( |
| 153 | + <UsageTab |
| 154 | + usage={usage.value} |
| 155 | + isFilling={isFilling} |
| 156 | + limits={limits} |
| 157 | + groupings={state.usageGroupings} |
| 158 | + onGroupingsChange={(groupings) => changeGroupings("usageGroupings", groupings)} |
| 159 | + /> |
| 160 | + ) : ( |
| 161 | + <ErrorGrid result={usage} /> |
| 162 | + ) |
| 163 | + ) : state.tab === "addons" ? ( |
| 164 | + addOns.ok ? ( |
| 165 | + <AddOnsTab addOns={addOns.value} /> |
| 166 | + ) : ( |
| 167 | + <ErrorGrid result={addOns} /> |
| 168 | + ) |
| 169 | + ) : projectUsage.ok ? ( |
| 170 | + <ProjectUsageTab |
| 171 | + usage={projectUsage.value} |
| 172 | + isFilling={isFilling} |
| 173 | + selectedProject={state.project} |
| 174 | + groupings={state.projectGroupings} |
| 175 | + metric={state.metric} |
| 176 | + timeFormat={state.timeFormat} |
| 177 | + onGroupingsChange={(groupings) => changeGroupings("projectGroupings", groupings)} |
| 178 | + onMetricChange={(metric) => replace({ [USAGE_PAGE_PARAMS.metric]: metric })} |
| 179 | + onTimeFormatChange={(timeFormat) => |
| 180 | + replace({ [USAGE_PAGE_PARAMS.timeFormat]: timeFormat }) |
| 181 | + } |
| 182 | + /> |
| 183 | + ) : ( |
| 184 | + <ErrorGrid result={projectUsage} /> |
| 185 | + )} |
| 186 | + </MetricsLayout.Root> |
| 187 | + ); |
| 188 | +} |
| 189 | + |
| 190 | +function ErrorGrid({ result }: { result: Extract<BillingUsageResult<unknown>, { ok: false }> }) { |
| 191 | + return ( |
| 192 | + <MetricsLayout.Grid columns={{ base: 1 }}> |
| 193 | + <BillingUsageErrorPanel error={result.error} retryable={result.retryable} /> |
| 194 | + </MetricsLayout.Grid> |
| 195 | + ); |
| 196 | +} |
0 commit comments