Skip to content

Improve type-safety when using createFileMap #5428

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion beta/src/components/MDX/Sandpack/SandpackRoot.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ function SandpackRoot(props: SandpackProps) {

files['/styles.css'] = {
code: [sandboxStyle, files['/styles.css']?.code ?? ''].join('\n\n'),
hidden: !files['/styles.css']?.visible,
hidden: files['/styles.css']?.hidden,
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

files used to be any since createFileMap returned any. Now that we type the input of createFileMap, TypeScript is able to infer a more accurate return type of createFileMap.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Couldn't find neither css styles.css hidden nor css styles.css visible so maybe this should be

Suggested change
hidden: files['/styles.css']?.hidden,
hidden: true,

instead which would match the old behavior?

};

return (
Expand Down
6 changes: 3 additions & 3 deletions beta/src/components/MDX/Sandpack/createFileMap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

import type {SandpackFile} from '@codesandbox/sandpack-react';

export const createFileMap = (codeSnippets: any) => {
return codeSnippets.reduce(
(result: Record<string, SandpackFile>, codeSnippet: React.ReactElement) => {
export const createFileMap = (codeSnippets: React.ReactElement[]) => {
return codeSnippets.reduce<Record<string, SandpackFile>>(
(result, codeSnippet) => {
if ((codeSnippet.type as any).mdxName !== 'pre') {
return result;
}
Expand Down
12 changes: 6 additions & 6 deletions beta/src/components/MDX/Sandpack/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,20 +46,20 @@ const SandpackGlimmer = ({code}: {code: string}) => (
);

export default memo(function SandpackWrapper(props: any): any {
const codeSnippet = createFileMap(Children.toArray(props.children));
const codeSnippets = Children.toArray(props.children) as React.ReactElement[];
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using same pattern as beta/src/components/MDX/Sandpack/SandpackRoot.tsx. Should we pass files as a prop instead of recomputing in beta/src/components/MDX/Sandpack/SandpackRoot.tsx?

const files = createFileMap(codeSnippets);

// To set the active file in the fallback we have to find the active file first.
// If there are no active files we fallback to App.js as default.
let activeCodeSnippet = Object.keys(codeSnippet).filter(
let activeCodeSnippet = Object.keys(files).filter(
(fileName) =>
codeSnippet[fileName]?.active === true &&
codeSnippet[fileName]?.hidden === false
files[fileName]?.active === true && files[fileName]?.hidden === false
);
let activeCode;
if (!activeCodeSnippet.length) {
activeCode = codeSnippet['/App.js'].code;
activeCode = files['/App.js'].code;
} else {
activeCode = codeSnippet[activeCodeSnippet[0]].code;
activeCode = files[activeCodeSnippet[0]].code;
}

return (
Expand Down