Skip to content

Commit

Permalink
feat: Add a link to each service's analytics page in the editor (#2387)
Browse files Browse the repository at this point in the history
* feat: conditionally render link to analytics in PreviewBrowser


- Add new analytics_link column to the flow table
- Add analytics_link via bar chart icon in the editor PreviewBrowser
- Disable the link if there is no analytics_link associated with the flow
  • Loading branch information
Mike-Heneghan authored Nov 9, 2023
1 parent 33a3e9b commit f44bcbb
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,13 @@ import Tooltip from "@mui/material/Tooltip";
import Typography from "@mui/material/Typography";
import formatDistanceToNow from "date-fns/formatDistanceToNow";
import React, { useState } from "react";
import { ExternalLink, Globe, RefreshCw, Terminal } from "react-feather";
import {
BarChart,
ExternalLink,
Globe,
RefreshCw,
Terminal,
} from "react-feather";
import { useAsync } from "react-use";
import Input from "ui/Input";

Expand Down Expand Up @@ -93,13 +99,15 @@ const PreviewBrowser: React.FC<{
const [showDebugConsole, setDebugConsoleVisibility] = useState(false);
const [
flowId,
flowAnalyticsLink,
resetPreview,
publishFlow,
lastPublished,
lastPublisher,
validateAndDiffFlow,
] = useStore((state) => [
state.id,
state.flowAnalyticsLink,
state.resetPreview,
state.publishFlow,
state.lastPublished,
Expand Down Expand Up @@ -152,6 +160,25 @@ const PreviewBrowser: React.FC<{
/>
</Tooltip>

{flowAnalyticsLink ? (
<Tooltip arrow title="Open analytics page">
<Link
href={flowAnalyticsLink}
target="_blank"
rel="noopener noreferrer"
color="inherit"
>
<BarChart />
</Link>
</Tooltip>
) : (
<Tooltip arrow title="Analytics page unavailable">
<Link component={"button"} disabled aria-disabled={true}>
<BarChart />
</Link>
</Tooltip>
)}

<Tooltip arrow title="Open editor preview">
<Link
href={props.url.replace("/preview", "/unpublished")}
Expand Down
3 changes: 3 additions & 0 deletions editor.planx.uk/src/pages/FlowEditor/lib/store/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export interface SharedStore extends Store.Store {
flow: Store.flow;
flowSlug: string;
flowName: string;
flowAnalyticsLink: string | null;
id: string;
getNode: (id: Store.nodeId) => Store.node | undefined;
resetPreview: () => void;
Expand Down Expand Up @@ -51,6 +52,8 @@ export const sharedStore: StateCreator<

flowName: "",

flowAnalyticsLink: null,

id: "",
previewEnvironment: "standalone",

Expand Down
20 changes: 14 additions & 6 deletions editor.planx.uk/src/routes/flow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -173,10 +173,10 @@ const nodeRoutes = mount({
"/:parent/nodes/:id/edit": editNode,
});

const getFlowSettings = async (
const getFlowMetadata = async (
flow: string,
team: string,
): Promise<FlowSettings> => {
): Promise<{ flowSettings: FlowSettings; flowAnalyticsLink: string }> => {
const { data } = await client.query({
query: gql`
query GetFlow($slug: String!, $team_slug: String!) {
Expand All @@ -185,7 +185,8 @@ const getFlowSettings = async (
where: { slug: { _eq: $slug }, team: { slug: { _eq: $team_slug } } }
) {
id
settings
flowSettings: settings
flowAnalyticsLink: analytics_link
}
}
`,
Expand All @@ -194,7 +195,11 @@ const getFlowSettings = async (
team_slug: team,
},
});
return data.flows[0]?.settings;
const metadata = {
flowSettings: data.flows[0]?.flowSettings,
flowAnalyticsLink: data.flows[0]?.flowAnalyticsLink,
};
return metadata;
};

const routes = compose(
Expand All @@ -204,8 +209,11 @@ const routes = compose(

withView(async (req) => {
const [flow, ...breadcrumbs] = req.params.flow.split(",");
const settings: FlowSettings = await getFlowSettings(flow, req.params.team);
useStore.getState().setFlowSettings(settings);
const { flowSettings, flowAnalyticsLink } = await getFlowMetadata(
flow,
req.params.team,
);
useStore.setState({ flowSettings, flowAnalyticsLink });

return (
<>
Expand Down
2 changes: 2 additions & 0 deletions hasura.planx.uk/metadata/tables.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,7 @@
- creator_id
- data
- id
- analytics_link
- settings
- slug
- team_id
Expand Down Expand Up @@ -386,6 +387,7 @@
- creator_id
- data
- id
- analytics_link
- settings
- slug
- team_id
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE "public"."flows" DROP COLUMN "analytics_link";
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
alter table "public"."flows" add column "analytics_link" text
null;

0 comments on commit f44bcbb

Please sign in to comment.