@celljs/ta-extensions 4.6.1 → 4.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.en.md +27 -0
- package/README.md +10 -3
- package/host/index.mjs +331 -1
- package/package.json +3 -3
- package/skills/ta-package/SKILL.md +1 -1
- package/web/index.js +937 -54
- package/web-src/actions.ts +20 -0
- package/web-src/context.ts +24 -0
- package/web-src/host.ts +16 -2
- package/web-src/i18n.ts +85 -2
- package/web-src/index.tsx +26 -22
- package/web-src/market-detail.tsx +138 -0
- package/web-src/market-parts.tsx +94 -0
- package/web-src/market-store.ts +116 -0
- package/web-src/market-style.ts +94 -0
- package/web-src/market-util.spec.ts +79 -0
- package/web-src/market-util.ts +239 -0
- package/web-src/market-view.tsx +245 -0
- package/web-src/market.spec.ts +154 -0
- package/web-src/market.ts +232 -0
- package/web-src/page.tsx +124 -92
- package/web-src/signal.ts +34 -0
- package/web-src/style.ts +4 -2
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
import { describe, expect, it } from 'bun:test';
|
|
2
|
+
import { type Fetch, NpmMarket, PAGE_SIZE } from './market';
|
|
3
|
+
|
|
4
|
+
function found(name: string, extra: Record<string, unknown> = {}) {
|
|
5
|
+
return {
|
|
6
|
+
downloads: { monthly: 1200 },
|
|
7
|
+
package: {
|
|
8
|
+
name,
|
|
9
|
+
version: '1.0.0',
|
|
10
|
+
description: `${name} for pi`,
|
|
11
|
+
keywords: ['pi-package', 'extension'],
|
|
12
|
+
date: '2026-11-01T00:00:00Z',
|
|
13
|
+
publisher: { username: 'you' },
|
|
14
|
+
links: {},
|
|
15
|
+
...extra,
|
|
16
|
+
},
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/** npm, answering each URL by the function; it keeps the URLs asked. */
|
|
21
|
+
function registry(answer: (url: URL) => unknown) {
|
|
22
|
+
const urls: string[] = [];
|
|
23
|
+
const fetch: Fetch = async url => {
|
|
24
|
+
urls.push(url);
|
|
25
|
+
const body = answer(new URL(url));
|
|
26
|
+
if (body === undefined) {
|
|
27
|
+
return new Response('', { status: 404 });
|
|
28
|
+
}
|
|
29
|
+
return typeof body === 'string' ? new Response(body) : Response.json(body);
|
|
30
|
+
};
|
|
31
|
+
return { fetch, urls };
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
describe('NpmMarket', () => {
|
|
35
|
+
it('searches the pi packages of npm, and pages on until the end', async () => {
|
|
36
|
+
const npm = registry(url => {
|
|
37
|
+
expect(url.searchParams.get('text')).toBe('keywords:pi-package');
|
|
38
|
+
const from = Number(url.searchParams.get('from'));
|
|
39
|
+
const count = from === 0 ? PAGE_SIZE : 3;
|
|
40
|
+
return { total: PAGE_SIZE + 3, objects: Array.from({ length: count }, (_, i) => found(`pkg-${from + i}`)) };
|
|
41
|
+
});
|
|
42
|
+
const market = new NpmMarket(npm.fetch, 'https://npm.test');
|
|
43
|
+
const first = await market.search('');
|
|
44
|
+
expect(first).toMatchObject({ total: PAGE_SIZE + 3, next: PAGE_SIZE });
|
|
45
|
+
expect(first.packages[0]).toEqual({
|
|
46
|
+
name: 'pkg-0',
|
|
47
|
+
version: '1.0.0',
|
|
48
|
+
description: 'pkg-0 for pi',
|
|
49
|
+
publisher: 'you',
|
|
50
|
+
downloads: 1200,
|
|
51
|
+
date: '2026-11-01T00:00:00Z',
|
|
52
|
+
kinds: ['extensions'],
|
|
53
|
+
links: { npm: 'https://www.npmjs.com/package/pkg-0', repository: undefined, homepage: undefined },
|
|
54
|
+
});
|
|
55
|
+
const last = await market.search('', first.next);
|
|
56
|
+
expect(last.packages).toHaveLength(3);
|
|
57
|
+
expect(last.next).toBeUndefined();
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
it('leaves out what does not match the words, and ends the search there', async () => {
|
|
61
|
+
const npm = registry(url => {
|
|
62
|
+
expect(url.searchParams.get('text')).toBe('pomodoro keywords:pi-package');
|
|
63
|
+
return { total: 9999, objects: [found('pi-pomodoro'), found('timer', { description: 'A Pomodoro timer' }), found('pi-web-access')] };
|
|
64
|
+
});
|
|
65
|
+
const page = await new NpmMarket(npm.fetch).search(' pomodoro ');
|
|
66
|
+
expect(page.packages.map(pkg => pkg.name)).toEqual(['pi-pomodoro', 'timer']);
|
|
67
|
+
expect(page.total).toBeUndefined();
|
|
68
|
+
expect(page.next).toBeUndefined();
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
it('asks npm once in a few minutes for the same search, and again after a failure', async () => {
|
|
72
|
+
let now = 0;
|
|
73
|
+
let fail = true;
|
|
74
|
+
const npm = registry(() => (fail ? undefined : { total: 1, objects: [found('a')] }));
|
|
75
|
+
const market = new NpmMarket(npm.fetch, 'https://npm.test', () => now);
|
|
76
|
+
await expect(market.search('a')).rejects.toThrow('npm has no such package');
|
|
77
|
+
fail = false;
|
|
78
|
+
await market.search('a');
|
|
79
|
+
await market.search('a');
|
|
80
|
+
expect(npm.urls).toHaveLength(2);
|
|
81
|
+
now += 6 * 60_000;
|
|
82
|
+
await market.search('a');
|
|
83
|
+
expect(npm.urls).toHaveLength(3);
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
it('gives a package with its README as published, previews and what it holds', async () => {
|
|
87
|
+
const npm = registry(url => {
|
|
88
|
+
switch (url.origin + url.pathname) {
|
|
89
|
+
case 'https://npm.test/@you%2fpi-tools/latest':
|
|
90
|
+
return {
|
|
91
|
+
name: '@you/pi-tools',
|
|
92
|
+
version: '2.0.0',
|
|
93
|
+
description: 'Tools',
|
|
94
|
+
license: 'MIT',
|
|
95
|
+
keywords: ['pi-package'],
|
|
96
|
+
repository: { url: 'git+https://github.com/you/pi-tools.git' },
|
|
97
|
+
maintainers: [{ name: 'you' }],
|
|
98
|
+
_npmUser: { name: 'GitHub Actions' },
|
|
99
|
+
pi: { extensions: ['./a.ts'], skills: ['./skills'], image: 'https://x.dev/a.png', video: 'http://insecure/a.mp4' },
|
|
100
|
+
};
|
|
101
|
+
case 'https://api.npmjs.org/downloads/point/last-month/@you/pi-tools':
|
|
102
|
+
return { downloads: 4321 };
|
|
103
|
+
case 'https://cdn.jsdelivr.net/npm/@you/pi-tools@2.0.0/README.md':
|
|
104
|
+
return '<img src="./shot.png" alt="Shot">';
|
|
105
|
+
default:
|
|
106
|
+
return undefined;
|
|
107
|
+
}
|
|
108
|
+
});
|
|
109
|
+
const detail = await new NpmMarket(npm.fetch, 'https://npm.test').detail('@you/pi-tools');
|
|
110
|
+
expect(detail).toMatchObject({
|
|
111
|
+
name: '@you/pi-tools',
|
|
112
|
+
version: '2.0.0',
|
|
113
|
+
description: 'Tools',
|
|
114
|
+
license: 'MIT',
|
|
115
|
+
publisher: 'you',
|
|
116
|
+
downloads: 4321,
|
|
117
|
+
image: 'https://x.dev/a.png',
|
|
118
|
+
resources: ['extensions', 'skills'],
|
|
119
|
+
readme: '',
|
|
120
|
+
links: { npm: 'https://www.npmjs.com/package/@you/pi-tools', repository: 'https://github.com/you/pi-tools' },
|
|
121
|
+
});
|
|
122
|
+
expect(detail.video).toBeUndefined();
|
|
123
|
+
expect(npm.urls.some(url => url.includes('raw.githubusercontent'))).toBe(false);
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
it('takes the README of the repository when the CDN has none, and shows the package without one', async () => {
|
|
127
|
+
const manifest = { name: 'tools', version: '1.0.0', repository: { url: 'https://github.com/you/mono', directory: 'packages/tools' } };
|
|
128
|
+
const withRepository = registry(url => {
|
|
129
|
+
if (url.pathname === '/tools/latest') return manifest;
|
|
130
|
+
if (url.href === 'https://raw.githubusercontent.com/you/mono/HEAD/packages/tools/README.md') return '# Tools';
|
|
131
|
+
return undefined;
|
|
132
|
+
});
|
|
133
|
+
expect((await new NpmMarket(withRepository.fetch, 'https://npm.test').detail('tools')).readme).toBe('# Tools');
|
|
134
|
+
const without = registry(url => (url.pathname === '/tools/latest' ? { name: 'tools', version: '1.0.0' } : undefined));
|
|
135
|
+
expect(await new NpmMarket(without.fetch, 'https://npm.test').detail('tools')).toMatchObject({
|
|
136
|
+
name: 'tools',
|
|
137
|
+
readme: undefined,
|
|
138
|
+
downloads: undefined,
|
|
139
|
+
});
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
it('refuses what is not the name of a package, before asking npm', async () => {
|
|
143
|
+
const npm = registry(() => ({}));
|
|
144
|
+
await expect(new NpmMarket(npm.fetch).detail('../../etc')).rejects.toThrow('is not the name of an npm package');
|
|
145
|
+
expect(npm.urls).toHaveLength(0);
|
|
146
|
+
});
|
|
147
|
+
|
|
148
|
+
it('says when npm cannot be reached', async () => {
|
|
149
|
+
const market = new NpmMarket(async () => {
|
|
150
|
+
throw new Error('getaddrinfo ENOTFOUND');
|
|
151
|
+
});
|
|
152
|
+
await expect(market.search('')).rejects.toThrow('npm cannot be reached: getaddrinfo ENOTFOUND');
|
|
153
|
+
});
|
|
154
|
+
});
|
|
@@ -0,0 +1,232 @@
|
|
|
1
|
+
import { type MarketDetail, type MarketPackage, type MarketPage, MarketUtil } from './market-util';
|
|
2
|
+
|
|
3
|
+
export type Fetch = (url: string, init?: { signal?: AbortSignal; headers?: Record<string, string> }) => Promise<Response>;
|
|
4
|
+
|
|
5
|
+
interface Cached<T> {
|
|
6
|
+
at: number;
|
|
7
|
+
value: Promise<T>;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
/** The keyword by which pi's gallery finds packages on npm. */
|
|
11
|
+
const KEYWORD = 'pi-package';
|
|
12
|
+
/** npm keeps its answers to searches for five minutes; so does the market. */
|
|
13
|
+
const TTL = 5 * 60_000;
|
|
14
|
+
const LIMIT = 100;
|
|
15
|
+
const TIMEOUT = 15_000;
|
|
16
|
+
/** A README is a nicety: the page shows the package without it rather than wait. */
|
|
17
|
+
const TEXT_TIMEOUT = 8_000;
|
|
18
|
+
/** A README longer than this is cut: the page is to choose a package, not to read a book. */
|
|
19
|
+
const README_MAX = 80_000;
|
|
20
|
+
export const PAGE_SIZE = 24;
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* The market of extensions: the pi packages of npm, as pi's gallery (pi.dev/packages) finds them, by the
|
|
24
|
+
* keyword `pi-package`. Asked on the server, which installs them too, and kept a few minutes.
|
|
25
|
+
*/
|
|
26
|
+
export class NpmMarket {
|
|
27
|
+
protected readonly cache = new Map<string, Cached<unknown>>();
|
|
28
|
+
|
|
29
|
+
constructor(
|
|
30
|
+
protected readonly fetch: Fetch = globalThis.fetch,
|
|
31
|
+
protected readonly registry = 'https://registry.npmjs.org',
|
|
32
|
+
protected readonly now: () => number = Date.now,
|
|
33
|
+
protected readonly cdn = 'https://cdn.jsdelivr.net/npm',
|
|
34
|
+
protected readonly downloadsApi = 'https://api.npmjs.org',
|
|
35
|
+
) {}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Packages that match `query` (all of them without one), the best first, from `from` (MarketPage.next).
|
|
39
|
+
* npm ranks every package of the keyword, those that do not match the words last: they are left out,
|
|
40
|
+
* and the search ends where they begin.
|
|
41
|
+
*/
|
|
42
|
+
search(query: string, from = 0): Promise<MarketPage> {
|
|
43
|
+
const words = String(query ?? '').trim();
|
|
44
|
+
const text = `${words} keywords:${KEYWORD}`.trim();
|
|
45
|
+
const start = Math.max(0, Math.floor(Number(from) || 0));
|
|
46
|
+
const url = `${this.registry}/-/v1/search?text=${encodeURIComponent(text)}&size=${PAGE_SIZE}&from=${start}`;
|
|
47
|
+
return this.remember(url, async () => {
|
|
48
|
+
const body = (await this.json(url)) as { total?: number; objects?: SearchObject[] };
|
|
49
|
+
const objects = body.objects ?? [];
|
|
50
|
+
const all = objects.flatMap(object => {
|
|
51
|
+
const pkg = this.toPackage(object);
|
|
52
|
+
return pkg ? [{ pkg, keywords: object.package?.keywords }] : [];
|
|
53
|
+
});
|
|
54
|
+
const packages = all.filter(({ pkg, keywords }) => !words || MarketUtil.matches({ ...pkg, keywords }, words)).map(({ pkg }) => pkg);
|
|
55
|
+
const full = objects.length === PAGE_SIZE && packages.length === all.length;
|
|
56
|
+
return {
|
|
57
|
+
total: words ? undefined : body.total,
|
|
58
|
+
packages,
|
|
59
|
+
next: full ? start + objects.length : undefined,
|
|
60
|
+
};
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* A package with its README and previews, by its name on npm. Not from npm's document of the package,
|
|
66
|
+
* which holds every version (tens of megabytes for some): its latest manifest, its downloads, and its
|
|
67
|
+
* README as published, from jsDelivr, or else from its repository on GitHub.
|
|
68
|
+
*/
|
|
69
|
+
detail(name: string): Promise<MarketDetail> {
|
|
70
|
+
if (typeof name !== 'string' || !/^(@[a-z0-9][\w.-]*\/)?[a-z0-9][\w.-]*$/i.test(name)) {
|
|
71
|
+
return Promise.reject(new Error(`${String(name)} is not the name of an npm package.`));
|
|
72
|
+
}
|
|
73
|
+
const url = `${this.registry}/${name.replace('/', '%2f')}/latest`;
|
|
74
|
+
return this.remember(url, async () => {
|
|
75
|
+
const [manifest, downloads] = await Promise.all([this.json(url) as Promise<Manifest>, this.downloads(name)]);
|
|
76
|
+
const repository = MarketUtil.repositoryUrl(manifest.repository);
|
|
77
|
+
const readme = await this.readme(name, manifest, repository);
|
|
78
|
+
return this.toDetail(manifest, repository, readme, downloads);
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
protected toPackage(object: SearchObject): MarketPackage | undefined {
|
|
83
|
+
const pkg = object.package;
|
|
84
|
+
if (!pkg?.name || !pkg.version) {
|
|
85
|
+
return undefined;
|
|
86
|
+
}
|
|
87
|
+
return {
|
|
88
|
+
name: pkg.name,
|
|
89
|
+
version: pkg.version,
|
|
90
|
+
description: pkg.description || undefined,
|
|
91
|
+
publisher: MarketUtil.person(pkg.maintainers?.[0]?.username, pkg.publisher?.username),
|
|
92
|
+
downloads: object.downloads?.monthly,
|
|
93
|
+
date: pkg.date,
|
|
94
|
+
kinds: MarketUtil.kindsOf(pkg.keywords),
|
|
95
|
+
links: {
|
|
96
|
+
npm: pkg.links?.npm ?? `https://www.npmjs.com/package/${pkg.name}`,
|
|
97
|
+
repository: MarketUtil.repositoryUrl(pkg.links?.repository),
|
|
98
|
+
homepage: pkg.links?.homepage,
|
|
99
|
+
},
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
protected toDetail(manifest: Manifest, repository: string | undefined, readme: string | undefined, downloads: number | undefined): MarketDetail {
|
|
104
|
+
if (!manifest.name || !manifest.version) {
|
|
105
|
+
throw new Error('npm has no published version of this package.');
|
|
106
|
+
}
|
|
107
|
+
const pi = (manifest.pi ?? {}) as Record<string, unknown>;
|
|
108
|
+
const resources = (['extensions', 'web', 'skills', 'prompts', 'themes'] as const).filter(key => key in pi);
|
|
109
|
+
return {
|
|
110
|
+
name: manifest.name,
|
|
111
|
+
version: manifest.version,
|
|
112
|
+
description: manifest.description || undefined,
|
|
113
|
+
publisher: MarketUtil.person(manifest.maintainers?.[0]?.name, manifest._npmUser?.name),
|
|
114
|
+
downloads,
|
|
115
|
+
kinds: MarketUtil.kindsOf(manifest.keywords),
|
|
116
|
+
links: {
|
|
117
|
+
npm: `https://www.npmjs.com/package/${manifest.name}`,
|
|
118
|
+
repository,
|
|
119
|
+
homepage: typeof manifest.homepage === 'string' ? manifest.homepage : undefined,
|
|
120
|
+
},
|
|
121
|
+
license: typeof manifest.license === 'string' ? manifest.license : undefined,
|
|
122
|
+
readme: readme && MarketUtil.readableReadme(readme.length > README_MAX ? `${readme.slice(0, README_MAX)}\n\n…` : readme, repository),
|
|
123
|
+
image: this.web(pi.image),
|
|
124
|
+
video: this.web(pi.video),
|
|
125
|
+
resources,
|
|
126
|
+
};
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/** The README of the version, as npm got it; or the one of its repository; undefined when neither answers. */
|
|
130
|
+
protected async readme(name: string, manifest: Manifest, repository: string | undefined): Promise<string | undefined> {
|
|
131
|
+
const files = [...new Set([manifest.readmeFilename, 'README.md', 'readme.md'].filter((file): file is string => !!file && /^[\w.-]+$/.test(file)))];
|
|
132
|
+
const urls = files.map(file => `${this.cdn}/${name}@${manifest.version}/${file}`);
|
|
133
|
+
const github = repository && /^https:\/\/github\.com\/([^/]+\/[^/#?]+)/.exec(repository)?.[1];
|
|
134
|
+
if (github) {
|
|
135
|
+
const directory =
|
|
136
|
+
typeof manifest.repository === 'object' && typeof (manifest.repository as { directory?: unknown }).directory === 'string'
|
|
137
|
+
? `${(manifest.repository as { directory: string }).directory.replace(/^\/|\/$/g, '')}/`
|
|
138
|
+
: '';
|
|
139
|
+
urls.push(`https://raw.githubusercontent.com/${github}/HEAD/${directory}README.md`);
|
|
140
|
+
}
|
|
141
|
+
for (const url of urls) {
|
|
142
|
+
const text = await this.text(url);
|
|
143
|
+
if (text?.trim()) {
|
|
144
|
+
return text;
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
return undefined;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
protected async downloads(name: string): Promise<number | undefined> {
|
|
151
|
+
try {
|
|
152
|
+
const body = (await this.json(`${this.downloadsApi}/downloads/point/last-month/${name}`)) as { downloads?: unknown };
|
|
153
|
+
return typeof body.downloads === 'number' ? body.downloads : undefined;
|
|
154
|
+
} catch {
|
|
155
|
+
return undefined;
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
protected async text(url: string): Promise<string | undefined> {
|
|
160
|
+
try {
|
|
161
|
+
const response = await this.fetch(url, { signal: AbortSignal.timeout(TEXT_TIMEOUT) });
|
|
162
|
+
return response.ok ? await response.text() : undefined;
|
|
163
|
+
} catch {
|
|
164
|
+
return undefined;
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/** Only pages of the web: the page shows them. */
|
|
169
|
+
protected web(url: unknown): string | undefined {
|
|
170
|
+
return typeof url === 'string' && /^https:\/\//.test(url) ? url : undefined;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
protected async json(url: string): Promise<unknown> {
|
|
174
|
+
let response: Response;
|
|
175
|
+
try {
|
|
176
|
+
response = await this.fetch(url, { signal: AbortSignal.timeout(TIMEOUT), headers: { accept: 'application/json' } });
|
|
177
|
+
} catch (error) {
|
|
178
|
+
throw new Error(`npm cannot be reached: ${error instanceof Error ? error.message : String(error)}`);
|
|
179
|
+
}
|
|
180
|
+
if (response.status === 404) {
|
|
181
|
+
throw new Error('npm has no such package.');
|
|
182
|
+
}
|
|
183
|
+
if (!response.ok) {
|
|
184
|
+
throw new Error(`npm answered ${response.status} ${response.statusText}`.trim());
|
|
185
|
+
}
|
|
186
|
+
return response.json();
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
/** One request for many callers; a failure is not kept. */
|
|
190
|
+
protected remember<T>(key: string, load: () => Promise<T>): Promise<T> {
|
|
191
|
+
const now = this.now();
|
|
192
|
+
const cached = this.cache.get(key);
|
|
193
|
+
if (cached && now - cached.at < TTL) {
|
|
194
|
+
return cached.value as Promise<T>;
|
|
195
|
+
}
|
|
196
|
+
const value = load();
|
|
197
|
+
this.cache.set(key, { at: now, value });
|
|
198
|
+
value.catch(() => this.cache.get(key)?.value === value && this.cache.delete(key));
|
|
199
|
+
if (this.cache.size > LIMIT) {
|
|
200
|
+
this.cache.delete(this.cache.keys().next().value as string);
|
|
201
|
+
}
|
|
202
|
+
return value;
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
interface SearchObject {
|
|
207
|
+
downloads?: { monthly?: number };
|
|
208
|
+
package?: {
|
|
209
|
+
name?: string;
|
|
210
|
+
version?: string;
|
|
211
|
+
description?: string;
|
|
212
|
+
keywords?: string[];
|
|
213
|
+
date?: string;
|
|
214
|
+
publisher?: { username?: string };
|
|
215
|
+
maintainers?: { username?: string }[];
|
|
216
|
+
links?: { npm?: string; repository?: string; homepage?: string };
|
|
217
|
+
};
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
interface Manifest {
|
|
221
|
+
name?: string;
|
|
222
|
+
version?: string;
|
|
223
|
+
readmeFilename?: string;
|
|
224
|
+
maintainers?: { name?: string }[];
|
|
225
|
+
description?: string;
|
|
226
|
+
keywords?: string[];
|
|
227
|
+
license?: unknown;
|
|
228
|
+
homepage?: unknown;
|
|
229
|
+
repository?: unknown;
|
|
230
|
+
pi?: unknown;
|
|
231
|
+
_npmUser?: { name?: string };
|
|
232
|
+
}
|
package/web-src/page.tsx
CHANGED
|
@@ -1,56 +1,31 @@
|
|
|
1
1
|
import type { TaPackageInfo } from '@celljs/ta-protocol';
|
|
2
|
-
import type { WebExtensionAPI
|
|
3
|
-
import type { ExtensionActions
|
|
2
|
+
import type { WebExtensionAPI } from '@celljs/ta-web/lib/browser/extension/web-extension-api';
|
|
3
|
+
import type { ExtensionActions } from './actions';
|
|
4
|
+
import { type ExtensionsContext, type ExtensionsTab, icon } from './context';
|
|
4
5
|
import { ExtensionsUtil } from './extensions-util';
|
|
5
6
|
import { ExtensionWords } from './i18n';
|
|
6
7
|
import { ImportDialog } from './import-dialog';
|
|
8
|
+
import { MarketView } from './market-view';
|
|
7
9
|
import { KindPicture } from './pictures';
|
|
8
10
|
import { React } from './react';
|
|
9
11
|
|
|
10
|
-
export interface ExtensionsContext {
|
|
11
|
-
pi: WebExtensionAPI;
|
|
12
|
-
store: PackagesStore;
|
|
13
|
-
actions: ExtensionActions;
|
|
14
|
-
/** The dialog of Import, which the menu of the page opens too. */
|
|
15
|
-
importing: { open(): void; subscribe(listener: () => void): () => void; shown: boolean; close(): void };
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
function icon(pi: WebExtensionAPI, name: string, fallback = 'Square'): WebIcon {
|
|
19
|
-
return pi.ui.icons[name] ?? pi.ui.icons[fallback] ?? (() => null);
|
|
20
|
-
}
|
|
21
|
-
|
|
22
12
|
/**
|
|
23
|
-
* The page of extensions
|
|
24
|
-
*
|
|
13
|
+
* The page of extensions, in two tabs: the user's own, and the market where more are found. A file
|
|
14
|
+
* dropped anywhere on it imports.
|
|
25
15
|
*/
|
|
26
16
|
export function ExtensionsPage({ context }: { context: ExtensionsContext }): React.ReactElement {
|
|
27
17
|
const { pi, store, actions, importing } = context;
|
|
28
18
|
const words = actions.words;
|
|
29
|
-
const
|
|
30
|
-
|
|
31
|
-
() => store.state,
|
|
32
|
-
);
|
|
33
|
-
const dialog = React.useSyncExternalStore(
|
|
34
|
-
listener => importing.subscribe(listener),
|
|
35
|
-
() => importing.shown,
|
|
36
|
-
);
|
|
19
|
+
const tab = context.tab.use();
|
|
20
|
+
const dialog = importing.use();
|
|
37
21
|
const [dragging, setDragging] = React.useState(false);
|
|
38
|
-
const [starting, setStarting] = React.useState(false);
|
|
39
22
|
React.useEffect(() => {
|
|
40
23
|
void store.load();
|
|
41
24
|
}, [store]);
|
|
42
|
-
const
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
const make = async (text: string) => {
|
|
47
|
-
setStarting(true);
|
|
48
|
-
try {
|
|
49
|
-
await actions.make(text);
|
|
50
|
-
} finally {
|
|
51
|
-
setStarting(false);
|
|
52
|
-
}
|
|
53
|
-
};
|
|
25
|
+
const tabs: { key: ExtensionsTab; label: string; icon: string }[] = [
|
|
26
|
+
{ key: 'mine', label: words.tabMine, icon: 'Puzzle' },
|
|
27
|
+
{ key: 'market', label: words.tabMarket, icon: 'Store' },
|
|
28
|
+
];
|
|
54
29
|
const files = (event: React.DragEvent) => Array.from(event.dataTransfer.types).includes('Files');
|
|
55
30
|
return (
|
|
56
31
|
<section
|
|
@@ -79,71 +54,128 @@ export function ExtensionsPage({ context }: { context: ExtensionsContext }): Rea
|
|
|
79
54
|
}}
|
|
80
55
|
>
|
|
81
56
|
<div className="tax-body">
|
|
82
|
-
<
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
57
|
+
<div className="tax-tabs" role="tablist" aria-label={words.title}>
|
|
58
|
+
{tabs.map(item => {
|
|
59
|
+
const TabIcon = icon(pi, item.icon, 'PackageOpen');
|
|
60
|
+
return (
|
|
61
|
+
<button
|
|
62
|
+
key={item.key}
|
|
63
|
+
type="button"
|
|
64
|
+
role="tab"
|
|
65
|
+
aria-selected={tab === item.key}
|
|
66
|
+
className={`tax-tab${tab === item.key ? ' tax-active' : ''}`}
|
|
67
|
+
onClick={() => {
|
|
68
|
+
context.tab.set(item.key);
|
|
69
|
+
context.selected.set(undefined);
|
|
70
|
+
}}
|
|
71
|
+
>
|
|
72
|
+
<TabIcon size={15} /> {item.label}
|
|
96
73
|
</button>
|
|
74
|
+
);
|
|
75
|
+
})}
|
|
76
|
+
</div>
|
|
77
|
+
{tab === 'market' ? <MarketView context={context} /> : <MineView context={context} />}
|
|
78
|
+
</div>
|
|
79
|
+
{dragging && <div className="tax-drop-overlay">{words.dropToImport}</div>}
|
|
80
|
+
{dialog && <ImportDialog pi={pi} actions={actions} onClose={() => importing.set(false)} />}
|
|
81
|
+
</section>
|
|
82
|
+
);
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* The user's own: a sentence makes one, each kind shown by a picture; the extensions of the user to turn
|
|
87
|
+
* on or off, change, hand on or remove; and, in three steps, how others take one in.
|
|
88
|
+
*/
|
|
89
|
+
function MineView({ context }: { context: ExtensionsContext }): React.ReactElement {
|
|
90
|
+
const { pi, store, actions, importing } = context;
|
|
91
|
+
const words = actions.words;
|
|
92
|
+
const { packages } = React.useSyncExternalStore(
|
|
93
|
+
listener => store.subscribe(listener),
|
|
94
|
+
() => store.state,
|
|
95
|
+
);
|
|
96
|
+
const [starting, setStarting] = React.useState(false);
|
|
97
|
+
const groups = ExtensionsUtil.group(packages ?? []);
|
|
98
|
+
const Composer = pi.ui.Composer;
|
|
99
|
+
const Sparkles = icon(pi, 'Sparkles');
|
|
100
|
+
const Upload = icon(pi, 'Upload');
|
|
101
|
+
const Store = icon(pi, 'Store', 'PackageOpen');
|
|
102
|
+
const make = async (text: string) => {
|
|
103
|
+
setStarting(true);
|
|
104
|
+
try {
|
|
105
|
+
await actions.make(text);
|
|
106
|
+
} finally {
|
|
107
|
+
setStarting(false);
|
|
108
|
+
}
|
|
109
|
+
};
|
|
110
|
+
return (
|
|
111
|
+
<>
|
|
112
|
+
<section className="tax-hero">
|
|
113
|
+
<h1>
|
|
114
|
+
<Sparkles size={20} /> {words.heroTitle}
|
|
115
|
+
</h1>
|
|
116
|
+
<p>{words.heroText}</p>
|
|
117
|
+
<Composer placeholder={words.placeholder} onSend={make} />
|
|
118
|
+
<div className="tax-kinds">
|
|
119
|
+
{words.examples.map(example => (
|
|
120
|
+
<button key={example.kind} type="button" className="tax-kind" disabled={starting} onClick={() => void make(example.text)}>
|
|
121
|
+
<KindPicture kind={example.kind} />
|
|
122
|
+
<span className="tax-kind-label">
|
|
123
|
+
<strong>{example.label}</strong>
|
|
124
|
+
<span>{example.example}</span>
|
|
125
|
+
</span>
|
|
126
|
+
</button>
|
|
127
|
+
))}
|
|
128
|
+
</div>
|
|
129
|
+
</section>
|
|
130
|
+
<section className="tax-section">
|
|
131
|
+
<div className="tax-section-head">
|
|
132
|
+
<h2>
|
|
133
|
+
{words.mine}
|
|
134
|
+
{groups.mine.length > 0 && <span className="tax-count">{groups.mine.length}</span>}
|
|
135
|
+
</h2>
|
|
136
|
+
<button type="button" className="tax-button" onClick={() => importing.set(true)}>
|
|
137
|
+
<Upload size={14} /> {words.import}
|
|
138
|
+
</button>
|
|
139
|
+
</div>
|
|
140
|
+
{packages === undefined ? (
|
|
141
|
+
<div className="tax-empty">{words.loading}</div>
|
|
142
|
+
) : groups.mine.length === 0 ? (
|
|
143
|
+
<div className="tax-empty tax-empty-action">
|
|
144
|
+
<span>{words.mineEmpty}</span>
|
|
145
|
+
<button type="button" className="tax-button" onClick={() => context.tab.set('market')}>
|
|
146
|
+
<Store size={14} /> {words.browseMarket}
|
|
147
|
+
</button>
|
|
148
|
+
</div>
|
|
149
|
+
) : (
|
|
150
|
+
<div className="tax-list">
|
|
151
|
+
{groups.mine.map(pkg => (
|
|
152
|
+
<PackageRow key={pkg.id} pi={pi} actions={actions} pkg={pkg} />
|
|
97
153
|
))}
|
|
98
154
|
</div>
|
|
99
|
-
|
|
155
|
+
)}
|
|
156
|
+
</section>
|
|
157
|
+
{groups.installed.length > 0 && (
|
|
100
158
|
<section className="tax-section">
|
|
101
159
|
<div className="tax-section-head">
|
|
102
160
|
<h2>
|
|
103
|
-
{words.
|
|
104
|
-
|
|
161
|
+
{words.installed}
|
|
162
|
+
<span className="tax-count">{groups.installed.length}</span>
|
|
105
163
|
</h2>
|
|
106
|
-
<button type="button" className="tax-button" onClick={() => importing.open()}>
|
|
107
|
-
<Upload size={14} /> {words.import}
|
|
108
|
-
</button>
|
|
109
164
|
</div>
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
) : (
|
|
115
|
-
<div className="tax-list">
|
|
116
|
-
{groups.mine.map(pkg => (
|
|
117
|
-
<PackageRow key={pkg.id} pi={pi} actions={actions} pkg={pkg} />
|
|
118
|
-
))}
|
|
119
|
-
</div>
|
|
120
|
-
)}
|
|
121
|
-
</section>
|
|
122
|
-
{groups.installed.length > 0 && (
|
|
123
|
-
<section className="tax-section">
|
|
124
|
-
<div className="tax-section-head">
|
|
125
|
-
<h2>
|
|
126
|
-
{words.installed}
|
|
127
|
-
<span className="tax-count">{groups.installed.length}</span>
|
|
128
|
-
</h2>
|
|
129
|
-
</div>
|
|
130
|
-
<div className="tax-list">
|
|
131
|
-
{groups.installed.map(pkg => (
|
|
132
|
-
<PackageRow key={pkg.id} pi={pi} actions={actions} pkg={pkg} />
|
|
133
|
-
))}
|
|
134
|
-
</div>
|
|
135
|
-
</section>
|
|
136
|
-
)}
|
|
137
|
-
<section className="tax-section">
|
|
138
|
-
<div className="tax-section-head">
|
|
139
|
-
<h2>{words.shareTitle}</h2>
|
|
165
|
+
<div className="tax-list">
|
|
166
|
+
{groups.installed.map(pkg => (
|
|
167
|
+
<PackageRow key={pkg.id} pi={pi} actions={actions} pkg={pkg} />
|
|
168
|
+
))}
|
|
140
169
|
</div>
|
|
141
|
-
<ShareSteps pi={pi} actions={actions} />
|
|
142
170
|
</section>
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
171
|
+
)}
|
|
172
|
+
<section className="tax-section">
|
|
173
|
+
<div className="tax-section-head">
|
|
174
|
+
<h2>{words.shareTitle}</h2>
|
|
175
|
+
</div>
|
|
176
|
+
<ShareSteps pi={pi} actions={actions} />
|
|
177
|
+
</section>
|
|
178
|
+
</>
|
|
147
179
|
);
|
|
148
180
|
}
|
|
149
181
|
|