@celljs/ta-extensions 0.0.0-stage → 4.6.1
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.md +19 -2
- package/host/index.mjs +271 -0
- package/package.json +44 -4
- package/skills/ta-package/SKILL.md +90 -0
- package/web/index.js +941 -0
- package/web-src/actions.ts +184 -0
- package/web-src/archive.spec.ts +101 -0
- package/web-src/archive.ts +229 -0
- package/web-src/extensions-util.spec.ts +38 -0
- package/web-src/extensions-util.ts +40 -0
- package/web-src/host.spec.ts +72 -0
- package/web-src/host.ts +101 -0
- package/web-src/i18n.ts +157 -0
- package/web-src/import-dialog.tsx +89 -0
- package/web-src/index.tsx +46 -0
- package/web-src/page.tsx +246 -0
- package/web-src/pictures.tsx +87 -0
- package/web-src/react.ts +11 -0
- package/web-src/style.ts +90 -0
package/web-src/host.ts
ADDED
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
import { randomBytes } from 'node:crypto';
|
|
2
|
+
import { chmodSync, existsSync, mkdirSync, renameSync, rmSync, writeFileSync } from 'node:fs';
|
|
3
|
+
import { basename, dirname, join } from 'node:path';
|
|
4
|
+
import type { TaExtensionHost, TaPackageService } from '@celljs/ta-protocol';
|
|
5
|
+
import { ExtensionArchive } from './archive';
|
|
6
|
+
|
|
7
|
+
/** A package in one file, as the page downloads it: the bytes in base64, which JSON carries. */
|
|
8
|
+
export interface ExportedPackage {
|
|
9
|
+
fileName: string;
|
|
10
|
+
data: string;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/** `exists`: a package of that directory is there, and nothing changed; import again with `overwrite`. */
|
|
14
|
+
export interface ImportedPackage {
|
|
15
|
+
status: 'imported' | 'exists';
|
|
16
|
+
name: string;
|
|
17
|
+
/** In `extensions/` of the agent directory. */
|
|
18
|
+
directory: string;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export namespace PackageFiles {
|
|
22
|
+
/** The directory of a package in `extensions/`: its name without scope, in the letters a directory takes everywhere. */
|
|
23
|
+
export function directoryName(name: string): string {
|
|
24
|
+
const plain = name
|
|
25
|
+
.slice(name.lastIndexOf('/') + 1)
|
|
26
|
+
.toLowerCase()
|
|
27
|
+
.replace(/[^a-z0-9._-]+/g, '-')
|
|
28
|
+
.replace(/^[-.]+|-+$/g, '');
|
|
29
|
+
return plain || 'extension';
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* The server side of ta-extensions: a package of the user in one file to hand on, and such a file put
|
|
35
|
+
* into `extensions/` of the agent directory, where pi finds it with no setting.
|
|
36
|
+
*/
|
|
37
|
+
export class ExtensionsService implements TaPackageService {
|
|
38
|
+
constructor(protected readonly host: TaExtensionHost) {}
|
|
39
|
+
|
|
40
|
+
/** Nothing to stop: each call stands alone. */
|
|
41
|
+
dispose(): void {}
|
|
42
|
+
|
|
43
|
+
async exportPackage(id: string, cwd?: string): Promise<ExportedPackage> {
|
|
44
|
+
const pkg = (await this.host.listPackages(cwd)).find(candidate => candidate.id === id);
|
|
45
|
+
if (!pkg) {
|
|
46
|
+
throw new Error(`No package has the id ${id}; it may have moved or gone.`);
|
|
47
|
+
}
|
|
48
|
+
if (pkg.origin === 'builtin') {
|
|
49
|
+
throw new Error(`${pkg.name} comes with Ta: everyone has it already.`);
|
|
50
|
+
}
|
|
51
|
+
const top = basename(pkg.root);
|
|
52
|
+
const archive = ExtensionArchive.pack(top, ExtensionArchive.collect(pkg.root));
|
|
53
|
+
return { fileName: `${top}${pkg.version ? `-${pkg.version}` : ''}.tgz`, data: Buffer.from(archive).toString('base64') };
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
async importPackage(data: string, overwrite = false): Promise<ImportedPackage> {
|
|
57
|
+
if (typeof data !== 'string' || data === '') {
|
|
58
|
+
throw new Error('Pass the bytes of the file in base64.');
|
|
59
|
+
}
|
|
60
|
+
const { top, files } = ExtensionArchive.unpack(Buffer.from(data, 'base64'));
|
|
61
|
+
const name = this.nameOf(files) ?? top;
|
|
62
|
+
const directory = PackageFiles.directoryName(name);
|
|
63
|
+
const extensions = join(this.host.agentDir, 'extensions');
|
|
64
|
+
const target = join(extensions, directory);
|
|
65
|
+
if (existsSync(target) && !overwrite) {
|
|
66
|
+
return { status: 'exists', name, directory };
|
|
67
|
+
}
|
|
68
|
+
// Written beside it under a name pi skips, then put in its place: a session never loads half of it.
|
|
69
|
+
const staging = join(extensions, `.${directory}.${randomBytes(4).toString('hex')}`);
|
|
70
|
+
try {
|
|
71
|
+
for (const file of files) {
|
|
72
|
+
const path = join(staging, ...file.path.split('/'));
|
|
73
|
+
mkdirSync(dirname(path), { recursive: true });
|
|
74
|
+
writeFileSync(path, file.data);
|
|
75
|
+
if (file.executable) {
|
|
76
|
+
chmodSync(path, 0o755);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
rmSync(target, { recursive: true, force: true });
|
|
80
|
+
renameSync(staging, target);
|
|
81
|
+
} finally {
|
|
82
|
+
rmSync(staging, { recursive: true, force: true });
|
|
83
|
+
}
|
|
84
|
+
return { status: 'imported', name, directory };
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
protected nameOf(files: { path: string; data: Uint8Array }[]): string | undefined {
|
|
88
|
+
const manifest = files.find(file => file.path === 'package.json');
|
|
89
|
+
try {
|
|
90
|
+
const name = JSON.parse(Buffer.from(manifest?.data ?? []).toString('utf8'))?.name;
|
|
91
|
+
return typeof name === 'string' && name.trim() ? name.trim() : undefined;
|
|
92
|
+
} catch {
|
|
93
|
+
return undefined;
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/** `pi.host` of the package: ta serve runs it once. */
|
|
99
|
+
export default function activate(host: TaExtensionHost): TaPackageService {
|
|
100
|
+
return new ExtensionsService(host);
|
|
101
|
+
}
|
package/web-src/i18n.ts
ADDED
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
import type { TaPackageResource } from '@celljs/ta-protocol';
|
|
2
|
+
import type { KindName } from './pictures';
|
|
3
|
+
|
|
4
|
+
/** A kind of extension, and the one Ta makes of it when picked. */
|
|
5
|
+
export interface Example {
|
|
6
|
+
kind: KindName;
|
|
7
|
+
label: string;
|
|
8
|
+
example: string;
|
|
9
|
+
text: string;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
const EN = {
|
|
13
|
+
title: 'Extensions',
|
|
14
|
+
heroTitle: 'Make an extension in a sentence',
|
|
15
|
+
heroText: 'It works as soon as the reply ends, with no restart.',
|
|
16
|
+
placeholder: 'Describe the extension, for example: a pomodoro timer beside the conversation',
|
|
17
|
+
makePrompt: 'Make an extension: {text}',
|
|
18
|
+
changePrompt: 'Change the extension {name} (in {root}): {text}',
|
|
19
|
+
changeTitle: 'Change {name}',
|
|
20
|
+
changePlaceholder: 'What should it do differently?',
|
|
21
|
+
mine: 'Yours',
|
|
22
|
+
mineEmpty: 'None yet: say what you want above.',
|
|
23
|
+
installed: 'Installed',
|
|
24
|
+
import: 'Import',
|
|
25
|
+
export: 'Export',
|
|
26
|
+
change: 'Change',
|
|
27
|
+
remove: 'Remove',
|
|
28
|
+
copySource: 'Copy the source',
|
|
29
|
+
copied: 'Copied {source}',
|
|
30
|
+
removeTitle: 'Remove {name}?',
|
|
31
|
+
removeDetail: 'Its pages, cards and commands go with it.',
|
|
32
|
+
removed: 'Removed {name}',
|
|
33
|
+
exported: 'Exported {file}',
|
|
34
|
+
imported: 'Imported {name}; it works now',
|
|
35
|
+
installedOk: 'Installed {name}; it works now',
|
|
36
|
+
existsTitle: '{name} is there already. Replace it?',
|
|
37
|
+
existsDetail: 'Its files give way to those of the file imported.',
|
|
38
|
+
replace: 'Replace',
|
|
39
|
+
project: 'This project',
|
|
40
|
+
turnOn: 'Turn {name} on',
|
|
41
|
+
turnedOn: '{name} is on',
|
|
42
|
+
turnedOff: '{name} is off',
|
|
43
|
+
shareTitle: 'Hand them on',
|
|
44
|
+
shareSteps: ['Export', 'Send the .tgz', 'They import it'],
|
|
45
|
+
shareMore: 'npm and Git sources import too, or just tell Ta.',
|
|
46
|
+
importTitle: 'Import an extension',
|
|
47
|
+
dropHere: 'Drop a .tgz file here, or',
|
|
48
|
+
chooseFile: 'choose a file',
|
|
49
|
+
orSource: 'Or install it from npm or Git',
|
|
50
|
+
sourcePlaceholder: 'npm:@scope/name or https://github.com/you/repo',
|
|
51
|
+
install: 'Install',
|
|
52
|
+
working: 'Working…',
|
|
53
|
+
trust: 'An extension runs code on this computer: import only those of people you trust.',
|
|
54
|
+
dropToImport: 'Drop to import',
|
|
55
|
+
notArchive: 'Only a .tgz file of an extension imports.',
|
|
56
|
+
loading: 'Loading…',
|
|
57
|
+
kinds: {
|
|
58
|
+
extensions: 'Commands',
|
|
59
|
+
web: 'On the page',
|
|
60
|
+
skills: 'Skills',
|
|
61
|
+
prompts: 'Prompts',
|
|
62
|
+
themes: 'Themes',
|
|
63
|
+
} as Record<TaPackageResource, string>,
|
|
64
|
+
examples: [
|
|
65
|
+
{
|
|
66
|
+
kind: 'card',
|
|
67
|
+
label: 'Card',
|
|
68
|
+
example: 'Pomodoro',
|
|
69
|
+
text: 'a pomodoro timer as a card beside the conversation: 25 minutes of work, 5 of rest, and a notice when each ends',
|
|
70
|
+
},
|
|
71
|
+
{
|
|
72
|
+
kind: 'page',
|
|
73
|
+
label: 'Page',
|
|
74
|
+
example: 'Commits by day',
|
|
75
|
+
text: 'a page that shows, by day, the commits and the lines changed of this repository over the last 30 days',
|
|
76
|
+
},
|
|
77
|
+
{
|
|
78
|
+
kind: 'command',
|
|
79
|
+
label: 'Command',
|
|
80
|
+
example: '/standup',
|
|
81
|
+
text: 'a /standup command that sums up what I committed and changed today in this repository, as words for the stand-up',
|
|
82
|
+
},
|
|
83
|
+
{ kind: 'skill', label: 'Skill', example: 'Into English', text: 'a skill: when I say "in English", Ta puts its last reply into natural English' },
|
|
84
|
+
] as Example[],
|
|
85
|
+
};
|
|
86
|
+
|
|
87
|
+
export type Words = typeof EN;
|
|
88
|
+
|
|
89
|
+
const ZH: Words = {
|
|
90
|
+
title: '扩展',
|
|
91
|
+
heroTitle: '用一句话做一个扩展',
|
|
92
|
+
heroText: '回答结束就生效,不用重启。',
|
|
93
|
+
placeholder: '描述你想要的扩展,例如:在对话旁放一个番茄钟',
|
|
94
|
+
makePrompt: '做一个扩展:{text}',
|
|
95
|
+
changePrompt: '修改扩展 {name}(在 {root}):{text}',
|
|
96
|
+
changeTitle: '修改 {name}',
|
|
97
|
+
changePlaceholder: '想让它有什么不同?',
|
|
98
|
+
mine: '我的扩展',
|
|
99
|
+
mineEmpty: '还没有,在上面说一句就行。',
|
|
100
|
+
installed: '安装的扩展',
|
|
101
|
+
import: '导入',
|
|
102
|
+
export: '导出',
|
|
103
|
+
change: '修改',
|
|
104
|
+
remove: '移除',
|
|
105
|
+
copySource: '复制来源',
|
|
106
|
+
copied: '已复制 {source}',
|
|
107
|
+
removeTitle: '移除扩展 {name}?',
|
|
108
|
+
removeDetail: '它的页面、卡片和命令也会一起移除。',
|
|
109
|
+
removed: '已移除 {name}',
|
|
110
|
+
exported: '已导出 {file}',
|
|
111
|
+
imported: '已导入 {name},已经生效',
|
|
112
|
+
installedOk: '已安装 {name},已经生效',
|
|
113
|
+
existsTitle: '已有扩展 {name},要替换吗?',
|
|
114
|
+
existsDetail: '现有的文件会换成导入的版本。',
|
|
115
|
+
replace: '替换',
|
|
116
|
+
project: '本项目',
|
|
117
|
+
turnOn: '启用 {name}',
|
|
118
|
+
turnedOn: '已启用 {name}',
|
|
119
|
+
turnedOff: '已停用 {name}',
|
|
120
|
+
shareTitle: '分享给别人',
|
|
121
|
+
shareSteps: ['导出', '发送 .tgz 文件', '对方导入'],
|
|
122
|
+
shareMore: '也支持 npm、Git 来源,或者直接对 Ta 说。',
|
|
123
|
+
importTitle: '导入扩展',
|
|
124
|
+
dropHere: '把 .tgz 文件拖到这里,或',
|
|
125
|
+
chooseFile: '选择文件',
|
|
126
|
+
orSource: '或者从 npm、Git 安装',
|
|
127
|
+
sourcePlaceholder: 'npm:@scope/name 或 https://github.com/you/repo',
|
|
128
|
+
install: '安装',
|
|
129
|
+
working: '正在处理…',
|
|
130
|
+
trust: '扩展会在这台电脑上运行代码:只导入你信任的人给的扩展。',
|
|
131
|
+
dropToImport: '松开即可导入',
|
|
132
|
+
notArchive: '只能导入扩展的 .tgz 文件。',
|
|
133
|
+
loading: '正在加载…',
|
|
134
|
+
kinds: {
|
|
135
|
+
extensions: '命令',
|
|
136
|
+
web: '界面',
|
|
137
|
+
skills: '技能',
|
|
138
|
+
prompts: '提示模板',
|
|
139
|
+
themes: '主题',
|
|
140
|
+
},
|
|
141
|
+
examples: [
|
|
142
|
+
{ kind: 'card', label: '卡片', example: '番茄钟', text: '在对话旁放一个番茄钟卡片:专注 25 分钟、休息 5 分钟,每段结束时提醒我' },
|
|
143
|
+
{ kind: 'page', label: '页面', example: '提交统计', text: '加一个页面,按天显示这个仓库最近 30 天的提交数和改动行数' },
|
|
144
|
+
{ kind: 'command', label: '命令', example: '/standup', text: '加一个 /standup 命令:总结我今天在这个仓库里的提交和改动,写成站会发言' },
|
|
145
|
+
{ kind: 'skill', label: '技能', example: '翻成英文', text: '加一个技能,我说“翻成英文”时,把上一条回复翻成地道的英文' },
|
|
146
|
+
],
|
|
147
|
+
};
|
|
148
|
+
|
|
149
|
+
export namespace ExtensionWords {
|
|
150
|
+
export function of(language: string): Words {
|
|
151
|
+
return language === 'zh-CN' ? ZH : EN;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
export function fill(template: string, values: Record<string, string>): string {
|
|
155
|
+
return template.replace(/\{(\w+)\}/g, (whole, key: string) => values[key] ?? whole);
|
|
156
|
+
}
|
|
157
|
+
}
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import type { WebExtensionAPI } from '@celljs/ta-web/lib/browser/extension/web-extension-api';
|
|
2
|
+
import type { ExtensionActions } from './actions';
|
|
3
|
+
import { React } from './react';
|
|
4
|
+
|
|
5
|
+
export interface ImportDialogProps {
|
|
6
|
+
pi: WebExtensionAPI;
|
|
7
|
+
actions: ExtensionActions;
|
|
8
|
+
onClose(): void;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
/** A .tgz file, dropped or chosen, or a source of npm or Git. */
|
|
12
|
+
export function ImportDialog({ pi, actions, onClose }: ImportDialogProps): React.ReactElement {
|
|
13
|
+
const words = actions.words;
|
|
14
|
+
const { Dialog, icons } = pi.ui;
|
|
15
|
+
const Upload = icons.Upload ?? icons.Download;
|
|
16
|
+
const input = React.useRef<HTMLInputElement>(null);
|
|
17
|
+
const [over, setOver] = React.useState(false);
|
|
18
|
+
const [busy, setBusy] = React.useState(false);
|
|
19
|
+
const [source, setSource] = React.useState('');
|
|
20
|
+
const run = async (action: () => Promise<boolean>) => {
|
|
21
|
+
setBusy(true);
|
|
22
|
+
try {
|
|
23
|
+
if (await action()) {
|
|
24
|
+
onClose();
|
|
25
|
+
}
|
|
26
|
+
} finally {
|
|
27
|
+
setBusy(false);
|
|
28
|
+
}
|
|
29
|
+
};
|
|
30
|
+
const take = (file: File | undefined) => file && void run(() => actions.importFile(file));
|
|
31
|
+
return (
|
|
32
|
+
<Dialog title={words.importTitle} onClose={onClose}>
|
|
33
|
+
<div className="tax-dialog">
|
|
34
|
+
<section
|
|
35
|
+
aria-label={words.importTitle}
|
|
36
|
+
className={`tax-drop${over ? ' tax-over' : ''}`}
|
|
37
|
+
onDragOver={event => {
|
|
38
|
+
event.preventDefault();
|
|
39
|
+
setOver(true);
|
|
40
|
+
}}
|
|
41
|
+
onDragLeave={() => setOver(false)}
|
|
42
|
+
onDrop={event => {
|
|
43
|
+
event.preventDefault();
|
|
44
|
+
setOver(false);
|
|
45
|
+
take(event.dataTransfer.files[0]);
|
|
46
|
+
}}
|
|
47
|
+
>
|
|
48
|
+
{Upload && <Upload size={22} />}
|
|
49
|
+
<div>
|
|
50
|
+
{busy ? (
|
|
51
|
+
words.working
|
|
52
|
+
) : (
|
|
53
|
+
<>
|
|
54
|
+
{words.dropHere}{' '}
|
|
55
|
+
<button type="button" className="tax-link" onClick={() => input.current?.click()}>
|
|
56
|
+
{words.chooseFile}
|
|
57
|
+
</button>
|
|
58
|
+
</>
|
|
59
|
+
)}
|
|
60
|
+
</div>
|
|
61
|
+
<input
|
|
62
|
+
ref={input}
|
|
63
|
+
type="file"
|
|
64
|
+
accept=".tgz,.gz,application/gzip"
|
|
65
|
+
hidden
|
|
66
|
+
onChange={event => {
|
|
67
|
+
take(event.target.files?.[0]);
|
|
68
|
+
event.target.value = '';
|
|
69
|
+
}}
|
|
70
|
+
/>
|
|
71
|
+
</section>
|
|
72
|
+
<div className="tax-or">{words.orSource}</div>
|
|
73
|
+
<form
|
|
74
|
+
className="tax-source"
|
|
75
|
+
onSubmit={event => {
|
|
76
|
+
event.preventDefault();
|
|
77
|
+
void run(() => actions.install(source));
|
|
78
|
+
}}
|
|
79
|
+
>
|
|
80
|
+
<input value={source} placeholder={words.sourcePlaceholder} spellCheck={false} onChange={event => setSource(event.target.value)} />
|
|
81
|
+
<button type="submit" className="tax-button" disabled={busy || !source.trim()}>
|
|
82
|
+
{busy ? words.working : words.install}
|
|
83
|
+
</button>
|
|
84
|
+
</form>
|
|
85
|
+
<div className="tax-note">{words.trust}</div>
|
|
86
|
+
</div>
|
|
87
|
+
</Dialog>
|
|
88
|
+
);
|
|
89
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import type { WebExtensionAPI } from '@celljs/ta-web/lib/browser/extension/web-extension-api';
|
|
2
|
+
import { ExtensionActions, PackagesStore } from './actions';
|
|
3
|
+
import { ExtensionWords } from './i18n';
|
|
4
|
+
import { type ExtensionsContext, ExtensionsPage } from './page';
|
|
5
|
+
// biome-ignore lint/correctness/noUnusedImports: the JSX below compiles to React.createElement (jsxFactory)
|
|
6
|
+
import { bind, React } from './react';
|
|
7
|
+
import { injectStyle } from './style';
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* The page of extensions, in the navigation: where the user learns that Ta makes extensions from what
|
|
11
|
+
* they say, and hands them on or takes them in. Files go through the host of the package (host.ts).
|
|
12
|
+
*/
|
|
13
|
+
export default function activate(pi: WebExtensionAPI): () => void {
|
|
14
|
+
bind(pi.React);
|
|
15
|
+
const removeStyle = injectStyle();
|
|
16
|
+
const store = new PackagesStore(pi);
|
|
17
|
+
const actions = new ExtensionActions(pi, store);
|
|
18
|
+
const listeners = new Set<() => void>();
|
|
19
|
+
const importing: ExtensionsContext['importing'] = {
|
|
20
|
+
shown: false,
|
|
21
|
+
open: () => set(true),
|
|
22
|
+
close: () => set(false),
|
|
23
|
+
subscribe: listener => {
|
|
24
|
+
listeners.add(listener);
|
|
25
|
+
return () => listeners.delete(listener);
|
|
26
|
+
},
|
|
27
|
+
};
|
|
28
|
+
function set(shown: boolean) {
|
|
29
|
+
importing.shown = shown;
|
|
30
|
+
for (const listener of listeners) {
|
|
31
|
+
listener();
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
const context: ExtensionsContext = { pi, store, actions, importing };
|
|
35
|
+
const Puzzle = pi.ui.icons.Puzzle ?? pi.ui.icons.Square;
|
|
36
|
+
pi.registerView('extensions', {
|
|
37
|
+
title: () => ExtensionWords.of(pi.ui.language).title,
|
|
38
|
+
icon: Puzzle,
|
|
39
|
+
render: () => <ExtensionsPage context={context} />,
|
|
40
|
+
menu: () => [[{ key: 'import', label: ExtensionWords.of(pi.ui.language).import, icon: pi.ui.icons.Upload, run: () => importing.open() }]],
|
|
41
|
+
order: 90,
|
|
42
|
+
});
|
|
43
|
+
// A reply of Ta may have made, changed or removed one.
|
|
44
|
+
pi.on('agent_end', () => void store.load());
|
|
45
|
+
return removeStyle;
|
|
46
|
+
}
|
package/web-src/page.tsx
ADDED
|
@@ -0,0 +1,246 @@
|
|
|
1
|
+
import type { TaPackageInfo } from '@celljs/ta-protocol';
|
|
2
|
+
import type { WebExtensionAPI, WebIcon } from '@celljs/ta-web/lib/browser/extension/web-extension-api';
|
|
3
|
+
import type { ExtensionActions, PackagesStore } from './actions';
|
|
4
|
+
import { ExtensionsUtil } from './extensions-util';
|
|
5
|
+
import { ExtensionWords } from './i18n';
|
|
6
|
+
import { ImportDialog } from './import-dialog';
|
|
7
|
+
import { KindPicture } from './pictures';
|
|
8
|
+
import { React } from './react';
|
|
9
|
+
|
|
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
|
+
/**
|
|
23
|
+
* The page of extensions: a sentence makes one, each kind shown by a picture; the extensions of the user
|
|
24
|
+
* to turn on or off, change, hand on or remove; and, in three steps, how others take one in.
|
|
25
|
+
*/
|
|
26
|
+
export function ExtensionsPage({ context }: { context: ExtensionsContext }): React.ReactElement {
|
|
27
|
+
const { pi, store, actions, importing } = context;
|
|
28
|
+
const words = actions.words;
|
|
29
|
+
const { packages } = React.useSyncExternalStore(
|
|
30
|
+
listener => store.subscribe(listener),
|
|
31
|
+
() => store.state,
|
|
32
|
+
);
|
|
33
|
+
const dialog = React.useSyncExternalStore(
|
|
34
|
+
listener => importing.subscribe(listener),
|
|
35
|
+
() => importing.shown,
|
|
36
|
+
);
|
|
37
|
+
const [dragging, setDragging] = React.useState(false);
|
|
38
|
+
const [starting, setStarting] = React.useState(false);
|
|
39
|
+
React.useEffect(() => {
|
|
40
|
+
void store.load();
|
|
41
|
+
}, [store]);
|
|
42
|
+
const groups = ExtensionsUtil.group(packages ?? []);
|
|
43
|
+
const Composer = pi.ui.Composer;
|
|
44
|
+
const Sparkles = icon(pi, 'Sparkles');
|
|
45
|
+
const Upload = icon(pi, 'Upload');
|
|
46
|
+
const make = async (text: string) => {
|
|
47
|
+
setStarting(true);
|
|
48
|
+
try {
|
|
49
|
+
await actions.make(text);
|
|
50
|
+
} finally {
|
|
51
|
+
setStarting(false);
|
|
52
|
+
}
|
|
53
|
+
};
|
|
54
|
+
const files = (event: React.DragEvent) => Array.from(event.dataTransfer.types).includes('Files');
|
|
55
|
+
return (
|
|
56
|
+
<section
|
|
57
|
+
aria-label={words.title}
|
|
58
|
+
className="tax-page"
|
|
59
|
+
onDragOver={event => {
|
|
60
|
+
if (files(event) && !dialog) {
|
|
61
|
+
event.preventDefault();
|
|
62
|
+
setDragging(true);
|
|
63
|
+
}
|
|
64
|
+
}}
|
|
65
|
+
onDragLeave={event => {
|
|
66
|
+
if (event.currentTarget === event.target) {
|
|
67
|
+
setDragging(false);
|
|
68
|
+
}
|
|
69
|
+
}}
|
|
70
|
+
onDrop={event => {
|
|
71
|
+
if (files(event) && !dialog) {
|
|
72
|
+
event.preventDefault();
|
|
73
|
+
setDragging(false);
|
|
74
|
+
const file = event.dataTransfer.files[0];
|
|
75
|
+
if (file) {
|
|
76
|
+
void actions.importFile(file);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
}}
|
|
80
|
+
>
|
|
81
|
+
<div className="tax-body">
|
|
82
|
+
<section className="tax-hero">
|
|
83
|
+
<h1>
|
|
84
|
+
<Sparkles size={20} /> {words.heroTitle}
|
|
85
|
+
</h1>
|
|
86
|
+
<p>{words.heroText}</p>
|
|
87
|
+
<Composer placeholder={words.placeholder} onSend={make} />
|
|
88
|
+
<div className="tax-kinds">
|
|
89
|
+
{words.examples.map(example => (
|
|
90
|
+
<button key={example.kind} type="button" className="tax-kind" disabled={starting} onClick={() => void make(example.text)}>
|
|
91
|
+
<KindPicture kind={example.kind} />
|
|
92
|
+
<span className="tax-kind-label">
|
|
93
|
+
<strong>{example.label}</strong>
|
|
94
|
+
<span>{example.example}</span>
|
|
95
|
+
</span>
|
|
96
|
+
</button>
|
|
97
|
+
))}
|
|
98
|
+
</div>
|
|
99
|
+
</section>
|
|
100
|
+
<section className="tax-section">
|
|
101
|
+
<div className="tax-section-head">
|
|
102
|
+
<h2>
|
|
103
|
+
{words.mine}
|
|
104
|
+
{groups.mine.length > 0 && <span className="tax-count">{groups.mine.length}</span>}
|
|
105
|
+
</h2>
|
|
106
|
+
<button type="button" className="tax-button" onClick={() => importing.open()}>
|
|
107
|
+
<Upload size={14} /> {words.import}
|
|
108
|
+
</button>
|
|
109
|
+
</div>
|
|
110
|
+
{packages === undefined ? (
|
|
111
|
+
<div className="tax-empty">{words.loading}</div>
|
|
112
|
+
) : groups.mine.length === 0 ? (
|
|
113
|
+
<div className="tax-empty">{words.mineEmpty}</div>
|
|
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>
|
|
140
|
+
</div>
|
|
141
|
+
<ShareSteps pi={pi} actions={actions} />
|
|
142
|
+
</section>
|
|
143
|
+
</div>
|
|
144
|
+
{dragging && <div className="tax-drop-overlay">{words.dropToImport}</div>}
|
|
145
|
+
{dialog && <ImportDialog pi={pi} actions={actions} onClose={() => importing.close()} />}
|
|
146
|
+
</section>
|
|
147
|
+
);
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/** Export, send the file, import: in pictures, with one line for the other ways. */
|
|
151
|
+
function ShareSteps({ pi, actions }: { pi: WebExtensionAPI; actions: ExtensionActions }): React.ReactElement {
|
|
152
|
+
const words = actions.words;
|
|
153
|
+
const steps = [icon(pi, 'Download'), icon(pi, 'FileArchive', 'File'), icon(pi, 'Upload')];
|
|
154
|
+
const Arrow = icon(pi, 'ArrowRight');
|
|
155
|
+
return (
|
|
156
|
+
<div className="tax-share">
|
|
157
|
+
<div className="tax-steps">
|
|
158
|
+
{steps.map((Step, index) => (
|
|
159
|
+
<React.Fragment key={words.shareSteps[index]}>
|
|
160
|
+
{index > 0 && (
|
|
161
|
+
<span className="tax-step-arrow">
|
|
162
|
+
<Arrow size={16} />
|
|
163
|
+
</span>
|
|
164
|
+
)}
|
|
165
|
+
<div className="tax-step">
|
|
166
|
+
<span className="tax-step-icon">
|
|
167
|
+
<Step size={20} />
|
|
168
|
+
</span>
|
|
169
|
+
<span>{words.shareSteps[index]}</span>
|
|
170
|
+
</div>
|
|
171
|
+
</React.Fragment>
|
|
172
|
+
))}
|
|
173
|
+
</div>
|
|
174
|
+
<div className="tax-note">{words.shareMore}</div>
|
|
175
|
+
</div>
|
|
176
|
+
);
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
interface PackageRowProps {
|
|
180
|
+
pi: WebExtensionAPI;
|
|
181
|
+
actions: ExtensionActions;
|
|
182
|
+
pkg: TaPackageInfo;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
/** One extension: what it is and holds, a switch; on hover, what can be done with it. */
|
|
186
|
+
function PackageRow({ pi, actions, pkg }: PackageRowProps): React.ReactElement {
|
|
187
|
+
const words = actions.words;
|
|
188
|
+
const mine = pkg.origin === 'directory';
|
|
189
|
+
const Icon = icon(pi, mine ? 'Puzzle' : 'PackageOpen');
|
|
190
|
+
const tags = [...pkg.resources.map(type => words.kinds[type]), ...(pkg.scope === 'project' ? [words.project] : [])];
|
|
191
|
+
const buttons: { key: string; label: string; icon: string; danger?: boolean; run(): Promise<void> }[] = mine
|
|
192
|
+
? [
|
|
193
|
+
{ key: 'change', label: words.change, icon: 'Pencil', run: () => actions.change(pkg) },
|
|
194
|
+
{ key: 'export', label: words.export, icon: 'Download', run: () => actions.exportPackage(pkg) },
|
|
195
|
+
{ key: 'remove', label: words.remove, icon: 'Trash2', danger: true, run: () => actions.remove(pkg) },
|
|
196
|
+
]
|
|
197
|
+
: [
|
|
198
|
+
{ key: 'copy', label: words.copySource, icon: 'Copy', run: () => actions.copySource(pkg) },
|
|
199
|
+
{ key: 'remove', label: words.remove, icon: 'Trash2', danger: true, run: () => actions.remove(pkg) },
|
|
200
|
+
];
|
|
201
|
+
return (
|
|
202
|
+
<div className={`tax-row${pkg.enabled ? '' : ' tax-off'}`}>
|
|
203
|
+
<span className="tax-icon">
|
|
204
|
+
<Icon size={17} />
|
|
205
|
+
</span>
|
|
206
|
+
<div className="tax-main">
|
|
207
|
+
<div className="tax-name">
|
|
208
|
+
<span>{pkg.name}</span>
|
|
209
|
+
{pkg.version && <span className="tax-version">{pkg.version}</span>}
|
|
210
|
+
{tags.map(tag => (
|
|
211
|
+
<span key={tag} className="tax-tag">
|
|
212
|
+
{tag}
|
|
213
|
+
</span>
|
|
214
|
+
))}
|
|
215
|
+
</div>
|
|
216
|
+
{(pkg.description || !mine) && <div className="tax-description">{pkg.description || pkg.source}</div>}
|
|
217
|
+
</div>
|
|
218
|
+
<div className="tax-actions">
|
|
219
|
+
{buttons.map(button => {
|
|
220
|
+
const ButtonIcon = icon(pi, button.icon);
|
|
221
|
+
return (
|
|
222
|
+
<button
|
|
223
|
+
key={button.key}
|
|
224
|
+
type="button"
|
|
225
|
+
className={`tax-action${button.danger ? ' tax-danger' : ''}`}
|
|
226
|
+
title={button.label}
|
|
227
|
+
aria-label={button.label}
|
|
228
|
+
onClick={() => void button.run()}
|
|
229
|
+
>
|
|
230
|
+
<ButtonIcon size={15} />
|
|
231
|
+
</button>
|
|
232
|
+
);
|
|
233
|
+
})}
|
|
234
|
+
</div>
|
|
235
|
+
<input
|
|
236
|
+
type="checkbox"
|
|
237
|
+
role="switch"
|
|
238
|
+
className="tax-switch"
|
|
239
|
+
aria-label={ExtensionWords.fill(words.turnOn, { name: pkg.name })}
|
|
240
|
+
aria-checked={pkg.enabled}
|
|
241
|
+
checked={pkg.enabled}
|
|
242
|
+
onChange={event => void actions.setEnabled(pkg, event.target.checked)}
|
|
243
|
+
/>
|
|
244
|
+
</div>
|
|
245
|
+
);
|
|
246
|
+
}
|