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

Intercom user id #675

Merged
merged 1 commit into from
Feb 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading