Skip to content

Commit

Permalink
fix: fix gatsby pages creation
Browse files Browse the repository at this point in the history
  • Loading branch information
atanasster committed Jul 25, 2020
1 parent d50e60e commit a48518e
Show file tree
Hide file tree
Showing 18 changed files with 319 additions and 190 deletions.
4 changes: 2 additions & 2 deletions core/store/src/Store/HMRStore.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { LoadingStore } from '@component-controls/loader';
import { Store } from './Store';
import { loadStoryStore } from '../serialization/load-store';
import { loadStore } from '../serialization/load-store';
import { StoryStore } from '../types';

export class HMRStore extends Store {
constructor(store?: LoadingStore) {
super(store ? loadStoryStore(store) : undefined);
super(store ? loadStore(store) : undefined);
}

hmr = (store?: LoadingStore): StoryStore => {
Expand Down
1 change: 1 addition & 0 deletions core/store/src/create-pages/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './pages-paths';
167 changes: 167 additions & 0 deletions core/store/src/create-pages/pages-paths.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
import {
StoriesStore,
defDocType,
DocType,
getDocTypePath,
removeTrailingSlash,
ensureStartingSlash,
Pages,
TabConfiguration,
getDocPath,
getStoryPath,
} from '@component-controls/core';

import { HomePageInfo } from '../types';

export const getIndexPage = (store: StoriesStore): HomePageInfo => {
const docs = Object.keys(store.docs);
const homePageId = docs.find(key => {
const doc = store.docs[key];
return doc.route === '/';
});
const homePage = homePageId
? store.docs[homePageId]
: docs.length > 0
? store.docs[docs[0]]
: undefined;
return {
docId: homePage?.title,
type: homePage?.type || defDocType,
};
};

export interface DocHomePagesPath {
type: DocType;
path: string;
docId?: string;
}
export const getHomePages = (store: StoriesStore): DocHomePagesPath[] => {
const { pages = {} } = store?.config || {};
if (pages) {
const docs = Object.keys(store.docs);
const paths: DocHomePagesPath[] = Object.keys(pages).map(
(type: DocType) => {
const page = pages[type];
const path = getDocTypePath(page) as string;

const docId =
docs.find(key => {
const doc = store.docs[key];
return (
removeTrailingSlash(ensureStartingSlash(doc?.route || '')) ===
path
);
}) || docs.find(key => (store.docs[key].type || defDocType) === type);
return {
type,
path,
docId,
};
},
);
return paths;
}
return [];
};

export const getPageList = (
store: StoriesStore,
type: DocType = defDocType,
): Pages => {
if (store) {
return Object.keys(store.docs).reduce((acc: Pages, key: string) => {
const doc = store.docs[key];
if (doc) {
const { type: docType = defDocType } = doc;
if (docType === type) {
return [...acc, doc];
}
}
return acc;
}, []);
}
return [];
};

export const getUniquesByField = (
store: StoriesStore,
field: string,
): { [key: string]: number } => {
return Object.keys(store.docs).reduce(
(acc: { [key: string]: number }, key) => {
const doc = store.docs[key];
const value = (doc as any)[field];
const values = Array.isArray(value) ? value : [value];
values.forEach(v => {
if (v !== undefined) {
if (typeof acc[v] === 'undefined') {
acc[v] = 0;
}
acc[v] = acc[v] = 1;
}
});
return acc;
},
{},
);
};

export interface DocPagesPath {
type: DocType;
path: string;
docId?: string | null;
storyId?: string | null;
category?: string | null;
activeTab?: string | null;
}
export const getDocPages = (store: StoriesStore): DocPagesPath[] => {
const { pages = {}, categories = [] } = store?.config || {};
const docPaths: DocPagesPath[] = [];
Object.keys(pages).forEach(type => {
if (!categories.includes(type as DocType)) {
const page = pages[type as DocType];
const docType = type as DocType;
const docs: Pages = getPageList(store, docType);
const tabs: Pick<TabConfiguration, 'route'>[] = page.tabs || [
{ route: undefined },
];
tabs.forEach((tab, tabIndex) => {
const route = tabIndex > 0 ? tab.route : undefined;
docs.forEach(doc => {
if (doc.route !== '/') {
const stories =
page.storyPaths && doc.stories?.length
? doc.stories
: [undefined];
stories.forEach((storyId?: string) => {
const path = getStoryPath(storyId, doc, pages, route);
docPaths.push({
path,
type: docType,
activeTab: route,
docId: doc.title,
storyId,
});
});
}
});
});
} else {
const cats = getUniquesByField(store, type);
const catKeys = Object.keys(cats);
catKeys.forEach(tag => {
const path = getDocPath(
type as DocType,
{ title: tag, componentsLookup: {} },
pages,
);
docPaths.push({
path,
type,
category: tag,
});
});
}
});
return docPaths;
};
34 changes: 2 additions & 32 deletions core/store/src/hooks/document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,46 +108,16 @@ const docsByTypeSelector = selectorFamily<Pages, DocType>({
get: type => ({ get }) => {
const store = get(storeAtom);
const docs = store.docs;
const { storySort } = get(configSelector) || {};
let resultDocs = Object.keys(docs).reduce((acc: Pages, key: string) => {
return Object.keys(docs).reduce((acc: Pages, key: string) => {
const doc: Document | undefined = docs[key];
if (doc) {
const { type: docType = defDocType } = doc;
if (docType === type) {
return [...acc, doc];
return [...acc, { ...doc }];
}
}
return acc;
}, []);

if (storySort) {
resultDocs = resultDocs.sort((a: Document, b: Document) => {
//@ts-ignore
const sort = storySort(a.title, b.title);
if (sort !== 0) {
return sort;
}
return resultDocs.indexOf(a) - resultDocs.indexOf(b);
});
}
//split documents by their common 'parent'
resultDocs = resultDocs
.map(doc => {
const levels = doc.title.split('/');
const parent = levels.slice(0, -1).join('/');
return { id: doc, parent };
})
.sort((a, b) => {
if (a.parent === b.parent) {
return (
(store.docs[a.id.title].order || 0) -
(store.docs[b.id.title].order || 0)
);
}
return 0;
})
.map(item => item.id);
return resultDocs;
},
});

Expand Down
1 change: 1 addition & 0 deletions core/store/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ export * from './Store/HMRStore';
export * from './types';
export * from './hooks';
export * from './serialization/load-store';
export * from './create-pages';
Loading

0 comments on commit a48518e

Please sign in to comment.