-
Notifications
You must be signed in to change notification settings - Fork 907
refactor(desktop): rewrite v1→v2 migration as pull-based importer #4122
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
8cd8364
refactor(desktop): rewrite v1→v2 migration as pull-based importer
saddlepaddle 4fbe22e
refactor(desktop): harden v1→v2 importer for forks, ghosts, and reloc…
saddlepaddle 92ab944
fix(host-service): restore local-DB short-circuit in findByPath
saddlepaddle 08c053c
refactor(host-service): gate findByPath multi-remote walk behind opt-…
saddlepaddle f4e4986
fix(desktop): address PR review nits on v1→v2 importer
saddlepaddle d2f176a
fix(desktop): more PR review nits on v1→v2 importer
saddlepaddle 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
103 changes: 103 additions & 0 deletions
103
...rc/renderer/routes/_authenticated/_dashboard/components/V1ImportBanner/V1ImportBanner.tsx
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,103 @@ | ||
| import { Button } from "@superset/ui/button"; | ||
| import { useEffect, useState } from "react"; | ||
| import { LuArrowRight, LuX } from "react-icons/lu"; | ||
| import { env } from "renderer/env.renderer"; | ||
| import { useIsV2CloudEnabled } from "renderer/hooks/useIsV2CloudEnabled"; | ||
| import { authClient } from "renderer/lib/auth-client"; | ||
| import { electronTrpc } from "renderer/lib/electron-trpc"; | ||
| import { useOpenV1ImportModal } from "renderer/stores/v1-import-modal"; | ||
| import { MOCK_ORG_ID } from "shared/constants"; | ||
|
|
||
| const DISMISS_SESSION_KEY_PREFIX = "v1-import-banner-dismissed"; | ||
|
|
||
| function dismissKey(organizationId: string): string { | ||
| return `${DISMISS_SESSION_KEY_PREFIX}:${organizationId}`; | ||
| } | ||
|
|
||
| function readDismissed(organizationId: string | null): boolean { | ||
| if (!organizationId || typeof window === "undefined") return false; | ||
| return sessionStorage.getItem(dismissKey(organizationId)) === "1"; | ||
| } | ||
|
|
||
| export function V1ImportBanner() { | ||
| const { data: session } = authClient.useSession(); | ||
| const isV2CloudEnabled = useIsV2CloudEnabled(); | ||
| const organizationId = env.SKIP_ENV_VALIDATION | ||
| ? MOCK_ORG_ID | ||
| : (session?.session?.activeOrganizationId ?? null); | ||
| const openModal = useOpenV1ImportModal(); | ||
| const [dismissed, setDismissed] = useState(() => | ||
| readDismissed(organizationId), | ||
| ); | ||
|
|
||
| // Re-sync local state when the active org changes — dismissal is per | ||
| // org, so flipping orgs should reveal the banner again if it hasn't | ||
| // been dismissed there yet. | ||
| useEffect(() => { | ||
| setDismissed(readDismissed(organizationId)); | ||
| }, [organizationId]); | ||
|
|
||
| const projectsQuery = electronTrpc.migration.readV1Projects.useQuery( | ||
| undefined, | ||
| { enabled: isV2CloudEnabled && !!organizationId && !dismissed }, | ||
| ); | ||
| const auditQuery = electronTrpc.migration.listState.useQuery( | ||
| { organizationId: organizationId ?? "" }, | ||
| { enabled: isV2CloudEnabled && !!organizationId && !dismissed }, | ||
| ); | ||
|
|
||
| if (!isV2CloudEnabled || !organizationId || dismissed) return null; | ||
|
|
||
| const projects = projectsQuery.data ?? []; | ||
| const importedV1Ids = new Set( | ||
| (auditQuery.data ?? []) | ||
| .filter( | ||
| (row) => | ||
| row.kind === "project" && | ||
| (row.status === "success" || row.status === "linked"), | ||
| ) | ||
| .map((row) => row.v1Id), | ||
| ); | ||
| const remaining = projects.filter((p) => !importedV1Ids.has(p.id)).length; | ||
|
|
||
| if (remaining === 0) return null; | ||
|
|
||
| const dismiss = () => { | ||
| if (organizationId) { | ||
| sessionStorage.setItem(dismissKey(organizationId), "1"); | ||
| } | ||
| setDismissed(true); | ||
| }; | ||
|
|
||
| return ( | ||
| <div className="flex items-center gap-3 border-b bg-muted/30 px-5 py-2"> | ||
| <div className="flex-1 text-sm text-foreground"> | ||
| You have{" "} | ||
| <span className="font-medium"> | ||
| {remaining} v1 project{remaining === 1 ? "" : "s"} | ||
| </span>{" "} | ||
| you can bring over to v2. | ||
| </div> | ||
| <Button | ||
| type="button" | ||
| size="sm" | ||
| variant="outline" | ||
| onClick={() => openModal()} | ||
| className="gap-1.5" | ||
| > | ||
| Import from v1 | ||
| <LuArrowRight className="size-3.5" strokeWidth={2} /> | ||
| </Button> | ||
| <Button | ||
| type="button" | ||
| size="icon" | ||
| variant="ghost" | ||
| onClick={dismiss} | ||
| aria-label="Dismiss" | ||
| className="h-7 w-7" | ||
| > | ||
| <LuX className="size-3.5" strokeWidth={2} /> | ||
| </Button> | ||
| </div> | ||
| ); | ||
| } |
1 change: 1 addition & 0 deletions
1
.../desktop/src/renderer/routes/_authenticated/_dashboard/components/V1ImportBanner/index.ts
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 @@ | ||
| export { V1ImportBanner } from "./V1ImportBanner"; |
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
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
188 changes: 188 additions & 0 deletions
188
...er/routes/_authenticated/components/V1ImportModal/ImportPresetsPage/ImportPresetsPage.tsx
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,188 @@ | ||
| import type { TerminalPreset } from "@superset/local-db"; | ||
| import { | ||
| AGENT_LABELS, | ||
| AGENT_TYPES, | ||
| type AgentType, | ||
| } from "@superset/shared/agent-command"; | ||
| import { useState } from "react"; | ||
| import { LuTerminal } from "react-icons/lu"; | ||
| import { electronTrpc } from "renderer/lib/electron-trpc"; | ||
| import { useCollections } from "renderer/routes/_authenticated/providers/CollectionsProvider"; | ||
| import type { V2TerminalPresetRow } from "renderer/routes/_authenticated/providers/CollectionsProvider/dashboardSidebarLocal"; | ||
| import { ImportPageShell } from "../components/ImportPageShell"; | ||
| import { ImportRow, type RowAction } from "../components/ImportRow"; | ||
|
|
||
| interface ImportPresetsPageProps { | ||
| organizationId: string; | ||
| } | ||
|
|
||
| interface AuditLogEntry { | ||
| v2Id: string | null; | ||
| status: string; | ||
| reason: string | null; | ||
| } | ||
|
|
||
| const BUILTIN_AGENT_IDS = new Set<string>(AGENT_TYPES); | ||
|
|
||
| export function ImportPresetsPage({ organizationId }: ImportPresetsPageProps) { | ||
| const presetsQuery = electronTrpc.settings.getTerminalPresets.useQuery(); | ||
| const auditQuery = electronTrpc.migration.listState.useQuery({ | ||
| organizationId, | ||
| }); | ||
| const [isRefreshing, setIsRefreshing] = useState(false); | ||
|
|
||
| const isLoading = presetsQuery.isPending || auditQuery.isPending; | ||
| const presets = presetsQuery.data ?? []; | ||
|
|
||
| const auditByV1Id = new Map<string, AuditLogEntry>(); | ||
| for (const row of auditQuery.data ?? []) { | ||
| if (row.kind !== "preset") continue; | ||
| auditByV1Id.set(row.v1Id, { | ||
| v2Id: row.v2Id, | ||
| status: row.status, | ||
| reason: row.reason, | ||
| }); | ||
| } | ||
|
|
||
| const refresh = async () => { | ||
| setIsRefreshing(true); | ||
| try { | ||
| await Promise.all([presetsQuery.refetch(), auditQuery.refetch()]); | ||
| } finally { | ||
| setIsRefreshing(false); | ||
| } | ||
| }; | ||
|
|
||
| return ( | ||
| <ImportPageShell | ||
| title="Bring over your terminal presets" | ||
| description="Import each v1 terminal preset into v2." | ||
| isLoading={isLoading} | ||
| itemCount={presets.length} | ||
| emptyMessage="No v1 terminal presets found." | ||
| onRefresh={refresh} | ||
| isRefreshing={isRefreshing} | ||
| > | ||
| {presets.map((preset, index) => ( | ||
| <PresetRow | ||
| key={preset.id} | ||
| preset={preset} | ||
| tabOrder={index} | ||
| audit={auditByV1Id.get(preset.id)} | ||
| organizationId={organizationId} | ||
| /> | ||
| ))} | ||
| </ImportPageShell> | ||
| ); | ||
| } | ||
|
|
||
| interface PresetRowProps { | ||
| preset: TerminalPreset; | ||
| tabOrder: number; | ||
| audit: AuditLogEntry | undefined; | ||
| organizationId: string; | ||
| } | ||
|
|
||
| function PresetRow({ | ||
| preset, | ||
| tabOrder, | ||
| audit, | ||
| organizationId, | ||
| }: PresetRowProps) { | ||
| const collections = useCollections(); | ||
| const upsertState = electronTrpc.migration.upsertState.useMutation(); | ||
| const trpcUtils = electronTrpc.useUtils(); | ||
| const [running, setRunning] = useState(false); | ||
| const [errorMessage, setErrorMessage] = useState<string | null>(null); | ||
|
|
||
| const auditImported = audit !== undefined && audit.status === "success"; | ||
| const auditError = | ||
| audit !== undefined && audit.status === "error" ? audit.reason : null; | ||
|
|
||
| const runImport = async () => { | ||
| setRunning(true); | ||
| setErrorMessage(null); | ||
| try { | ||
| const linkedAgentId: AgentType | undefined = BUILTIN_AGENT_IDS.has( | ||
| preset.name, | ||
| ) | ||
| ? (preset.name as AgentType) | ||
| : undefined; | ||
|
|
||
| // Reuse the audit row's v2Id when present so a retry after a | ||
| // partial failure (insert succeeded, audit upsert failed) doesn't | ||
| // create a duplicate v2 preset row. Insert is upsert-by-id, so | ||
| // re-running with the same id is a no-op. | ||
| const v2Id = audit?.v2Id ?? crypto.randomUUID(); | ||
| const row: V2TerminalPresetRow = { | ||
| id: v2Id, | ||
| name: linkedAgentId ? AGENT_LABELS[linkedAgentId] : preset.name, | ||
| description: preset.description, | ||
| cwd: preset.cwd, | ||
| commands: preset.commands, | ||
| projectIds: preset.projectIds ?? null, | ||
| pinnedToBar: preset.pinnedToBar, | ||
| applyOnWorkspaceCreated: preset.applyOnWorkspaceCreated, | ||
| applyOnNewTab: preset.applyOnNewTab, | ||
| executionMode: preset.executionMode ?? "new-tab", | ||
| tabOrder, | ||
| createdAt: new Date(), | ||
| agentId: linkedAgentId, | ||
| }; | ||
| collections.v2TerminalPresets.insert(row); | ||
|
|
||
| await upsertState.mutateAsync({ | ||
| v1Id: preset.id, | ||
| kind: "preset", | ||
| v2Id, | ||
| organizationId, | ||
| status: "success", | ||
| reason: null, | ||
| }); | ||
|
|
||
| await trpcUtils.migration.listState.invalidate({ organizationId }); | ||
| } catch (err) { | ||
| const message = err instanceof Error ? err.message : String(err); | ||
| setErrorMessage(message); | ||
| await upsertState | ||
| .mutateAsync({ | ||
| v1Id: preset.id, | ||
| kind: "preset", | ||
| v2Id: null, | ||
| organizationId, | ||
| status: "error", | ||
| reason: message, | ||
| }) | ||
| .catch((auditErr) => { | ||
| console.warn( | ||
| "[v1-import] failed to record preset import error in audit", | ||
| { presetId: preset.id, auditErr }, | ||
| ); | ||
| }); | ||
| await trpcUtils.migration.listState.invalidate({ organizationId }); | ||
| } finally { | ||
| setRunning(false); | ||
| } | ||
| }; | ||
|
|
||
| const action: RowAction = (() => { | ||
| if (running) return { kind: "running" }; | ||
| if (auditImported) return { kind: "imported" }; | ||
| if (errorMessage) { | ||
| return { kind: "error", message: errorMessage, onRetry: runImport }; | ||
| } | ||
| if (auditError) { | ||
| return { kind: "error", message: auditError, onRetry: runImport }; | ||
| } | ||
| return { kind: "ready", label: "Import", onClick: runImport }; | ||
| })(); | ||
|
|
||
| return ( | ||
| <ImportRow | ||
| icon={<LuTerminal className="size-3.5" strokeWidth={2} />} | ||
| primary={preset.name} | ||
| secondary={preset.description ?? preset.commands[0]} | ||
| action={action} | ||
| /> | ||
| ); | ||
| } | ||
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.