|
| 1 | +import reflex as rx |
| 2 | +import reflex.components.recharts as recharts |
| 3 | + |
| 4 | +from admin_dashboard.states.dashboard_state import ( |
| 5 | + CustomerData, |
| 6 | + DashboardState, |
| 7 | +) |
| 8 | + |
| 9 | + |
| 10 | +def detail_item(label: str, value: rx.Var[str]) -> rx.Component: |
| 11 | + """Displays a single detail item with label and value.""" |
| 12 | + return rx.el.div( |
| 13 | + rx.el.dt( |
| 14 | + label, |
| 15 | + class_name="text-sm font-medium text-gray-500", |
| 16 | + ), |
| 17 | + rx.el.dd(value, class_name="mt-1 text-sm text-gray-900"), |
| 18 | + class_name="py-2", |
| 19 | + ) |
| 20 | + |
| 21 | + |
| 22 | +def license_stat(label: str, value: rx.Var[int], change: rx.Var[int]) -> rx.Component: |
| 23 | + """Displays a license statistic card.""" |
| 24 | + return rx.el.div( |
| 25 | + rx.el.p( |
| 26 | + label, |
| 27 | + class_name="text-sm font-medium text-gray-500", |
| 28 | + ), |
| 29 | + rx.el.div( |
| 30 | + rx.el.span( |
| 31 | + value, |
| 32 | + class_name="text-3xl font-bold text-gray-900 mr-2", |
| 33 | + ), |
| 34 | + rx.el.span( |
| 35 | + rx.icon( |
| 36 | + rx.cond(change > 0, "arrow-up", "arrow-down"), |
| 37 | + size=16, |
| 38 | + class_name="mr-1", |
| 39 | + ), |
| 40 | + rx.cond(change < 0, change * -1, change), |
| 41 | + "%", |
| 42 | + class_name=rx.cond( |
| 43 | + change > 0, |
| 44 | + "text-sm font-medium text-green-600 bg-green-100 px-2 py-0.5 rounded-full inline-flex items-center", |
| 45 | + "text-sm font-medium text-red-600 bg-red-100 px-2 py-0.5 rounded-full inline-flex items-center", |
| 46 | + ), |
| 47 | + ), |
| 48 | + class_name="flex items-baseline", |
| 49 | + ), |
| 50 | + class_name="p-4 bg-white rounded-lg border border-gray-200 shadow-sm", |
| 51 | + ) |
| 52 | + |
| 53 | + |
| 54 | +def customer_details_panel( |
| 55 | + customer: CustomerData, |
| 56 | +) -> rx.Component: |
| 57 | + """Panel showing detailed information about a selected customer.""" |
| 58 | + usage_percentage = rx.cond( |
| 59 | + customer["licenses"] > 0, |
| 60 | + round(customer["active_licenses"] * 100 / customer["licenses"]).to(int), |
| 61 | + 0, |
| 62 | + ) |
| 63 | + return rx.el.div( |
| 64 | + rx.el.h3( |
| 65 | + customer["customer_name"], |
| 66 | + class_name="text-xl font-semibold text-gray-900 mb-4", |
| 67 | + ), |
| 68 | + rx.el.div( |
| 69 | + rx.el.h4( |
| 70 | + "Usage", |
| 71 | + class_name="text-md font-medium text-gray-700 mb-2", |
| 72 | + ), |
| 73 | + rx.el.div( |
| 74 | + rx.el.p( |
| 75 | + usage_percentage.to_string() + "%", |
| 76 | + class_name="text-5xl font-bold text-emerald-600 text-center", |
| 77 | + ), |
| 78 | + class_name="flex items-center justify-center w-32 h-32 rounded-full bg-emerald-50 border-4 border-emerald-200 mx-auto mb-4", |
| 79 | + ), |
| 80 | + class_name="mb-6 p-4 bg-white rounded-lg border border-gray-200 shadow-sm", |
| 81 | + ), |
| 82 | + rx.el.div( |
| 83 | + rx.el.h4( |
| 84 | + "Details", |
| 85 | + class_name="text-md font-medium text-gray-700 mb-2", |
| 86 | + ), |
| 87 | + rx.el.dl( |
| 88 | + detail_item( |
| 89 | + "Revenue", |
| 90 | + "$" + customer["revenue"].to_string(), |
| 91 | + ), |
| 92 | + detail_item("Platform Type", customer["platform"]), |
| 93 | + detail_item("Industry", customer["industry"]), |
| 94 | + ), |
| 95 | + class_name="mb-6 p-4 bg-white rounded-lg border border-gray-200 divide-y divide-gray-200 shadow-sm", |
| 96 | + ), |
| 97 | + rx.el.div( |
| 98 | + rx.el.h4( |
| 99 | + "License Utilization", |
| 100 | + class_name="text-md font-medium text-gray-700 mb-3", |
| 101 | + ), |
| 102 | + rx.el.div( |
| 103 | + license_stat( |
| 104 | + "Active Licenses", |
| 105 | + customer["active_licenses"], |
| 106 | + customer["active_license_growth"], |
| 107 | + ), |
| 108 | + license_stat( |
| 109 | + "Total Licenses", |
| 110 | + customer["licenses"], |
| 111 | + customer["license_growth"], |
| 112 | + ), |
| 113 | + class_name="grid grid-cols-2 gap-4 mb-4", |
| 114 | + ), |
| 115 | + class_name="mb-6", |
| 116 | + ), |
| 117 | + rx.el.div( |
| 118 | + rx.el.h4( |
| 119 | + "Usage Over Time", |
| 120 | + class_name="text-md font-medium text-gray-700 mb-2", |
| 121 | + ), |
| 122 | + rx.el.div( |
| 123 | + recharts.line_chart( |
| 124 | + recharts.cartesian_grid( |
| 125 | + horizontal=True, |
| 126 | + vertical=False, |
| 127 | + class_name="opacity-25 stroke-gray-300", |
| 128 | + ), |
| 129 | + recharts.line( |
| 130 | + data_key="usage", |
| 131 | + stroke="#10B981", |
| 132 | + stroke_width=2, |
| 133 | + dot=False, |
| 134 | + type_="natural", |
| 135 | + ), |
| 136 | + recharts.x_axis( |
| 137 | + data_key="month", |
| 138 | + axis_line=False, |
| 139 | + tick_size=10, |
| 140 | + tick_line=False, |
| 141 | + interval="preserveStartEnd", |
| 142 | + ), |
| 143 | + recharts.y_axis( |
| 144 | + axis_line=False, |
| 145 | + tick_size=0, |
| 146 | + tick_line=False, |
| 147 | + width=30, |
| 148 | + ), |
| 149 | + data=customer["usage_history"], |
| 150 | + height=250, |
| 151 | + margin={ |
| 152 | + "top": 5, |
| 153 | + "right": 10, |
| 154 | + "left": 0, |
| 155 | + "bottom": 5, |
| 156 | + }, |
| 157 | + ), |
| 158 | + class_name="p-2 bg-white rounded-lg border border-gray-200 flex items-center justify-center shadow-sm", |
| 159 | + ), |
| 160 | + ), |
| 161 | + class_name="px-5 pt-5 pb-14 bg-gray-50 rounded-lg shadow-inner h-[100vh] overflow-y-auto", |
| 162 | + ) |
| 163 | + |
| 164 | + |
| 165 | +def customer_details() -> rx.Component: |
| 166 | + """Component to display details of the selected customer or a placeholder.""" |
| 167 | + return rx.el.div( |
| 168 | + rx.cond( |
| 169 | + DashboardState.selected_customer, |
| 170 | + customer_details_panel(DashboardState.selected_customer), |
| 171 | + rx.el.div( |
| 172 | + rx.el.p( |
| 173 | + "Select a customer to see details.", |
| 174 | + class_name="text-center text-gray-500 text-lg", |
| 175 | + ), |
| 176 | + class_name="flex items-center justify-center h-[100vh] p-5 bg-gray-50 rounded-lg shadow-inner", |
| 177 | + ), |
| 178 | + ), |
| 179 | + class_name="w-1/3 flex-shrink-0 h-[100vh] overflow-hidden sticky top-0 right-0 border-l border-gray-200", |
| 180 | + ) |
0 commit comments