Skip to content

Commit

Permalink
feat: add usage button for telemetry
Browse files Browse the repository at this point in the history
  • Loading branch information
moonrailgun committed Mar 3, 2024
1 parent 1802717 commit daef9ff
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 13 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ It's good to specialize in one thing, if we are experts in related abilities we
- [ ] utm track
- [ ] waitlist
- [ ] survey
- [ ] lighthouse report

## Preview

Expand Down
1 change: 0 additions & 1 deletion src/client/components/PageHeader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ export const PageHeader: React.FC<{
)}
</div>

<div className="text-2xl flex-1">{props.title}</div>
{props.action}
</div>
);
Expand Down
84 changes: 76 additions & 8 deletions src/client/components/telemetry/TelemetryList.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { Trans, t } from '@i18next-toolkit/react';
import { Button, Form, Input, Modal, Table } from 'antd';
import { Button, Collapse, Form, Input, Modal, Table, Typography } from 'antd';
import React, { useMemo, useState } from 'react';
import { AppRouterOutput, trpc } from '../../api/trpc';
import { useCurrentWorkspaceId } from '../../store/user';
import { type ColumnsType } from 'antd/es/table/interface';
import {
BarChartOutlined,
CodeOutlined,
EditOutlined,
PlusOutlined,
} from '@ant-design/icons';
Expand All @@ -18,10 +19,11 @@ type TelemetryInfo = AppRouterOutput['telemetry']['all'][number];

export const TelemetryList: React.FC = React.memo(() => {
const workspaceId = useCurrentWorkspaceId();
const [isModalOpen, setIsModalOpen] = useState(false);
const [isEditModalOpen, setIsEditModalOpen] = useState(false);
const [form] = Form.useForm<{ id?: string; name: string }>();
const upsertTelemetryMutation = trpc.telemetry.upsert.useMutation();
const utils = trpc.useUtils();
const [modal, contextHolder] = Modal.useModal();

const handleAddTelemetry = useEvent(async () => {
await form.validateFields();
Expand All @@ -35,13 +37,67 @@ export const TelemetryList: React.FC = React.memo(() => {

utils.telemetry.all.refetch();

setIsModalOpen(false);
setIsEditModalOpen(false);

form.resetFields();
});

const handleShowUsage = useEvent((info: TelemetryInfo) => {
const blankGif = `${window.location.origin}/telemetry/${workspaceId}/${info.id}.gif`;
const countBadgeUrl = `${window.location.origin}/telemetry/${workspaceId}/${info.id}/badge.svg`;
modal.info({
maskClosable: true,
title: 'How to Use Telemetry',
content: (
<div>
<p>Here is some way to use telemetry:</p>
<h2>Insert to article:</h2>
<p>
if your article support raw html, you can direct insert it{' '}
<Typography.Text code={true} copyable={{ text: blankGif }}>
{blankGif}
</Typography.Text>
</p>
<Collapse
ghost
items={[
{
label: 'Advanced',
children: (
<div>
<p>
Some website will not allow send `referer` field. so its
maybe can not track source. so you can mark it by
yourself. for example:
</p>
<p>
<Typography.Text code={true}>
{blankGif}?url=https://xxxxxxxx
</Typography.Text>
</p>
</div>
),
},
]}
/>

<h2>Count your website visitor:</h2>
<p>
if your article support raw html, you can direct insert it{' '}
<Typography.Text code={true} copyable={{ text: blankGif }}>
{countBadgeUrl}
</Typography.Text>
</p>
<p>
Like this: <img src={countBadgeUrl} />
</p>
</div>
),
});
});

const handleEditTelemetry = useEvent(async (info: TelemetryInfo) => {
setIsModalOpen(true);
setIsEditModalOpen(true);
form.setFieldsValue({
id: info.id,
name: info.name,
Expand Down Expand Up @@ -87,24 +143,27 @@ export const TelemetryList: React.FC = React.memo(() => {
type="primary"
icon={<PlusOutlined />}
size="large"
onClick={() => setIsModalOpen(true)}
onClick={() => setIsEditModalOpen(true)}
>
{t('Add Telemetry')}
</Button>
</div>
}
/>

<TelemetryListTable onEdit={handleEditTelemetry} />
<TelemetryListTable
onShowUsage={handleShowUsage}
onEdit={handleEditTelemetry}
/>

<Modal
title={t('Add Telemetry')}
open={isModalOpen}
open={isEditModalOpen}
okButtonProps={{
loading: upsertTelemetryMutation.isLoading,
}}
onOk={() => handleAddTelemetry()}
onCancel={() => setIsModalOpen(false)}
onCancel={() => setIsEditModalOpen(false)}
>
<Form layout="vertical" form={form}>
<Form.Item name="id" hidden={true} />
Expand All @@ -118,13 +177,16 @@ export const TelemetryList: React.FC = React.memo(() => {
</Form.Item>
</Form>
</Modal>

{contextHolder}
</div>
);
});
TelemetryList.displayName = 'TelemetryList';

const TelemetryListTable: React.FC<{
onEdit: (info: TelemetryInfo) => void;
onShowUsage: (info: TelemetryInfo) => void;
}> = React.memo((props) => {
const workspaceId = useCurrentWorkspaceId();
const { data = [], isLoading } = trpc.telemetry.all.useQuery({
Expand Down Expand Up @@ -155,6 +217,12 @@ const TelemetryListTable: React.FC<{
render: (_, record) => {
return (
<div className="flex gap-2 justify-end">
<Button
icon={<CodeOutlined />}
onClick={() => props.onShowUsage(record)}
>
{t('Usage')}
</Button>
<Button
icon={<EditOutlined />}
onClick={() => props.onEdit(record)}
Expand Down
8 changes: 4 additions & 4 deletions src/server/model/telemetry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ export async function recordTelemetryEvent(req: Request) {
}
const eventName = name ? String(name) : undefined;

const session = await findSession(req, url);
if (!session) {
const workspaceId = req.params.workspaceId;
if (!workspaceId) {
return;
}

const workspaceId = req.params.workspaceId;
if (!workspaceId) {
const session = await findSession(req, url);
if (!session) {
return;
}

Expand Down

0 comments on commit daef9ff

Please sign in to comment.