Skip to content

Commit

Permalink
Intercom user id (#675)
Browse files Browse the repository at this point in the history
  • Loading branch information
macjuul authored Feb 6, 2025
1 parent ec65eb7 commit b5d9ac5
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 8 deletions.
36 changes: 29 additions & 7 deletions src/components/App/hooks/intercom.tsx
Original file line number Diff line number Diff line change
@@ -1,19 +1,41 @@
import { Intercom } from "@intercom/messenger-js-sdk";
import { Intercom, update } from "@intercom/messenger-js-sdk";
import { useEffect, useMemo, useRef } from "react";
import { useLocation } from "wouter";
import { useCloudStore } from "~/stores/cloud";

export function useIntercom() {
const [location] = useLocation();
const initialize = useRef(true);

const authState = useCloudStore((s) => s.authState);
const profile = useCloudStore((s) => s.profile);
const userId = useCloudStore((s) => s.userId);

const isReady = authState !== "unknown" && authState !== "loading";

if (isReady && !import.meta.env.DEV) {
Intercom({
app_id: import.meta.env.VITE_INTERCOM_APP_ID,
user_id: profile.username || undefined,
const metadata = useMemo(
() => ({
user_id: userId || undefined,
name: profile.name || undefined,
avatar: profile.picture || undefined,
user_hash: profile.user_hmac || undefined,
});
}
}),
[profile, userId],
);

// biome-ignore lint/correctness/useExhaustiveDependencies: Track location change
useEffect(() => {
if (!isReady) return;

if (initialize.current) {
Intercom({
app_id: import.meta.env.VITE_INTERCOM_APP_ID,
...metadata,
});

initialize.current = false;
} else {
update(metadata);
}
}, [isReady, location, metadata]);
}
5 changes: 4 additions & 1 deletion src/screens/surrealist/cloud-panel/api/auth.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,8 @@ export async function refreshAccess() {
*/
export async function acquireSession(accessToken: string) {
try {
const { setSessionToken, setAuthProvider, setSessionExpired } = useCloudStore.getState();
const { setSessionToken, setAuthProvider, setUserId, setSessionExpired } =
useCloudStore.getState();
const referralCode = sessionStorage.getItem(REFERRER_KEY);

adapter.log("Cloud", "Acquiring cloud session");
Expand All @@ -222,6 +223,8 @@ export async function acquireSession(accessToken: string) {

setSessionToken(result.token);
setAuthProvider(result.provider);
setUserId(result.id);

await updateCloudInformation();

adapter.log("Cloud", `Session acquired`);
Expand Down
1 change: 1 addition & 0 deletions src/screens/surrealist/views/query/ResultPane/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ export function ResultPane({ activeTab, selection, editor, corners }: ResultPane
variant="light"
size="xs"
radius="sm"
color="slate"
leftSection={<Icon path={iconUpload} />}
disabled={!canExport}
>
Expand Down
8 changes: 8 additions & 0 deletions src/stores/cloud.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export type CloudStore = {
authState: AuthState;
sessionToken: string;
authProvider: string;
userId: string;
isSupported: boolean;
profile: CloudProfile;
instanceVersions: string[];
Expand All @@ -48,6 +49,7 @@ export type CloudStore = {

setLoading: () => void;
setSessionToken: (token: string) => void;
setUserId: (id: string) => void;
setAuthProvider: (provider: string) => void;
setAccountProfile: (profile: CloudProfile) => void;
setIsSupported: (supported: boolean) => void;
Expand All @@ -67,6 +69,7 @@ export const useCloudStore = create<CloudStore>()(
immer((set) => ({
authState: "unknown",
sessionToken: "",
userId: "",
authProvider: "",
isSupported: true,
profile: EMPTY_PROFILE,
Expand All @@ -89,6 +92,11 @@ export const useCloudStore = create<CloudStore>()(
sessionToken: token,
}),

setUserId: (id) =>
set({
userId: id,
}),

setAuthProvider: (provider) =>
set({
authProvider: provider,
Expand Down
1 change: 1 addition & 0 deletions src/types.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -489,6 +489,7 @@ export interface Driver {
}

export interface CloudSignin {
id: string;
token: string;
provider: string;
terms_accepted_at?: string;
Expand Down

0 comments on commit b5d9ac5

Please sign in to comment.