From 15922be0602de732d9ff1869e211ac24368747c2 Mon Sep 17 00:00:00 2001 From: Willie Date: Wed, 7 Oct 2026 00:04:57 +0800 Subject: [PATCH] refactor(ui): share one reviewed-write dialog between Pi and OMP extensions The Pi and Oh My Pi extension tabs each carried a ReviewDialog with the same flow, nearly line for line: preview once, apply only that plan's revision, show busy and stale refusals, and offer "review again" when the server reports the plan stale. A third target of this kind would have added a third copy. ExtensionsReviewDialog now owns that flow and the dialog frame. Each target supplies its plan rows, texts and endpoints, so the two protocols stay as they were: Pi previews without a revision, OMP previews against the view's revision, and both apply the previewed plan's revision. Hooks, MCP and Plugins use different protocols and are left alone. Refs #447 --- .../targets/ExtensionsReviewDialog.tsx | 78 ++++++++++++ .../targets/TargetOmpExtensions.tsx | 110 ++++++----------- .../components/targets/TargetPiExtensions.tsx | 116 ++++++------------ 3 files changed, 158 insertions(+), 146 deletions(-) create mode 100644 ui/src/components/targets/ExtensionsReviewDialog.tsx diff --git a/ui/src/components/targets/ExtensionsReviewDialog.tsx b/ui/src/components/targets/ExtensionsReviewDialog.tsx new file mode 100644 index 000000000..8e03ab298 --- /dev/null +++ b/ui/src/components/targets/ExtensionsReviewDialog.tsx @@ -0,0 +1,78 @@ +import { useState } from 'react'; +import type { ReactNode } from 'react'; +import { useQuery, useQueryClient, type QueryKey } from '@tanstack/react-query'; +import { AlertCircle, X } from 'lucide-react'; +import { ApiError } from '../../api/client'; +import { invalidate } from '../../lib/queryEvents'; +import { useT } from '../../i18n'; +import Button from '../Button'; +import DialogShell from '../DialogShell'; + +/** + * The reviewed write of a Pi or Oh My Pi target's extension settings: preview once, apply only that + * plan's revision, and send the user back to review again when the server says the plan is stale. + * How a preview is asked for (with or without the view's revision) is the target's own protocol. + */ +export default function ExtensionsReviewDialog({ kind, name, text, previewKey, preview: fetchPreview, apply: applyPlan, children, onClose, onApplied }: { + kind: 'pi' | 'omp'; + name: string; + text: { title: string; subtitle: string; loading: string; apply: string; reviewAgain: string; stale: string; busy: string }; + previewKey: QueryKey; + preview: () => Promise; + apply: (revision: string) => Promise; + /** What the plan would write, above the failure note. */ + children: (plan: Plan) => ReactNode; + onClose: () => void; + onApplied: (plan: Plan) => void; +}) { + const t = useT(); + const queryClient = useQueryClient(); + const preview = useQuery({ queryKey: previewKey, queryFn: fetchPreview, retry: false, gcTime: 0, staleTime: Infinity }); + const [applying, setApplying] = useState(false); + const [error, setError] = useState(null); + const plan = preview.data; + const apply = async () => { + if (!plan) return; + setApplying(true); + setError(null); + try { + const done = await applyPlan(plan.revision); + await invalidate(queryClient, `${kind}ExtensionsChanged`, name); + onApplied(done); + } catch (err) { + setError(err as Error); + setApplying(false); + } + }; + const failure = error ?? preview.error; + const code = failure instanceof ApiError ? failure.code : undefined; + const stale = code === `${kind}_extensions_stale`; + const busy = code === `${kind}_extensions_busy`; + return ( + +
+
+

{text.title}

+

{text.subtitle}

+
+ +
+
+ {!plan && !failure &&

{text.loading}

} + {plan && children(plan)} + {failure && ( +
+ + {stale ? text.stale : busy ? text.busy : failure.message} +
+ )} +
+
+ + {stale + ? + : } +
+
+ ); +} diff --git a/ui/src/components/targets/TargetOmpExtensions.tsx b/ui/src/components/targets/TargetOmpExtensions.tsx index 5e563fc1a..5115dabda 100644 --- a/ui/src/components/targets/TargetOmpExtensions.tsx +++ b/ui/src/components/targets/TargetOmpExtensions.tsx @@ -1,18 +1,16 @@ import { useState } from 'react'; -import { useQuery, useQueryClient } from '@tanstack/react-query'; +import { useQuery } from '@tanstack/react-query'; import { Link } from 'react-router-dom'; import { AlertCircle, ArrowRight, ChevronDown, CircleCheck, FileDiff, Folder, Info, Lock, Package, Puzzle, X } from 'lucide-react'; -import { ApiError } from '../../api/client'; import { ompExtensionsApi, type OmpExtensionChange, type OmpExtensionRow, type OmpExtensionsPlan, type OmpExtensionsView } from '../../api/ompExtensions'; import Button from '../Button'; -import DialogShell from '../DialogShell'; import EmptyState from '../EmptyState'; +import ExtensionsReviewDialog from './ExtensionsReviewDialog'; import Tooltip from '../Tooltip'; import { PageSkeleton } from '../Skeleton'; import { messagesByLocale, useT } from '../../i18n'; import { queryKeys } from '../../lib/queryKeys'; import { fileName, shortenHome } from '../../lib/paths'; -import { invalidate } from '../../lib/queryEvents'; type T = ReturnType; type Pending = Record; @@ -222,73 +220,45 @@ function Row({ row, dir, name, switchable, showLock, draft, onFlip, t }: { row: /** Previews the drafts against the view's revision, then applies exactly that plan. */ function ReviewDialog({ name, view, changes, onClose, onApplied, t }: { name: string; view: OmpExtensionsView; changes: OmpExtensionChange[]; onClose: () => void; onApplied: (plan: OmpExtensionsPlan) => void; t: T }) { - const queryClient = useQueryClient(); - const preview = useQuery({ queryKey: queryKeys.ompExtensionsPreview(name, view.revision, changes), queryFn: () => ompExtensionsApi.preview(name, changes, view.revision), retry: false, gcTime: 0, staleTime: Infinity }); - const [applying, setApplying] = useState(false); - const [error, setError] = useState(null); - const plan = preview.data; - const refresh = () => invalidate(queryClient, 'ompExtensionsStale', name); - const apply = async () => { - if (!plan) return; - setApplying(true); - setError(null); - try { - const done = await ompExtensionsApi.apply(name, changes, plan.revision); - await invalidate(queryClient, 'ompExtensionsChanged', name); - onApplied(done); - } catch (err) { - setError(err as Error); - setApplying(false); - } - }; - const failure = error ?? preview.error; - const code = failure instanceof ApiError ? failure.code : undefined; - const stale = code === 'omp_extensions_stale'; - const title = t('targetDetail.ompExtensions.dialog.title', { name }); const rowOf = (key: string) => view.rows.find((r) => r.key === key); return ( - -
-
-

{title}

-

{t('targetDetail.ompExtensions.dialog.subtitle', { path: shortenHome(view.settingsPath) })}

-
- -
-
- {!plan && !failure &&

{t('targetDetail.ompExtensions.dialog.loading')}

} - {plan && ( - <> -
    - {plan.rows.map((r) => ( -
  • - - {rowOf(r.key)?.name ?? r.derivedId} - {rowOf(r.key)?.path ?? r.key} - - - {t(`targetDetail.ompExtensions.sel.${selOf(r.before)}`)} → {t(`targetDetail.ompExtensions.sel.${selOf(r.after)}`)} -
  • - ))} -
- {plan.warnings.map((w) =>
{guidance(w, t)}
)} -

{t('targetDetail.ompExtensions.dialog.kept')}

-

{t('targetDetail.ompExtensions.dialog.revision', { revision: plan.revision.slice(0, 7) })}

- - )} - {failure && ( -
- - {stale ? t('targetDetail.ompExtensions.stale') : code === 'omp_extensions_busy' ? t('targetDetail.ompExtensions.busy') : failure.message} -
- )} -
-
- - {stale - ? - : } -
-
+ ompExtensionsApi.preview(name, changes, view.revision)} + apply={(revision) => ompExtensionsApi.apply(name, changes, revision)} + onClose={onClose} + onApplied={onApplied} + > + {(plan) => ( + <> +
    + {plan.rows.map((r) => ( +
  • + + {rowOf(r.key)?.name ?? r.derivedId} + {rowOf(r.key)?.path ?? r.key} + + + {t(`targetDetail.ompExtensions.sel.${selOf(r.before)}`)} → {t(`targetDetail.ompExtensions.sel.${selOf(r.after)}`)} +
  • + ))} +
+ {plan.warnings.map((w) =>
{guidance(w, t)}
)} +

{t('targetDetail.ompExtensions.dialog.kept')}

+

{t('targetDetail.ompExtensions.dialog.revision', { revision: plan.revision.slice(0, 7) })}

+ + )} +
); } diff --git a/ui/src/components/targets/TargetPiExtensions.tsx b/ui/src/components/targets/TargetPiExtensions.tsx index f0004626a..548489e24 100644 --- a/ui/src/components/targets/TargetPiExtensions.tsx +++ b/ui/src/components/targets/TargetPiExtensions.tsx @@ -1,9 +1,8 @@ import { useState } from 'react'; import type { ReactNode } from 'react'; import { Link } from 'react-router-dom'; -import { useQuery, useQueryClient } from '@tanstack/react-query'; +import { useQuery } from '@tanstack/react-query'; import { AlertCircle, ArrowRight, ChevronDown, CircleCheck, FileDiff, Folder, Info, Lock, Package, Puzzle, RotateCcw, X } from 'lucide-react'; -import { ApiError } from '../../api/client'; import { piExtensionsApi } from '../../api/piExtensions'; import PiPackageIcon from '../PiPackageIcon'; import type { PiExtensionAction, PiExtensionChange, PiExtensionFolder, PiExtensionPackage, PiExtensionRow, PiExtensionsPlan, PiExtensionsView, PiSelection } from '../../api/piExtensions'; @@ -11,11 +10,10 @@ import { queryKeys } from '../../lib/queryKeys'; import { shortenHome } from '../../lib/paths'; import { useT } from '../../i18n'; import Button from '../Button'; -import DialogShell from '../DialogShell'; import EmptyState from '../EmptyState'; +import ExtensionsReviewDialog from './ExtensionsReviewDialog'; import { PageSkeleton } from '../Skeleton'; import Tooltip from '../Tooltip'; -import { invalidate } from '../../lib/queryEvents'; type T = ReturnType; type Scope = PiExtensionChange['scope']; @@ -355,74 +353,51 @@ function OddSelection({ value, t }: { value: PiSelection; t: T }) { function ReviewDialog({ name, view, changes, onClose, onApplied, t }: { name: string; view: PiExtensionsView; changes: PiExtensionChange[]; onClose: () => void; onApplied: (plan: PiExtensionsPlan) => void; t: T; }) { - const queryClient = useQueryClient(); - const preview = useQuery({ queryKey: queryKeys.piExtensionsPreview(name, changes), queryFn: () => piExtensionsApi.preview(name, changes), retry: false, gcTime: 0, staleTime: Infinity }); - const [applying, setApplying] = useState(false); - const [error, setError] = useState(null); - const plan = preview.data; const project = view.scope === 'project'; - const refresh = () => invalidate(queryClient, 'piExtensionsStale', name); - const apply = async () => { - if (!plan) return; - setApplying(true); - setError(null); - try { - const done = await piExtensionsApi.apply(name, changes, plan.revision); - await invalidate(queryClient, 'piExtensionsChanged', name); - onApplied(done); - } catch (err) { - setError(err as Error); - setApplying(false); - } - }; - const failure = error ?? preview.error; - const code = failure instanceof ApiError ? failure.code : undefined; - const stale = code === 'pi_extensions_stale'; - const title = t(project ? 'targetDetail.piExtensions.dialog.titleProject' : 'targetDetail.piExtensions.dialog.title', { name }); const pkgName = (scope: Scope | undefined, index: number, identity?: string) => { const p = view.packages.find((x) => x.scope === (scope ?? 'global') && x.index === index); return identity || p?.identity || p?.source || String(index); }; return ( - -
-
-

{title}

-

{t(project ? 'targetDetail.piExtensions.dialog.subtitleProject' : 'targetDetail.piExtensions.dialog.subtitle', { path: shortenHome(view.settingsPath) })}

-
- -
-
- {!plan && !failure &&

{t('targetDetail.piExtensions.dialog.loading')}

} - {plan && ( - <> -
    - {plan.rows.map((r) => ( -
  • - {r.path} - {pkgName(r.scope, r.index)} - - {t(`targetDetail.piExtensions.sel.${r.before}`)} → {t(`targetDetail.piExtensions.sel.${r.after}`)} -
  • - ))} -
- {plan.entries.map((e) => ( - + piExtensionsApi.preview(name, changes)} + apply={(revision) => piExtensionsApi.apply(name, changes, revision)} + onClose={onClose} + onApplied={onApplied} + > + {(plan) => ( + <> +
    + {plan.rows.map((r) => ( +
  • + {r.path} + {pkgName(r.scope, r.index)} + + {t(`targetDetail.piExtensions.sel.${r.before}`)} → {t(`targetDetail.piExtensions.sel.${r.after}`)} +
  • ))} -

    {t('targetDetail.piExtensions.dialog.kept')}

    - {project &&

    {t('targetDetail.piExtensions.dialog.mayNotLoad')}

    } -

    {t('targetDetail.piExtensions.dialog.revision', { revision: plan.revision.slice(0, 7) })}

    - - )} - {failure && } -
-
- - {stale - ? - : } -
-
+ + {plan.entries.map((e) => ( + + ))} +

{t('targetDetail.piExtensions.dialog.kept')}

+ {project &&

{t('targetDetail.piExtensions.dialog.mayNotLoad')}

} +

{t('targetDetail.piExtensions.dialog.revision', { revision: plan.revision.slice(0, 7) })}

+ + )} + ); } @@ -443,14 +418,3 @@ function EntryDiff({ entry: e, pkg, t }: { entry: PiExtensionsPlan['entries'][nu ); } - -function ReviewFailure({ failure, code, t }: { failure: Error; code?: string; t: T }) { - const stale = code === 'pi_extensions_stale'; - const busy = code === 'pi_extensions_busy'; - return ( -
- - {stale ? t('targetDetail.piExtensions.stale') : busy ? t('targetDetail.piExtensions.busy') : failure.message} -
- ); -}