Skip to content

Commit ac64de6

Browse files
authored
refactor: change the way to query pages data (#18)
1 parent 8a0d14b commit ac64de6

File tree

2 files changed

+18
-37
lines changed

2 files changed

+18
-37
lines changed

gatsby-theme-woly/gatsby-node.js

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ async function createUsagePages({ actions, graphql, reporter }) {
3333
{
3434
usages: allMdx {
3535
nodes {
36-
id
3736
fileAbsolutePath
37+
body
3838
}
3939
}
4040
}
@@ -48,46 +48,46 @@ async function createUsagePages({ actions, graphql, reporter }) {
4848
const component = require.resolve('./src/templates/usage.js');
4949

5050
const groupedMdx = result.data.usages.nodes.reduce(
51-
(all, { id, fileAbsolutePath }) => {
51+
(all, { fileAbsolutePath, body }) => {
5252
const [filename, name, category, packageName] = fileAbsolutePath
5353
.split('/')
5454
.reverse();
5555

56-
const key = `${packageName}-${category}-${name}`;
56+
const id = `${packageName}-${category}-${name}`;
5757
const type = path.basename(filename, '.mdx');
5858

59-
if (!all[key]) {
60-
all[key] = {
59+
if (!all[id]) {
60+
all[id] = {
6161
id,
6262
name,
6363
category,
6464
package: packageName,
65-
ids: [],
66-
pages: {},
65+
pages: [],
6766
};
6867
}
6968

70-
all[key].ids.push(id);
71-
all[key].pages[id] = {
72-
name,
73-
category,
74-
package: packageName,
69+
all[id].pages.push({
7570
type,
76-
};
71+
meta: {
72+
name,
73+
category,
74+
package: packageName,
75+
},
76+
body,
77+
});
7778

7879
return all;
7980
},
8081
{},
8182
);
8283

8384
Object.values(groupedMdx).forEach(
84-
({ id, name, category, package: p, ids, pages }) => {
85+
({ id, name, category, package: p, pages }) => {
8586
actions.createPage({
8687
id,
8788
path: paths.componentPage({ package: p, category, name }),
8889
component,
8990
context: {
90-
ids,
9191
package: p,
9292
category,
9393
name,
Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import React from 'react';
2-
import { graphql } from 'gatsby';
32
import { MDXRenderer } from 'gatsby-plugin-mdx';
43
import { Layout } from '../components/layout';
54
import { Tabs } from '../components/tabs';
@@ -8,7 +7,7 @@ import { pageSections } from '../lib/constants';
87

98
const getTabData = ({ pages, mapper }) => {
109
return mapper.reduce((all, { code, label, renderHeader }) => {
11-
const page = pages.find((page) => page.meta.type === code);
10+
const page = pages.find((page) => page.type === code);
1211

1312
if (page) {
1413
all.push({
@@ -23,15 +22,8 @@ const getTabData = ({ pages, mapper }) => {
2322
}, []);
2423
};
2524

26-
const ComponentPage = ({ data, pageContext }) => {
27-
const pages = data.allMdx.nodes.map(({ id, body }) => {
28-
const meta = pageContext.pages[id];
29-
30-
return {
31-
meta,
32-
body,
33-
};
34-
});
25+
const ComponentPage = ({ pageContext }) => {
26+
const { pages } = pageContext;
3527

3628
const tabData = getTabData({ pages, mapper: pageSections });
3729

@@ -45,15 +37,4 @@ const ComponentPage = ({ data, pageContext }) => {
4537
);
4638
};
4739

48-
export const pageQuery = graphql`
49-
query($ids: [String]) {
50-
allMdx(filter: { id: { in: $ids } }) {
51-
nodes {
52-
id
53-
body
54-
}
55-
}
56-
}
57-
`;
58-
5940
export default ComponentPage;

0 commit comments

Comments
 (0)