Skip to content

Commit

Permalink
add telemetry events to matches
Browse files Browse the repository at this point in the history
  • Loading branch information
tjsilver committed Aug 27, 2020
1 parent 245774b commit 9e57458
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 70 deletions.
6 changes: 2 additions & 4 deletions src/ts/components/Match.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@ class Match<TMatch extends IMatch> extends Component<IMatchProps<TMatch>> {
suggestions,
replacement,
markAsCorrect,
matchContext,
matchedText
matchContext
} = match;
const url = document.URL;
const feedbackInfo = {
Expand All @@ -52,8 +51,7 @@ class Match<TMatch extends IMatch> extends Component<IMatchProps<TMatch>> {
{suggestionsToRender && applySuggestions && !markAsCorrect && (
<SuggestionList
applySuggestions={applySuggestions}
matchId={matchId}
matchedText={matchedText}
match={match}
suggestions={suggestionsToRender}
/>
)}
Expand Down
22 changes: 15 additions & 7 deletions src/ts/components/SidebarMatch.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import compact from "lodash/compact";

import React, { useState } from "react";
import React, { useState, useContext } from "react";

import { IMatch } from "../interfaces/IMatch";
import {
Expand All @@ -12,6 +12,7 @@ import titleCase from "lodash/startCase";
import { ApplySuggestionOptions } from "../commands";
import SuggestionList from "./SuggestionList";
import { getHtmlFromMarkdown } from "../utils/dom";
import TelemetryContext from "../contexts/TelemetryContext";

interface IProps {
match: IMatch;
Expand All @@ -37,11 +38,12 @@ const SidebarMatch = ({
stopHighlight,
selectedMatch,
editorScrollElement,
getScrollOffset,
getScrollOffset
}: IProps) => {

const [isOpen, setIsOpen] = useState<boolean>(false);

const { telemetryService } = useContext(TelemetryContext);

const color = getColourForMatch(match, matchColours, false).borderColour;
const hasSuggestions = !!match.suggestions && !!match.suggestions.length;
const suggestions = compact([
Expand All @@ -57,10 +59,18 @@ const SidebarMatch = ({
e.preventDefault();
e.stopPropagation();

telemetryService?.sidebarMatchClicked({
documentUrl: document.URL,
ruleId: match.ruleId,
matchId: match.matchId,
matchedText: match.matchedText,
matchContext: match.matchContext
});

if (!editorScrollElement) {
return;
}

const decorationElement = maybeGetDecorationElement(match.matchId);

if (decorationElement) {
Expand All @@ -80,7 +90,6 @@ const SidebarMatch = ({
stopHighlight();
};


return (
<div
className={`SidebarMatch__container ${
Expand Down Expand Up @@ -128,8 +137,7 @@ const SidebarMatch = ({
<div className="SidebarMatch__suggestion-list">
<SuggestionList
applySuggestions={applySuggestions}
matchId={match.matchId}
matchedText={match.matchedText}
match={match}
suggestions={suggestions}
/>
</div>
Expand Down
46 changes: 28 additions & 18 deletions src/ts/components/Suggestion.tsx
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
import React from "react";
import React, { useContext } from "react";
import jsDiff, { Change } from "diff";

import { ApplySuggestionOptions } from "../commands";
import { ISuggestion } from "../interfaces/IMatch";
import { ISuggestion, IMatch } from "../interfaces/IMatch";
import WikiSuggestion from "./WikiSuggestion";
import TelemetryContext from "../contexts/TelemetryContext";

interface IProps {
matchId: string;
matchedText: string;
match: IMatch;
suggestion: ISuggestion;
applySuggestions: (opts: ApplySuggestionOptions) => void;
}

/**
* At the moment, only show fancy diffs for smaller words.
*/
const shouldShowDiff = (matchedText: string) => matchedText.length < 16
const shouldShowDiff = (matchedText: string) => matchedText.length < 16;

/**
* Render a diff between the matched text and the suggestion, only showing
Expand Down Expand Up @@ -56,31 +56,41 @@ const renderSuggestionText = (matchedText: string, suggestionText: string) => {
<span className="Suggestion__arrow">&nbsp;→&nbsp;</span>
<span className="Suggestion__text">{suggestionText}</span>
</>
)
);
}

return <span className="Suggestion__text">{suggestionText}</span>
}
return <span className="Suggestion__text">{suggestionText}</span>;
};

const Suggestion = ({ match, suggestion, applySuggestions }: IProps) => {
const { telemetryService } = useContext(TelemetryContext);

const boundApplySuggestions = () => {
if (!applySuggestions) {
return;
}

const Suggestion = ({
matchId,
suggestion,
matchedText,
applySuggestions
}: IProps) => {
const boundApplySuggestions = () =>
applySuggestions &&
applySuggestions([
{
matchId,
matchId: match.matchId,
text: suggestion.text
}
]);

telemetryService?.suggestionIsAccepted({
documentUrl: document.URL,
ruleId: match.ruleId,
matchId: match.matchId,
matchedText: match.matchedText,
matchContext: match.matchContext,
suggestion: suggestion.text
});
};
switch (suggestion.type) {
case "TEXT_SUGGESTION": {
return (
<div className="Suggestion" onClick={boundApplySuggestions}>
{renderSuggestionText(matchedText, suggestion.text)}
{renderSuggestionText(match.matchedText, suggestion.text)}
</div>
);
}
Expand Down
73 changes: 37 additions & 36 deletions src/ts/components/SuggestionList.tsx
Original file line number Diff line number Diff line change
@@ -1,51 +1,52 @@
import React, { useState } from "react";
import { ISuggestion } from "../interfaces/IMatch";
import React, { useState } from "react";
import { ISuggestion, IMatch } from "../interfaces/IMatch";
import Suggestion from "./Suggestion";
import { ApplySuggestionOptions } from "../commands";

interface IProps {
suggestions: ISuggestion[];
matchId: string;
matchedText: string;
match: IMatch;
applySuggestions: (opts: ApplySuggestionOptions) => void;
}

const SuggestionList = ({ suggestions, matchId, matchedText, applySuggestions }: IProps) => {
const SuggestionList = ({
suggestions,
match,
applySuggestions,
}: IProps) => {
const [isOpen, setIsOpen] = useState(false);
const firstSuggestion = suggestions[0];
const otherSuggestions = suggestions.slice(1);
return (
<div className="SidebarMatch__suggestion-list">
{suggestions.length ? (
<Suggestion
matchId={matchId}
matchedText={matchedText}
suggestion={firstSuggestion}
applySuggestions={applySuggestions}
/>
) : null}
{!!otherSuggestions.length ? (
<div
className="Button SuggestionList__see-more"
onClick={() => setIsOpen(!isOpen)}
>
See {!isOpen ? "more" : "fewer"} suggestions (
{otherSuggestions.length})
</div>
) : null}
{isOpen && (
<>
{otherSuggestions.map(suggestion => (
<Suggestion
matchId={matchId}
matchedText={matchedText}
suggestion={suggestion}
applySuggestions={applySuggestions}
/>
))}
</>
)}
</div>
<div className="SidebarMatch__suggestion-list">
{suggestions.length ? (
<Suggestion
match={match}
suggestion={firstSuggestion}
applySuggestions={applySuggestions}
/>
) : null}
{!!otherSuggestions.length ? (
<div
className="Button SuggestionList__see-more"
onClick={() => setIsOpen(!isOpen)}
>
See {!isOpen ? "more" : "fewer"} suggestions (
{otherSuggestions.length})
</div>
) : null}
{isOpen && (
<>
{otherSuggestions.map(suggestion => (
<Suggestion
match={match}
suggestion={suggestion}
applySuggestions={applySuggestions}
/>
))}
</>
)}
</div>
);
};

Expand Down
12 changes: 9 additions & 3 deletions src/ts/createView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ const createView = ({

// Finally, render our components.
render(
<TelemetryContext.Provider value={{telemetryService}}>
<TelemetryContext.Provider value={{ telemetryService }}>
<MatchOverlay
store={store}
applySuggestions={suggestionOpts => {
Expand All @@ -71,18 +71,24 @@ const createView = ({
(match => {
commands.ignoreMatch(match.matchId);
onMarkCorrect(match);
telemetryService.matchIsMarkedAsCorrect({
documentUrl: document.URL,
ruleId: match.ruleId,
matchId: match.matchId,
matchedText: match.matchedText,
matchContext: match.matchContext
});
})
}
feedbackHref={feedbackHref}
stopHover={commands.stopHover}
/>
</TelemetryContext.Provider>,
overlayNode

);

render(
<TelemetryContext.Provider value={{telemetryService}}>
<TelemetryContext.Provider value={{ telemetryService }}>
<Sidebar
store={store}
matcherService={matcherService}
Expand Down
5 changes: 3 additions & 2 deletions src/ts/interfaces/ITelemetryData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,10 @@ export interface ITyperighterTelemetryEvent extends ITelemetryEvent {
}

interface IMatchEventTags {
ruleId: string;
ruleId: string,
suggestion?: string;
match: string;
matchId: string;
matchedText: string;
matchContext: string;
}

Expand Down

0 comments on commit 9e57458

Please sign in to comment.