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

Add (unstable) telemetry support with a hook #2368

Merged
merged 7 commits into from
Oct 31, 2019
Merged
Show file tree
Hide file tree
Changes from 3 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
2 changes: 2 additions & 0 deletions UNRELEASED.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ Use [the changelog guidelines](https://git.io/polaris-changelog-guidelines) to f

### Enhancements

- Added unstable API to integrate with Monorail and gather analytics about icon usage ([#2368](https://github.com/Shopify/polaris-react/pull/2368))

### Bug fixes

### Documentation
Expand Down
10 changes: 8 additions & 2 deletions src/components/AppProvider/AppProvider.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React from 'react';
import {ThemeConfig} from '../../utilities/theme';
import {MonorailContext, MonorailObject} from '../../utilities/monorail';
import {ThemeProvider} from '../ThemeProvider';
import {MediaQueryProvider} from '../MediaQueryProvider';
import {I18n, I18nContext, TranslationDictionary} from '../../utilities/i18n';
Expand Down Expand Up @@ -41,6 +42,7 @@ export interface AppProviderProps extends AppBridgeOptions {
features?: Features;
/** Inner content of the application */
children?: React.ReactNode;
UNSTABLE_monorail?: MonorailObject; // eslint-disable-line babel/camelcase
}

export class AppProvider extends React.Component<AppProviderProps, State> {
Expand Down Expand Up @@ -97,8 +99,9 @@ export class AppProvider extends React.Component<AppProviderProps, State> {
});
}

/* eslint-disable babel/camelcase */
render() {
const {theme = {}, features = {}, children} = this.props;
const {theme = {}, features = {}, UNSTABLE_monorail, children} = this.props;
const {intl, appBridge, link} = this.state;

return (
Expand All @@ -110,7 +113,9 @@ export class AppProvider extends React.Component<AppProviderProps, State> {
<AppBridgeContext.Provider value={appBridge}>
<LinkContext.Provider value={link}>
<ThemeProvider theme={theme}>
<MediaQueryProvider>{children}</MediaQueryProvider>
<MonorailContext.Provider value={UNSTABLE_monorail}>
<MediaQueryProvider>{children}</MediaQueryProvider>
</MonorailContext.Provider>
</ThemeProvider>
</LinkContext.Provider>
</AppBridgeContext.Provider>
Expand All @@ -121,4 +126,5 @@ export class AppProvider extends React.Component<AppProviderProps, State> {
</FeaturesContext.Provider>
);
}
/* eslint-enable babel/camelcase */
}
26 changes: 25 additions & 1 deletion src/components/Icon/Icon.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React from 'react';
import React, {useEffect} from 'react';
import {classNames, variationName} from '../../utilities/css';
import {useI18n} from '../../utilities/i18n';
import {useMonorail} from '../../utilities/monorail';
import {IconProps} from '../../types';

import styles from './Icon.scss';
Expand All @@ -21,6 +22,29 @@ interface Props extends IconProps {}

export function Icon({source, color, backdrop, accessibilityLabel}: Props) {
const i18n = useI18n();
const unstableMonorail = useMonorail();

/* eslint-disable babel/camelcase */
useEffect(() => {
if (unstableMonorail) {
let loggedSource;
if (typeof source === 'function') {
loggedSource = source.name;
} else if (source === 'placeholder') {
loggedSource = source;
} else {
loggedSource = 'custom icon string';
}

unstableMonorail.produce('polaris_icons_usage/1.0', {
icon_source: loggedSource,
color,
backdrop,
accessibility_label: accessibilityLabel,
});
}
}, [accessibilityLabel, backdrop, color, source, unstableMonorail]);
/* eslint-enable babel/camelcase */

if (color && backdrop && COLORS_WITH_BACKDROPS.indexOf(color) < 0) {
// eslint-disable-next-line no-console
Expand Down
9 changes: 9 additions & 0 deletions src/utilities/monorail/context.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import React from 'react';

export interface MonorailObject {
produce(schemaId: string, payload: Record<string, any>): boolean;
}

export const MonorailContext = React.createContext<MonorailObject | undefined>(
undefined,
);
6 changes: 6 additions & 0 deletions src/utilities/monorail/hooks.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import React from 'react';
import {MonorailContext} from './context';

export function useMonorail() {
return React.useContext(MonorailContext);
}
3 changes: 3 additions & 0 deletions src/utilities/monorail/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export {MonorailContext, MonorailObject} from './context';

export {useMonorail} from './hooks';