|
| 1 | +import { OptimizeTableHeaderProps } from "@/components/gui/table-optimized"; |
| 2 | +import OptimizeTableState from "@/components/gui/table-optimized/OptimizeTableState"; |
| 3 | +import { |
| 4 | + DropdownMenuItem, |
| 5 | + DropdownMenuSeparator, |
| 6 | + DropdownMenuSub, |
| 7 | + DropdownMenuSubContent, |
| 8 | + DropdownMenuSubTrigger, |
| 9 | +} from "@/components/ui/dropdown-menu"; |
| 10 | +import { Check } from "@phosphor-icons/react"; |
| 11 | +import { useState } from "react"; |
| 12 | +import z from "zod"; |
| 13 | + |
| 14 | +function currencyDecorator(value: unknown) { |
| 15 | + if (typeof value === "number" || typeof value === "bigint") { |
| 16 | + return ( |
| 17 | + <div className="w-full text-right"> |
| 18 | + {new Intl.NumberFormat("en-US", { |
| 19 | + style: "currency", |
| 20 | + currency: "USD", |
| 21 | + }).format(Number(value))} |
| 22 | + </div> |
| 23 | + ); |
| 24 | + } |
| 25 | + |
| 26 | + return null; |
| 27 | +} |
| 28 | + |
| 29 | +function unixToDateTimeDecorator(value: unknown) { |
| 30 | + if (typeof value === "number" || typeof value === "bigint") { |
| 31 | + return ( |
| 32 | + <div className="w-full text-right"> |
| 33 | + {new Date(Number(value) * 1000).toISOString()} |
| 34 | + </div> |
| 35 | + ); |
| 36 | + } |
| 37 | + |
| 38 | + return null; |
| 39 | +} |
| 40 | + |
| 41 | +function unixMsToDateTimeDecorator(value: unknown) { |
| 42 | + if (typeof value === "number" || typeof value === "bigint") { |
| 43 | + return ( |
| 44 | + <div className="w-full text-right"> |
| 45 | + {new Date(Number(value)).toISOString()} |
| 46 | + </div> |
| 47 | + ); |
| 48 | + } |
| 49 | + |
| 50 | + return null; |
| 51 | +} |
| 52 | + |
| 53 | +export function DecoratorEditor({ |
| 54 | + header, |
| 55 | + state, |
| 56 | +}: { |
| 57 | + header: OptimizeTableHeaderProps; |
| 58 | + state: OptimizeTableState; |
| 59 | +}) { |
| 60 | + const [type, setType] = useState(() => { |
| 61 | + const setting = header.store.get("decorator"); |
| 62 | + |
| 63 | + const schema = z.object({ type: z.enum(["currency", "unix", "unix-ms"]) }); |
| 64 | + const validated = schema.safeParse(setting); |
| 65 | + |
| 66 | + if (validated.error) { |
| 67 | + return null; |
| 68 | + } |
| 69 | + |
| 70 | + return validated.data.type; |
| 71 | + }); |
| 72 | + |
| 73 | + return ( |
| 74 | + <DropdownMenuSub> |
| 75 | + <DropdownMenuSubTrigger inset>Format</DropdownMenuSubTrigger> |
| 76 | + <DropdownMenuSubContent> |
| 77 | + <DropdownMenuItem |
| 78 | + inset={type !== null} |
| 79 | + onClick={() => { |
| 80 | + state.updateHeaderDecorator(header, undefined); |
| 81 | + setType(null); |
| 82 | + header.store.set("decorator", undefined); |
| 83 | + }} |
| 84 | + > |
| 85 | + {type === null ? <Check className="mr-2 h-4 w-4" /> : null} |
| 86 | + None |
| 87 | + </DropdownMenuItem> |
| 88 | + <DropdownMenuSeparator /> |
| 89 | + <DropdownMenuItem |
| 90 | + inset={type !== "currency"} |
| 91 | + onClick={() => { |
| 92 | + state.updateHeaderDecorator(header, currencyDecorator); |
| 93 | + setType("currency"); |
| 94 | + header.store.set("decorator", { type: "currency" }); |
| 95 | + }} |
| 96 | + > |
| 97 | + {type === "currency" ? <Check className="mr-2 h-4 w-4" /> : null} |
| 98 | + Currency |
| 99 | + </DropdownMenuItem> |
| 100 | + <DropdownMenuSeparator /> |
| 101 | + <DropdownMenuItem |
| 102 | + inset={type !== "unix-ms"} |
| 103 | + onClick={() => { |
| 104 | + state.updateHeaderDecorator(header, unixMsToDateTimeDecorator); |
| 105 | + setType("unix-ms"); |
| 106 | + header.store.set("decorator", { type: "unix-ms" }); |
| 107 | + }} |
| 108 | + > |
| 109 | + {type === "unix-ms" ? <Check className="mr-2 h-4 w-4" /> : null} |
| 110 | + Unix Timestamp (ms) to datetime |
| 111 | + </DropdownMenuItem> |
| 112 | + <DropdownMenuItem |
| 113 | + inset={type !== "unix"} |
| 114 | + onClick={() => { |
| 115 | + state.updateHeaderDecorator(header, unixToDateTimeDecorator); |
| 116 | + setType("unix"); |
| 117 | + header.store.set("decorator", { type: "unix" }); |
| 118 | + }} |
| 119 | + > |
| 120 | + {type === "unix" ? <Check className="mr-2 h-4 w-4" /> : null} |
| 121 | + Unix Timestamp (s) to datetime |
| 122 | + </DropdownMenuItem> |
| 123 | + </DropdownMenuSubContent> |
| 124 | + </DropdownMenuSub> |
| 125 | + ); |
| 126 | +} |
0 commit comments