|
| 1 | +import type { Meta, StoryObj } from "@storybook/react"; |
| 2 | +import { useMutation } from "@tanstack/react-query"; |
| 3 | +import { useState } from "react"; |
| 4 | +import { mobileViewport } from "../../../../../../stories/utils"; |
| 5 | +import { BluePrintPlaygroundUI } from "./BluePrintPlayground"; |
| 6 | +import type { BluePrintMetadata } from "./utils"; |
| 7 | + |
| 8 | +const meta = { |
| 9 | + title: "Insight/BluePrintPlayground", |
| 10 | + component: Story, |
| 11 | + parameters: { |
| 12 | + nextjs: { |
| 13 | + appDirectory: true, |
| 14 | + }, |
| 15 | + }, |
| 16 | +} satisfies Meta<typeof Story>; |
| 17 | + |
| 18 | +export default meta; |
| 19 | +type Story = StoryObj<typeof meta>; |
| 20 | + |
| 21 | +export const Desktop: Story = { |
| 22 | + args: { |
| 23 | + metadata: getBlueprintMetadata().transactionsMetadata, |
| 24 | + }, |
| 25 | +}; |
| 26 | + |
| 27 | +export const Mobile: Story = { |
| 28 | + args: { |
| 29 | + metadata: getBlueprintMetadata().transactionsMetadata, |
| 30 | + }, |
| 31 | + parameters: { |
| 32 | + viewport: mobileViewport("iphone14"), |
| 33 | + }, |
| 34 | +}; |
| 35 | + |
| 36 | +function Story() { |
| 37 | + return ( |
| 38 | + <div className="flex flex-col gap-10"> |
| 39 | + <Variant metadata={getBlueprintMetadata().transactionsMetadata} /> |
| 40 | + <Variant metadata={getBlueprintMetadata().eventsMetadata} /> |
| 41 | + <Variant metadata={getBlueprintMetadata().tokensMetadata} /> |
| 42 | + </div> |
| 43 | + ); |
| 44 | +} |
| 45 | + |
| 46 | +function Variant(props: { |
| 47 | + metadata: BluePrintMetadata; |
| 48 | +}) { |
| 49 | + const [abortController, setAbortController] = |
| 50 | + useState<AbortController | null>(null); |
| 51 | + |
| 52 | + const mutation = useMutation({ |
| 53 | + mutationFn: async () => { |
| 54 | + const controller = new AbortController(); |
| 55 | + setAbortController(controller); |
| 56 | + const start = performance.now(); |
| 57 | + const promise = new Promise((resolve) => |
| 58 | + setTimeout(resolve, Math.random() * 4000), |
| 59 | + ); |
| 60 | + await Promise.race([ |
| 61 | + promise, |
| 62 | + new Promise((_, reject) => |
| 63 | + controller.signal.addEventListener("abort", reject), |
| 64 | + ), |
| 65 | + ]); |
| 66 | + |
| 67 | + const dummyResponse = { |
| 68 | + data: { |
| 69 | + title: "This is a dummy response", |
| 70 | + content: crypto.getRandomValues(new Uint8Array(100)), |
| 71 | + }, |
| 72 | + }; |
| 73 | + |
| 74 | + return { |
| 75 | + data: JSON.stringify(dummyResponse, null, 2), |
| 76 | + status: 200, |
| 77 | + time: performance.now() - start, |
| 78 | + }; |
| 79 | + }, |
| 80 | + }); |
| 81 | + return ( |
| 82 | + <div className="flex min-h-[800px] flex-col"> |
| 83 | + <BluePrintPlaygroundUI |
| 84 | + metadata={props.metadata} |
| 85 | + backLink="/" |
| 86 | + isPending={mutation.isPending} |
| 87 | + onRun={async () => { |
| 88 | + mutation.mutateAsync(); |
| 89 | + }} |
| 90 | + response={mutation.data} |
| 91 | + clientId="68665db28327c771c9a1bd5fc4580a0a" |
| 92 | + abortRequest={() => { |
| 93 | + abortController?.abort(); |
| 94 | + }} |
| 95 | + /> |
| 96 | + </div> |
| 97 | + ); |
| 98 | +} |
| 99 | + |
| 100 | +function getBlueprintMetadata() { |
| 101 | + const transactionsMetadata: BluePrintMetadata = { |
| 102 | + domain: "https://{chainId}.insight.thirdweb.com", |
| 103 | + path: "/v1/{clientId}/transactions", |
| 104 | + parameters: [ |
| 105 | + { |
| 106 | + name: "chainId", |
| 107 | + in: "path", |
| 108 | + required: true, |
| 109 | + description: "Chain ID", |
| 110 | + type: "string", |
| 111 | + }, |
| 112 | + { |
| 113 | + name: "filter", |
| 114 | + in: "query", |
| 115 | + description: "Filter parameters", |
| 116 | + type: "string", |
| 117 | + }, |
| 118 | + { |
| 119 | + name: "group_by", |
| 120 | + in: "query", |
| 121 | + description: "Field to group results by", |
| 122 | + type: "string", |
| 123 | + }, |
| 124 | + { |
| 125 | + name: "sort_by", |
| 126 | + in: "query", |
| 127 | + description: "Field to sort results by", |
| 128 | + type: "string", |
| 129 | + }, |
| 130 | + { |
| 131 | + name: "sort_order", |
| 132 | + in: "query", |
| 133 | + description: "Sort order (asc or desc)", |
| 134 | + type: "string", |
| 135 | + }, |
| 136 | + { |
| 137 | + name: "page", |
| 138 | + in: "query", |
| 139 | + description: "Page number for pagination", |
| 140 | + type: "integer", |
| 141 | + }, |
| 142 | + { |
| 143 | + name: "limit", |
| 144 | + in: "query", |
| 145 | + description: "Number of items per page", |
| 146 | + type: "integer", |
| 147 | + }, |
| 148 | + { |
| 149 | + name: "aggregate", |
| 150 | + in: "query", |
| 151 | + description: "List of aggregate functions to apply", |
| 152 | + type: "array", |
| 153 | + }, |
| 154 | + ], |
| 155 | + title: "Transactions", |
| 156 | + description: "Retrieve all transactions across all contracts", |
| 157 | + }; |
| 158 | + |
| 159 | + const eventsMetadata: BluePrintMetadata = { |
| 160 | + domain: "https://{chainId}.insight.thirdweb.com", |
| 161 | + path: "/v1/{clientId}/events", |
| 162 | + parameters: [ |
| 163 | + { |
| 164 | + name: "chainId", |
| 165 | + in: "path", |
| 166 | + required: true, |
| 167 | + description: "Chain ID", |
| 168 | + type: "string", |
| 169 | + }, |
| 170 | + { |
| 171 | + name: "filter", |
| 172 | + in: "query", |
| 173 | + description: "Filter parameters", |
| 174 | + type: "string", |
| 175 | + }, |
| 176 | + { |
| 177 | + name: "group_by", |
| 178 | + in: "query", |
| 179 | + description: "Field to group results by", |
| 180 | + type: "string", |
| 181 | + }, |
| 182 | + { |
| 183 | + name: "sort_by", |
| 184 | + in: "query", |
| 185 | + description: "Field to sort results by", |
| 186 | + type: "string", |
| 187 | + }, |
| 188 | + { |
| 189 | + name: "sort_order", |
| 190 | + in: "query", |
| 191 | + description: "Sort order (asc or desc)", |
| 192 | + type: "string", |
| 193 | + }, |
| 194 | + { |
| 195 | + name: "page", |
| 196 | + in: "query", |
| 197 | + description: "Page number for pagination", |
| 198 | + type: "integer", |
| 199 | + }, |
| 200 | + { |
| 201 | + name: "limit", |
| 202 | + in: "query", |
| 203 | + description: "Number of items per page", |
| 204 | + type: "integer", |
| 205 | + }, |
| 206 | + { |
| 207 | + name: "aggregate", |
| 208 | + in: "query", |
| 209 | + description: "List of aggregate functions to apply", |
| 210 | + type: "array", |
| 211 | + }, |
| 212 | + ], |
| 213 | + title: "Events", |
| 214 | + description: "Retrieve all logs across all contracts", |
| 215 | + }; |
| 216 | + |
| 217 | + const tokensMetadata: BluePrintMetadata = { |
| 218 | + domain: "https://{chainId}.insight.thirdweb.com", |
| 219 | + path: "/v1/{clientId}/tokens/erc20/:ownerAddress", |
| 220 | + parameters: [ |
| 221 | + { |
| 222 | + name: "ownerAddress", |
| 223 | + in: "path", |
| 224 | + required: true, |
| 225 | + type: "string", |
| 226 | + }, |
| 227 | + { |
| 228 | + name: "clientId", |
| 229 | + in: "path", |
| 230 | + required: false, |
| 231 | + type: "string", |
| 232 | + }, |
| 233 | + ], |
| 234 | + title: "Tokens", |
| 235 | + description: "Retrieve tokens balances for a given owner address", |
| 236 | + }; |
| 237 | + |
| 238 | + return { |
| 239 | + transactionsMetadata, |
| 240 | + eventsMetadata, |
| 241 | + tokensMetadata, |
| 242 | + }; |
| 243 | +} |
0 commit comments