refactor(client): simplify sort challenge files (#59179)

Co-authored-by: Naomi <accounts+github@nhcarrigan.com>
This commit is contained in:
Oliver Eyton-Williams
2025-04-02 20:50:43 +02:00
committed by GitHub
parent 6e1b87cc78
commit a82316e469
10 changed files with 64 additions and 94 deletions
@@ -1,5 +1,4 @@
const path = require('path');
const { sortChallengeFiles } = require('../sort-challengefiles');
const { viewTypes } = require('../../../shared/config/challenge-types');
const backend = path.resolve(
@@ -138,15 +137,16 @@ function getProjectPreviewConfig(challenge, allChallengeEdges) {
.filter(({ node: { challenge } }) => challenge.block === block)
.map(({ node: { challenge } }) => challenge);
const lastChallenge = challengesInBlock[challengesInBlock.length - 1];
const solutionToLastChallenge = sortChallengeFiles(
lastChallenge.solutions[0] ?? []
);
const lastChallengeFiles = sortChallengeFiles(
lastChallenge.challengeFiles ?? []
);
const projectPreviewChallengeFiles = lastChallengeFiles.map((file, id) => ({
const solutionFiles = lastChallenge.solutions[0] ?? [];
const lastChallengeFiles = lastChallenge.challengeFiles ?? [];
const findFileByKey = (key, files) =>
files.find(file => file.fileKey === key);
const projectPreviewChallengeFiles = lastChallengeFiles.map(file => ({
...file,
contents: solutionToLastChallenge[id]?.contents ?? file.contents
contents:
findFileByKey(file.fileKey, solutionFiles)?.contents ?? file.contents
}));
return {
-17
View File
@@ -1,17 +0,0 @@
exports.sortChallengeFiles = function sortChallengeFiles(challengeFiles) {
const xs = challengeFiles.slice();
xs.sort((a, b) => {
if (a.history[0] === 'index.jsx') return -1;
if (b.history[0] === 'index.jsx') return 1;
if (a.history[0] === 'index.html') return -1;
if (b.history[0] === 'index.html') return 1;
if (a.history[0] === 'styles.css') return -1;
if (b.history[0] === 'styles.css') return 1;
if (a.history[0] === 'script.js') return -1;
if (b.history[0] === 'script.js') return 1;
if (a.history[0] === 'index.ts') return -1;
if (b.history[0] === 'index.ts') return 1;
return 0;
});
return xs;
};
@@ -1,5 +1,5 @@
const { challengeFiles } = require('./__fixtures__/challenges');
const { sortChallengeFiles } = require('./sort-challengefiles');
import { challengeFiles } from './__fixtures__/challenges';
import { sortChallengeFiles } from './sort-challengefiles';
describe('sort-files', () => {
describe('sortChallengeFiles', () => {
+17
View File
@@ -0,0 +1,17 @@
export function sortChallengeFiles<File extends { fileKey: string }>(
challengeFiles: File[]
): File[] {
return challengeFiles.toSorted((a, b) => {
if (a.fileKey === 'indexjsx') return -1;
if (b.fileKey === 'indexjsx') return 1;
if (a.fileKey === 'indexhtml') return -1;
if (b.fileKey === 'indexhtml') return 1;
if (a.fileKey === 'stylescss') return -1;
if (b.fileKey === 'stylescss') return 1;
if (a.fileKey === 'scriptjs') return -1;
if (b.fileKey === 'scriptjs') return 1;
if (a.fileKey === 'indexts') return -1;
if (b.fileKey === 'indexts') return 1;
return 0;
});
}