Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

React Native: Fix on-device-notes #8692

Merged
merged 1 commit into from
Nov 4, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 17 additions & 40 deletions addons/ondevice-notes/src/components/Notes.tsx
Original file line number Diff line number Diff line change
@@ -1,59 +1,36 @@
/* eslint-disable react/destructuring-assignment */
import React, { Component } from 'react';
import React from 'react';
import { View } from 'react-native';
import Markdown from 'react-native-simple-markdown';
import { AddonStore } from '@storybook/addons';
import Events from '@storybook/core-events';
import { API } from '@storybook/api';
import { StoryStore } from '@storybook/client-api';

export const PARAM_KEY = `notes`;

type Selection = ReturnType<StoryStore['fromId']>;
interface NotesProps {
channel: ReturnType<AddonStore['getChannel']>;
api: API;
active: boolean;
}
interface NotesState {
selection: Selection;
}

export class Notes extends Component<NotesProps, NotesState> {
componentDidMount() {
this.props.channel.on(Events.SELECT_STORY, this.onStorySelected);
}

componentWillUnmount() {
this.props.channel.removeListener(Events.SELECT_STORY, this.onStorySelected);
export const Notes = ({ active, api }: NotesProps) => {
if (!active) {
return null;
}

onStorySelected = (selection: Selection) => {
this.setState({ selection });
};

render() {
const { active, api } = this.props;
const selection = api.store().getSelection();

if (!active) {
return null;
}

if (!this.state) {
return null;
}
if (!selection) {
return null;
}

const story = api
.store()
.getStoryAndParameters(this.state.selection.kind, this.state.selection.story);
const text = story.parameters[PARAM_KEY];
const story = api.store().fromId(selection.storyId);
const text = story.parameters[PARAM_KEY];

const textAfterFormatted: string = text ? text.trim() : '';
const textAfterFormatted: string = text ? text.trim() : '';

return (
<View style={{ padding: 10, flex: 1 }}>
<Markdown>{textAfterFormatted}</Markdown>
</View>
);
}
}
return (
<View style={{ padding: 10, flex: 1 }}>
<Markdown>{textAfterFormatted}</Markdown>
</View>
);
};