Skip to content

Commit

Permalink
🐛 Fix error thrown in handler when changing name (lukevella#598)
Browse files Browse the repository at this point in the history
  • Loading branch information
lukevella committed Mar 22, 2023
1 parent a3aae48 commit d049e91
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 8 deletions.
10 changes: 5 additions & 5 deletions apps/web/src/components/profile/user-details.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { m } from "framer-motion";
import { useTranslation } from "next-i18next";
import posthog from "posthog-js";
import * as React from "react";
import { useForm } from "react-hook-form";

import { usePostHog } from "@/utils/posthog";

import { requiredString, validEmail } from "../../utils/form-validation";
import { trpc } from "../../utils/trpc";
import { Button } from "../button";
Expand Down Expand Up @@ -31,12 +32,12 @@ export const UserDetails: React.FunctionComponent<UserDetailsProps> = ({
defaultValues: { name, email },
});

const { refresh } = useUser();
const posthog = usePostHog();

const changeName = trpc.user.changeName.useMutation({
onSuccess: (_, { name }) => {
posthog.people.set({ name });
refresh();
reset({ name, email });
posthog?.people.set({ name });
},
});

Expand All @@ -47,7 +48,6 @@ export const UserDetails: React.FunctionComponent<UserDetailsProps> = ({
if (dirtyFields.name) {
await changeName.mutateAsync({ userId, name: data.name });
}
reset(data);
})}
>
<div className="flex items-center justify-between border-b px-3 py-2 shadow-sm">
Expand Down
7 changes: 4 additions & 3 deletions apps/web/src/utils/posthog.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { usePostHog as usePostHogHook } from "posthog-js/react";

// Seems silly but annoyingly typescript tries to import usePostHog from
// posthog-js/react/dist/types which doesn't even work.
export const usePostHog = usePostHogHook;
export const usePostHog = () => {
const posthog = usePostHogHook();
return process.env.NEXT_PUBLIC_POSTHOG_API_KEY ? posthog : null;
};

0 comments on commit d049e91

Please sign in to comment.