This repository was archived by the owner on Aug 6, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 66
Expand file tree
/
Copy pathSkillCard.tsx
More file actions
276 lines (263 loc) · 7.21 KB
/
Copy pathSkillCard.tsx
File metadata and controls
276 lines (263 loc) · 7.21 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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
import {
CaretDown,
CaretRight,
Folder,
Package,
Robot,
Storefront,
User,
Warning,
} from "@phosphor-icons/react";
import type {
SkillAnalysis,
SkillIssue,
} from "@posthog/core/skills/analyzeSkills";
import type { SkillInfo, SkillSource } from "@posthog/shared";
import { Badge, Flex, Text, Tooltip } from "@radix-ui/themes";
import { useEffect, useRef } from "react";
import { SkillListCard } from "./SkillListCard";
import type { SkillViewMode } from "./skillsViewStore";
export function humanizeName(name: string): string {
return name
.split("-")
.map((w) => w.charAt(0).toUpperCase() + w.slice(1))
.join(" ");
}
export const SOURCE_CONFIG: Record<
SkillSource,
{ icon: typeof Package; label: string; sectionTitle: string }
> = {
user: { icon: User, label: "User", sectionTitle: "Your skills" },
bundled: {
icon: Package,
label: "PostHog Code",
sectionTitle: "PostHog Code",
},
repo: { icon: Folder, label: "Repo", sectionTitle: "Repository" },
marketplace: {
icon: Storefront,
label: "Marketplace",
sectionTitle: "Marketplace",
},
codex: { icon: Robot, label: "Codex", sectionTitle: "Codex" },
};
interface SkillCardProps {
skill: SkillInfo;
isSelected: boolean;
onClick: () => void;
/** When true, scroll this card into view once (used for deep-linked skills). */
scrollIntoView?: boolean;
onScrolledIntoView?: () => void;
issues?: SkillIssue[];
}
export function SkillCard({
skill,
isSelected,
onClick,
scrollIntoView,
onScrolledIntoView,
issues = [],
}: SkillCardProps) {
const config = SOURCE_CONFIG[skill.source];
const Icon = config?.icon ?? Package;
const ref = useRef<HTMLDivElement>(null);
useEffect(() => {
if (!scrollIntoView) return;
ref.current?.scrollIntoView({ block: "center" });
onScrolledIntoView?.();
}, [scrollIntoView, onScrolledIntoView]);
return (
<SkillListCard
cardRef={ref}
icon={<Icon size={14} weight="duotone" className="text-gray-11" />}
title={humanizeName(skill.name)}
subtitle={skill.description || undefined}
isSelected={isSelected}
onClick={onClick}
trailing={
<>
{issues.length > 0 && (
<Tooltip
content={
<Flex direction="column" gap="1">
{issues.map((issue) => (
<Text key={issue.message} size="1">
{issue.message}
</Text>
))}
</Flex>
}
>
<Warning size={14} className="shrink-0 text-amber-11" />
</Tooltip>
)}
{skill.repoName && (
<Badge size="1" variant="soft" color="gray" className="shrink-0">
{skill.repoName}
</Badge>
)}
</>
}
/>
);
}
export function SkillGridCard({
skill,
isSelected,
onClick,
scrollIntoView,
onScrolledIntoView,
issues = [],
}: SkillCardProps) {
const config = SOURCE_CONFIG[skill.source];
const Icon = config?.icon ?? Package;
const ref = useRef<HTMLButtonElement>(null);
useEffect(() => {
if (!scrollIntoView) return;
ref.current?.scrollIntoView({ block: "center" });
onScrolledIntoView?.();
}, [scrollIntoView, onScrolledIntoView]);
return (
<button
ref={ref}
type="button"
onClick={onClick}
className={`flex w-full cursor-pointer flex-col gap-2 rounded-lg border p-2.5 text-left transition-colors ${
isSelected
? "border-accent-8 bg-accent-3"
: "border-gray-6 bg-gray-2 hover:border-gray-8 hover:bg-gray-3"
}`}
>
<div className="flex items-start justify-between">
<div className="flex items-center justify-center rounded bg-gray-4 p-1.5">
<Icon size={16} weight="duotone" className="text-gray-11" />
</div>
{issues.length > 0 && (
<Warning size={12} className="shrink-0 text-amber-11" />
)}
</div>
<div className="min-w-0">
<p className="line-clamp-2 font-medium text-[12px] text-gray-12 leading-tight">
{humanizeName(skill.name)}
</p>
{skill.description && (
<p className="mt-0.5 line-clamp-2 text-[11px] text-gray-9 leading-tight">
{skill.description}
</p>
)}
</div>
</button>
);
}
interface SkillCardListProps {
skills: SkillInfo[];
selectedPath: string | null;
onSelect: (path: string) => void;
scrollToPath: string | null;
onScrolledIntoView: () => void;
analysis?: SkillAnalysis;
viewMode?: SkillViewMode;
}
export function SkillCardList({
skills,
selectedPath,
onSelect,
scrollToPath,
onScrolledIntoView,
analysis,
viewMode = "list",
}: SkillCardListProps) {
if (viewMode === "grid") {
return (
<div className="grid grid-cols-2 gap-2">
{skills.map((skill) => (
<SkillGridCard
key={skill.path}
skill={skill}
isSelected={selectedPath === skill.path}
onClick={() => onSelect(skill.path)}
scrollIntoView={scrollToPath === skill.path}
onScrolledIntoView={onScrolledIntoView}
issues={analysis?.[skill.path]}
/>
))}
</div>
);
}
return (
<Flex direction="column" gap="1">
{skills.map((skill) => (
<SkillCard
key={skill.path}
skill={skill}
isSelected={selectedPath === skill.path}
onClick={() => onSelect(skill.path)}
scrollIntoView={scrollToPath === skill.path}
onScrolledIntoView={onScrolledIntoView}
issues={analysis?.[skill.path]}
/>
))}
</Flex>
);
}
interface SkillSectionProps {
title: string;
skills: SkillInfo[];
selectedPath: string | null;
onSelect: (path: string) => void;
scrollToPath: string | null;
onScrolledIntoView: () => void;
analysis?: SkillAnalysis;
viewMode?: SkillViewMode;
isCollapsed?: boolean;
onToggle?: () => void;
}
export function SkillSection({
title,
skills,
selectedPath,
onSelect,
scrollToPath,
onScrolledIntoView,
analysis,
viewMode = "list",
isCollapsed = false,
onToggle,
}: SkillSectionProps) {
return (
<Flex direction="column" gap="1">
{onToggle ? (
<button
type="button"
onClick={onToggle}
className="mb-1 flex items-center gap-1.5 text-left text-gray-9 hover:text-gray-11"
>
{isCollapsed ? (
<CaretRight size={11} className="shrink-0" />
) : (
<CaretDown size={11} className="shrink-0" />
)}
<span className="font-medium text-[12px] uppercase tracking-wider">
{title}
</span>
<span className="text-[11px] text-gray-7">{skills.length}</span>
</button>
) : (
<Text className="mb-1 font-medium text-[12px] text-gray-9 uppercase tracking-wider">
{title}
</Text>
)}
{!isCollapsed && (
<SkillCardList
skills={skills}
selectedPath={selectedPath}
onSelect={onSelect}
scrollToPath={scrollToPath}
onScrolledIntoView={onScrolledIntoView}
analysis={analysis}
viewMode={viewMode}
/>
)}
</Flex>
);
}