Skip to content

Commit

Permalink
refactor: added analytics track and custom parameter support (langflo…
Browse files Browse the repository at this point in the history
…w-ai#3617)

* Added analytics track

* Changed post add user to just use the function

* Added tracking in various components

* Added custom parameter support

* Added centered footer
  • Loading branch information
lucaseduoli authored Aug 29, 2024
1 parent 32d51b6 commit 9102c62
Show file tree
Hide file tree
Showing 11 changed files with 111 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import useHandleNodeClass from "@/CustomNodes/hooks/use-handle-node-class";
import { ParameterRenderComponent } from "@/components/parameterRenderComponent";
import { usePostTemplateValue } from "@/controllers/API/queries/nodes/use-post-template-value";
import { ReactNode, useEffect, useRef, useState } from "react";
import {
CustomParameterComponent,
getCustomParameterTitle,
} from "@/customization/components/custom-parameter";
import { useEffect, useRef } from "react";
import { default as IconComponent } from "../../../../components/genericIconComponent";
import ShadTooltip from "../../../../components/shadTooltipComponent";
import { LANGFLOW_SUPPORTED_TYPES } from "../../../../constants/constants";
import useFlowStore from "../../../../stores/flowStore";
import { useTypesStore } from "../../../../stores/typesStore";
import {
NodeInputFieldComponentType,
ParameterComponentType,
} from "../../../../types/components";
import { NodeInputFieldComponentType } from "../../../../types/components";
import { scapedJSONStringfy } from "../../../../utils/reactflowUtils";
import useFetchDataOnMount from "../../../hooks/use-fetch-data-on-mount";
import useHandleOnNewValue from "../../../hooks/use-handle-new-value";
Expand Down Expand Up @@ -105,11 +105,21 @@ export default function NodeInputField({
<div className="flex w-full items-center truncate text-sm">
{proxy ? (
<ShadTooltip content={<span>{proxy.id}</span>}>
{<span>{title}</span>}
{
<span>
{getCustomParameterTitle({ title, nodeId: data.id })}
</span>
}
</ShadTooltip>
) : (
<div className="flex gap-2">
<span>{<span>{title}</span>}</span>
<span>
{
<span>
{getCustomParameterTitle({ title, nodeId: data.id })}
</span>
}
</span>
</div>
)}
<span className={(required ? "ml-2 " : "") + "text-status-red"}>
Expand All @@ -133,7 +143,7 @@ export default function NodeInputField({
{displayHandle && Handle}
{data.node?.template[name] !== undefined && (
<div className="mt-2 w-full">
<ParameterRenderComponent
<CustomParameterComponent
handleOnNewValue={handleOnNewValue}
name={name}
nodeId={data.id}
Expand Down
2 changes: 2 additions & 0 deletions src/frontend/src/components/cardComponent/index.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { track } from "@/customization/utils/analytics";
import { useState } from "react";
import { Control } from "react-hook-form";
import IOModal from "../../modals/IOModal";
Expand Down Expand Up @@ -137,6 +138,7 @@ export default function CollectionCardComponent({
onClick={(e) => {
e.preventDefault();
e.stopPropagation();
track("Playground Button Clicked", { flowId: data.id });
setLoadingPlayground(true);
const flow = getFlowById(data.id);
if (flow) {
Expand Down
9 changes: 8 additions & 1 deletion src/frontend/src/components/chatComponent/index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { ENABLE_API } from "@/customization/feature-flags";
import { track } from "@/customization/utils/analytics";
import { Transition } from "@headlessui/react";
import { useMemo, useState } from "react";
import { useEffect, useMemo, useState } from "react";
import { useHotkeys } from "react-hotkeys-hook";
import IOModal from "../../modals/IOModal";
import ApiModal from "../../modals/apiModal";
Expand Down Expand Up @@ -48,6 +49,12 @@ export default function FlowToolbar(): JSX.Element {
const hasApiKey = useStoreStore((state) => state.hasApiKey);
const currentFlow = useFlowStore((state) => state.currentFlow);

useEffect(() => {
if (open) {
track("Playground Button Clicked");
}
}, [open]);

const ModalMemo = useMemo(
() => (
<ShareModal
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,7 @@ export const useAddUser: useMutationFunctionType<undefined, UserInputType> = (

const mutation: UseMutationResult<Array<Users>, any, UserInputType> = mutate(
["useAddUser"],
async (payload: UserInputType) => {
const res = await addUserFunction(payload);
return res;
},
addUserFunction,
options,
);

Expand Down
49 changes: 49 additions & 0 deletions src/frontend/src/customization/components/custom-parameter.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { ParameterRenderComponent } from "@/components/parameterRenderComponent";
import { handleOnNewValueType } from "@/CustomNodes/hooks/use-handle-new-value";
import { APIClassType, InputFieldType } from "@/types/api";

export function CustomParameterComponent({
handleOnNewValue,
name,
nodeId,
templateData,
templateValue,
editNode,
handleNodeClass,
nodeClass,
disabled,
}: {
handleOnNewValue: handleOnNewValueType;
name: string;
nodeId: string;
templateData: Partial<InputFieldType>;
templateValue: any;
editNode: boolean;
handleNodeClass: (value: any, code?: string, type?: string) => void;
nodeClass: APIClassType;
disabled: boolean;
}) {
return (
<ParameterRenderComponent
handleOnNewValue={handleOnNewValue}
name={name}
nodeId={nodeId}
templateData={templateData}
templateValue={templateValue}
editNode={editNode}
handleNodeClass={handleNodeClass}
nodeClass={nodeClass}
disabled={disabled}
/>
);
}

export function getCustomParameterTitle({
title,
nodeId,
}: {
title: string;
nodeId: string;
}) {
return title;
}
7 changes: 7 additions & 0 deletions src/frontend/src/customization/utils/analytics.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export const track = async (
name: string,
properties: Record<string, any> = {},
id: string = "",
): Promise<void> => {
return;
};
11 changes: 9 additions & 2 deletions src/frontend/src/modals/baseModal/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,16 @@ const Footer: React.FC<{
onClick?: () => void;
};
close?: boolean;
}> = ({ children, submit, close }) => {
centered?: boolean;
}> = ({ children, submit, close, centered }) => {
return (
<div className="flex flex-shrink-0 flex-row-reverse">
<div
className={
centered
? "flex flex-shrink-0 justify-center"
: "flex flex-shrink-0 flex-row-reverse"
}
>
{submit ? (
<div className="flex w-full items-center justify-between">
{children ?? <div />}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import LoadingComponent from "@/components/loadingComponent";
import { useGetBuildsQuery } from "@/controllers/API/queries/_builds";
import { track } from "@/customization/utils/analytics";
import useAutoSaveFlow from "@/hooks/flows/use-autosave-flow";
import useUploadFlow from "@/hooks/flows/use-upload-flow";
import _, { cloneDeep } from "lodash";
Expand Down Expand Up @@ -334,6 +335,8 @@ export default function Page({ view }: { view?: boolean }): JSX.Element {
event.dataTransfer.getData("nodedata"),
);

track(`Component Added: ${data.node?.display_name}`);

const newId = getNodeId(data.type);

const newNode: NodeType = {
Expand Down
6 changes: 5 additions & 1 deletion src/frontend/src/pages/MainPage/pages/mainPage/index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import FolderSidebarNav from "@/components/folderSidebarComponent";
import { useDeleteFolders } from "@/controllers/API/queries/folders";
import { useCustomNavigate } from "@/customization/hooks/use-custom-navigate";
import { track } from "@/customization/utils/analytics";
import useAlertStore from "@/stores/alertStore";
import { useState } from "react";
import { Outlet, useLocation } from "react-router-dom";
Expand Down Expand Up @@ -65,7 +66,10 @@ export default function HomePage(): JSX.Element {
<div className="flex gap-2">
<DropdownButton
firstButtonName="New Project"
onFirstBtnClick={() => setOpenModal(true)}
onFirstBtnClick={() => {
setOpenModal(true);
track("New Project Button Clicked");
}}
options={dropdownOptions}
plusButton={true}
dropdownOptions={false}
Expand Down
5 changes: 5 additions & 0 deletions src/frontend/src/pages/Playground/index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { useGetRefreshFlows } from "@/controllers/API/queries/flows/use-get-refresh-flows";
import { useGetGlobalVariables } from "@/controllers/API/queries/variables";
import { useCustomNavigate } from "@/customization/hooks/use-custom-navigate";
import { track } from "@/customization/utils/analytics";
import { useStoreStore } from "@/stores/storeStore";
import { useTypesStore } from "@/stores/typesStore";
import { useEffect } from "react";
Expand Down Expand Up @@ -57,6 +58,10 @@ export default function PlaygroundPage() {
awaitgetTypes();
}, [id, flows, validApiKey]);

useEffect(() => {
if (id) track("Playground Page Loaded", { flowId: id });
}, []);

return (
<div className="flex h-full w-full flex-col items-center justify-center align-middle">
{currentSavedFlow && (
Expand Down
4 changes: 3 additions & 1 deletion src/frontend/src/pages/SignUpPage/index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { useAddUser } from "@/controllers/API/queries/auth";
import { CustomLink } from "@/customization/components/custom-link";
import { useCustomNavigate } from "@/customization/hooks/use-custom-navigate";
import { track } from "@/customization/utils/analytics";
import * as Form from "@radix-ui/react-form";
import { FormEvent, useEffect, useState } from "react";
import InputComponent from "../../components/inputComponent";
Expand Down Expand Up @@ -52,7 +53,8 @@ export default function SignUp(): JSX.Element {
};

mutateAddUser(newUser, {
onSuccess: () => {
onSuccess: (user) => {
track("User Signed Up", user);
setSuccessData({
title: SIGN_UP_SUCCESS,
});
Expand Down

0 comments on commit 9102c62

Please sign in to comment.