From 7cdec347ece38b11dd57cc5140885935d82d7dc3 Mon Sep 17 00:00:00 2001 From: Willie Date: Wed, 7 Oct 2026 00:23:02 +0800 Subject: [PATCH] refactor(ui): use the keyed-text helper where the rule was still inline Two places still wrote out 'translate the backend key, else show its text' by hand next to keyedText, so the rule had three copies. The Pi package test also rebuilt the set of Pi targets for every package on every render; it is now built once per inventory, as before the flow moved out of the page. Refs #446 --- ui/src/components/plugins/PluginAddDialog.tsx | 3 ++- ui/src/components/plugins/agentReasons.ts | 5 +++-- ui/src/components/plugins/pluginsFlow.test.ts | 4 ++-- ui/src/components/plugins/pluginsFlow.ts | 16 +++++++++------- 4 files changed, 16 insertions(+), 12 deletions(-) diff --git a/ui/src/components/plugins/PluginAddDialog.tsx b/ui/src/components/plugins/PluginAddDialog.tsx index a7d745cfc..d02ad88a7 100644 --- a/ui/src/components/plugins/PluginAddDialog.tsx +++ b/ui/src/components/plugins/PluginAddDialog.tsx @@ -3,6 +3,7 @@ import { pluginsApi, targetMap, type PluginBinding, type PluginDiscovery, type P import { Check, ChevronDown, ChevronRight, Search, X } from 'lucide-react'; import AgentIcon from '../AgentIcon'; import { agentReasons } from './agentReasons'; +import { keyedText } from './outcomeText'; import SourceHint from './SourceHint'; import { pluginErrorMessage } from './pluginError'; import PluginDocsLink from './PluginDocsLink'; @@ -165,7 +166,7 @@ export default function PluginAddDialog({ onClose, onPreview, initialSource = '' {c.name}{c.version && {c.version}} - {(c.problem || c.description) && {c.problemKey ? t(c.problemKey, c.problemArgs, c.problem) : c.problem || c.description}} + {(c.problem || c.description) && {keyedText(t, c.problemKey, c.problem, c.problemArgs) || c.description}} {c.components.length > 0 && {c.components.join(' ยท ')}} diff --git a/ui/src/components/plugins/agentReasons.ts b/ui/src/components/plugins/agentReasons.ts index 906589fbd..a8eee7f22 100644 --- a/ui/src/components/plugins/agentReasons.ts +++ b/ui/src/components/plugins/agentReasons.ts @@ -1,5 +1,6 @@ import type { PluginCandidate, PluginTarget, PluginTargetDefinition } from '../../api/plugins'; import type { useT } from '../../i18n'; +import { keyedText } from './outcomeText'; type T = ReturnType; @@ -8,10 +9,10 @@ export function agentReasons(candidate: PluginCandidate | undefined, definitions return (Object.keys(definitions) as PluginTarget[]).map((target) => { const definition = definitions[target]; const info = candidate?.targetInfo?.[target]; - const reason = (info?.problemKey ? t(info.problemKey, info.problemArgs, info.problem) : info?.problem) + const reason = keyedText(t, info?.problemKey, info?.problem, info?.problemArgs) || (!candidate?.targets.includes(target) ? t('plugins.unsupported') : isProjectMode && !definition.project ? t('plugins.globalOnly') - : !definition.operations.includes('add') ? (definition.reasonKey ? t(definition.reasonKey, undefined, definition.reason) : definition.reason) || t('plugins.unsupported') + : !definition.operations.includes('add') ? keyedText(t, definition.reasonKey, definition.reason) || t('plugins.unsupported') : ''); return { target, definition, reason }; }); diff --git a/ui/src/components/plugins/pluginsFlow.test.ts b/ui/src/components/plugins/pluginsFlow.test.ts index ca8acf656..19bfc1793 100644 --- a/ui/src/components/plugins/pluginsFlow.test.ts +++ b/ui/src/components/plugins/pluginsFlow.test.ts @@ -1,7 +1,7 @@ import { act, renderHook } from '@testing-library/react'; import { beforeEach, describe, expect, it, vi } from 'vitest'; import { pluginsApi, type PluginInventory, type PluginPlan } from '../../api/plugins'; -import { changedRuns, failureText, isPiPackage, reviewChanges, syncTodo, updatesFound, updatesLeft, usePluginsFlow } from './pluginsFlow'; +import { changedRuns, failureText, piPackageTest, reviewChanges, syncTodo, updatesFound, updatesLeft, usePluginsFlow } from './pluginsFlow'; vi.mock('../../api/plugins', async (importOriginal) => ({ ...await importOriginal(), pluginsApi: { preview: vi.fn(), apply: vi.fn() } })); @@ -167,7 +167,7 @@ describe('plugins flow rules', () => { expect(syncTodo(undefined)).toEqual([]); }); it('calls a package Pi installs itself, bound only to Pi targets, a Pi package', () => { - expect(['demo', 'driver', 'powers', 'unbound', 'gone'].filter((name) => isPiPackage(inventory, name))).toEqual(['driver']); + expect(['demo', 'driver', 'powers', 'unbound', 'gone'].filter(piPackageTest(inventory))).toEqual(['driver']); }); it('splits a preview into what it changes and what it leaves alone, with the version each has', () => { const { active, idle } = reviewChanges(plan('r', { action: 'update' }, { action: 'noop' }, { name: 'driver', target: 'omo', action: 'noop', binding: { id: 'npm:@scope/driver', version: '3.0.0' } }), inventory); diff --git a/ui/src/components/plugins/pluginsFlow.ts b/ui/src/components/plugins/pluginsFlow.ts index 61337ee12..32e82a6e0 100644 --- a/ui/src/components/plugins/pluginsFlow.ts +++ b/ui/src/components/plugins/pluginsFlow.ts @@ -46,13 +46,15 @@ export function syncTodo(inventory?: PluginInventory) { * A package Pi installs itself (no source Skillshare copies from) and bound only to Pi targets * is a Pi package: it gets its own list, in the same rows. A plugin Skillshare installs stays above. */ -export function isPiPackage(inventory: PluginInventory | undefined, name: string) { - // A run can name a package config no longer has, such as one it just removed. - const pack = inventory?.packages[name]; - if (!pack) return false; +export function piPackageTest(inventory: PluginInventory | undefined) { const piTargets = new Set((inventory?.targetDefinitions ?? []).filter((d) => d.npm).map((d) => d.target)); - const bound = Object.entries(pack.bindings); - return !pack.source && bound.length > 0 && bound.every(([target, b]) => piTargets.has(target) && !b?.source); + return (name: string) => { + // A run can name a package config no longer has, such as one it just removed. + const pack = inventory?.packages[name]; + if (!pack) return false; + const bound = Object.entries(pack.bindings); + return !pack.source && bound.length > 0 && bound.every(([target, b]) => piTargets.has(target) && !b?.source); + }; } export function installedVersion(inventory: PluginInventory | undefined, name: string, target: string) { @@ -140,6 +142,6 @@ export function usePluginsFlow({ t, inventory, refresh, onPreviewed }: { todo: syncTodo(inventory), ...reviewChanges(review?.plan, inventory), changed: changedRuns(t, outcomes), - isPi: (name: string) => isPiPackage(inventory, name), + isPi: piPackageTest(inventory), }; }