Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 | 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 7x 2x 22x 7x 4x 4x 4x 22x 22x 22x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 1x 1x 1x 1x 1x 1x 1x 1x 2x 22x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 22x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 22x 1x 1x 1x 1x 1x 1x 1x | import * as React from "react";
import { Button } from "@/components/ui/button";
import { Input } from "@/components/ui/input";
import {
ExternalExamplesError,
normalizeSelection,
refreshExternalExamplesCatalog,
setExternalExamplesOverride,
useExternalExamples,
validateExternalExamplesSelection,
} from "@/lib/external-examples";
import { getStatusTextClass } from "@/lib/status-semantics";
import { getServerCapabilities, type ServerCapabilities } from "@/lib/server-capabilities";
export function ExternalExamplesSettings({
open = true,
capabilities = getServerCapabilities(true),
}: {
readonly open?: boolean;
readonly capabilities?: ServerCapabilities;
}) {
const { override, catalog } = useExternalExamples();
const [repository, setRepository] = React.useState("");
const [ref, setRef] = React.useState("");
const [message, setMessage] = React.useState<{
kind: "error" | "success";
text: string;
} | null>(null);
const [validatedRevision, setValidatedRevision] = React.useState<
string | null
>(null);
const [busy, setBusy] = React.useState<"validate" | "apply" | "reset" | null>(
null,
);
const operation = React.useRef(0);
const controller = React.useRef<AbortController | undefined>();
const capabilitiesRef = React.useRef(capabilities);
capabilitiesRef.current = capabilities;
React.useEffect(() => {
if (open && !catalog && !override && capabilitiesRef.current.canUseServerExamples)
void refreshExternalExamplesCatalog().catch(() => undefined);
}, [catalog, open, override]);
React.useEffect(() => {
if (!catalog && !override) return;
if (override) {
setRepository(override.repository);
setRef(override.ref);
} else Eif (catalog?.source.selection === "default") {
setRepository(catalog.source.repository ?? "");
setRef(catalog.source.ref ?? "");
}
}, [catalog, override]);
React.useEffect(() => () => controller.current?.abort(), []);
const source = catalog?.source;
const validateDraft = async (): Promise<ReturnType<
typeof normalizeSelection
> | null> => {
Iif (!capabilitiesRef.current.canUseServerExamples) return null;
const currentOperation = ++operation.current;
try {
const selection = normalizeSelection(repository, ref);
controller.current?.abort();
const nextController = new AbortController();
controller.current = nextController;
setBusy("validate");
setMessage(null);
const validated = await validateExternalExamplesSelection(
selection,
nextController.signal,
);
Eif (currentOperation === operation.current) {
setValidatedRevision(validated.revision);
setMessage({
kind: "success",
text: "Repository and ref are valid and the snapshot is ready.",
});
}
return currentOperation === operation.current ? selection : null;
} catch (error) {
Iif (error instanceof DOMException && error.name === "AbortError")
return null;
Eif (currentOperation === operation.current) {
setMessage({
kind: "error",
text:
error instanceof ExternalExamplesError
? error.message
: "Could not validate the selection.",
});
}
return null;
} finally {
Eif (currentOperation === operation.current) setBusy(null);
}
};
const apply = async () => {
Iif (!capabilitiesRef.current.canUseServerExamples) return;
const selection = await validateDraft();
Iif (!selection || !capabilitiesRef.current.canUseServerExamples) return;
const currentOperation = ++operation.current;
setBusy("apply");
try {
setExternalExamplesOverride(selection);
Iif (!capabilitiesRef.current.canUseServerExamples) return;
await refreshExternalExamplesCatalog();
Eif (currentOperation === operation.current)
setMessage({
kind: "success",
text: "External Examples override applied.",
});
} catch (error) {
if (!(error instanceof DOMException && error.name === "AbortError")) {
setMessage({
kind: "error",
text:
error instanceof ExternalExamplesError
? error.message
: "Could not reload examples.",
});
}
} finally {
setBusy(null);
}
};
const reset = async () => {
Iif (!capabilitiesRef.current.canUseServerExamples) return;
++operation.current;
controller.current?.abort();
setBusy("reset");
setMessage(null);
setExternalExamplesOverride(null);
try {
await refreshExternalExamplesCatalog();
setMessage({ kind: "success", text: "Using the server default again." });
} catch (error) {
if (!(error instanceof DOMException && error.name === "AbortError")) {
setMessage({
kind: "error",
text:
error instanceof ExternalExamplesError
? error.message
: "Could not reload the default examples.",
});
}
} finally {
setBusy(null);
}
};
return (
<section
className="rounded-md border p-3"
aria-label="External Examples settings"
>
<div className="text-ui-sm font-semibold text-foreground">External Examples</div>
<div className="mt-0.5 mb-3 text-ui-xs leading-snug text-muted-foreground">
Select one public Course Content repository and ref for this browser
only. The selection controls both Examples and Tutor content; no
credentials are needed.
</div>
{!capabilities.canUseServerExamples && (
<output className="mb-3 text-ui-xs text-muted-foreground" aria-live="polite">
Server connection required
</output>
)}
<div className="grid gap-3 sm:grid-cols-[minmax(0,1fr)_minmax(0,1fr)] sm:gap-x-4 sm:gap-y-2">
<label className="text-ui-sm" htmlFor="external-examples-repository">
Repository
</label>
<Input
id="external-examples-repository"
value={repository}
onChange={(event) => {
setRepository(event.target.value);
setValidatedRevision(null);
}}
placeholder="owner/repository or GitHub URL"
disabled={busy !== null || !capabilities.canUseServerExamples}
/>
<label className="text-ui-sm" htmlFor="external-examples-ref">
Ref
</label>
<Input
id="external-examples-ref"
value={ref}
onChange={(event) => {
setRef(event.target.value);
setValidatedRevision(null);
}}
placeholder="main"
disabled={busy !== null || !capabilities.canUseServerExamples}
/>
</div>
<div className="mt-3 space-y-1 text-ui-xs text-muted-foreground">
{(source?.revision ?? validatedRevision) && (
<div>
Active revision:{" "}
<code className="break-all">
{source?.revision ?? validatedRevision}
</code>
</div>
)}
{source?.stale && (
<div className="text-status-warning">
The last known good snapshot is currently active (stale).
</div>
)}
{source?.tutor && (
<div>
Tutor content: {getTutorContentLabel(source.tutor.status)}
</div>
)}
</div>
{message && (
<output
aria-live="polite"
className={`mt-3 text-ui-sm ${getStatusTextClass(message.kind)}`}
>
{message.text}
</output>
)}
<div className="mt-3 flex flex-wrap justify-end gap-2">
<Button
type="button"
variant="outline"
size="sm"
onClick={() => void validateDraft()}
disabled={busy !== null || !capabilities.canUseServerExamples}
>
Validate
</Button>
<Button
type="button"
size="sm"
onClick={() => void apply()}
disabled={busy !== null || !repository || !ref || !capabilities.canUseServerExamples}
>
Apply
</Button>
<Button
type="button"
variant="outline"
size="sm"
onClick={() => void reset()}
disabled={busy !== null || !override || !capabilities.canUseServerExamples}
>
Reset to default
</Button>
</div>
</section>
);
}
function getTutorContentLabel(status: "valid" | "invalid" | "absent"): string {
if (status === "valid") return "available";
if (status === "invalid") return "built-in fallback active";
return "not configured";
}
|