From 5d5508c18c27e07ac0ea70c99bf6b633bdae1ecc Mon Sep 17 00:00:00 2001 From: Nick Zelei <2420177+nickzelei@users.noreply.github.com> Date: Tue, 28 Nov 2023 11:05:52 -0800 Subject: [PATCH] adds system app config for FE env vars (#679) --- frontend/app/api/config/route.ts | 15 +++++++++++++++ frontend/app/config/app-config.ts | 4 ++++ frontend/app/settings/components/InviteTable.tsx | 7 ++++--- .../app/settings/components/InviteUserForm.tsx | 9 +++++++-- frontend/libs/hooks/useGetInviteLink.ts | 3 --- frontend/libs/hooks/useGetSystemAppConfig.ts | 7 +++++++ 6 files changed, 37 insertions(+), 8 deletions(-) create mode 100644 frontend/app/api/config/route.ts create mode 100644 frontend/app/config/app-config.ts delete mode 100644 frontend/libs/hooks/useGetInviteLink.ts create mode 100644 frontend/libs/hooks/useGetSystemAppConfig.ts diff --git a/frontend/app/api/config/route.ts b/frontend/app/api/config/route.ts new file mode 100644 index 0000000000..90ac98bae6 --- /dev/null +++ b/frontend/app/api/config/route.ts @@ -0,0 +1,15 @@ +import { isAuthEnabled } from '@/api-only/auth-config'; +import { withNeosyncContext } from '@/api-only/neosync-context'; +import { SystemAppConfig } from '@/app/config/app-config'; +import { NextRequest, NextResponse } from 'next/server'; + +export async function GET(req: NextRequest): Promise { + return withNeosyncContext(async () => { + const sysConfig: SystemAppConfig = { + isAuthEnabled: isAuthEnabled(), + publicAppBaseUrl: + process.env.NEXT_PUBLIC_APP_BASE_URL ?? 'http://localhost:3000', + }; + return sysConfig; + })(req); +} diff --git a/frontend/app/config/app-config.ts b/frontend/app/config/app-config.ts new file mode 100644 index 0000000000..dbd3c60249 --- /dev/null +++ b/frontend/app/config/app-config.ts @@ -0,0 +1,4 @@ +export interface SystemAppConfig { + isAuthEnabled: boolean; + publicAppBaseUrl: string; +} diff --git a/frontend/app/settings/components/InviteTable.tsx b/frontend/app/settings/components/InviteTable.tsx index 49a63de851..726df0e242 100644 --- a/frontend/app/settings/components/InviteTable.tsx +++ b/frontend/app/settings/components/InviteTable.tsx @@ -29,12 +29,12 @@ import { } from '@/components/ui/table'; import { useToast } from '@/components/ui/use-toast'; import { useGetAccountInvites } from '@/libs/hooks/useGetAccountInvites'; -import { useGetInviteLink } from '@/libs/hooks/useGetInviteLink'; +import { useGetSystemAppConfig } from '@/libs/hooks/useGetSystemAppConfig'; import { AccountInvite } from '@/neosync-api-client/mgmt/v1alpha1/user_account_pb'; import { formatDateTime, getErrorMessage } from '@/util/util'; import { PlainMessage, Timestamp } from '@bufbuild/protobuf'; import { TrashIcon } from '@radix-ui/react-icons'; -import InviteUserForm from './InviteUserForm'; +import InviteUserForm, { buildInviteLink } from './InviteUserForm'; interface ColumnProps { onDeleted(id: string): void; @@ -269,7 +269,8 @@ interface CopyInviteButtonProps { } function CopyInviteButton({ token }: CopyInviteButtonProps) { - const link = useGetInviteLink(token); + const { data: systemAppData } = useGetSystemAppConfig(); + const link = buildInviteLink(systemAppData?.publicAppBaseUrl ?? '', token); return ( @@ -207,3 +208,7 @@ async function inviteUserToTeamAccount( } return InviteUserToTeamAccountResponse.fromJson(await res.json()); } + +export function buildInviteLink(baseUrl: string, token: string): string { + return `${baseUrl}/invite?token=${token}`; +} diff --git a/frontend/libs/hooks/useGetInviteLink.ts b/frontend/libs/hooks/useGetInviteLink.ts deleted file mode 100644 index 7d67d7f48e..0000000000 --- a/frontend/libs/hooks/useGetInviteLink.ts +++ /dev/null @@ -1,3 +0,0 @@ -export function useGetInviteLink(token: string): string { - return `${process.env.NEXT_PUBLIC_APP_BASE_URL}/invite?token=${token}`; -} diff --git a/frontend/libs/hooks/useGetSystemAppConfig.ts b/frontend/libs/hooks/useGetSystemAppConfig.ts new file mode 100644 index 0000000000..509731236d --- /dev/null +++ b/frontend/libs/hooks/useGetSystemAppConfig.ts @@ -0,0 +1,7 @@ +import type { SystemAppConfig } from '@/app/config/app-config'; +import type { HookReply } from './types'; +import { useNucleusAuthenticatedFetch } from './useNucleusAuthenticatedFetch'; + +export function useGetSystemAppConfig(): HookReply { + return useNucleusAuthenticatedFetch(`/api/config`); +}