|
1 | 1 | import BlobCell from "@/components/gui/table-cell/blob-cell"; |
2 | 2 | import { DatabaseValue } from "@/drivers/base-driver"; |
3 | 3 | import parseSafeJson from "@/lib/json-safe"; |
| 4 | +import { deserializeV8 } from "@/lib/v8-derialization"; |
4 | 5 | import { ColumnType } from "@outerbase/sdk-transform"; |
| 6 | +import { useMemo } from "react"; |
5 | 7 | import BigNumberCell from "../table-cell/big-number-cell"; |
6 | 8 | import GenericCell from "../table-cell/generic-cell"; |
7 | 9 | import NumberCell from "../table-cell/number-cell"; |
@@ -42,17 +44,93 @@ function determineCellType(value: unknown) { |
42 | 44 | return undefined; |
43 | 45 | } |
44 | 46 |
|
45 | | -export default function tableResultCellRenderer({ |
46 | | - y, |
47 | | - x, |
48 | | - state, |
49 | | - header, |
50 | | - isFocus, |
51 | | -}: OptimizeTableCellRenderProps<TableHeaderMetadata>) { |
| 47 | +function CloudflareKvValue({ |
| 48 | + props, |
| 49 | +}: { |
| 50 | + props: OptimizeTableCellRenderProps<TableHeaderMetadata>; |
| 51 | +}) { |
| 52 | + const { y, x, state, header, isFocus } = props; |
| 53 | + |
| 54 | + const value = useMemo(() => { |
| 55 | + const rawBuffer = state.getValue(y, x); |
| 56 | + let buffer = new ArrayBuffer(); |
| 57 | + |
| 58 | + if (rawBuffer instanceof ArrayBuffer) { |
| 59 | + buffer = rawBuffer; |
| 60 | + } else if (rawBuffer instanceof Uint8Array) { |
| 61 | + buffer = rawBuffer.buffer as ArrayBuffer; |
| 62 | + } else if (rawBuffer instanceof Array) { |
| 63 | + buffer = new Uint8Array(rawBuffer).buffer; |
| 64 | + } |
| 65 | + |
| 66 | + return deserializeV8(buffer); |
| 67 | + }, [y, x, state]); |
| 68 | + |
| 69 | + let displayValue: string | null = ""; |
| 70 | + |
| 71 | + if (value.value !== undefined) { |
| 72 | + if (typeof value.value === "string") { |
| 73 | + displayValue = value.value; |
| 74 | + } else if (value.value === null) { |
| 75 | + displayValue = null; |
| 76 | + } else if (typeof value.value === "object") { |
| 77 | + // Protect from circular references |
| 78 | + try { |
| 79 | + displayValue = JSON.stringify(value.value, null); |
| 80 | + } catch (e) { |
| 81 | + if (e instanceof Error) { |
| 82 | + value.error = e.message; |
| 83 | + } else { |
| 84 | + value.error = String(e); |
| 85 | + } |
| 86 | + } |
| 87 | + } else { |
| 88 | + displayValue = String(value.value); |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + if (value.error) { |
| 93 | + return ( |
| 94 | + <div className="h-[35px] px-2 font-mono leading-[35px] text-red-500!"> |
| 95 | + Error: {value.error} |
| 96 | + </div> |
| 97 | + ); |
| 98 | + } |
| 99 | + |
| 100 | + return ( |
| 101 | + <TextCell |
| 102 | + header={header} |
| 103 | + state={state} |
| 104 | + editor={detectTextEditorType(displayValue)} |
| 105 | + editMode={false} |
| 106 | + value={displayValue} |
| 107 | + valueType={ColumnType.TEXT} |
| 108 | + focus={isFocus} |
| 109 | + onChange={(newValue) => { |
| 110 | + state.changeValue(y, x, newValue); |
| 111 | + }} |
| 112 | + /> |
| 113 | + ); |
| 114 | +} |
| 115 | + |
| 116 | +export default function tableResultCellRenderer( |
| 117 | + props: OptimizeTableCellRenderProps<TableHeaderMetadata> |
| 118 | +) { |
| 119 | + const { y, x, state, header, isFocus } = props; |
| 120 | + |
52 | 121 | const editMode = isFocus && state.isInEditMode(); |
53 | 122 | const value = state.getValue(y, x); |
| 123 | + |
54 | 124 | const valueType = determineCellType(value); |
55 | 125 |
|
| 126 | + // Check if it is Cloudflare KV type |
| 127 | + if ( |
| 128 | + header.metadata?.from?.table === "_cf_KV" && |
| 129 | + header.metadata?.from?.column === "value" |
| 130 | + ) { |
| 131 | + return <CloudflareKvValue props={props} />; |
| 132 | + } |
| 133 | + |
56 | 134 | switch (valueType ?? header.metadata.type) { |
57 | 135 | case ColumnType.INTEGER: |
58 | 136 | return ( |
|
0 commit comments