@backmatter/studio 0.0.18 → 0.0.19
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/dist/chunks/{UiKitApp-DkuQBiQZ.js → UiKitApp-B1ANqCac.js} +528 -481
- package/dist/chunks/{WorkspaceRoutes-DVvb3N-V.js → WorkspaceRoutes-BHNGrf-j.js} +24371 -23915
- package/dist/chunks/{dashboard-B4FITg6c.js → dashboard-b-W6dGm5.js} +586 -565
- package/dist/chunks/{index-CegtXnEI.js → index-HYioDjux.js} +104 -104
- package/dist/dashboard.js +2 -2
- package/dist/index.js +166 -159
- package/dist/site-widget.css +25 -6
- package/dist/site-widget.js +6925 -6840
- package/dist/studio.css +159 -36
- package/package.json +2 -2
- package/src/api.ts +41 -1
- package/src/chat/ChatMessage.tsx +4 -4
- package/src/chat/ChatProvider.tsx +6 -0
- package/src/chat/chat-helpers.ts +54 -2
- package/src/chrome/CommandPalette.tsx +3 -3
- package/src/chrome/ContentExplorerSidebar.tsx +421 -74
- package/src/chrome/WorkspaceStatusBar.tsx +24 -24
- package/src/chrome/commit-error.ts +48 -60
- package/src/chrome/commit-failure.tsx +2 -2
- package/src/chrome/content-path-tree.ts +4 -0
- package/src/chrome/explorer-row-menu.ts +28 -0
- package/src/chrome/folder-create-context.ts +14 -0
- package/src/chrome/use-publish-check.ts +75 -0
- package/src/fields/FieldControl.tsx +173 -62
- package/src/pages/DocEditorPage.tsx +47 -64
- package/src/pages/EmbedPublishPage.tsx +13 -5
- package/src/pages/GithubConnectPage.tsx +1 -1
- package/src/pages/LoginPage.tsx +67 -5
- package/src/pages/MediaFieldInput.tsx +155 -63
- package/src/pages/MediaGalleryPage.tsx +56 -3
- package/src/pages/PreviewAddress.tsx +30 -0
- package/src/pages/WorkspaceHomePage.tsx +1 -1
- package/src/pages/ui-kit/colors.tsx +2 -0
- package/src/pages/ui-kit/fields.tsx +60 -0
- package/src/styles/themes.ts +2 -0
- package/src/styles/tokens.stylex.ts +1 -0
- package/src/ui/Button.tsx +14 -0
- package/src/ui/Field.tsx +12 -1
- package/src/ui/Input.tsx +191 -97
- package/src/ui/popup.ts +13 -6
|
@@ -19,7 +19,6 @@ import {
|
|
|
19
19
|
notifyWorkspaceChanged,
|
|
20
20
|
pullFromGithub,
|
|
21
21
|
resolveDoc,
|
|
22
|
-
type ApiError,
|
|
23
22
|
type ChangeStatus,
|
|
24
23
|
type GitChange,
|
|
25
24
|
} from "../api";
|
|
@@ -27,10 +26,12 @@ import { contentDocPath, isWorkspaceCodePath } from "../workspace-path";
|
|
|
27
26
|
import { useCodeSessionContext } from "../pages/code-session-context";
|
|
28
27
|
import {
|
|
29
28
|
explainCommitError,
|
|
29
|
+
problemText,
|
|
30
30
|
problemsForRef,
|
|
31
31
|
type CommitErrorView,
|
|
32
32
|
} from "./commit-error";
|
|
33
33
|
import { CommitFailureNotice, commitFailureLink } from "./commit-failure";
|
|
34
|
+
import { publishButtonState, publishCheckKey, usePublishCheck } from "./use-publish-check";
|
|
34
35
|
import { CheckIcon } from "../chat/icons";
|
|
35
36
|
import { WorkspaceSwitcher } from "../components/WorkspaceSwitcher";
|
|
36
37
|
import { colors, radius, space, type } from "../styles/tokens.stylex";
|
|
@@ -260,6 +261,13 @@ export function WorkspaceStatusBar({ workspaceId }: { workspaceId: string }) {
|
|
|
260
261
|
const changeCount = changes.length;
|
|
261
262
|
const conflictCount = countConflicts(git);
|
|
262
263
|
const action = statusBarAction(conflictCount, publish);
|
|
264
|
+
const { failure: publishCheck, checking } = usePublishCheck(
|
|
265
|
+
workspaceId,
|
|
266
|
+
commitOpen && action === "commit",
|
|
267
|
+
publishCheckKey(changes),
|
|
268
|
+
);
|
|
269
|
+
const shownFailure = commitFailure ?? publishCheck;
|
|
270
|
+
const publishButton = publishButtonState({ committing, checking, failure: publishCheck });
|
|
263
271
|
const conflicts = useMemo(
|
|
264
272
|
() => changes.filter((doc) => doc.status === "conflict"),
|
|
265
273
|
[changes],
|
|
@@ -376,15 +384,7 @@ export function WorkspaceStatusBar({ workspaceId }: { workspaceId: string }) {
|
|
|
376
384
|
notifyHeadChanged();
|
|
377
385
|
await reloadGit();
|
|
378
386
|
} catch (err) {
|
|
379
|
-
|
|
380
|
-
if (apiErr.code === "branch_moved") {
|
|
381
|
-
setError("The branch changed while committing. Try again.");
|
|
382
|
-
} else if (apiErr.code === "working_tree") {
|
|
383
|
-
const paths = apiErr.details?.length ? apiErr.details.join(", ") : "";
|
|
384
|
-
setError(paths ? `Uncommitted Studio changes: ${paths}` : "Uncommitted Studio changes block this commit.");
|
|
385
|
-
} else {
|
|
386
|
-
setError(err instanceof Error ? err.message : "Commit failed");
|
|
387
|
-
}
|
|
387
|
+
setError(problemText(err, "Commit failed. Try again."));
|
|
388
388
|
} finally {
|
|
389
389
|
setCodeCommitting(false);
|
|
390
390
|
}
|
|
@@ -421,12 +421,12 @@ export function WorkspaceStatusBar({ workspaceId }: { workspaceId: string }) {
|
|
|
421
421
|
}
|
|
422
422
|
|
|
423
423
|
const commitLabel = committing
|
|
424
|
-
? "
|
|
424
|
+
? "Publishing…"
|
|
425
425
|
: conflictCount > 0
|
|
426
426
|
? `Resolve ${conflictCount} ${conflictCount === 1 ? "conflict" : "conflicts"}`
|
|
427
427
|
: changeCount === 0
|
|
428
|
-
? "
|
|
429
|
-
: `
|
|
428
|
+
? "Publish"
|
|
429
|
+
: `Publish ${changeCount} ${changeCount === 1 ? "change" : "changes"}`;
|
|
430
430
|
|
|
431
431
|
const codeEditCount =
|
|
432
432
|
codeFiles.length > 0 ? codeFiles.length : (codeSession?.editCount ?? 0);
|
|
@@ -529,7 +529,7 @@ export function WorkspaceStatusBar({ workspaceId }: { workspaceId: string }) {
|
|
|
529
529
|
<>
|
|
530
530
|
<div {...stylex.props(popup.separator)} />
|
|
531
531
|
<div {...stylex.props(styles.changes)}>
|
|
532
|
-
<p {...stylex.props(popup.groupLabel)}>
|
|
532
|
+
<p {...stylex.props(popup.groupLabel)}>Unpublished</p>
|
|
533
533
|
{changes.map((doc) => (
|
|
534
534
|
<ChangeRow key={changeKey(doc)} doc={doc} />
|
|
535
535
|
))}
|
|
@@ -597,13 +597,13 @@ export function WorkspaceStatusBar({ workspaceId }: { workspaceId: string }) {
|
|
|
597
597
|
</div>
|
|
598
598
|
{!onCodePage && changeCount > 0 ? (
|
|
599
599
|
<StatusItem tone={conflictCount > 0 ? "danger" : "muted"}>
|
|
600
|
-
{changeCount}
|
|
600
|
+
{changeCount} unpublished
|
|
601
601
|
</StatusItem>
|
|
602
602
|
) : null}
|
|
603
603
|
{error ? (
|
|
604
604
|
<StatusItem tone="danger">{error}</StatusItem>
|
|
605
605
|
) : commitFailure ? (
|
|
606
|
-
<StatusItem tone="danger">Can't
|
|
606
|
+
<StatusItem tone="danger">Can't publish</StatusItem>
|
|
607
607
|
) : refreshError ? (
|
|
608
608
|
<StatusItem tone="danger">{refreshError}</StatusItem>
|
|
609
609
|
) : null}
|
|
@@ -712,12 +712,12 @@ export function WorkspaceStatusBar({ workspaceId }: { workspaceId: string }) {
|
|
|
712
712
|
}
|
|
713
713
|
aria-label={
|
|
714
714
|
changeCount === 0
|
|
715
|
-
? "
|
|
715
|
+
? "Publish"
|
|
716
716
|
: action === "blocked"
|
|
717
717
|
? "Writers cannot publish"
|
|
718
718
|
: action === "resolve"
|
|
719
719
|
? commitLabel
|
|
720
|
-
: `
|
|
720
|
+
: `Publish ${changeCount} ${changeCount === 1 ? "file" : "files"}`
|
|
721
721
|
}
|
|
722
722
|
title={
|
|
723
723
|
changeCount > 0 && action === "blocked" ? "Writers cannot publish" : undefined
|
|
@@ -751,9 +751,9 @@ export function WorkspaceStatusBar({ workspaceId }: { workspaceId: string }) {
|
|
|
751
751
|
<p {...stylex.props(popup.groupLabel)}>
|
|
752
752
|
{changeCount} {changeCount === 1 ? "file" : "files"} → {git?.currentBranch}
|
|
753
753
|
</p>
|
|
754
|
-
{
|
|
754
|
+
{shownFailure ? (
|
|
755
755
|
<CommitFailureNotice
|
|
756
|
-
failure={
|
|
756
|
+
failure={shownFailure}
|
|
757
757
|
renderRef={(ref) => {
|
|
758
758
|
const href = hrefForChange(ref);
|
|
759
759
|
if (!href) return ref;
|
|
@@ -775,7 +775,7 @@ export function WorkspaceStatusBar({ workspaceId }: { workspaceId: string }) {
|
|
|
775
775
|
<ChangeRow
|
|
776
776
|
doc={doc}
|
|
777
777
|
invalid={
|
|
778
|
-
problemsForRef(
|
|
778
|
+
problemsForRef(shownFailure, `${doc.type}/${doc.slug}`).length > 0
|
|
779
779
|
}
|
|
780
780
|
/>
|
|
781
781
|
</li>
|
|
@@ -787,7 +787,7 @@ export function WorkspaceStatusBar({ workspaceId }: { workspaceId: string }) {
|
|
|
787
787
|
value={message}
|
|
788
788
|
onChange={(e) => setMessage(e.target.value)}
|
|
789
789
|
placeholder={defaultCommitMessage(changes)}
|
|
790
|
-
aria-label="
|
|
790
|
+
aria-label="Publish message"
|
|
791
791
|
autoComplete="off"
|
|
792
792
|
spellCheck={false}
|
|
793
793
|
data-no-ring
|
|
@@ -803,10 +803,10 @@ export function WorkspaceStatusBar({ workspaceId }: { workspaceId: string }) {
|
|
|
803
803
|
</span> */}
|
|
804
804
|
<button
|
|
805
805
|
type="submit"
|
|
806
|
-
disabled={
|
|
806
|
+
disabled={publishButton.disabled}
|
|
807
807
|
{...stylex.props(styles.commitSubmitBtn)}
|
|
808
808
|
>
|
|
809
|
-
{
|
|
809
|
+
{publishButton.label}
|
|
810
810
|
</button>
|
|
811
811
|
</div>
|
|
812
812
|
</div>
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
// packages/studio/src/chrome/commit-error.ts
|
|
2
|
+
import type { Problem } from "@backmatter/core";
|
|
2
3
|
import type { ApiError } from "../api";
|
|
3
4
|
|
|
4
5
|
export type CommitFileProblem = {
|
|
@@ -11,36 +12,62 @@ export type CommitErrorView =
|
|
|
11
12
|
| { kind: "validation"; files: CommitFileProblem[] }
|
|
12
13
|
| { kind: "message"; text: string };
|
|
13
14
|
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
15
|
+
const FALLBACK = "Publishing failed. Try again.";
|
|
16
|
+
const WORKSPACE_REF = "Workspace";
|
|
17
|
+
|
|
18
|
+
function sentence(problem: Problem): string {
|
|
19
|
+
const text = problem.fix ? `${problem.message} ${problem.fix}` : problem.message;
|
|
20
|
+
// A field problem starts with the field's key. Other sentences start as written.
|
|
21
|
+
return problem.field ? text.charAt(0).toUpperCase() + text.slice(1) : text;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/** The server's sentence for a failed request, or `fallback` when it sent none. */
|
|
25
|
+
export function problemText(err: unknown, fallback: string): string {
|
|
26
|
+
const first = (err as ApiError | null)?.problems?.[0];
|
|
27
|
+
return first ? sentence(first) : fallback;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/** Group problems for the publish window. The server writes the words. */
|
|
31
|
+
export function viewFromProblems(problems: Problem[]): CommitErrorView {
|
|
32
|
+
const order: string[] = [];
|
|
33
|
+
const byRef = new Map<string, string[]>();
|
|
34
|
+
for (const problem of problems) {
|
|
35
|
+
const ref = problem.doc ? `${problem.doc.type}/${problem.doc.slug}` : WORKSPACE_REF;
|
|
36
|
+
if (!byRef.has(ref)) {
|
|
37
|
+
byRef.set(ref, []);
|
|
38
|
+
order.push(ref);
|
|
39
|
+
}
|
|
40
|
+
byRef.get(ref)!.push(sentence(problem));
|
|
29
41
|
}
|
|
30
|
-
if (
|
|
31
|
-
|
|
42
|
+
if (order.length === 0) return { kind: "message", text: FALLBACK };
|
|
43
|
+
if (order.length === 1 && order[0] === WORKSPACE_REF) {
|
|
44
|
+
return { kind: "message", text: byRef.get(WORKSPACE_REF)![0]! };
|
|
32
45
|
}
|
|
33
|
-
|
|
34
|
-
|
|
46
|
+
return {
|
|
47
|
+
kind: "validation",
|
|
48
|
+
files: order.map((ref) => ({ ref, problems: byRef.get(ref)! })),
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/** What a check found, or null when a publish would go through. */
|
|
53
|
+
export function checkToFailure(problems: Problem[]): CommitErrorView | null {
|
|
54
|
+
const blocking = problems.filter((problem) => problem.code !== "nothing_to_publish");
|
|
55
|
+
return blocking.length > 0 ? viewFromProblems(blocking) : null;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** Turn a failed publish into what the publish window should say. */
|
|
59
|
+
export function explainCommitError(err: unknown): CommitErrorView {
|
|
60
|
+
if ((err as ApiError | null)?.status === 401) {
|
|
61
|
+
return { kind: "message", text: "Your session ended. Reload the page to keep working." };
|
|
35
62
|
}
|
|
36
|
-
return
|
|
63
|
+
return viewFromProblems((err as ApiError | null)?.problems ?? []);
|
|
37
64
|
}
|
|
38
65
|
|
|
39
66
|
/** One line for the status bar. The window lists every problem. */
|
|
40
67
|
export function commitErrorSummary(view: CommitErrorView): string {
|
|
41
68
|
if (view.kind === "message") return view.text;
|
|
42
69
|
const first = view.files[0];
|
|
43
|
-
if (!first || first.problems.length === 0) return "Can't
|
|
70
|
+
if (!first || first.problems.length === 0) return "Can't publish";
|
|
44
71
|
const extra =
|
|
45
72
|
view.files.reduce((n, file) => n + file.problems.length, 0) - 1;
|
|
46
73
|
const lead = `${first.ref} — ${first.problems[0]}`;
|
|
@@ -54,42 +81,3 @@ export function problemsForRef(
|
|
|
54
81
|
if (!view || view.kind !== "validation") return [];
|
|
55
82
|
return view.files.find((file) => file.ref === ref)?.problems ?? [];
|
|
56
83
|
}
|
|
57
|
-
|
|
58
|
-
function unknownTypeMessage(types: string[]): string {
|
|
59
|
-
if (types.length === 0) {
|
|
60
|
-
return "A file uses a model this workspace doesn't have, so it can't be committed.";
|
|
61
|
-
}
|
|
62
|
-
if (types.length === 1) {
|
|
63
|
-
return `${types[0]} isn't a model in this workspace. Pull couldn't load that model file, so this draft can't be committed.`;
|
|
64
|
-
}
|
|
65
|
-
return `${types.join(", ")} aren't models in this workspace. Pull couldn't load those model files, so these drafts can't be committed.`;
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
function validationLines(err: ApiError): string[] {
|
|
69
|
-
if (err.details && err.details.length > 0) return err.details;
|
|
70
|
-
const body = err.message.replace(/^validation:\s*/, "");
|
|
71
|
-
if (!body || body === err.message) return [];
|
|
72
|
-
return body.split(", ").filter((line) => line.length > 0);
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
function groupValidation(lines: string[]): CommitFileProblem[] {
|
|
76
|
-
const order: string[] = [];
|
|
77
|
-
const byRef = new Map<string, string[]>();
|
|
78
|
-
for (const line of lines) {
|
|
79
|
-
const split = line.indexOf(": ");
|
|
80
|
-
const ref = split === -1 ? line : line.slice(0, split);
|
|
81
|
-
const problem = sentence(split === -1 ? line : line.slice(split + 2));
|
|
82
|
-
if (!byRef.has(ref)) {
|
|
83
|
-
byRef.set(ref, []);
|
|
84
|
-
order.push(ref);
|
|
85
|
-
}
|
|
86
|
-
byRef.get(ref)!.push(problem);
|
|
87
|
-
}
|
|
88
|
-
return order.map((ref) => ({ ref, problems: byRef.get(ref)! }));
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
function sentence(problem: string): string {
|
|
92
|
-
const trimmed = problem.trim();
|
|
93
|
-
if (!trimmed) return trimmed;
|
|
94
|
-
return trimmed.charAt(0).toUpperCase() + trimmed.slice(1);
|
|
95
|
-
}
|
|
@@ -6,7 +6,7 @@ import { colors, space, type } from "../styles/tokens.stylex";
|
|
|
6
6
|
|
|
7
7
|
const hover = "@media (hover: hover)";
|
|
8
8
|
|
|
9
|
-
/** The explanation inside the
|
|
9
|
+
/** The explanation inside the publish window after a publish is refused. */
|
|
10
10
|
export function CommitFailureNotice({
|
|
11
11
|
failure,
|
|
12
12
|
renderRef,
|
|
@@ -24,7 +24,7 @@ export function CommitFailureNotice({
|
|
|
24
24
|
|
|
25
25
|
return (
|
|
26
26
|
<div role="alert" {...stylex.props(styles.box)}>
|
|
27
|
-
<p {...stylex.props(styles.title)}>Can't
|
|
27
|
+
<p {...stylex.props(styles.title)}>Can't publish yet</p>
|
|
28
28
|
<ul {...stylex.props(styles.list)}>
|
|
29
29
|
{failure.files.map((file) => (
|
|
30
30
|
<li key={file.ref} {...stylex.props(styles.item)}>
|
|
@@ -16,6 +16,10 @@ export type PathTreeFile = {
|
|
|
16
16
|
href: string;
|
|
17
17
|
active: boolean;
|
|
18
18
|
dirty: boolean;
|
|
19
|
+
type: string;
|
|
20
|
+
slug: string;
|
|
21
|
+
/** False when the model path has no `{slug}` (a fixed singleton file). */
|
|
22
|
+
slugInPath: boolean;
|
|
19
23
|
};
|
|
20
24
|
|
|
21
25
|
export type PathTreeFolder = {
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
// packages/studio/src/chrome/explorer-row-menu.ts
|
|
2
|
+
|
|
3
|
+
export type FileMenuAction = "new-file" | "rename" | "delete";
|
|
4
|
+
export type FolderMenuAction = "new-file";
|
|
5
|
+
|
|
6
|
+
/** Right-click actions for one document row. A fixed path has no slug to rename. */
|
|
7
|
+
export function fileExplorerMenu(input: {
|
|
8
|
+
canWrite: boolean;
|
|
9
|
+
slugInPath: boolean;
|
|
10
|
+
/** The file's folder maps to one model, so a sibling document can be created. */
|
|
11
|
+
canCreate: boolean;
|
|
12
|
+
}): FileMenuAction[] {
|
|
13
|
+
if (!input.canWrite) return [];
|
|
14
|
+
const actions: FileMenuAction[] = [];
|
|
15
|
+
if (input.canCreate) actions.push("new-file");
|
|
16
|
+
if (input.slugInPath) actions.push("rename");
|
|
17
|
+
actions.push("delete");
|
|
18
|
+
return actions;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/** Right-click actions for one folder. New file only when the folder is one model. */
|
|
22
|
+
export function folderExplorerMenu(input: {
|
|
23
|
+
canWrite: boolean;
|
|
24
|
+
canCreate: boolean;
|
|
25
|
+
}): FolderMenuAction[] {
|
|
26
|
+
if (!input.canWrite || !input.canCreate) return [];
|
|
27
|
+
return ["new-file"];
|
|
28
|
+
}
|
|
@@ -33,6 +33,20 @@ export function slugFromNewFileName(name: string, expectedExt: string): string {
|
|
|
33
33
|
return base;
|
|
34
34
|
}
|
|
35
35
|
|
|
36
|
+
/**
|
|
37
|
+
* Leaving the rename field. An unchanged or unusable name drops the field.
|
|
38
|
+
* A new slug is committed.
|
|
39
|
+
*/
|
|
40
|
+
export function renameFieldExit(
|
|
41
|
+
currentSlug: string,
|
|
42
|
+
rawName: string,
|
|
43
|
+
expectedExt: string,
|
|
44
|
+
): "cancel" | "commit" {
|
|
45
|
+
const slug = slugFromNewFileName(rawName, expectedExt);
|
|
46
|
+
if (!slug || slug === currentSlug || !isValidContentSlug(slug)) return "cancel";
|
|
47
|
+
return "commit";
|
|
48
|
+
}
|
|
49
|
+
|
|
36
50
|
export function folderPathKeyForModel(model: ContentTypeNav): string {
|
|
37
51
|
const repo = docRepoPath(model, { slug: "new" });
|
|
38
52
|
const tree = treePathFromRepo(repo);
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
// packages/studio/src/chrome/use-publish-check.ts
|
|
2
|
+
import { useEffect, useState } from "react";
|
|
3
|
+
import { checkWorkspaceChanges } from "../api";
|
|
4
|
+
import { checkToFailure, type CommitErrorView } from "./commit-error";
|
|
5
|
+
|
|
6
|
+
const CHECK_DEBOUNCE_MS = 800;
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Check the working tree while the publish window is open, and again when the
|
|
10
|
+
* changes move. A failed check shows nothing, because the publish still
|
|
11
|
+
* validates on the server and the check should not block anyone.
|
|
12
|
+
*/
|
|
13
|
+
export function usePublishCheck(
|
|
14
|
+
workspaceId: string,
|
|
15
|
+
open: boolean,
|
|
16
|
+
changesKey: string,
|
|
17
|
+
): { failure: CommitErrorView | null; checking: boolean } {
|
|
18
|
+
const [failure, setFailure] = useState<CommitErrorView | null>(null);
|
|
19
|
+
const [checking, setChecking] = useState(false);
|
|
20
|
+
|
|
21
|
+
useEffect(() => {
|
|
22
|
+
if (!open || !workspaceId || !changesKey) {
|
|
23
|
+
setFailure(null);
|
|
24
|
+
setChecking(false);
|
|
25
|
+
return;
|
|
26
|
+
}
|
|
27
|
+
let stale = false;
|
|
28
|
+
// The changes moved, so the last answer no longer applies.
|
|
29
|
+
setFailure(null);
|
|
30
|
+
setChecking(true);
|
|
31
|
+
// Autosave moves the key often, and each check reads from GitHub.
|
|
32
|
+
const timer = setTimeout(() => {
|
|
33
|
+
void checkWorkspaceChanges(workspaceId)
|
|
34
|
+
.then((result) => {
|
|
35
|
+
if (!stale) setFailure(checkToFailure(result.problems));
|
|
36
|
+
})
|
|
37
|
+
.catch(() => {
|
|
38
|
+
if (!stale) setFailure(null);
|
|
39
|
+
})
|
|
40
|
+
.finally(() => {
|
|
41
|
+
if (!stale) setChecking(false);
|
|
42
|
+
});
|
|
43
|
+
}, CHECK_DEBOUNCE_MS);
|
|
44
|
+
return () => {
|
|
45
|
+
stale = true;
|
|
46
|
+
clearTimeout(timer);
|
|
47
|
+
};
|
|
48
|
+
}, [workspaceId, open, changesKey]);
|
|
49
|
+
|
|
50
|
+
return { failure, checking };
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* What the Publish button says. A running check doesn't block, because the
|
|
55
|
+
* server validates the publish anyway and a slow check shouldn't stop anyone.
|
|
56
|
+
*/
|
|
57
|
+
export function publishButtonState(input: {
|
|
58
|
+
committing: boolean;
|
|
59
|
+
checking: boolean;
|
|
60
|
+
failure: CommitErrorView | null;
|
|
61
|
+
}): { label: string; disabled: boolean } {
|
|
62
|
+
if (input.committing) return { label: "Publishing…", disabled: true };
|
|
63
|
+
if (input.failure) return { label: "Publish", disabled: true };
|
|
64
|
+
if (input.checking) return { label: "Checking…", disabled: false };
|
|
65
|
+
return { label: "Publish", disabled: false };
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/** Changes to `changes` that a new check should look at. */
|
|
69
|
+
export function publishCheckKey(
|
|
70
|
+
changes: Array<{ type: string; slug: string; status: string; updatedAt?: string }>,
|
|
71
|
+
): string {
|
|
72
|
+
return changes
|
|
73
|
+
.map((change) => `${change.type}/${change.slug}:${change.status}:${change.updatedAt ?? ""}`)
|
|
74
|
+
.join("|");
|
|
75
|
+
}
|