Branchwell

Skillfarm / website · skillfarm.ai / src / views / components.tsx

Raw 8.0 KB on c724bfa127f7a93b48788385cedf1e60cb4e7943
1import type { Child, FC } from 'hono/jsx';
2import type { SkillCard as SkillCardRow } from '../lib/skills';
3import { categoryName, platformName, splitPlatforms } from '../lib/skills';
4import type { SendOption } from '../lib/install';
5import { nextTier, tierFor, tierProgress } from '../lib/tiers';
6import { compactNumber, timeAgo } from '../lib/util';
7import { defaultAvatar, icon, spot, tierArt } from './art';
8
9type AvatarUser = { id?: string; handle: string; avatar_key: string | null; avatar_fallback: string | null };
10
11export const Avatar: FC<{ user: AvatarUser; size?: number }> = ({ user, size = 40 }) => {
12 if (user.avatar_key) {
13 const small = size <= 96 ? user.avatar_key.replace(/(\.\w+)$/, '-128$1') : user.avatar_key;
14 return <img class="avatar" src={`/${small}`} alt="" width={size} height={size} loading="lazy" />;
15 }
16 if (user.avatar_fallback) return <img class="avatar" src={user.avatar_fallback} alt="" width={size} height={size} loading="lazy" referrerpolicy="no-referrer" />;
17 return defaultAvatar(user.id ?? user.handle, size);
18};
19
20export const TierBadge: FC<{ seeds: number; size?: number; label?: boolean }> = ({ seeds, size = 22, label = true }) => {
21 const t = tierFor(seeds);
22 return (
23 <span class={`tier tier-${t.key}`} title={`${t.name} tier`}>
24 {tierArt(t.key, size)}
25 {label ? <span>{t.name}</span> : null}
26 </span>
27 );
28};
29
30export const TierProgress: FC<{ seeds: number }> = ({ seeds }) => {
31 const next = nextTier(seeds);
32 return (
33 <div class="tier-progress">
34 <div class="bar" role="img" aria-label={next ? `${tierProgress(seeds)} percent of the way to ${next.name}` : 'Top tier reached'}>
35 <span style={`width:${tierProgress(seeds)}%`}></span>
36 </div>
37 <p>{next ? `${(next.threshold - seeds).toLocaleString('en-US')} Seeds to ${next.name}` : 'Top tier reached'}</p>
38 </div>
39 );
40};
41
42export const LikeButton: FC<{ skill: { id: string; likes_count: number; liked?: number | null; owner_id: string }; viewerId?: string | null; large?: boolean }> = ({ skill, viewerId, large }) => {
43 const own = viewerId === skill.owner_id;
44 const cls = `like${skill.liked ? ' is-liked' : ''}${large ? ' like-lg' : ''}`;
45 if (own) {
46 return (
47 <span class={cls + ' is-static'} title="Skillfarm likes">
48 {icon('leaf', large ? 20 : 17)}
49 <span>{compactNumber(skill.likes_count)}</span>
50 </span>
51 );
52 }
53 return (
54 <button type="button" class={cls} data-like={skill.id} aria-pressed={skill.liked ? 'true' : 'false'} title={skill.liked ? 'Remove your like' : 'Like this skill'}>
55 <span class="like-off">{icon('leaf', large ? 20 : 17)}</span>
56 <span class="like-on">{icon('leaf-filled', large ? 20 : 17)}</span>
57 <span data-like-count>{compactNumber(skill.likes_count)}</span>
58 <span class="sr-only">likes</span>
59 </button>
60 );
61};
62
63export const Stars: FC<{ skill: { github_repo: string | null; stars: number | null; repo_fetched_at: number | null; repo_unavailable: number | null } }> = ({ skill }) => {
64 if (!skill.github_repo || skill.stars == null) return null;
65 const tip = `Stars on ${skill.github_repo}, checked ${timeAgo(skill.repo_fetched_at)}${skill.repo_unavailable ? '. Repo unavailable.' : ''}`;
66 return (
67 <span class="stat stat-stars" title={tip}>
68 {icon('star', 17)}
69 <span>{compactNumber(skill.stars)}</span>
70 <span class="sr-only">GitHub stars on {skill.github_repo}</span>
71 </span>
72 );
73};
74
75/** Marks Sprout wherever a tier badge would sit for a member. */
76export const AgentBadge: FC<{ label?: string }> = ({ label = 'Site agent' }) => (
77 <span class="pill pill-agent" title="Sprout is Skillfarm's own agent, not a person">
78 {icon('plug', 13)} {label}
79 </span>
80);
81
82export const SkillCard: FC<{ skill: SkillCardRow; viewerId?: string | null; showState?: boolean }> = ({ skill, viewerId, showState }) => {
83 const href = `/skills/${skill.owner_handle}/${skill.slug}`;
84 const t = tierFor(skill.owner_seeds);
85 const pick = skill.kind === 'link';
86 const cat = categoryName(skill.category);
87 return (
88 <article class={`card skill-card${pick ? ' skill-card-pick' : ''}`}>
89 <div class="skill-card-top">
90 <h3>
91 <a href={href}>{skill.title}</a>
92 </h3>
93 {showState && skill.state !== 'live' ? <span class={`pill pill-${skill.state}`}>{stateLabel(skill.state)}</span> : null}
94 {pick ? <span class="pill pill-pick" title="Recommended by Sprout, the Skillfarm agent. It links to the maker's repo.">Sprout's pick</span> : null}
95 </div>
96 <p class="skill-summary">{skill.summary}</p>
97 <ul class="chips">
98 {cat ? <li class="chip-cat">{cat}</li> : null}
99 {splitPlatforms(skill.platforms).slice(0, 4).map((p) => (
100 <li>{platformName(p)}</li>
101 ))}
102 </ul>
103 <div class="skill-card-foot">
104 {pick ? (
105 <span class="by by-maker" title={`Made by ${skill.maker}`}>
106 {icon('github', 20)}
107 <span>{skill.maker}</span>
108 </span>
109 ) : (
110 <a class="by" href={`/u/${skill.owner_handle}`}>
111 <Avatar user={{ id: skill.owner_id, handle: skill.owner_handle, avatar_key: skill.owner_avatar_key, avatar_fallback: skill.owner_avatar_fallback }} size={26} />
112 <span>{skill.owner_handle}</span>
113 {t.threshold > 0 ? <TierBadge seeds={skill.owner_seeds} size={18} label={false} /> : null}
114 </a>
115 )}
116 <div class="stats">
117 <Stars skill={skill} />
118 <LikeButton skill={skill} viewerId={viewerId} />
119 </div>
120 </div>
121 </article>
122 );
123};
124
125export function stateLabel(state: string): string {
126 return ({ pending: 'In review', live: 'Live', rejected: 'Not approved', hidden: 'Hidden', removed: 'Removed' } as Record<string, string>)[state] ?? state;
127}
128
129export const Empty: FC<{ title: string; children?: Child; art?: 'empty' | 'plant' | 'harvest' | 'tend' | 'mcp' }> = ({ title, children, art = 'empty' }) => (
130 <div class="empty">
131 {spot(art)}
132 <h3>{title}</h3>
133 <div>{children}</div>
134 </div>
135);
136
137export const Copy: FC<{ text: string; label?: string }> = ({ text, label }) => (
138 <div class="copy">
139 <pre>
140 <code>{text}</code>
141 </pre>
142 <button type="button" class="btn btn-ghost btn-sm" data-copy={text}>
143 {icon('copy', 16)}
144 <span>{label ?? 'Copy'}</span>
145 </button>
146 </div>
147);
148
149/**
150 * "Send to agent": one button that hands a prompt to the visitor's agent, and a picker that remembers which agent that is.
151 * Without script the button still works for the first agent, because it is a plain link.
152 */
153export const SendToAgent: FC<{ options: SendOption[]; small?: boolean; block?: boolean }> = ({ options, small, block }) => {
154 if (!options.length) return null;
155 const first = options[0];
156 return (
157 <div class={`send${options.length === 1 ? ' is-single' : ''}${block ? ' send-block' : ''}`} data-send>
158 <div class="send-row">
159 <a class={`btn btn-primary${small ? ' btn-sm' : ''}`} href={first.href ?? '#'} data-send-go rel="nofollow">
160 {icon('send', small ? 16 : 18)}
161 <span data-send-label>{first.href ? 'Send to' : 'Copy for'} {first.name}</span>
162 </a>
163 <label class="send-pick">
164 <span class="send-pick-label">Your agent</span>
165 <select data-send-pick>
166 {options.map((o) => (
167 <option value={o.agent} data-href={o.href ?? ''} data-prompt={o.prompt} data-note={o.note} data-after={o.after}>{o.name}</option>
168 ))}
169 </select>
170 </label>
171 </div>
172 <p class="send-note fine" data-send-note aria-live="polite">{first.note}</p>
173 </div>
174 );
175};
176
177export const Pager: FC<{ page: number; hasMore: boolean; base: string }> = ({ page, hasMore, base }) => {
178 if (page <= 1 && !hasMore) return null;
179 const sep = base.includes('?') ? '&' : '?';
180 return (
181 <nav class="pager" aria-label="Pages">
182 {page > 1 ? <a class="btn btn-ghost btn-sm" href={`${base}${sep}page=${page - 1}`}>Newer</a> : <span></span>}
183 <span>Page {page}</span>
184 {hasMore ? <a class="btn btn-ghost btn-sm" href={`${base}${sep}page=${page + 1}`}>Older</a> : <span></span>}
185 </nav>
186 );
187};