@bridgebrew/project 0.0.0-stage → 0.0.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/LICENSE +21 -0
- package/browser/branches.tsx +526 -0
- package/browser/browser.tsx +265 -0
- package/browser/mobile.tsx +62 -0
- package/browser/new-project.tsx +185 -0
- package/browser/overview.tsx +191 -0
- package/browser/project.d.ts +60 -0
- package/browser/sources.tsx +310 -0
- package/browser/tsconfig.json +15 -0
- package/names.ts +28 -0
- package/package.json +101 -4
- package/server.ts +254 -0
- package/tsconfig.json +1 -0
- package/README.md +0 -3
|
@@ -0,0 +1,265 @@
|
|
|
1
|
+
// The Projects view (AD-24, research-061): the workspace's projects, a list (title, then folder and git state), and New
|
|
2
|
+
// project, the wizard (./new-project.tsx, bridge#126) that makes one under the node shown. A project is any folder of
|
|
3
|
+
// work: code, documents, anything. Choosing a project shows its node. What a project needs is in its folder's
|
|
4
|
+
// `bridge.yml`, applied by Bridge: no approval here. Each folder's git state (slice 4): its own repository or its
|
|
5
|
+
// parent's, its branch, uncommitted changes and remote; none outside git.
|
|
6
|
+
// The plugin provides `projectSources` (./project.d.ts), where a project comes from, with its own three
|
|
7
|
+
// (./sources.tsx); it adds "New project…" to the sidebar's menus (`shell.nodeActions`) wherever it is active, whose
|
|
8
|
+
// wizard shows in Bridge's `dialogs`.
|
|
9
|
+
import * as React from 'react';
|
|
10
|
+
import { Button, EmptyState, Icon, List, ListItem, isRecord } from '@bridge/ui';
|
|
11
|
+
import type { Context } from 'cordis';
|
|
12
|
+
import type {} from '@bridge/ui-renderer/client';
|
|
13
|
+
import { Branches } from './branches.tsx';
|
|
14
|
+
import { MobileProjects } from './mobile.tsx';
|
|
15
|
+
import { NewProject } from './new-project.tsx';
|
|
16
|
+
import { Overview } from './overview.tsx';
|
|
17
|
+
import { ownSources } from './sources.tsx';
|
|
18
|
+
|
|
19
|
+
export const name = 'project';
|
|
20
|
+
export const inject = ['slots', 'workspace', 'nodes', 'shell'];
|
|
21
|
+
|
|
22
|
+
type Sources = Context['projectSources'];
|
|
23
|
+
type Source = ReturnType<Sources['list']>[number];
|
|
24
|
+
|
|
25
|
+
interface Git {
|
|
26
|
+
own: boolean;
|
|
27
|
+
top: string;
|
|
28
|
+
branch?: string;
|
|
29
|
+
/** Absent when git could not read it. */
|
|
30
|
+
changes?: number;
|
|
31
|
+
remote?: string;
|
|
32
|
+
}
|
|
33
|
+
interface Project {
|
|
34
|
+
id: string;
|
|
35
|
+
title: string;
|
|
36
|
+
path: string;
|
|
37
|
+
git?: Git;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/** A folder's git state in words: whose repository, its branch, its changes, its remote. */
|
|
41
|
+
export const gitText = (git: Git, parentRepository: boolean) =>
|
|
42
|
+
[
|
|
43
|
+
git.own ? 'Own repository' : parentRepository ? "In its parent's repository" : `In ${git.top}`,
|
|
44
|
+
git.branch,
|
|
45
|
+
git.changes === undefined
|
|
46
|
+
? 'status unknown'
|
|
47
|
+
: git.changes
|
|
48
|
+
? `${git.changes} uncommitted ${git.changes === 1 ? 'change' : 'changes'}`
|
|
49
|
+
: 'clean',
|
|
50
|
+
git.remote ?? 'no remote',
|
|
51
|
+
]
|
|
52
|
+
.filter(Boolean)
|
|
53
|
+
.join(' · ');
|
|
54
|
+
|
|
55
|
+
/** The sources' registry: by `order`, a taken id refused. */
|
|
56
|
+
export function sourceRegistry(): Sources {
|
|
57
|
+
let sources: readonly Source[] = [];
|
|
58
|
+
const listeners = new Set<() => void>();
|
|
59
|
+
const changed = () => {
|
|
60
|
+
for (const listener of listeners) listener();
|
|
61
|
+
};
|
|
62
|
+
return {
|
|
63
|
+
register(source) {
|
|
64
|
+
if (sources.some((each) => each.id === source.id))
|
|
65
|
+
throw new Error(`a project source "${source.id}" is registered already`);
|
|
66
|
+
sources = [...sources, source].sort((a, b) => (a.order ?? 100) - (b.order ?? 100));
|
|
67
|
+
changed();
|
|
68
|
+
return () => {
|
|
69
|
+
sources = sources.filter((each) => each !== source);
|
|
70
|
+
changed();
|
|
71
|
+
};
|
|
72
|
+
},
|
|
73
|
+
list: () => sources,
|
|
74
|
+
subscribe(listener) {
|
|
75
|
+
listeners.add(listener);
|
|
76
|
+
return () => void listeners.delete(listener);
|
|
77
|
+
},
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/** Who asks for the wizard, and for which node: the sidebar's menus and the Projects screen. */
|
|
82
|
+
function wizardRequests() {
|
|
83
|
+
let asked: string | undefined;
|
|
84
|
+
const listeners = new Set<() => void>();
|
|
85
|
+
return {
|
|
86
|
+
get: () => asked,
|
|
87
|
+
set(node: string | undefined) {
|
|
88
|
+
asked = node;
|
|
89
|
+
for (const listener of listeners) listener();
|
|
90
|
+
},
|
|
91
|
+
subscribe(listener: () => void) {
|
|
92
|
+
listeners.add(listener);
|
|
93
|
+
return () => void listeners.delete(listener);
|
|
94
|
+
},
|
|
95
|
+
};
|
|
96
|
+
}
|
|
97
|
+
type Requests = ReturnType<typeof wizardRequests>;
|
|
98
|
+
|
|
99
|
+
/** The wizard, in Bridge's `dialogs`, for the node asked. */
|
|
100
|
+
function Dialog({ ctx, requests }: { ctx: Context; requests: Requests }) {
|
|
101
|
+
const asked = React.useSyncExternalStore(requests.subscribe, requests.get);
|
|
102
|
+
const tree = React.useSyncExternalStore(ctx.shell.subscribe, () => ctx.shell.get().nodes);
|
|
103
|
+
// Kept while it closes, so its content does not change under the closing dialog.
|
|
104
|
+
const [parent, setParent] = React.useState('root');
|
|
105
|
+
React.useEffect(() => {
|
|
106
|
+
if (asked !== undefined) setParent(asked);
|
|
107
|
+
}, [asked]);
|
|
108
|
+
const title =
|
|
109
|
+
parent === 'root'
|
|
110
|
+
? (ctx.shell.get().current?.title ?? ctx.workspace.slug)
|
|
111
|
+
: (tree?.nodes.find((node) => node.id === parent)?.title ?? '');
|
|
112
|
+
return (
|
|
113
|
+
<NewProject
|
|
114
|
+
ctx={ctx}
|
|
115
|
+
parent={parent}
|
|
116
|
+
parentTitle={title}
|
|
117
|
+
open={asked !== undefined}
|
|
118
|
+
onOpenChange={(open) => {
|
|
119
|
+
if (!open) requests.set(undefined);
|
|
120
|
+
}}
|
|
121
|
+
/>
|
|
122
|
+
);
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
function GitLine({ git, parent }: { git: Git | undefined; parent: boolean }) {
|
|
126
|
+
if (!git) return null;
|
|
127
|
+
return <span data-git={git.own ? 'own' : 'parent'}>{gitText(git, parent)}</span>;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
function Projects({ ctx, node, requests }: { ctx: Context; node: string; requests: Requests }) {
|
|
131
|
+
const { workspace, nodes } = ctx;
|
|
132
|
+
const api = `/${workspace.slug}/api/project/projects`;
|
|
133
|
+
const revision = React.useSyncExternalStore(nodes.subscribe, () => nodes.revision);
|
|
134
|
+
const [list, setList] = React.useState<Project[]>();
|
|
135
|
+
const [root, setRoot] = React.useState<{ path: string | null; git?: Git }>();
|
|
136
|
+
// A project made elsewhere (an agent, the CLI, bridge.yml edited) makes a node: the tree's revision moves.
|
|
137
|
+
React.useEffect(() => {
|
|
138
|
+
let live = true;
|
|
139
|
+
Promise.all([
|
|
140
|
+
workspace.fetch(`${api}?git=1`).then((res) => res.json() as Promise<unknown>),
|
|
141
|
+
workspace.fetch(`${api}/workspace`).then((res) => res.json() as Promise<unknown>),
|
|
142
|
+
]).then(
|
|
143
|
+
([projects, folder]) => {
|
|
144
|
+
if (!live) return;
|
|
145
|
+
// This plugin's own answers.
|
|
146
|
+
setList(Array.isArray(projects) ? (projects as Project[]) : []);
|
|
147
|
+
if (isRecord(folder)) setRoot(folder as { path: string | null; git?: Git });
|
|
148
|
+
},
|
|
149
|
+
(cause: Error) => {
|
|
150
|
+
if (cause.name !== 'AbortError') console.warn(`projects: ${cause.message}`);
|
|
151
|
+
if (live) setList((was) => was ?? []);
|
|
152
|
+
},
|
|
153
|
+
);
|
|
154
|
+
return () => {
|
|
155
|
+
live = false;
|
|
156
|
+
};
|
|
157
|
+
}, [revision]);
|
|
158
|
+
|
|
159
|
+
return (
|
|
160
|
+
<section aria-label="Screen" className="flex max-w-[720px] flex-col gap-4 p-6">
|
|
161
|
+
<header className="flex items-center gap-3">
|
|
162
|
+
<h1 className="flex-1 text-xl font-semibold">Projects</h1>
|
|
163
|
+
<Button variant="primary" onClick={() => requests.set(node)}>
|
|
164
|
+
<Icon name="folder-plus" />
|
|
165
|
+
New project
|
|
166
|
+
</Button>
|
|
167
|
+
</header>
|
|
168
|
+
{root?.path && (
|
|
169
|
+
<p aria-label="Workspace repository" className="flex flex-col gap-1 text-sm">
|
|
170
|
+
<code>{root.path}</code>
|
|
171
|
+
{root.git && (
|
|
172
|
+
<span className="text-xs text-muted">
|
|
173
|
+
<GitLine git={root.git} parent={false} />
|
|
174
|
+
</span>
|
|
175
|
+
)}
|
|
176
|
+
</p>
|
|
177
|
+
)}
|
|
178
|
+
{list?.length === 0 ? (
|
|
179
|
+
<EmptyState title="No project yet">
|
|
180
|
+
A project is a folder of work: code, documents, anything.
|
|
181
|
+
</EmptyState>
|
|
182
|
+
) : (
|
|
183
|
+
<List aria-label="Projects">
|
|
184
|
+
{(list ?? []).map((row) => (
|
|
185
|
+
<ListItem
|
|
186
|
+
key={row.id}
|
|
187
|
+
value={row.id}
|
|
188
|
+
textValue={row.title}
|
|
189
|
+
data-node={row.id}
|
|
190
|
+
// Choosing a project shows its node, by Bridge's rule (⌘, ⌥, a middle click: beside, research-048).
|
|
191
|
+
onClick={(event) => ctx.shell.follow({ node: row.id }, event)}
|
|
192
|
+
onAuxClick={(event) => {
|
|
193
|
+
if (event.button !== 1) return;
|
|
194
|
+
event.preventDefault();
|
|
195
|
+
ctx.shell.follow({ node: row.id }, event);
|
|
196
|
+
}}
|
|
197
|
+
onKeyDown={(event) => {
|
|
198
|
+
if (event.key === 'Enter') ctx.shell.follow({ node: row.id }, event);
|
|
199
|
+
}}
|
|
200
|
+
leading={<Icon name="folder-root" />}
|
|
201
|
+
description={
|
|
202
|
+
<>
|
|
203
|
+
{row.path}
|
|
204
|
+
{row.git && (
|
|
205
|
+
<>
|
|
206
|
+
{' · '}
|
|
207
|
+
<GitLine git={row.git} parent={row.git.top === root?.git?.top} />
|
|
208
|
+
</>
|
|
209
|
+
)}
|
|
210
|
+
</>
|
|
211
|
+
}
|
|
212
|
+
>
|
|
213
|
+
{row.title}
|
|
214
|
+
</ListItem>
|
|
215
|
+
))}
|
|
216
|
+
</List>
|
|
217
|
+
)}
|
|
218
|
+
</section>
|
|
219
|
+
);
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
export function apply(ctx: Context) {
|
|
223
|
+
const sources = sourceRegistry();
|
|
224
|
+
ctx.provide('projectSources', sources);
|
|
225
|
+
const list = async (parent: string, path: string) => {
|
|
226
|
+
const query = new URLSearchParams({ node: parent, path });
|
|
227
|
+
const res = await ctx.workspace.fetch(
|
|
228
|
+
`/${ctx.workspace.slug}/api/project/projects/folders?${query}`,
|
|
229
|
+
);
|
|
230
|
+
const body: unknown = await res.json();
|
|
231
|
+
if (!res.ok) throw new Error(`HTTP ${res.status}`);
|
|
232
|
+
// This plugin's own answer.
|
|
233
|
+
return body as {
|
|
234
|
+
root: string;
|
|
235
|
+
path: string;
|
|
236
|
+
folders: { name: string; path: string; project?: string }[];
|
|
237
|
+
};
|
|
238
|
+
};
|
|
239
|
+
ctx.effect(() => {
|
|
240
|
+
const offs = ownSources(list).map((source) => sources.register(source));
|
|
241
|
+
return () => offs.forEach((off) => off());
|
|
242
|
+
});
|
|
243
|
+
const requests = wizardRequests();
|
|
244
|
+
ctx.effect(() =>
|
|
245
|
+
ctx.shell.nodeActions.register({
|
|
246
|
+
id: 'new-project',
|
|
247
|
+
plugin: name,
|
|
248
|
+
workspace: ctx.workspace.slug,
|
|
249
|
+
label: 'New project…',
|
|
250
|
+
newLabel: 'Project…',
|
|
251
|
+
run: (node) => requests.set(node),
|
|
252
|
+
}),
|
|
253
|
+
);
|
|
254
|
+
ctx.slots.register({ name: 'dialogs', id: 'project-new' }, () => (
|
|
255
|
+
<Dialog ctx={ctx} requests={requests} />
|
|
256
|
+
));
|
|
257
|
+
// Its root screens (package.json): a project node's Overview and Branches, shown where chosen, and Projects.
|
|
258
|
+
ctx.slots.register({ name: 'screen', key: name }, ({ node, screen }) => {
|
|
259
|
+
const root = screen.split('/')[0];
|
|
260
|
+
if (root === 'overview') return <Overview ctx={ctx} node={node} />;
|
|
261
|
+
if (root === 'branches') return <Branches ctx={ctx} node={node} />;
|
|
262
|
+
return <Projects ctx={ctx} node={node} requests={requests} />;
|
|
263
|
+
});
|
|
264
|
+
ctx.slots.register({ name: 'mobileScreen', key: name }, () => <MobileProjects ctx={ctx} />);
|
|
265
|
+
}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
// The Projects screen on a phone (the shell's `mobileScreen` slot): the workspace's projects, read-only. A tap shows the
|
|
2
|
+
// project's node, as the desktop list's button does. A new project is made from the desktop, or by an agent.
|
|
3
|
+
import * as React from 'react';
|
|
4
|
+
import type { Context } from 'cordis';
|
|
5
|
+
import type {} from '@bridge/ui-renderer/client';
|
|
6
|
+
import { EmptyState, PaneNote } from '@bridge/ui';
|
|
7
|
+
|
|
8
|
+
interface Project {
|
|
9
|
+
id: string;
|
|
10
|
+
title: string;
|
|
11
|
+
path: string;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export function MobileProjects({ ctx }: { ctx: Context }) {
|
|
15
|
+
const { workspace, nodes, shell } = ctx;
|
|
16
|
+
const api = `/${workspace.slug}/api/project/projects`;
|
|
17
|
+
const revision = React.useSyncExternalStore(nodes.subscribe, () => nodes.revision);
|
|
18
|
+
const [list, setList] = React.useState<Project[]>();
|
|
19
|
+
React.useEffect(() => {
|
|
20
|
+
let live = true;
|
|
21
|
+
workspace
|
|
22
|
+
.fetch(api)
|
|
23
|
+
.then((res) => (res.ok ? res.json() : Promise.reject(new Error(`HTTP ${res.status}`))))
|
|
24
|
+
.then(
|
|
25
|
+
(found: Project[]) => live && setList(found),
|
|
26
|
+
(cause: Error) => {
|
|
27
|
+
if (cause.name !== 'AbortError') console.warn(`projects: ${cause.message}`);
|
|
28
|
+
if (live) setList((was) => was ?? []);
|
|
29
|
+
},
|
|
30
|
+
);
|
|
31
|
+
return () => {
|
|
32
|
+
live = false;
|
|
33
|
+
};
|
|
34
|
+
}, [api, workspace, revision]);
|
|
35
|
+
|
|
36
|
+
return (
|
|
37
|
+
<div className="flex min-h-0 flex-1 flex-col overflow-y-auto pb-[var(--mobile-inset-bottom,env(safe-area-inset-bottom))]">
|
|
38
|
+
{list === undefined ? (
|
|
39
|
+
<PaneNote role="status">Loading…</PaneNote>
|
|
40
|
+
) : list.length === 0 ? (
|
|
41
|
+
<EmptyState title="No projects yet." />
|
|
42
|
+
) : (
|
|
43
|
+
<ul aria-label="Projects" className="m-0 list-none p-0">
|
|
44
|
+
{list.map((project) => (
|
|
45
|
+
<li key={project.id} data-node={project.id} className="border-b border-border">
|
|
46
|
+
<button
|
|
47
|
+
type="button"
|
|
48
|
+
// oxlint-disable-next-line bridge/screen-change-reads-click -- a phone has no ⌘, ⌥ or middle click, nor windows to open beside
|
|
49
|
+
onClick={() => shell.setNode(project.id)}
|
|
50
|
+
className="flex min-h-[56px] w-full cursor-pointer flex-col items-start justify-center gap-0.5 border-0 bg-transparent px-4 py-2 text-left text-foreground"
|
|
51
|
+
>
|
|
52
|
+
<span className="w-full truncate text-[16px] font-medium">{project.title}</span>
|
|
53
|
+
<span className="w-full truncate text-[12px] text-secondary">{project.path}</span>
|
|
54
|
+
</button>
|
|
55
|
+
</li>
|
|
56
|
+
))}
|
|
57
|
+
</ul>
|
|
58
|
+
)}
|
|
59
|
+
<p className="p-4 text-center text-[13px] text-secondary">Make a project from the desktop.</p>
|
|
60
|
+
</div>
|
|
61
|
+
);
|
|
62
|
+
}
|
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
// The New project wizard (bridge#126, a "Sources" step): a kit `Wizard` whose first step lists where a project comes
|
|
2
|
+
// from (`projectSources`: this plugin's own, ./sources.tsx, then those other plugins add), each with its icon, title and
|
|
3
|
+
// one line; choosing one shows its step, whose submit makes the project under node `parent` (in its nearest project's
|
|
4
|
+
// folder). Made, the wizard closes and the new project's node shows, once the tree has it. A dialog: no navigation entry. Opened from the
|
|
5
|
+
// sidebar's menus (`shell.nodeActions`, through Bridge's `dialogs`) and from the Projects screen.
|
|
6
|
+
import * as React from 'react';
|
|
7
|
+
import { List, ListItem, Wizard, WizardStep, isRecord, refusalOf, useWizard } from '@bridge/ui';
|
|
8
|
+
import type { Context } from 'cordis';
|
|
9
|
+
|
|
10
|
+
type Sources = Context['projectSources'];
|
|
11
|
+
type Input = Parameters<
|
|
12
|
+
React.ComponentProps<ReturnType<Sources['list']>[number]['Step']>['make']
|
|
13
|
+
>[0];
|
|
14
|
+
|
|
15
|
+
/** Asks the project plugin to make a project under `parent`: `{ id, title }`, or the refusal (its `details` kept). */
|
|
16
|
+
export async function makeProject(
|
|
17
|
+
workspace: Context['workspace'],
|
|
18
|
+
parent: string,
|
|
19
|
+
input: Input,
|
|
20
|
+
): Promise<{ id: string; title: string }> {
|
|
21
|
+
const api = `/${workspace.slug}/api/project/projects`;
|
|
22
|
+
const res = await workspace.fetch('path' in input ? api : `${api}/new`, {
|
|
23
|
+
method: 'POST',
|
|
24
|
+
headers: { 'content-type': 'application/json', 'x-bridge-node': parent },
|
|
25
|
+
body: JSON.stringify(input),
|
|
26
|
+
});
|
|
27
|
+
const body: unknown = await res.json().catch(() => undefined);
|
|
28
|
+
if (!res.ok) {
|
|
29
|
+
const details =
|
|
30
|
+
isRecord(body) && isRecord(body.error) && isRecord(body.error.details)
|
|
31
|
+
? body.error.details
|
|
32
|
+
: undefined;
|
|
33
|
+
throw Object.assign(new Error(refusalOf(body, res.status).message), details && { details });
|
|
34
|
+
}
|
|
35
|
+
// This plugin's own answer.
|
|
36
|
+
return (body as { node: { id: string; title: string } }).node;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/** Shows node `id` once the shell's tree has it: a project's node can reach the tree after the answer that made it,
|
|
40
|
+
* and a node not in the tree yet is not shown. Not if the node shown changes meanwhile (the user went elsewhere). */
|
|
41
|
+
export function showWhenKnown(shell: Context['shell'], id: string) {
|
|
42
|
+
const from = shell.get().node;
|
|
43
|
+
const known = () => shell.get().nodes?.nodes.some((node) => node.id === id) === true;
|
|
44
|
+
if (known()) return shell.setNode(id);
|
|
45
|
+
const off = shell.subscribe(() => {
|
|
46
|
+
if (shell.get().node !== from) return off();
|
|
47
|
+
if (!known()) return;
|
|
48
|
+
off();
|
|
49
|
+
shell.setNode(id);
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/** The first step: the sources, a choice going on to its step. */
|
|
54
|
+
function Choose({
|
|
55
|
+
sources,
|
|
56
|
+
go,
|
|
57
|
+
}: {
|
|
58
|
+
sources: ReturnType<Sources['list']>;
|
|
59
|
+
go: (id: string) => void;
|
|
60
|
+
}) {
|
|
61
|
+
return (
|
|
62
|
+
<List
|
|
63
|
+
aria-label="Sources"
|
|
64
|
+
selectionMode="single"
|
|
65
|
+
selectedKey=""
|
|
66
|
+
onSelectionChange={(keys) => {
|
|
67
|
+
if (keys[0]) go(keys[0]);
|
|
68
|
+
}}
|
|
69
|
+
>
|
|
70
|
+
{sources.map((source) => (
|
|
71
|
+
<ListItem
|
|
72
|
+
key={source.id}
|
|
73
|
+
value={source.id}
|
|
74
|
+
textValue={source.title}
|
|
75
|
+
leading={source.icon}
|
|
76
|
+
description={source.description}
|
|
77
|
+
>
|
|
78
|
+
{source.title}
|
|
79
|
+
</ListItem>
|
|
80
|
+
))}
|
|
81
|
+
</List>
|
|
82
|
+
);
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
function Steps({
|
|
86
|
+
sources,
|
|
87
|
+
parent,
|
|
88
|
+
parentTitle,
|
|
89
|
+
folder,
|
|
90
|
+
workspace,
|
|
91
|
+
made,
|
|
92
|
+
}: {
|
|
93
|
+
sources: ReturnType<Sources['list']>;
|
|
94
|
+
parent: string;
|
|
95
|
+
parentTitle: string;
|
|
96
|
+
folder: string | undefined;
|
|
97
|
+
workspace: Context['workspace'];
|
|
98
|
+
made: (node: { id: string; title: string }) => void;
|
|
99
|
+
}) {
|
|
100
|
+
const wizard = useWizard();
|
|
101
|
+
const [prefill, setPrefill] = React.useState<Record<string, string>>();
|
|
102
|
+
return (
|
|
103
|
+
<>
|
|
104
|
+
<WizardStep
|
|
105
|
+
id="sources"
|
|
106
|
+
title={`New project in ${parentTitle}`}
|
|
107
|
+
description="Where does it come from?"
|
|
108
|
+
>
|
|
109
|
+
<Choose
|
|
110
|
+
sources={sources}
|
|
111
|
+
go={(id) => {
|
|
112
|
+
setPrefill(undefined);
|
|
113
|
+
wizard.go(id);
|
|
114
|
+
}}
|
|
115
|
+
/>
|
|
116
|
+
</WizardStep>
|
|
117
|
+
{sources.map(({ id, Step }) => (
|
|
118
|
+
<Step
|
|
119
|
+
key={id}
|
|
120
|
+
step={id}
|
|
121
|
+
parent={parent}
|
|
122
|
+
folder={folder}
|
|
123
|
+
prefill={wizard.step === id ? prefill : undefined}
|
|
124
|
+
goTo={(source, values) => {
|
|
125
|
+
setPrefill(values);
|
|
126
|
+
wizard.go(source);
|
|
127
|
+
}}
|
|
128
|
+
make={async (input) => {
|
|
129
|
+
const node = await makeProject(workspace, parent, input);
|
|
130
|
+
made(node);
|
|
131
|
+
return node;
|
|
132
|
+
}}
|
|
133
|
+
/>
|
|
134
|
+
))}
|
|
135
|
+
</>
|
|
136
|
+
);
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
export function NewProject({
|
|
140
|
+
ctx,
|
|
141
|
+
parent,
|
|
142
|
+
parentTitle,
|
|
143
|
+
open,
|
|
144
|
+
onOpenChange,
|
|
145
|
+
}: {
|
|
146
|
+
ctx: Context;
|
|
147
|
+
parent: string;
|
|
148
|
+
parentTitle: string;
|
|
149
|
+
open: boolean;
|
|
150
|
+
onOpenChange: (open: boolean) => void;
|
|
151
|
+
}) {
|
|
152
|
+
const sources = React.useSyncExternalStore(ctx.projectSources.subscribe, ctx.projectSources.list);
|
|
153
|
+
const [folder, setFolder] = React.useState<string>();
|
|
154
|
+
React.useEffect(() => {
|
|
155
|
+
if (!open) return;
|
|
156
|
+
let live = true;
|
|
157
|
+
const query = new URLSearchParams({ node: parent });
|
|
158
|
+
ctx.workspace
|
|
159
|
+
.fetch(`/${ctx.workspace.slug}/api/project/projects/folders?${query}`)
|
|
160
|
+
.then((res) => res.json() as Promise<unknown>)
|
|
161
|
+
.then(
|
|
162
|
+
(answer) => {
|
|
163
|
+
if (live && isRecord(answer) && typeof answer.root === 'string') setFolder(answer.root);
|
|
164
|
+
},
|
|
165
|
+
(cause: Error) => {
|
|
166
|
+
if (cause.name !== 'AbortError') console.warn(`projects: ${cause.message}`);
|
|
167
|
+
},
|
|
168
|
+
);
|
|
169
|
+
return () => {
|
|
170
|
+
live = false;
|
|
171
|
+
};
|
|
172
|
+
}, [open, parent]);
|
|
173
|
+
return (
|
|
174
|
+
<Wizard open={open} onOpenChange={onOpenChange} initial="sources">
|
|
175
|
+
<Steps
|
|
176
|
+
sources={sources}
|
|
177
|
+
parent={parent}
|
|
178
|
+
parentTitle={parentTitle}
|
|
179
|
+
folder={folder}
|
|
180
|
+
workspace={ctx.workspace}
|
|
181
|
+
made={(node) => showWhenKnown(ctx.shell, node.id)}
|
|
182
|
+
/>
|
|
183
|
+
</Wizard>
|
|
184
|
+
);
|
|
185
|
+
}
|