|
| 1 | +import reflex as rx |
| 2 | + |
| 3 | +from stock_graph_app.states.stock_state import StockState |
| 4 | + |
| 5 | +TOOLTIP_PROPS = { |
| 6 | + "content_style": { |
| 7 | + "background": "#2d2e30", |
| 8 | + "borderColor": "#2d2e3065", |
| 9 | + "borderRadius": "0.75rem", |
| 10 | + "boxShadow": "0px 24px 12px 0px light-dark(rgba(28, 32, 36, 0.02), rgba(0, 0, 0, 0.00)), 0px 8px 8px 0px light-dark(rgba(28, 32, 36, 0.02), rgba(0, 0, 0, 0.00)), 0px 2px 6px 0px light-dark(rgba(28, 32, 36, 0.02), rgba(0, 0, 0, 0.00))", |
| 11 | + "fontFamily": "sans-serif", |
| 12 | + "fontSize": "0.875rem", |
| 13 | + "lineHeight": "1.25rem", |
| 14 | + "fontWeight": "500", |
| 15 | + "minWidth": "8rem", |
| 16 | + "padding": "0.375rem 0.625rem", |
| 17 | + "position": "relative", |
| 18 | + }, |
| 19 | + "item_style": { |
| 20 | + "display": "flex", |
| 21 | + "paddingBottom": "0px", |
| 22 | + "position": "relative", |
| 23 | + "paddingTop": "2px", |
| 24 | + }, |
| 25 | + "label_style": { |
| 26 | + "color": "#d4d4d4", |
| 27 | + "fontWeight": "500", |
| 28 | + "alignSelf": "flex-end", |
| 29 | + }, |
| 30 | + "separator": "", |
| 31 | +} |
| 32 | + |
| 33 | + |
| 34 | +def live_indicator_dot() -> rx.Component: |
| 35 | + """Creates a pulsing green dot to indicate live market activity.""" |
| 36 | + return rx.el.span( |
| 37 | + rx.el.span( |
| 38 | + class_name="animate-ping absolute inline-flex h-full w-full rounded-full bg-green-400 opacity-75" |
| 39 | + ), |
| 40 | + rx.el.span(class_name="relative inline-flex rounded-full h-2 w-2 bg-green-500"), |
| 41 | + class_name="relative flex h-2 w-2", |
| 42 | + ) |
| 43 | + |
| 44 | + |
| 45 | +def search_bar_component() -> rx.Component: |
| 46 | + return rx.el.form( |
| 47 | + rx.el.input( |
| 48 | + name="ticker_input", |
| 49 | + placeholder="Enter Ticker (e.g., AAPL, MSFT)", |
| 50 | + value=StockState.search_ticker_input, |
| 51 | + on_change=StockState.set_search_ticker_input, |
| 52 | + class_name="bg-[#2d2e30] text-white placeholder-neutral-400 border border-neutral-600 rounded-lg p-2 text-sm w-full sm:flex-grow focus:ring-2 focus:ring-emerald-500 focus:border-emerald-500 outline-none transition-shadow shadow-sm hover:shadow-md focus:shadow-lg", |
| 53 | + ), |
| 54 | + rx.el.button( |
| 55 | + "Get Data", |
| 56 | + type="submit", |
| 57 | + class_name="text-nowrap bg-emerald-600 hover:bg-emerald-700 text-white font-semibold p-2 text-sm rounded-lg transition-all duration-150 ease-in-out shadow hover:shadow-md active:bg-emerald-800 w-full sm:w-auto sm:px-3", |
| 58 | + is_loading=StockState.is_loading, |
| 59 | + on_click=lambda: StockState.fetch_stock_data(None), |
| 60 | + ), |
| 61 | + on_submit=StockState.fetch_stock_data, |
| 62 | + reset_on_submit=False, |
| 63 | + class_name="mb-8 flex flex-col sm:flex-row items-stretch sm:items-center space-y-2 sm:space-y-0 sm:space-x-2", |
| 64 | + ) |
| 65 | + |
| 66 | + |
| 67 | +def stock_header_component() -> rx.Component: |
| 68 | + return rx.el.div( |
| 69 | + rx.el.div( |
| 70 | + rx.el.div( |
| 71 | + rx.el.h1( |
| 72 | + StockState.stock_ticker, |
| 73 | + class_name="text-2xl sm:text-3xl md:text-4xl font-bold tracking-tight text-white", |
| 74 | + ), |
| 75 | + rx.el.div( |
| 76 | + rx.cond( |
| 77 | + StockState.logo_url != "", |
| 78 | + rx.image( |
| 79 | + src=StockState.logo_url, |
| 80 | + alt=StockState.company_name, |
| 81 | + height="24px", |
| 82 | + width="24px", |
| 83 | + class_name="mr-2 rounded-md self-center", |
| 84 | + fallback="", |
| 85 | + ), |
| 86 | + ), |
| 87 | + rx.el.p( |
| 88 | + StockState.company_name, |
| 89 | + class_name="text-sm sm:text-base text-neutral-300 self-center", |
| 90 | + ), |
| 91 | + class_name="flex items-center mt-1 sm:mt-1.5 mb-1", |
| 92 | + ), |
| 93 | + rx.el.p( |
| 94 | + StockState.exchange_info, |
| 95 | + class_name="text-xs text-neutral-500", |
| 96 | + ), |
| 97 | + class_name="flex-grow pb-3 w-full sm:w-auto", |
| 98 | + ), |
| 99 | + rx.el.div( |
| 100 | + rx.el.div( |
| 101 | + rx.cond( |
| 102 | + StockState.is_market_currently_active |
| 103 | + & StockState.has_data_to_display, |
| 104 | + rx.el.div(live_indicator_dot(), class_name="mr-2 self-center"), |
| 105 | + ), |
| 106 | + rx.el.span(StockState.current_price_display_val), |
| 107 | + rx.el.span( |
| 108 | + StockState.company_info.get("currency", ""), |
| 109 | + class_name="text-neutral-400 text-base sm:text-lg ml-1 self-baseline", |
| 110 | + ), |
| 111 | + class_name="flex items-baseline text-2xl sm:text-3xl md:text-4xl font-bold text-white text-left sm:text-right", |
| 112 | + ), |
| 113 | + rx.el.p( |
| 114 | + "Market Cap: ", |
| 115 | + StockState.market_cap_display_val, |
| 116 | + class_name="text-xs text-neutral-500 text-left sm:text-right mt-0.5", |
| 117 | + ), |
| 118 | + rx.el.p( |
| 119 | + "At Regular Market Close", |
| 120 | + class_name="text-xs text-neutral-500 text-left sm:text-right", |
| 121 | + ), |
| 122 | + class_name="text-left sm:text-right flex-shrink-0 w-full sm:w-auto mt-3 sm:mt-0 sm:ml-4 pb-3", |
| 123 | + ), |
| 124 | + class_name="flex flex-col sm:flex-row justify-between items-stretch sm:items-start mb-2 border-neutral-700", |
| 125 | + ), |
| 126 | + rx.cond( |
| 127 | + StockState.show_after_hours_section, |
| 128 | + rx.el.div( |
| 129 | + rx.el.p( |
| 130 | + StockState.after_hours_price_display_val, |
| 131 | + " ", |
| 132 | + rx.el.span( |
| 133 | + StockState.after_hours_change_display_val, |
| 134 | + class_name="text-neutral-500 font-semibold", |
| 135 | + ), |
| 136 | + rx.el.span( |
| 137 | + StockState.company_info.get("currency", ""), |
| 138 | + class_name="text-neutral-400 text-xs sm:text-sm ml-1 self-baseline", |
| 139 | + ), |
| 140 | + class_name="text-base sm:text-lg md:text-xl font-semibold text-white text-left sm:text-right", |
| 141 | + ), |
| 142 | + rx.el.p( |
| 143 | + StockState.after_hours_label, |
| 144 | + class_name="text-xs text-neutral-500 text-left sm:text-right mt-0.5", |
| 145 | + ), |
| 146 | + class_name="mt-3 mb-1 w-full text-left sm:text-right", |
| 147 | + ), |
| 148 | + ), |
| 149 | + class_name="mb-5", |
| 150 | + ) |
| 151 | + |
| 152 | + |
| 153 | +def time_range_selector_component() -> rx.Component: |
| 154 | + return rx.el.div( |
| 155 | + rx.foreach( |
| 156 | + StockState.time_ranges, |
| 157 | + lambda time_range: rx.el.button( |
| 158 | + time_range, |
| 159 | + on_click=lambda: StockState.set_time_range(time_range), |
| 160 | + class_name=rx.cond( |
| 161 | + StockState.selected_time_range == time_range, |
| 162 | + "bg-emerald-600 text-white font-semibold px-2.5 py-1.5 rounded-md text-xs sm:text-sm mx-0.5 sm:mx-1 my-1 min-w-[3rem] text-center shadow-md", |
| 163 | + "bg-[#2d2e30] text-neutral-300 hover:bg-neutral-700 hover:text-white px-2.5 py-1.5 rounded-md text-xs sm:text-sm mx-0.5 sm:mx-1 my-1 min-w-[3rem] text-center transition-colors duration-150 ease-in-out shadow-sm hover:shadow-md", |
| 164 | + ), |
| 165 | + disabled=StockState.is_loading, |
| 166 | + ), |
| 167 | + ), |
| 168 | + class_name="flex flex-wrap justify-center md:justify-start items-center my-4 md:my-6 pb-3 sm:pb-4 border-neutral-700", |
| 169 | + ) |
| 170 | + |
| 171 | + |
| 172 | +def chart_component() -> rx.Component: |
| 173 | + gradient_id = "stockPriceGradient" |
| 174 | + return rx.el.div( |
| 175 | + rx.el.svg( |
| 176 | + rx.el.defs( |
| 177 | + rx.el.svg.linear_gradient( |
| 178 | + rx.el.svg.stop( |
| 179 | + offset="5%", |
| 180 | + stop_color="#22C55E", |
| 181 | + stop_opacity=0.4, |
| 182 | + ), |
| 183 | + rx.el.svg.stop( |
| 184 | + offset="95%", |
| 185 | + stop_color="#22C55E", |
| 186 | + stop_opacity=0.05, |
| 187 | + ), |
| 188 | + id=gradient_id, |
| 189 | + x1="0", |
| 190 | + y1="0", |
| 191 | + x2="0", |
| 192 | + y2="1", |
| 193 | + ) |
| 194 | + ), |
| 195 | + class_name="w-0 h-0 absolute", |
| 196 | + ), |
| 197 | + rx.recharts.area_chart( |
| 198 | + rx.recharts.cartesian_grid( |
| 199 | + horizontal=True, |
| 200 | + vertical=False, |
| 201 | + stroke_dasharray="3 3", |
| 202 | + stroke_opacity=0.2, |
| 203 | + stroke="#555555", |
| 204 | + ), |
| 205 | + rx.recharts.graphing_tooltip( |
| 206 | + **TOOLTIP_PROPS, |
| 207 | + ), |
| 208 | + rx.recharts.y_axis( |
| 209 | + orientation="left", |
| 210 | + data_key="price", |
| 211 | + domain=StockState.y_axis_domain, |
| 212 | + axis_line=False, |
| 213 | + tick_line=False, |
| 214 | + class_name="text-neutral-400 text-sm", |
| 215 | + label=StockState.y_axis_label_config, |
| 216 | + ), |
| 217 | + rx.recharts.x_axis( |
| 218 | + data_key="name", |
| 219 | + include_hidden=False, |
| 220 | + type_="category", |
| 221 | + tick_line=False, |
| 222 | + axis_line=False, |
| 223 | + tick_count=10, |
| 224 | + tick_size=16, |
| 225 | + tick_margin=8, |
| 226 | + min_tick_gap=32, |
| 227 | + interval="equidistantPreserveStart", |
| 228 | + class_name="text-neutral-400 text-xs", |
| 229 | + ), |
| 230 | + rx.recharts.area( |
| 231 | + data_key="price", |
| 232 | + name=StockState.currency_code, |
| 233 | + type_="monotone", |
| 234 | + stroke="#16A34A", |
| 235 | + fill=f"url(#{gradient_id})", |
| 236 | + stroke_width=2, |
| 237 | + dot=False, |
| 238 | + active_dot=True, |
| 239 | + ), |
| 240 | + data=StockState.current_stock_data_for_chart, |
| 241 | + height=350, |
| 242 | + margin={ |
| 243 | + "top": 5, |
| 244 | + "right": 0, |
| 245 | + "left": 0, |
| 246 | + "bottom": 5, |
| 247 | + }, |
| 248 | + class_name="w-full [&_.recharts-tooltip-item-unit]:text-neutral-100 [&_.recharts-tooltip-item-unit]:font-mono [&_.recharts-tooltip-item-value]:!text-neutral-100 [&_.recharts-tooltip-item-value]:!font-mono [&_.recharts-tooltip-item-value]:mr-[0.2rem] [&_.recharts-tooltip-item]:flex [&_.recharts-tooltip-item]:items-center [&_.recharts-tooltip-item]:before:content-[''] [&_.recharts-tooltip-item]:before:size-2.5 [&_.recharts-tooltip-item]:before:rounded-[2px] [&_.recharts-tooltip-item]:before:shrink-0 [&_.recharts-tooltip-item]:before:!bg-[currentColor] [&_.recharts-tooltip-item-name]:text-neutral-300 [&_.recharts-tooltip-item-list]:flex [&_.recharts-tooltip-item-list]:flex-col [&_.recharts-tooltip-item-name]:pr-[3rem] [&_.recharts-tooltip-item-name]:pl-1.5 [&_.recharts-tooltip-item-separator]:w-full [&_.recharts-tooltip-wrapper]:z-[1]", |
| 249 | + ), |
| 250 | + class_name="w-full h-[350px] mt-4", |
| 251 | + ) |
| 252 | + |
| 253 | + |
| 254 | +def loading_spinner_component() -> rx.Component: |
| 255 | + return rx.el.div( |
| 256 | + rx.spinner(class_name="text-green-500 w-12 h-12"), |
| 257 | + class_name="w-full h-[300px] flex justify-center items-center", |
| 258 | + ) |
| 259 | + |
| 260 | + |
| 261 | +def error_message_component() -> rx.Component: |
| 262 | + return rx.el.div( |
| 263 | + StockState.error_message, |
| 264 | + class_name="text-red-400 text-sm my-4 p-3 bg-red-900/40 border border-red-700/50 rounded-lg flex items-center shadow", |
| 265 | + ) |
| 266 | + |
| 267 | + |
| 268 | +def no_data_component() -> rx.Component: |
| 269 | + return rx.el.div( |
| 270 | + "No data to display. Please search for a stock ticker above or select a different time range.", |
| 271 | + class_name="w-full h-[300px] flex flex-col justify-center items-center text-neutral-500 text-center p-4", |
| 272 | + ) |
| 273 | + |
| 274 | + |
| 275 | +def stock_graph_page() -> rx.Component: |
| 276 | + return rx.el.div( |
| 277 | + search_bar_component(), |
| 278 | + rx.cond(StockState.error_message, error_message_component(), rx.el.div()), |
| 279 | + rx.cond( |
| 280 | + StockState.is_loading, |
| 281 | + loading_spinner_component(), |
| 282 | + rx.cond( |
| 283 | + StockState.has_data_to_display, |
| 284 | + rx.fragment( |
| 285 | + stock_header_component(), |
| 286 | + time_range_selector_component(), |
| 287 | + chart_component(), |
| 288 | + ), |
| 289 | + rx.cond(~StockState.error_message, no_data_component(), rx.el.div()), |
| 290 | + ), |
| 291 | + ), |
| 292 | + rx.moment( |
| 293 | + interval=5000, |
| 294 | + on_change=StockState.refresh_data.temporal, |
| 295 | + class_name="hidden pointer-events-none", |
| 296 | + ), |
| 297 | + class_name="bg-gradient-to-br from-[#202123] to-[#2a2b2f] text-white p-3 sm:p-4 md:p-6 rounded-lg sm:rounded-xl shadow-xl sm:shadow-2xl w-full max-w-4xl mx-auto my-4 sm:my-8", |
| 298 | + on_mount=StockState.on_load_fetch, |
| 299 | + ) |
0 commit comments