Skip to content

Commit f2def82

Browse files
committed
Add getPostHogUrl util for region-aware URLs
1 parent c5c3eba commit f2def82

6 files changed

Lines changed: 28 additions & 38 deletions

File tree

apps/code/src/renderer/features/billing/stores/seatStore.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { useAuthStore } from "@features/auth/stores/authStore";
2-
import { getCloudUrlFromRegion } from "@shared/constants/oauth";
2+
import { getPostHogUrl } from "@shared/utils/urls";
33
import type { SeatData } from "@shared/types/seat";
44
import { PLAN_FREE, PLAN_PRO } from "@shared/types/seat";
55
import { electronStorage } from "@utils/electronStorage";
@@ -52,9 +52,7 @@ function parseFetcherError(
5252
}
5353

5454
function getBillingUrl(): string {
55-
const region = useAuthStore.getState().cloudRegion;
56-
const base = region ? getCloudUrlFromRegion(region) : "http://localhost:8010";
57-
return `${base}/organization/billing`;
55+
return getPostHogUrl("/organization/billing");
5856
}
5957

6058
function handleSeatError(

apps/code/src/renderer/features/git-interaction/hooks/useGitInteraction.ts

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import {
1717
import { invalidateGitBranchQueries } from "@features/git-interaction/utils/gitCacheKeys";
1818
import { updateGitCacheFromSnapshot } from "@features/git-interaction/utils/updateGitCache";
1919
import { trpc, trpcClient } from "@renderer/trpc";
20+
import { getCloudUrlFromRegion } from "@shared/constants/oauth";
2021
import { ANALYTICS_EVENTS } from "@shared/types/analytics";
2122
import { useQueryClient } from "@tanstack/react-query";
2223
import { track } from "@utils/analytics";
@@ -137,10 +138,7 @@ export function useGitInteraction(
137138
const cloudRegion = authState.cloudRegion;
138139
if (!apiKey || !cloudRegion) return;
139140

140-
const apiHost =
141-
cloudRegion === "eu"
142-
? "https://eu.posthog.com"
143-
: "https://us.posthog.com";
141+
const apiHost = getCloudUrlFromRegion(cloudRegion);
144142

145143
modal.setIsGeneratingPr(true);
146144
try {
@@ -213,10 +211,7 @@ export function useGitInteraction(
213211
return;
214212
}
215213

216-
const apiHost =
217-
cloudRegion === "eu"
218-
? "https://eu.posthog.com"
219-
: "https://us.posthog.com";
214+
const apiHost = getCloudUrlFromRegion(cloudRegion);
220215

221216
try {
222217
const generated = await trpcClient.git.generateCommitMessage.mutate({
@@ -400,10 +395,7 @@ export function useGitInteraction(
400395
return;
401396
}
402397

403-
const apiHost =
404-
cloudRegion === "eu"
405-
? "https://eu.posthog.com"
406-
: "https://us.posthog.com";
398+
const apiHost = getCloudUrlFromRegion(cloudRegion);
407399

408400
modal.setIsGeneratingCommitMessage(true);
409401
modal.setCommitError(null);
@@ -445,10 +437,7 @@ export function useGitInteraction(
445437
return;
446438
}
447439

448-
const apiHost =
449-
cloudRegion === "eu"
450-
? "https://eu.posthog.com"
451-
: "https://us.posthog.com";
440+
const apiHost = getCloudUrlFromRegion(cloudRegion);
452441

453442
modal.setIsGeneratingPr(true);
454443
modal.setPrError(null);

apps/code/src/renderer/features/settings/components/sections/GeneralSettings.tsx

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import {
2020
TextField,
2121
} from "@radix-ui/themes";
2222
import { useTRPC } from "@renderer/trpc";
23-
import { getCloudUrlFromRegion } from "@shared/constants/oauth";
23+
import { getPostHogUrl } from "@shared/utils/urls";
2424
import { ANALYTICS_EVENTS } from "@shared/types/analytics";
2525
import { useSettingsStore as useTerminalSettingsStore } from "@stores/settingsStore";
2626
import type { ThemePreference } from "@stores/themeStore";
@@ -50,7 +50,7 @@ const TERMINAL_FONT_PRESETS = [
5050

5151
export function GeneralSettings() {
5252
const trpcReact = useTRPC();
53-
const { isAuthenticated, cloudRegion } = useAuthStore();
53+
const { isAuthenticated } = useAuthStore();
5454

5555
// Appearance state
5656
const theme = useThemeStore((state) => state.theme);
@@ -363,13 +363,11 @@ export function GeneralSettings() {
363363
? terminalFontFamily
364364
: CUSTOM_TERMINAL_FONT_VALUE;
365365

366-
const accountUrl = cloudRegion
367-
? `${getCloudUrlFromRegion(cloudRegion)}/settings/user`
368-
: null;
366+
const accountUrl = getPostHogUrl("/settings/user");
369367

370368
return (
371369
<Flex direction="column">
372-
{isAuthenticated && accountUrl && (
370+
{isAuthenticated && (
373371
<SettingRow
374372
label="Manage Account"
375373
description="Manage your account and billing on PostHog"
@@ -697,13 +695,11 @@ export function GeneralSettings() {
697695
}
698696

699697
function HedgehogDescription() {
700-
const cloudRegion = useAuthStore((s) => s.cloudRegion);
701698
const projectId = useAuthStore((s) => s.projectId);
702699

703-
const customizeUrl =
704-
cloudRegion && projectId
705-
? `${getCloudUrlFromRegion(cloudRegion)}/project/${projectId}/settings/user-customization`
706-
: null;
700+
const customizeUrl = projectId
701+
? getPostHogUrl(`/project/${projectId}/settings/user-customization`)
702+
: null;
707703

708704
return (
709705
<Flex direction="column" gap="1">

apps/code/src/renderer/features/settings/components/sections/PlanUsageSettings.tsx

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { useAuthStore } from "@features/auth/stores/authStore";
21
import { useSeatStore } from "@features/billing/stores/seatStore";
32
import { useSeat } from "@hooks/useSeat";
43
import {
@@ -14,7 +13,7 @@ import {
1413
Spinner,
1514
Text,
1615
} from "@radix-ui/themes";
17-
import { getCloudUrlFromRegion } from "@shared/constants/oauth";
16+
import { getPostHogUrl } from "@shared/utils/urls";
1817

1918
export function PlanUsageSettings() {
2019
const {
@@ -272,11 +271,8 @@ export function PlanUsageSettings() {
272271
size="1"
273272
variant="outline"
274273
onClick={() => {
275-
const region = useAuthStore.getState().cloudRegion;
276-
const base = region
277-
? getCloudUrlFromRegion(region)
278-
: "http://localhost:8010";
279-
window.open(`${base}/organization/billing`, "_blank");
274+
const url = getPostHogUrl("/organization/billing");
275+
window.open(url, "_blank");
280276
}}
281277
>
282278
Open

apps/code/src/shared/constants/oauth.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ export function getCloudUrlFromRegion(region: CloudRegion): string {
6464
}
6565
}
6666

67+
6768
export function getOauthClientIdFromRegion(region: CloudRegion): string {
6869
switch (region) {
6970
case "us":

apps/code/src/shared/utils/urls.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { useAuthStore } from "@features/auth/stores/authStore";
2+
import { getCloudUrlFromRegion } from "@shared/constants/oauth";
3+
4+
export function getPostHogUrl(path: string): string {
5+
const region = useAuthStore.getState().cloudRegion;
6+
const base = region
7+
? getCloudUrlFromRegion(region)
8+
: "http://localhost:8010";
9+
return `${base}${path.startsWith("/") ? path : `/${path}`}`;
10+
}

0 commit comments

Comments
 (0)