Skip to content

Commit

Permalink
feat(ChangelogDialog): metrica
Browse files Browse the repository at this point in the history
  • Loading branch information
Feverqwe committed Oct 18, 2023
1 parent 4499f51 commit a7d7084
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 10 deletions.
56 changes: 53 additions & 3 deletions src/components/ChangelogDialog/ChangelogDialog.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import React from 'react';
import React, {useCallback, useEffect, useRef} from 'react';

import {ArrowUpRightFromSquare} from '@gravity-ui/icons';
import {Dialog, Icon, Link} from '@gravity-ui/uikit';
import type {DialogProps} from '@gravity-ui/uikit';
import {Dialog, Icon, Link} from '@gravity-ui/uikit';

import {Metrica} from '../../types';
import {block} from '../utils/cn';

import {Item} from './components/Item/Item';
import {Goals} from './constants';
import i18n from './i18n';
import type {ChangelogItem} from './types';

Expand All @@ -23,6 +25,8 @@ export interface ChangelogDialogProps {
disableOutsideClick?: boolean;
onClose: DialogProps['onClose'];
onStoryClick?: (storyId: string) => void;
service?: string;
metrica?: Metrica;
}

let nextId = 1;
Expand All @@ -39,10 +43,55 @@ export function ChangelogDialog({
disableOutsideClick,
onClose,
onStoryClick,
service,
metrica,
}: ChangelogDialogProps) {
const idRef = React.useRef<number>();
idRef.current = idRef.current || getNextId();
const dialogCaptionId = `changelog-dialog-title-${idRef.current}`;
const refActionCount = useRef(0);

useEffect(() => {
if (!service || !metrica || !open) return () => {};

metrica.reachGoal(Goals.Show, {service});

let fired = false;
const timeoutId = window.setTimeout(() => {
fired = true;
}, 3000);

return () => {
clearTimeout(timeoutId);
if (!fired && !refActionCount.current) {
metrica.reachGoal(Goals.Bounce, {service});
}
metrica.reachGoal(Goals.Hide, {service});
};
}, [open, service, metrica]);

const handleStoryClick = useCallback(
(storyId: string) => {
refActionCount.current++;
if (service && metrica) {
metrica.reachGoal(Goals.Click, {service, action: storyId});
}
if (onStoryClick) {
onStoryClick(storyId);
}
},
[onStoryClick, service, metrica],
);

const handleLinkClick = useCallback(
(link: string) => {
refActionCount.current++;
if (service && metrica) {
metrica.reachGoal(Goals.Click, {service, action: link});
}
},
[service, metrica],
);

return (
<Dialog
Expand Down Expand Up @@ -71,7 +120,8 @@ export function ChangelogDialog({
key={index}
className={b('item')}
data={item}
onStoryClick={onStoryClick}
onStoryClick={handleStoryClick}
onLinkClick={handleLinkClick}
/>
))
) : (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,4 +115,10 @@ Default.args = {
// eslint-disable-next-line no-console
console.log(storyId);
},
service: 'storybook',
metrica: {
reachGoal: (target: string, params: Record<string, string>) => {
console.log('reachGoal', target, params);
},
},
};
24 changes: 17 additions & 7 deletions src/components/ChangelogDialog/components/Item/Item.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from 'react';
import React, {useCallback} from 'react';

import {CirclePlay} from '@gravity-ui/icons';
import {Button, Icon, Label} from '@gravity-ui/uikit';
Expand All @@ -16,9 +16,22 @@ export interface ItemProps {
className?: string;
data: ChangelogItem;
onStoryClick?: (storyId: string) => void;
onLinkClick?: (link: string) => void;
}

export function Item({className, data, onStoryClick}: ItemProps) {
export function Item({className, data, onStoryClick, onLinkClick}: ItemProps) {
const handleLinkClick = useCallback(() => {
if (onLinkClick && data.link) {
onLinkClick(data.link);
}
}, [data.link, onLinkClick]);

const handleStoryClick = useCallback(() => {
if (onStoryClick && data.storyId) {
onStoryClick(data.storyId);
}
}, [data.storyId, onStoryClick]);

return (
<article className={b(null, className)}>
<div className={b('meta')}>
Expand Down Expand Up @@ -48,6 +61,7 @@ export function Item({className, data, onStoryClick}: ItemProps) {
view="outlined"
href={data.link}
target={'_blank'}
onClick={handleLinkClick}
>
{i18n('action_read-more')}
</Button>
Expand All @@ -56,11 +70,7 @@ export function Item({className, data, onStoryClick}: ItemProps) {
<Button
className={b('button')}
view="outlined-action"
onClick={() => {
if (data.storyId) {
onStoryClick(data.storyId);
}
}}
onClick={handleStoryClick}
>
{i18n('button_view_story')}
<Icon data={CirclePlay} size={14} />
Expand Down
6 changes: 6 additions & 0 deletions src/components/ChangelogDialog/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export enum Goals {
Show = 'CHANGELOG_POP-UP_SHOW',
Click = 'CHANGELOG_ACTION-BUTTON_CLICK',
Hide = 'CHANGELOG_POP-UP_HIDE',
Bounce = 'CHANGELOG_POP-UP_BOUNCE',
}
3 changes: 3 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export interface Metrica {
reachGoal: (target: string, params: Record<string, string>) => void;
}

0 comments on commit a7d7084

Please sign in to comment.