-
Notifications
You must be signed in to change notification settings - Fork 675
Expand file tree
/
Copy pathcode-segment.client.tsx
More file actions
154 lines (141 loc) · 3.55 KB
/
Copy pathcode-segment.client.tsx
File metadata and controls
154 lines (141 loc) · 3.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
"use client";
import type React from "react";
import { type Dispatch, type SetStateAction, useMemo } from "react";
import { CodeClient } from "@/components/ui/code/code.client";
import { TabButtons } from "@/components/ui/tabs";
import { cn } from "@/lib/utils";
export type CodeEnvironment =
| "api"
| "javascript"
| "typescript"
| "react"
| "react-native"
| "dotnet"
| "unity"
| "curl";
type SupportedEnvironment = {
environment: CodeEnvironment;
title: string;
};
type CodeSnippet = Partial<Record<CodeEnvironment, string>>;
const Environments: SupportedEnvironment[] = [
{
environment: "api",
title: "API",
},
{
environment: "javascript",
title: "JavaScript",
},
{
environment: "typescript",
title: "TypeScript",
},
{
environment: "react",
title: "React",
},
{
environment: "react-native",
title: "React Native",
},
{
environment: "dotnet",
title: ".NET",
},
{
environment: "unity",
title: "Unity",
},
{
environment: "curl",
title: "cURL",
},
];
interface CodeSegmentProps {
snippet: CodeSnippet;
environment: CodeEnvironment;
setEnvironment:
| Dispatch<SetStateAction<CodeEnvironment>>
| ((language: CodeEnvironment) => void);
isInstallCommand?: boolean;
hideTabs?: boolean;
onlyTabs?: boolean;
codeContainerClassName?: string;
}
export const CodeSegment: React.FC<CodeSegmentProps> = ({
snippet,
environment,
setEnvironment,
isInstallCommand,
hideTabs,
onlyTabs,
codeContainerClassName,
}) => {
const activeEnvironment: CodeEnvironment = useMemo(() => {
return (
snippet[environment] ? environment : Object.keys(snippet)[0]
) as CodeEnvironment;
}, [environment, snippet]);
const activeSnippet = useMemo(() => {
return snippet[activeEnvironment];
}, [activeEnvironment, snippet]);
const lines = useMemo(
() => (activeSnippet ? activeSnippet.split("\n") : []),
[activeSnippet],
);
const code = lines.join("\n").trim();
const environments = Environments.filter(
(env) =>
Object.keys(snippet).includes(env.environment) &&
snippet[env.environment],
);
return (
<div
className={
"flex flex-col overflow-hidden rounded-lg border border-border grow"
}
>
{!hideTabs && (
<TabButtons
hideBottomLine={!!onlyTabs}
tabContainerClassName="px-3 pt-1.5 gap-0.5"
tabIconClassName="size-4"
tabs={environments.map((env) => ({
isActive: activeEnvironment === env.environment,
label: env.title,
name: env.title,
onClick: () => setEnvironment(env.environment),
}))}
/>
)}
{onlyTabs ? null : (
<CodeClient
className={cn(
"rounded-none border-none grow",
codeContainerClassName,
)}
scrollableContainerClassName="h-full"
code={code}
lang={
isInstallCommand
? activeEnvironment === "react-native"
? "tsx"
: "bash"
: activeEnvironment === "react" ||
activeEnvironment === "react-native"
? "tsx"
: activeEnvironment === "unity" ||
activeEnvironment === "dotnet"
? "cpp"
: activeEnvironment === "api"
? "javascript"
: activeEnvironment === "curl"
? "bash"
: activeEnvironment
}
/>
)}
</div>
);
};