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

feat: Add a link to each service's analytics page in the editor #2387

Merged
merged 9 commits into from
Nov 9, 2023
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">
Mike-Heneghan marked this conversation as resolved.
Show resolved Hide resolved
<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 @@ -347,6 +347,7 @@
- creator_id
- data
- id
- analytics_link
- settings
- slug
- team_id
Expand Down Expand Up @@ -377,6 +378,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;
Loading