-
Notifications
You must be signed in to change notification settings - Fork 70
Make dashboard terminal team scoped #440
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
Open
matthewlouisbrockman
wants to merge
16
commits into
main
Choose a base branch
from
make-teamterminal-instead-of-just-dashboardterminal-eng-4336
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
8540954
Make dashboard terminal team scoped
matthewlouisbrockman 00015f0
Stop terminal redirect when team lookup fails
matthewlouisbrockman 3353d82
Give team terminal wrapper full height
matthewlouisbrockman a1f97af
Merge remote-tracking branch 'origin/main' into make-teamterminal-ins…
matthewlouisbrockman 2829591
Rename team terminal URL helper
matthewlouisbrockman a4c2767
Move legacy terminal redirect to route handler
matthewlouisbrockman 3ccfbea
Use sandbox template for team terminal
matthewlouisbrockman 3bef5bf
Route legacy dashboard terminal through dashboard resolver
matthewlouisbrockman a93e3b0
Revert "Route legacy dashboard terminal through dashboard resolver"
matthewlouisbrockman 31496a5
Simplify legacy terminal redirect
matthewlouisbrockman a5256b8
Use dashboard tab map for terminal redirect
matthewlouisbrockman a36cfbe
Add legacy terminal path shim
matthewlouisbrockman dde5dd7
pass query params to tab
matthewlouisbrockman 9af0263
style: apply biome formatting
matthewlouisbrockman 578ae01
Format terminal redirect params
matthewlouisbrockman 103c668
Merge remote-tracking branch 'origin/make-teamterminal-instead-of-jus…
matthewlouisbrockman File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,270 @@ | ||
| import Link from 'next/link' | ||
| import type { Metadata } from 'next/types' | ||
| import { authHeaders } from '@/configs/api' | ||
| import { AUTH_URLS, PROTECTED_URLS } from '@/configs/urls' | ||
| import { createUserTeamsRepository } from '@/core/modules/teams/user-teams-repository.server' | ||
| import { | ||
| createDefaultTemplatesRepository, | ||
| createTemplatesRepository, | ||
| } from '@/core/modules/templates/repository.server' | ||
| import { getAuthContext } from '@/core/server/auth' | ||
| import { infra } from '@/core/shared/clients/api' | ||
| import { createSandboxManagementAuth } from '@/core/shared/sandbox-management-auth.server' | ||
| import { SandboxIdSchema } from '@/core/shared/schemas/api' | ||
| import DashboardTerminal from '@/features/dashboard/terminal/dashboard-terminal' | ||
| import { normalizeTerminalTemplate } from '@/features/dashboard/terminal/template' | ||
| import { Button } from '@/ui/primitives/button' | ||
|
|
||
| export const metadata: Metadata = { | ||
| title: 'Terminal - E2B', | ||
| robots: 'noindex, nofollow', | ||
| } | ||
|
|
||
| interface TeamTerminalPageProps { | ||
| params: Promise<{ | ||
| teamSlug: string | ||
| }> | ||
| searchParams: Promise<{ | ||
| command?: string | ||
| sandboxId?: string | ||
| template?: string | ||
| }> | ||
| } | ||
|
|
||
| export default async function TeamTerminalPage({ | ||
| params, | ||
| searchParams, | ||
| }: TeamTerminalPageProps) { | ||
| const [{ teamSlug }, { command = '', sandboxId, template }] = | ||
| await Promise.all([params, searchParams]) | ||
| const terminalTemplate = normalizeTerminalTemplate(template) | ||
| const terminalSandboxId = normalizeTerminalSandboxId(sandboxId) | ||
|
|
||
| if (!terminalTemplate) { | ||
| return <TerminalUnavailable message="The terminal template is invalid." /> | ||
| } | ||
|
|
||
| if (terminalSandboxId === null) { | ||
| return <TerminalUnavailable message="The terminal sandbox ID is invalid." /> | ||
| } | ||
|
|
||
| const authContext = await getAuthContext() | ||
|
|
||
| if (!authContext) { | ||
| return ( | ||
| <TerminalSignIn | ||
| command={command} | ||
| sandboxId={terminalSandboxId} | ||
| teamSlug={teamSlug} | ||
| template={terminalTemplate} | ||
| /> | ||
| ) | ||
| } | ||
|
|
||
| const teamsRepository = createUserTeamsRepository({ | ||
| accessToken: authContext.accessToken, | ||
| }) | ||
| const teamsResult = await teamsRepository.listUserTeams() | ||
|
|
||
| if (!teamsResult.ok) { | ||
| return <TerminalUnavailable /> | ||
| } | ||
|
|
||
| const team = teamsResult.data.find((candidate) => candidate.slug === teamSlug) | ||
|
|
||
| if (!team) { | ||
| return <TerminalUnavailable /> | ||
| } | ||
|
|
||
| if ( | ||
| terminalSandboxId && | ||
| !(await hasSandboxInTeam({ | ||
| accessToken: authContext.accessToken, | ||
| sandboxId: terminalSandboxId, | ||
| teamId: team.id, | ||
| })) | ||
| ) { | ||
| return ( | ||
| <TerminalUnavailable message="Sandbox not found or you do not have access to it." /> | ||
| ) | ||
| } | ||
|
|
||
| const templateAvailable = terminalSandboxId | ||
| ? { ok: true as const, available: true } | ||
| : await isTerminalTemplateAvailable({ | ||
| accessToken: authContext.accessToken, | ||
| teamId: team.id, | ||
| template: terminalTemplate, | ||
| }) | ||
|
|
||
| if (!templateAvailable.ok) { | ||
| return ( | ||
| <TerminalUnavailable message="We could not verify the terminal template for this account." /> | ||
| ) | ||
| } | ||
|
|
||
| if (!templateAvailable.available) { | ||
| return ( | ||
| <TerminalUnavailable | ||
| message={`Template "${terminalTemplate}" is not available for this account.`} | ||
| /> | ||
| ) | ||
| } | ||
|
|
||
| return ( | ||
| <div className="flex min-h-0 flex-1 overflow-hidden p-3 md:p-6"> | ||
| <DashboardTerminal | ||
| autoStart | ||
| launchTarget={{ | ||
| command, | ||
| sandboxId: terminalSandboxId, | ||
| template: terminalTemplate, | ||
| }} | ||
| sandboxManagementAuth={createSandboxManagementAuth( | ||
| authContext, | ||
| team.id | ||
| )} | ||
| teamSlug={team.slug} | ||
| /> | ||
| </div> | ||
| ) | ||
| } | ||
|
|
||
| function normalizeTerminalSandboxId(sandboxId?: string) { | ||
| const value = sandboxId?.trim() | ||
| if (!value) return undefined | ||
|
|
||
| const parsedSandboxId = SandboxIdSchema.safeParse(value) | ||
| return parsedSandboxId.success ? parsedSandboxId.data : null | ||
| } | ||
|
|
||
| async function hasSandboxInTeam({ | ||
| accessToken, | ||
| sandboxId, | ||
| teamId, | ||
| }: { | ||
| accessToken: string | ||
| sandboxId: string | ||
| teamId: string | ||
| }) { | ||
| try { | ||
| const result = await infra.GET('/sandboxes/{sandboxID}', { | ||
| params: { | ||
| path: { | ||
| sandboxID: sandboxId, | ||
| }, | ||
| }, | ||
| headers: { | ||
| ...authHeaders(accessToken, teamId), | ||
| }, | ||
| cache: 'no-store', | ||
| }) | ||
|
|
||
| return result.response.ok && Boolean(result.data) | ||
| } catch { | ||
| return false | ||
| } | ||
| } | ||
|
|
||
| async function isTerminalTemplateAvailable({ | ||
| accessToken, | ||
| teamId, | ||
| template, | ||
| }: { | ||
| accessToken: string | ||
| teamId: string | ||
| template: string | ||
| }) { | ||
| if (template === 'base') { | ||
| return { ok: true as const, available: true } | ||
| } | ||
|
|
||
| const defaultTemplatesRepository = createDefaultTemplatesRepository({ | ||
| accessToken, | ||
| }) | ||
| const teamTemplatesRepository = createTemplatesRepository({ | ||
| accessToken, | ||
| teamId, | ||
| }) | ||
| const [defaultTemplates, teamTemplates] = await Promise.all([ | ||
| defaultTemplatesRepository.getDefaultTemplatesCached(), | ||
| teamTemplatesRepository.getTeamTemplates(), | ||
| ]) | ||
|
|
||
| if (!defaultTemplates.ok || !teamTemplates.ok) { | ||
| return { ok: false as const } | ||
| } | ||
|
|
||
| const templates = [ | ||
| ...defaultTemplates.data.templates, | ||
| ...teamTemplates.data.templates, | ||
| ] | ||
|
|
||
| return { | ||
| ok: true as const, | ||
| available: templates.some((candidate) => | ||
| [ | ||
| candidate.templateID, | ||
| ...(candidate.aliases ?? []), | ||
| ...(candidate.names ?? []), | ||
| ].includes(template) | ||
| ), | ||
| } | ||
| } | ||
|
|
||
| function TerminalSignIn({ | ||
| command, | ||
| sandboxId, | ||
| teamSlug, | ||
| template, | ||
| }: { | ||
| command?: string | ||
| sandboxId?: string | ||
| teamSlug: string | ||
| template: string | ||
| }) { | ||
| const returnToQuery = new URLSearchParams({ | ||
| ...(command ? { command } : {}), | ||
|
matthewlouisbrockman marked this conversation as resolved.
|
||
| ...(sandboxId ? { sandboxId } : {}), | ||
| template, | ||
| }).toString() | ||
| const returnTo = `${PROTECTED_URLS.TEAM_TERMINAL(teamSlug)}${ | ||
| returnToQuery ? `?${returnToQuery}` : '' | ||
| }` | ||
| const signInHref = `${AUTH_URLS.SIGN_IN}?${new URLSearchParams({ | ||
| returnTo, | ||
| }).toString()}` | ||
|
|
||
| return ( | ||
| <main className="flex h-dvh min-h-[360px] items-center justify-center bg-bg p-6"> | ||
| <div className="flex max-w-sm flex-col items-center gap-4 text-center"> | ||
| <div> | ||
| <h1 className="text-lg font-medium">Sign in to open a terminal</h1> | ||
| <p className="text-fg-secondary mt-2 text-sm"> | ||
| The terminal runs in your E2B dashboard account. | ||
| </p> | ||
| </div> | ||
| <Button asChild> | ||
| <Link href={signInHref} target="_top"> | ||
| Sign in | ||
| </Link> | ||
| </Button> | ||
| </div> | ||
| </main> | ||
| ) | ||
| } | ||
|
|
||
| function TerminalUnavailable({ | ||
| message = 'We could not resolve a dashboard team for this account.', | ||
| }: { | ||
| message?: string | ||
| }) { | ||
| return ( | ||
| <main className="flex h-dvh min-h-[360px] items-center justify-center bg-bg p-6"> | ||
| <div className="max-w-sm text-center"> | ||
| <h1 className="text-lg font-medium">Terminal unavailable</h1> | ||
| <p className="text-fg-secondary mt-2 text-sm">{message}</p> | ||
| </div> | ||
| </main> | ||
| ) | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.