Skip to content

Commit

Permalink
add another extension point for the main content
Browse files Browse the repository at this point in the history
  • Loading branch information
signed committed Jun 1, 2020
1 parent 56e356d commit 6f4dac3
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 10 deletions.
7 changes: 4 additions & 3 deletions src/extension-api/extension-registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ export interface ExtensionRegistry {
extensionsFor<T extends Object>(point: ExtensionPoint<T>): (Extension & T) [];
}

export const ExtensionPointTypes = ['BottomBarItem'] as const;
// todo find a way to dynamically collect those and generate the type
export const ExtensionPointTypes = ['BottomBarItem', 'MainViewContent'] as const;
export type ExtensionPointType = typeof ExtensionPointTypes[number];

export interface ExtensionPoint<T> {
Expand Down Expand Up @@ -48,7 +49,7 @@ export class DefaultExtensionRegistry implements ExtensionRegistry {

extensionsFor<T extends object>(point: ExtensionPoint<T>): (Extension & T)[] {
const mayBe = this.extensions.get(point.type) ?? [];
return mayBe as ((Extension & T) []);
return [...mayBe as ((Extension & T) [])];
}
}

Expand All @@ -60,4 +61,4 @@ export const useExtensionRegistry = (): ExtensionRegistry => {
throw new Error('No extension registry found in the parent context');
}
return mayBeExtensionRegistry;
}
};
40 changes: 33 additions & 7 deletions src/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Box, BoxProps, CSSReset, Flex, Link, ThemeProvider } from '@chakra-ui/core';
import { Box, BoxProps, CSSReset, Flex, Link, ThemeProvider, Image, Text } from '@chakra-ui/core';
import * as React from 'react';
import { useContext } from 'react';
import { render } from 'react-dom';
Expand All @@ -9,7 +9,7 @@ export interface StaticConfiguration {
applicationName: string;
}

export const StaticConfiguration = React.createContext<StaticConfiguration|undefined>(undefined);
export const StaticConfiguration = React.createContext<StaticConfiguration | undefined>(undefined);

export const useStaticConfiguration = (): StaticConfiguration => {
const mayBe = useContext(StaticConfiguration);
Expand All @@ -33,8 +33,20 @@ const TopBar: React.FC<BoxProps> = (props) => {
};

const MainView: React.FC<BoxProps> = (props) => {
return <Box {...props}>
Look at me two
const extensions = useExtensionRegistry().extensionsFor(mainViewContentExtension);
if (extensions.length > 1) {
throw new Error('only one mainViewContentExtension allowed');
}
const extension = extensions[0] ?? {
alt: 'nothing here',
caption: 'Messier 87',
imageUrl: 'https://upload.wikimedia.org/wikipedia/commons/4/4f/Black_hole_-_Messier_87_crop_max_res.jpg'
};
return <Box as={'main'} {...props}>
<Flex direction={'column'} alignItems={'center'}>
<Image flexGrow={0} src={extension.imageUrl} height={'80vh'}/>
<Text>{extension.caption}</Text>
</Flex>
</Box>;
};

Expand All @@ -52,11 +64,17 @@ const PrivacyPolicy = () => {
return <Link>Privacy Policy</Link>;
};


type BottomBarExtensionContext = { element: () => JSX.Element }

const bottomBarExtension = BasicExtensionPoint.ofType<BottomBarExtensionContext>('BottomBarItem');

type MainViewContentContext = {
imageUrl: string,
caption: string,
alt: string
}
const mainViewContentExtension = BasicExtensionPoint.ofType<MainViewContentContext>('MainViewContent');

const BottomBar: React.FC<BoxProps> = (props) => {
const registry = useExtensionRegistry();
return <Box {...props} as={'footer'}>
Expand Down Expand Up @@ -86,7 +104,6 @@ const createBrandedExperience = (registry: ExtensionRegistry) => {
};



const nut = () => {
const config: StaticConfiguration = {
applicationName: 'Nut'
Expand All @@ -95,6 +112,11 @@ const nut = () => {
registry.register(bottomBarExtension.implementWith({ element: TermsOfUse }));
registry.register(bottomBarExtension.implementWith({ element: PrivacyPolicy }));
registry.register(bottomBarExtension.implementWith({ element: Contact }));
registry.register(mainViewContentExtension.implementWith({
imageUrl: 'https://upload.wikimedia.org/wikipedia/commons/d/df/Nut.svg',
caption: 'Nut - Egyptian goddess of the Sky',
alt: 'Nut'
}));
const BrandedComponent = createBrandedExperience(registry);
const dynamicConfiguration: DynamicConfiguration = { language: 'EN' };
return <StaticConfiguration.Provider value={config}>
Expand All @@ -111,10 +133,14 @@ const geb = () => {
return <Link>Geb Shows</Link>;
};


const registry = new DefaultExtensionRegistry();
registry.register(bottomBarExtension.implementWith({ element: GebShows }));
registry.register(bottomBarExtension.implementWith({ element: Contact }));
registry.register(mainViewContentExtension.implementWith({
imageUrl: 'https://upload.wikimedia.org/wikipedia/commons/5/53/Geb.svg',
caption: 'Geb - Egyptian god of the Earth',
alt: 'Geb'
}));
const BrandedComponent = createBrandedExperience(registry);
const dynamicConfiguration: DynamicConfiguration = { language: 'EN' };
return <StaticConfiguration.Provider value={config}>
Expand Down

0 comments on commit 6f4dac3

Please sign in to comment.