Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 2 additions & 5 deletions src/pages/edit/Editor/components/TitleBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Close, Edit, KeyboardDoubleArrowRight } from "@mui/icons-material";
import { Box, IconButton, Paper } from "@mui/material";
import nullthrows from "nullthrows";
import { useMessages } from "../../../../i18n";
import { useStore } from "../../../../store/react";
import { useComponent } from "../../../../store/react/selectors";
import { useComponentEditorStore } from "../store";

export type CCComponentEditorTitleBarProps = {
Expand All @@ -16,11 +16,8 @@ export default function CCComponentEditorTitleBar({
}: CCComponentEditorTitleBarProps) {
const componentEditorStore = useComponentEditorStore();
const componentEditorState = componentEditorStore();
const { store } = useStore();
const messages = useMessages();
const component = nullthrows(
store.components.get(componentEditorState.componentId),
);
const component = nullthrows(useComponent(componentEditorState.componentId));

return (
<Paper
Expand Down
306 changes: 167 additions & 139 deletions src/pages/tutorial/stage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,13 @@ import {
Typography,
} from "@mui/material";
import nullthrows from "nullthrows";
import { type ReactNode, useEffect, useRef, useState } from "react";
import {
type ReactNode,
useCallback,
useEffect,
useRef,
useState,
} from "react";
import {
Navigate,
Link as RouterLink,
Expand All @@ -25,7 +31,8 @@ import {
import { TruthTableView } from "../../components/TruthTableView";
import { useMessages } from "../../i18n";
import { definitionByComponentId } from "../../store/intrinsics/definitions";
import { StoreScope } from "../../store/react";
import { StoreScope, useStore } from "../../store/react";
import { useComponents } from "../../store/react/selectors";
import { theme } from "../../style";
import {
type CheckResult,
Expand All @@ -36,6 +43,7 @@ import { describeFailure } from "../../tutorial/failure";
import { markStageCleared } from "../../tutorial/progress";
import {
getTutorialStageStore,
nameTutorialStage,
resetTutorialStage,
} from "../../tutorial/stageStore";
import {
Expand Down Expand Up @@ -67,14 +75,31 @@ function SectionHeading({ children }: { children: ReactNode }) {
);
}

function CheckFailure({
result,
}: {
result: Exclude<CheckResult, { type: "passed" }>;
}) {
const { store } = useStore();
const messages = useMessages();
// The message may name a part, which is renamed when the language changes
useComponents();
return (
<Alert severity="error">{describeFailure(result, store, messages)}</Alert>
);
}

function StageView({ stage }: { stage: TutorialStage }) {
const messages = useMessages();
const texts = messages.tutorial;
const navigate = useNavigate();
const stageIndex = tutorialStages.indexOf(stage);
const nextStage = tutorialStages[stageIndex + 1];
const titleOf: StageTitleOf = (id) =>
isTutorialStageId(id) ? texts.stageTexts[id].title : texts.partTitles[id];
const titleOf = useCallback<StageTitleOf>(
(id) =>
isTutorialStageId(id) ? texts.stageTexts[id].title : texts.partTitles[id],
[texts],
);
const goalTable = goalTableOf(stage.check);
const stageTexts = texts.stageTexts[stage.id];

Expand All @@ -89,6 +114,10 @@ function StageView({ stage }: { stage: TutorialStage }) {
// The editor keeps state of the store it was made with, so it starts anew on a reset
const [resetCount, setResetCount] = useState(0);

useEffect(() => {
nameTutorialStage(store, stage, titleOf);
}, [store, stage, titleOf]);

// A result about a circuit that has since changed would mislead
useEffect(() => {
const clearResult = () => setResult(null);
Expand Down Expand Up @@ -154,135 +183,134 @@ function StageView({ stage }: { stage: TutorialStage }) {

return (
<BitNotationContext value={stage.tableNotation}>
<div
style={{
display: "grid",
// Wider on a wide screen, for long texts and tables of many columns
gridTemplateColumns:
"clamp(300px, 25vw, 420px) max-content 200px max-content 1fr",
minHeight: 0,
}}
>
<Box sx={{ display: "flex", flexDirection: "column", minHeight: 0 }}>
<Stack spacing={2} sx={{ p: 2, overflowY: "auto", flexGrow: 1 }}>
<Link
component={RouterLink}
to="/tutorial"
underline="hover"
sx={{ display: "flex", alignItems: "center", gap: 0.5 }}
>
<ArrowBack fontSize="small" />
{texts.stages}
</Link>
<div>
<Typography color="textSecondary" variant="body2">
{texts.stageNumber(stageIndex + 1)}
</Typography>
<Typography
variant="h2"
typography="h5"
sx={{ fontWeight: "bold" }}
<StoreScope store={store}>
<div
style={{
display: "grid",
// Wider on a wide screen, for long texts and tables of many columns
gridTemplateColumns:
"clamp(300px, 25vw, 420px) max-content 200px max-content 1fr",
minHeight: 0,
}}
>
<Box sx={{ display: "flex", flexDirection: "column", minHeight: 0 }}>
<Stack spacing={2} sx={{ p: 2, overflowY: "auto", flexGrow: 1 }}>
<Link
component={RouterLink}
to="/tutorial"
underline="hover"
sx={{ display: "flex", alignItems: "center", gap: 0.5 }}
>
{stageTexts.title}
</Typography>
</div>
<div>
<SectionHeading>{texts.goalHeading}</SectionHeading>
<Typography>{stageTexts.goal}</Typography>
{goalTable && (
<Box sx={{ mt: 1 }}>
<Box sx={{ overflowX: "auto" }}>
<TruthTableView table={goalTable} />
<ArrowBack fontSize="small" />
{texts.stages}
</Link>
<div>
<Typography color="textSecondary" variant="body2">
{texts.stageNumber(stageIndex + 1)}
</Typography>
<Typography
variant="h2"
typography="h5"
sx={{ fontWeight: "bold" }}
>
{stageTexts.title}
</Typography>
</div>
<div>
<SectionHeading>{texts.goalHeading}</SectionHeading>
<Typography>{stageTexts.goal}</Typography>
{goalTable && (
<Box sx={{ mt: 1 }}>
<Box sx={{ overflowX: "auto" }}>
<TruthTableView table={goalTable} />
</Box>
{stage.tableNotation === "digits" && (
<Typography variant="body2" sx={{ mt: 0.5 }}>
{texts.digitsLegend}
</Typography>
)}
</Box>
{stage.tableNotation === "digits" && (
<Typography variant="body2" sx={{ mt: 0.5 }}>
{texts.digitsLegend}
</Typography>
)}
</Box>
)}
</div>
{stageTexts.steps.length > 0 && (
<div>
<SectionHeading>{texts.stepsHeading}</SectionHeading>
<Box component="ol" sx={{ m: 0, pl: 3 }}>
{stageTexts.steps.map((step) => (
<Typography key={step} component="li" sx={{ mb: 0.5 }}>
{step}
</Typography>
))}
</Box>
</div>
)}
</div>
{stageTexts.steps.length > 0 && (
<div>
<SectionHeading>{texts.stepsHeading}</SectionHeading>
<Box component="ol" sx={{ m: 0, pl: 3 }}>
{stageTexts.steps.map((step) => (
<Typography key={step} component="li" sx={{ mb: 0.5 }}>
{step}
</Typography>
<SectionHeading>{messages.parts.ofStage}</SectionHeading>
<Stack spacing={1.5}>
{stage.parts.map((partId) => (
<PartDescription
key={partId}
component={
nullthrows(definitionByComponentId.get(partId))
.component
}
/>
))}
</Box>
{stage.givenParts.map(givenPartById).map((given) => (
<PartDescriptionView
key={given.id}
name={titleOf(given.id)}
summary={null}
table={goalTableOf(given.check)}
/>
))}
</Stack>
</div>
)}
<div>
<SectionHeading>{messages.parts.ofStage}</SectionHeading>
<Stack spacing={1.5}>
{stage.parts.map((partId) => (
<PartDescription
key={partId}
component={
nullthrows(definitionByComponentId.get(partId)).component
}
/>
))}
{stage.givenParts.map(givenPartById).map((given) => (
<PartDescriptionView
key={given.id}
name={titleOf(given.id)}
summary={null}
table={goalTableOf(given.check)}
/>
))}
</Stack>
</div>
<Box sx={{ flexGrow: 1 }} />
{hasResetFailed && (
<Alert severity="error">{texts.resetStageFailed}</Alert>
)}
<Button
variant="text"
color="error"
onClick={() => setIsConfirmingReset(true)}
>
{texts.resetStage}
</Button>
</Stack>
<Divider />
{/* Outside the scrolling, so that Check and its result stay in view */}
<Stack spacing={1} sx={{ p: 2 }}>
<Button
variant="contained"
onClick={check}
disabled={isChecking}
sx={{ background: theme.colors.tutorial }}
>
{isChecking ? texts.checking : texts.check}
</Button>
{result?.type === "passed" && (
<>
<Alert severity="success">
{stage.check.kind === "sequential"
? texts.passedSteps(stage.check.steps.length)
: texts.passed}
</Alert>
<Button
variant="outlined"
component={RouterLink}
to={nextStage ? `/tutorial/${nextStage.id}` : "/tutorial"}
>
{nextStage ? texts.next : texts.backToStages}
</Button>
</>
)}
{result && result.type !== "passed" && (
<Alert severity="error">
{describeFailure(result, store, messages)}
</Alert>
)}
</Stack>
</Box>
<Divider orientation="vertical" />
<StoreScope store={store}>
<Box sx={{ flexGrow: 1 }} />
{hasResetFailed && (
<Alert severity="error">{texts.resetStageFailed}</Alert>
)}
<Button
variant="text"
color="error"
onClick={() => setIsConfirmingReset(true)}
>
{texts.resetStage}
</Button>
</Stack>
<Divider />
{/* Outside the scrolling, so that Check and its result stay in view */}
<Stack spacing={1} sx={{ p: 2 }}>
<Button
variant="contained"
onClick={check}
disabled={isChecking}
sx={{ background: theme.colors.tutorial }}
>
{isChecking ? texts.checking : texts.check}
</Button>
{result?.type === "passed" && (
<>
<Alert severity="success">
{stage.check.kind === "sequential"
? texts.passedSteps(stage.check.steps.length)
: texts.passed}
</Alert>
<Button
variant="outlined"
component={RouterLink}
to={nextStage ? `/tutorial/${nextStage.id}` : "/tutorial"}
>
{nextStage ? texts.next : texts.backToStages}
</Button>
</>
)}
{result && result.type !== "passed" && (
<CheckFailure result={result} />
)}
</Stack>
</Box>
<Divider orientation="vertical" />
<Box sx={{ overflowY: "auto" }}>
<Typography
sx={{ px: 2, pt: 1 }}
Expand Down Expand Up @@ -318,18 +346,18 @@ function StageView({ stage }: { stage: TutorialStage }) {
else setEditedComponentId(stage.componentId);
}}
/>
</StoreScope>
{isConfirmingReset && (
<ConfirmDialog
title={texts.resetStageDialog.title}
confirmLabel={texts.resetStageDialog.confirm}
onConfirm={resetStage}
onCancel={() => setIsConfirmingReset(false)}
>
{texts.resetStageDialog.body}
</ConfirmDialog>
)}
</div>
{isConfirmingReset && (
<ConfirmDialog
title={texts.resetStageDialog.title}
confirmLabel={texts.resetStageDialog.confirm}
onConfirm={resetStage}
onCancel={() => setIsConfirmingReset(false)}
>
{texts.resetStageDialog.body}
</ConfirmDialog>
)}
</div>
</StoreScope>
</BitNotationContext>
);
}
Expand Down
Loading
Loading