Sort the files after fetching

This commit is contained in:
Valtteri Karesto
2022-06-02 14:04:52 +03:00
parent b09d029931
commit 4a22861860

View File

@@ -58,6 +58,22 @@ export const fetchFiles = (gistId: string) => {
language: res.data.files?.[filename]?.language?.toLowerCase() || "",
content: res.data.files?.[filename]?.content || "",
}));
// Sort files so that the source files are first
// In case of other files leave the order as it its
files.sort((a, b) => {
const aExtension = a.name.split('.')?.[1];
const bExtension = b.name.split('.')?.[1];
if (!aExtension || !bExtension) {
return 0
};
if (aExtension === 'c' && bExtension === 'c') {
return 0
}
if (aExtension === 'c' && bExtension !== 'c') {
return -1
}
return 0
})
state.loading = false;
if (files.length > 0) {
state.logs.push({