Skip to content

armgit / E2B

Public Imported from GitHub
0

Open-source, secure environment with real-world tools for enterprise-grade agents.

Assign the name for teams in posthog analytics

Jakub Novak43b1c6fd51d2
diff --git a/apps/docs/src/utils/usePostHog.tsx b/apps/docs/src/utils/usePostHog.tsx
index fcba9d0cc..ff1653315 100644
--- a/apps/docs/src/utils/usePostHog.tsx
+++ b/apps/docs/src/utils/usePostHog.tsx
@@ -40,7 +40,7 @@ export function PostHogAnalytics() {
const pathname = usePathname()
const searchParams = useSearchParams()
const { user } = useUser()
- const defaultTeamId = user?.teams?.[0]?.team_id
+ const defaultTeamId = user?.defaultTeamId
useEffect(() => {
if (pathname) {
let url = window.origin + pathname
@@ -52,7 +52,8 @@ export function PostHogAnalytics() {
useEffect(() => {
if (user) {
posthog?.identify(user.id, { email: user.email })
- posthog?.group('team', defaultTeamId, { name: user?.teams?.[0]?.name })
+ // TODO: When we add teams, make sure to change this
+ posthog?.group('team', defaultTeamId, {name: user?.teams?.[0]?.name })
}
}, [user, defaultTeamId])
diff --git a/apps/docs/src/utils/useUser.tsx b/apps/docs/src/utils/useUser.tsx
index b2be0177f..34cb4fd51 100644
--- a/apps/docs/src/utils/useUser.tsx
+++ b/apps/docs/src/utils/useUser.tsx
@@ -7,12 +7,18 @@ import { type Session } from '@supabase/supabase-js'
import * as Sentry from '@sentry/nextjs'
import { useSandboxStore } from './useSandbox'
+type Team = {
+ id: string,
+ name: string,
+ is_default: boolean
+}
+
type UserContextType = {
isLoading: boolean;
session: Session | null;
user:
| (User & {
- teams: any[];
+ teams: Team[];
apiKeys: any[];
accessToken: string;
defaultTeamId: string;
@@ -21,6 +27,7 @@ type UserContextType = {
error: Error | null;
};
+
export const UserContext = createContext(undefined)
export const CustomUserContextProvider = (props) => {
@@ -85,16 +92,20 @@ export const CustomUserContextProvider = (props) => {
useEffect(() => {
async function getUserCustom() {
// @ts-ignore
- const { data: teams, teamsError } = await supabase
+ const { data: userTeams, teamsError } = await supabase
.from('users_teams')
- .select('*')
+ .select('teams (id, name, is_default)')
.eq('user_id', session?.user.id) // Due to RLS, we could also safely just fetch all, but let's be explicit for sure
if (teamsError) Sentry.captureException(teamsError)
+ // TODO: Adjust when user can be part of multiple teams
+ // @ts-ignore
+ const teams = userTeams?.map(userTeam=> userTeam.teams as Team)
+ const defaultTeam= teams?.filter(team => team.is_default)?.[0]
const { data: apiKeys, error: apiKeysError } = await supabase
.from('team_api_keys')
.select('*')
- .in('team_id', teams?.map((team) => team.team_id)) // Due to RLS, we could also safely just fetch all, but let's be explicit for sure
+ .in('team_id', teams?.map((team) => team.id)) // Due to RLS, we could also safely just fetch all, but let's be explicit for sure
if (apiKeysError) Sentry.captureException(apiKeysError)
// as soon as we have apiKey, start initializing sandboxes, so they are ready when user want to use them
@@ -104,7 +115,7 @@ export const CustomUserContextProvider = (props) => {
void initSandbox(apiKey)
}
- const defaultTeamId = teams?.[0]?.team_id // TODO: Adjust when user can be part of multiple teams
+ const defaultTeamId = defaultTeam?.id // TODO: Adjust when user can be part of multiple teams
const { data: accessToken, error: accessTokenError } = await supabase
.from('access_tokens')