Skip to content

Commit 5930afb

Browse files
refactor: slightly simplify rendering of ToolPanel (freeCodeCamp#55601)
1 parent fe7a43d commit 5930afb

File tree

3 files changed

+19
-32
lines changed

3 files changed

+19
-32
lines changed

client/src/templates/Challenges/classic/mobile-layout.tsx

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import i18next from 'i18next';
2-
import React, { Component } from 'react';
2+
import React, { Component, type ReactNode } from 'react';
33
import { faWindowRestore } from '@fortawesome/free-solid-svg-icons';
44
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
55
import { createSelector } from 'reselect';
@@ -17,14 +17,12 @@ import {
1717
showPreviewPaneSelector
1818
} from '../redux/selectors';
1919
import { TOOL_PANEL_HEIGHT } from '../../../../config/misc';
20-
import ToolPanel from '../components/tool-panel';
2120
import PreviewPortal from '../components/preview-portal';
2221
import Notes from '../components/notes';
2322
import EditorTabs from './editor-tabs';
2423

2524
interface MobileLayoutProps {
2625
editor: JSX.Element | null;
27-
guideUrl: string;
2826
hasEditableBoundaries: boolean;
2927
hasPreview: boolean;
3028
instructions: JSX.Element;
@@ -34,13 +32,13 @@ interface MobileLayoutProps {
3432
windowTitle: string;
3533
showPreviewPortal: boolean;
3634
showPreviewPane: boolean;
35+
toolPanel: ReactNode;
3736
removePortalWindow: () => void;
3837
setShowPreviewPortal: (arg: boolean) => void;
3938
setShowPreviewPane: (arg: boolean) => void;
4039
portalWindow: null | Window;
4140
updateUsingKeyboardInTablist: (arg0: boolean) => void;
4241
testOutput: JSX.Element;
43-
videoUrl: string;
4442
usesMultifileEditor: boolean;
4543
}
4644

@@ -160,13 +158,12 @@ class MobileLayout extends Component<MobileLayoutProps, MobileLayoutState> {
160158
onPreviewResize,
161159
showPreviewPane,
162160
showPreviewPortal,
161+
toolPanel,
163162
removePortalWindow,
164163
setShowPreviewPane,
165164
setShowPreviewPortal,
166165
portalWindow,
167166
windowTitle,
168-
guideUrl,
169-
videoUrl,
170167
usesMultifileEditor
171168
} = this.props;
172169

@@ -309,13 +306,7 @@ class MobileLayout extends Component<MobileLayoutProps, MobileLayoutState> {
309306
)}
310307
</TabsContent>
311308
)}
312-
{!hasEditableBoundaries && (
313-
<ToolPanel
314-
guideUrl={guideUrl}
315-
isMobile={true}
316-
videoUrl={videoUrl}
317-
/>
318-
)}
309+
{!hasEditableBoundaries && toolPanel}
319310
{hasPreview && this.state.currentTab !== 'preview' && (
320311
<div className='portal-button-wrap'>
321312
<button

client/src/templates/Challenges/classic/show.tsx

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ import { savedChallengesSelector } from '../../../redux/selectors';
6060
import { getGuideUrl } from '../utils';
6161
import { preloadPage } from '../../../../utils/gatsby/page-loading';
6262
import envData from '../../../../config/env.json';
63+
import ToolPanel from '../components/tool-panel';
6364
import { XtermTerminal } from './xterm';
6465
import MultifileEditor from './multifile-editor';
6566
import DesktopLayout from './desktop-layout';
@@ -227,6 +228,7 @@ function ShowClassic({
227228
const isMobile = useMediaQuery({
228229
query: `(max-width: ${MAX_MOBILE_WIDTH}px)`
229230
});
231+
const guideUrl = getGuideUrl({ forumTopicId, title });
230232

231233
const blockNameTitle = `${t(
232234
`intro:${superBlock}.blocks.${block}.title`
@@ -369,9 +371,9 @@ function ShowClassic({
369371
};
370372

371373
const renderInstructionsPanel = ({
372-
showToolPanel
374+
toolPanel
373375
}: {
374-
showToolPanel: boolean;
376+
toolPanel: React.ReactNode;
375377
}) => {
376378
return (
377379
<SidePanel
@@ -392,11 +394,9 @@ function ShowClassic({
392394
{title}
393395
</ChallengeTitle>
394396
}
395-
guideUrl={getGuideUrl({ forumTopicId, title })}
396397
instructionsPanelRef={instructionsPanelRef}
397-
showToolPanel={showToolPanel}
398+
toolPanel={toolPanel}
398399
superBlock={superBlock}
399-
videoUrl={videoUrl}
400400
/>
401401
);
402402
};
@@ -445,11 +445,10 @@ function ShowClassic({
445445
isMobileLayout: true,
446446
isUsingKeyboardInTablist: usingKeyboardInTablist
447447
})}
448-
guideUrl={getGuideUrl({ forumTopicId, title })}
449448
hasEditableBoundaries={hasEditableBoundaries}
450449
hasPreview={showPreview}
451450
instructions={renderInstructionsPanel({
452-
showToolPanel: false
451+
toolPanel: null
453452
})}
454453
notes={notes}
455454
onPreviewResize={onPreviewResize}
@@ -465,9 +464,11 @@ function ShowClassic({
465464
testOutput={
466465
<Output defaultOutput={defaultOutput} output={output} />
467466
}
467+
toolPanel={
468+
<ToolPanel guideUrl={guideUrl} isMobile videoUrl={videoUrl} />
469+
}
468470
updateUsingKeyboardInTablist={updateUsingKeyboardInTablist}
469471
usesMultifileEditor={usesMultifileEditor}
470-
videoUrl={videoUrl}
471472
/>
472473
)}
473474
{!isMobile && (
@@ -481,7 +482,7 @@ function ShowClassic({
481482
hasEditableBoundaries={hasEditableBoundaries}
482483
hasPreview={showPreview}
483484
instructions={renderInstructionsPanel({
484-
showToolPanel: true
485+
toolPanel: <ToolPanel guideUrl={guideUrl} videoUrl={videoUrl} />
485486
})}
486487
isFirstStep={isFirstStep}
487488
layoutState={layout}

client/src/templates/Challenges/components/side-panel.tsx

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { useEffect, ReactElement } from 'react';
1+
import React, { useEffect, ReactElement, ReactNode } from 'react';
22
import { connect } from 'react-redux';
33
import { createSelector } from 'reselect';
44
import { Test } from '../../../redux/prop-types';
@@ -7,7 +7,6 @@ import { SuperBlocks } from '../../../../../shared/config/curriculum';
77
import { initializeMathJax } from '../../../utils/math-jax';
88
import { challengeTestsSelector } from '../redux/selectors';
99
import TestSuite from './test-suite';
10-
import ToolPanel from './tool-panel';
1110

1211
import './side-panel.css';
1312

@@ -21,24 +20,20 @@ interface SidePanelProps {
2120
block: string;
2221
challengeDescription: ReactElement;
2322
challengeTitle: ReactElement;
24-
guideUrl: string;
2523
instructionsPanelRef: React.RefObject<HTMLDivElement>;
26-
showToolPanel: boolean;
24+
toolPanel: ReactNode;
2725
superBlock: SuperBlocks;
2826
tests: Test[];
29-
videoUrl: string;
3027
}
3128

3229
export function SidePanel({
3330
block,
3431
challengeDescription,
3532
challengeTitle,
36-
guideUrl,
3733
instructionsPanelRef,
38-
showToolPanel = false,
34+
toolPanel,
3935
superBlock,
40-
tests,
41-
videoUrl
36+
tests
4237
}: SidePanelProps): JSX.Element {
4338
useEffect(() => {
4439
const mathJaxChallenge =
@@ -56,7 +51,7 @@ export function SidePanel({
5651
>
5752
{challengeTitle}
5853
{challengeDescription}
59-
{showToolPanel && <ToolPanel guideUrl={guideUrl} videoUrl={videoUrl} />}
54+
{toolPanel}
6055
<TestSuite tests={tests} />
6156
</div>
6257
);

0 commit comments

Comments
 (0)