Skip to content

Commit

Permalink
chore: add docContext
Browse files Browse the repository at this point in the history
  • Loading branch information
atanasster committed Jul 15, 2020
1 parent e458838 commit 5acab47
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 26 deletions.
9 changes: 3 additions & 6 deletions ui/app/src/DocPage/DocPage.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/** @jsx jsx */
import { FC, useContext } from 'react';
import { FC } from 'react';
import { jsx } from 'theme-ui';
import { BlockContext } from '@component-controls/blocks';
import { useDocContext } from '@component-controls/blocks';
import { PageContainer } from '../PageContainer';
import { SidebarsPage, DocPageProps } from '../SidebarsPage';
import { CategoryPage } from '../CategoryPage';
Expand All @@ -15,13 +15,10 @@ export const DocPage: FC<Omit<DocPageProps, 'doc'> & { category?: string }> = ({
category,
...props
}) => {
const { storeProvider, docId } = useContext(BlockContext);
const { doc } = useDocContext();
if (category) {
return <CategoryPage type={type} category={category} />;
}

const doc = docId ? storeProvider.getStoryDoc(docId) : undefined;

const hasNoSideBars = doc && !doc.navSidebar && !doc.contextSidebar;
const isFullPage = doc && doc.fullPage;
if (hasNoSideBars || isFullPage) {
Expand Down
4 changes: 2 additions & 2 deletions ui/blocks/src/Container/Container.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { Title } from '../Title';
import { Subtitle } from '../Subtitle';
import { EditPage } from '../EditPage';

import { useStoryContext } from '../context';
import { useDocContext } from '../context';

export interface ContainerProps {
author?: ReactNode;
Expand All @@ -16,7 +16,7 @@ export interface ContainerProps {
* page inner container. will display a like to edit the page and a last updated date.
*/
export const Container: FC<ContainerProps> = ({ children, author }) => {
const { doc } = useStoryContext({ id: '.' });
const { doc } = useDocContext();
return (
<Box variant="blockpagecontainer.container">
<Box variant="blockpagecontainer.inforow">
Expand Down
4 changes: 2 additions & 2 deletions ui/blocks/src/EditPage/EditPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ import { FC } from 'react';
import { Box, Text } from 'theme-ui';
import { MarkGithubIcon } from '@primer/octicons-react';
import { ExternalLink } from '@component-controls/components';
import { useStoryContext } from '../context';
import { useDocContext } from '../context';

/**
* Display a Edit this page link to the page source repository.
* In order for this to work, you need to set up the `repository` field in `package.json`.
*/
export const EditPage: FC = () => {
const { docPackage } = useStoryContext({ id: '.' });
const { package: docPackage } = useDocContext();
return docPackage && docPackage.repository && docPackage.repository.browse ? (
<Box variant="editpage.container">
<ExternalLink
Expand Down
6 changes: 3 additions & 3 deletions ui/blocks/src/Pagination/Pagination.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ import {
Pagination as PaginationControl,
PaginationPage,
} from '@component-controls/components';
import { useStoryContext } from '../context';
import { useDocContext } from '../context';

/**
* displays automatic pagination to the next/previous document of this same type.
*/
export const Pagination: FC = () => {
const { doc, storeProvider } = useStoryContext({ id: '.' });
if (doc) {
const { doc, storeProvider } = useDocContext();
if (doc && storeProvider) {
const prevDoc = storeProvider.getPrevPage(doc.type, doc.title);
const nextDoc = storeProvider.getNextPage(doc.type, doc.title);
const prevLink: PaginationPage | undefined = prevDoc
Expand Down
42 changes: 29 additions & 13 deletions ui/blocks/src/context/story/StoryContext.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { FC, useState, useEffect } from 'react';
import React, { FC, useState, useEffect, useContext } from 'react';
import {
Story,
Document,
Expand Down Expand Up @@ -60,13 +60,10 @@ export const useStoryContext = ({
id = CURRENT_STORY,
name,
}: StoryInputProps): StoryContextProps => {
const {
storyId: currentId,
storeProvider,
options,
docId,
} = React.useContext(BlockContext);
const { getStoryData, storyIdFromName } = React.useContext(BlockDataContext);
const { storyId: currentId, storeProvider, options, docId } = useContext(
BlockContext,
);
const { getStoryData, storyIdFromName } = useContext(BlockDataContext);
const storyId = name
? storyIdFromName(name)
: id === CURRENT_STORY
Expand All @@ -80,7 +77,7 @@ export const useStoryContext = ({
}>(getStoryData(storyId, docId));

useEffect(() => {
const updateData = (updateId?: string) => {
const updateStoryData = (updateId?: string) => {
if (!updateId || updateId === storyId) {
const { story, doc, component, docPackage } = getStoryData(
storyId,
Expand All @@ -89,12 +86,13 @@ export const useStoryContext = ({
setData({ story, doc, component, docPackage });
}
};
const { story } = data;
if (story?.id !== storyId) {
updateData(storyId);

const { story, doc } = data;
if (story?.id !== storyId || doc?.title !== docId) {
updateStoryData(storyId);
}
const onChange = (id?: string) => {
updateData(id);
updateStoryData(id);
};
storeProvider.addObserver(onChange);
return () => {
Expand All @@ -121,3 +119,21 @@ export const StoryContextConsumer: FC<StoryContextConsumer &
const context = useStoryContext(rest);
return children ? children(context) || null : null;
};

export interface DocContextProps {
doc?: Document;
package?: PackageInfo;
storeProvider?: StoryStore;
}
export const useDocContext = (): DocContextProps => {
const { storeProvider, docId } = useContext(BlockContext);
const doc = docId ? storeProvider.getStoryDoc(docId) : undefined;
return {
doc,
storeProvider,
package:
doc && doc.package
? storeProvider.getStore()?.packages[doc.package]
: undefined,
};
};

0 comments on commit 5acab47

Please sign in to comment.