Branchwell

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

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