|
| 1 | +import { Codesnippet } from "@/components/CodeSnippet"; |
| 2 | +import { CollapsibleChevron } from "@/components/collapsible-chevron"; |
| 3 | +import { Deployment } from "@/types/deployments"; |
| 4 | +import { |
| 5 | + CollapsibleContent, |
| 6 | + CollapsibleHeader, |
| 7 | + CollapsiblePanel, |
| 8 | + CollapsibleTrigger, |
| 9 | + Skeleton, |
| 10 | + Tabs, |
| 11 | + TabsContent, |
| 12 | + TabsList, |
| 13 | + TabsTrigger |
| 14 | +} from "@zenml-io/react-component-library"; |
| 15 | +import { useState } from "react"; |
| 16 | +import { DeploymentDetailWrapper } from "./fetch-wrapper"; |
| 17 | +import { |
| 18 | + buildCurl, |
| 19 | + buildPythonCommand, |
| 20 | + buildTs, |
| 21 | + buildZenCommand, |
| 22 | + replaceAuthTokenPlaceholder |
| 23 | +} from "./command-builder"; |
| 24 | +import { useQuery } from "@tanstack/react-query"; |
| 25 | +import { pipelineSnapshotQueries } from "@/data/pipeline-snapshots"; |
| 26 | +import { JSONSchema } from "@/types/forms"; |
| 27 | +import { JSONSchemaFaker } from "@/lib/json-faker"; |
| 28 | + |
| 29 | +export function DeploymentCodeCollapsible() { |
| 30 | + return <DeploymentDetailWrapper Component={DeploymentCodeContent} />; |
| 31 | +} |
| 32 | + |
| 33 | +type Props = { |
| 34 | + deployment: Deployment; |
| 35 | +}; |
| 36 | + |
| 37 | +function DeploymentCodeContent({ deployment }: Props) { |
| 38 | + const [open, setOpen] = useState(true); |
| 39 | + const [selectedTab, setSelectedTab] = useState("cli"); |
| 40 | + |
| 41 | + const snapshotId = deployment.resources?.snapshot?.id; |
| 42 | + |
| 43 | + const snapshotQuery = useQuery({ |
| 44 | + ...pipelineSnapshotQueries.detail(snapshotId!), |
| 45 | + enabled: !!snapshotId |
| 46 | + }); |
| 47 | + |
| 48 | + if (!snapshotId) return null; |
| 49 | + if (snapshotQuery.isPending) return <Skeleton className="h-[200px] w-full" />; |
| 50 | + if (snapshotQuery.isError) return null; |
| 51 | + const snapshot = snapshotQuery.data; |
| 52 | + |
| 53 | + const schema = snapshot.metadata?.pipeline_spec?.input_schema as JSONSchema; |
| 54 | + |
| 55 | + const defaultBody = schema ? JSONSchemaFaker.generate(schema) : {}; |
| 56 | + |
| 57 | + const url = deployment.body?.url; |
| 58 | + |
| 59 | + const authKey = deployment.metadata?.auth_key ?? undefined; |
| 60 | + |
| 61 | + return ( |
| 62 | + <CollapsiblePanel open={open} onOpenChange={setOpen}> |
| 63 | + <CollapsibleHeader className="flex items-center gap-[10px]"> |
| 64 | + <CollapsibleTrigger className="flex w-full gap-2"> |
| 65 | + <CollapsibleChevron open={open} /> |
| 66 | + Endpoint |
| 67 | + </CollapsibleTrigger> |
| 68 | + </CollapsibleHeader> |
| 69 | + <CollapsibleContent className="space-y-5 border-t border-theme-border-moderate bg-theme-surface-primary px-5 py-3"> |
| 70 | + <Tabs value={selectedTab} onValueChange={setSelectedTab}> |
| 71 | + <TabsList> |
| 72 | + <TabsTrigger className="flex items-center gap-2 truncate text-text-md" value="cli"> |
| 73 | + <span>CLI</span> |
| 74 | + </TabsTrigger> |
| 75 | + <TabsTrigger className="flex items-center gap-2 truncate text-text-md" value="python"> |
| 76 | + <span>Python</span> |
| 77 | + </TabsTrigger> |
| 78 | + {!!url && ( |
| 79 | + <TabsTrigger className="flex items-center gap-2 truncate text-text-md" value="curl"> |
| 80 | + <span>cURL</span> |
| 81 | + </TabsTrigger> |
| 82 | + )} |
| 83 | + {!!url && ( |
| 84 | + <TabsTrigger className="flex items-center gap-2 truncate text-text-md" value="ts"> |
| 85 | + <span>Typescript</span> |
| 86 | + </TabsTrigger> |
| 87 | + )} |
| 88 | + </TabsList> |
| 89 | + |
| 90 | + <TabsContent className="m-0 mt-5 border-0 bg-transparent p-0" value="cli"> |
| 91 | + <Codesnippet |
| 92 | + fullWidth |
| 93 | + highlightCode |
| 94 | + language="bash" |
| 95 | + wrap |
| 96 | + code={buildZenCommand(deployment.name, defaultBody)} |
| 97 | + /> |
| 98 | + </TabsContent> |
| 99 | + <TabsContent className="m-0 mt-5 border-0 bg-transparent p-0" value="python"> |
| 100 | + <Codesnippet |
| 101 | + fullWidth |
| 102 | + highlightCode |
| 103 | + language="python" |
| 104 | + wrap |
| 105 | + code={buildPythonCommand({ |
| 106 | + deploymentId: deployment.id, |
| 107 | + defaultBody: defaultBody |
| 108 | + })} |
| 109 | + /> |
| 110 | + </TabsContent> |
| 111 | + {!!url && ( |
| 112 | + <TabsContent className="m-0 mt-5 border-0 bg-transparent p-0" value="curl"> |
| 113 | + {(() => { |
| 114 | + const curlCode = buildCurl(url, defaultBody, authKey); |
| 115 | + return ( |
| 116 | + <Codesnippet |
| 117 | + fullWidth |
| 118 | + highlightCode |
| 119 | + wrap |
| 120 | + language="bash" |
| 121 | + copyCode={replaceAuthTokenPlaceholder(curlCode, authKey)} |
| 122 | + code={curlCode} |
| 123 | + /> |
| 124 | + ); |
| 125 | + })()} |
| 126 | + </TabsContent> |
| 127 | + )} |
| 128 | + {!!url && ( |
| 129 | + <TabsContent className="m-0 mt-5 border-0 bg-transparent p-0" value="ts"> |
| 130 | + {(() => { |
| 131 | + const tsCode = buildTs(url, defaultBody, authKey); |
| 132 | + return ( |
| 133 | + <Codesnippet |
| 134 | + fullWidth |
| 135 | + wrap |
| 136 | + highlightCode |
| 137 | + language="ts" |
| 138 | + copyCode={replaceAuthTokenPlaceholder(tsCode, authKey)} |
| 139 | + code={tsCode} |
| 140 | + /> |
| 141 | + ); |
| 142 | + })()} |
| 143 | + </TabsContent> |
| 144 | + )} |
| 145 | + </Tabs> |
| 146 | + </CollapsibleContent> |
| 147 | + </CollapsiblePanel> |
| 148 | + ); |
| 149 | +} |
0 commit comments