Skip to content

Commit

Permalink
Only rely on blockId (not blockData) to display the readable componen…
Browse files Browse the repository at this point in the history
…t. This way we don't have to wait for the toolbox info to display cached results.
  • Loading branch information
thsparks committed Sep 24, 2024
1 parent dbe3493 commit 39a8500
Showing 1 changed file with 13 additions and 9 deletions.
22 changes: 13 additions & 9 deletions teachertool/src/components/CriteriaInstanceDisplay.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,10 @@ const InlineInputSegment: React.FC<InlineInputSegmentProps> = ({
};

interface ReadableBlockNameProps {
blockData: BlockData;
blockId: string;
}
const ReadableBlockName: React.FC<ReadableBlockNameProps> = ({ blockData }) => {
const ReadableBlockName: React.FC<ReadableBlockNameProps> = ({ blockId }) => {
const { state: teacherTool } = useContext(AppStateContext);
const [blockTextParts, setBlockTextParts] = useState<pxt.editor.BlockTextParts | undefined>(undefined);

useEffect(() => {
Expand All @@ -96,16 +97,19 @@ const ReadableBlockName: React.FC<ReadableBlockNameProps> = ({ blockData }) => {

if (blockReadableName) {
setBlockTextParts(blockReadableName);
} else if (!teacherTool.toolboxCategories) {
// If teacherTool.toolboxCategories has not loaded yet, we may get the readable component later once it loads.
// Show a spinner (handled below).
setBlockTextParts(undefined);
} else {
// We were unable to get block name from the editor. Fallback to snippet name and/or name.
setBlockTextParts({
parts: [{ kind: "label", content: blockData.block.snippetName || blockData.block.name }],
} as pxt.editor.BlockTextParts);
// TeacherTool.toolboxCategories has loaded and we still don't have a readable component.
// We won't be able to get it, so fallback to the id.
setBlockTextParts({ parts: [ { kind: "label", content: blockId } ] });
}
}

updateReadableName(blockData.block.blockId);
}, [blockData]);
updateReadableName(blockId);
}, [blockId, teacherTool.toolboxCategories]);

const readableComponent = blockTextParts?.parts.map((part, i) => {
let content = "";
Expand Down Expand Up @@ -171,7 +175,7 @@ const BlockInputSegment: React.FC<BlockInputSegmentProps> = ({ instance, param }
}, [param.value, teacherTool.toolboxCategories]);

const style = blockData ? { backgroundColor: blockData.category.color, color: "white" } : undefined;
const blockDisplay = blockData ? <ReadableBlockName blockData={blockData} /> : param.value || param.name;
const blockDisplay = param.value ? <ReadableBlockName blockId={param.value} /> : param.name;
return (
<Button
label={blockDisplay}
Expand Down

0 comments on commit 39a8500

Please sign in to comment.