-
Notifications
You must be signed in to change notification settings - Fork 0
feat(ui): Patterns library pilot on the shared design system (U5 B1) #437
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
3 commits
Select commit
Hold shift + click to select a range
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
105 changes: 105 additions & 0 deletions
105
apps/frontend/src/renderer/__tests__/patterns-ui.test.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,105 @@ | ||
| /** | ||
| * Tests for the Pattern → PatternLibrary mapping helpers. | ||
| */ | ||
|
|
||
| import { describe, expect, it } from 'vitest'; | ||
| import { | ||
| aggregatePatterns, | ||
| filterPatterns, | ||
| mapPatternToUi, | ||
| } from '../lib/patterns-ui'; | ||
| import type { Pattern } from '../../preload/api/modules/pattern-api'; | ||
|
|
||
| const LABELS = { high: 'high confidence', medium: 'medium confidence', low: 'low confidence' }; | ||
|
|
||
| function makePattern(overrides: Partial<Pattern> = {}): Pattern { | ||
| return { | ||
| index: 1, | ||
| id: '1', | ||
| text: 'Prefer typed nav config from a string-literal union', | ||
| category: 'code-organization', | ||
| confidence: 'high', | ||
| reasoning: 'A typo then fails at compile time.', | ||
| ...overrides, | ||
| }; | ||
| } | ||
|
|
||
| describe('mapPatternToUi', () => { | ||
| it('maps a backend pattern onto a sparse card with a spec-qualified id', () => { | ||
| const ui = mapPatternToUi(makePattern(), '001-auth', LABELS); | ||
| expect(ui).toEqual({ | ||
| id: '001-auth#1', | ||
| kind: 'pattern', | ||
| title: 'Prefer typed nav config from a string-literal union', | ||
| lang: 'code-organization', | ||
| description: 'A typo then fails at compile time.', | ||
| footer: [{ text: 'high confidence' }], | ||
| }); | ||
| }); | ||
|
|
||
| it('omits the confidence footer and empty description when unknown', () => { | ||
| const ui = mapPatternToUi( | ||
| makePattern({ confidence: undefined, reasoning: '', category: undefined }), | ||
| '002', | ||
| LABELS, | ||
| ); | ||
| expect(ui.footer).toBeUndefined(); | ||
| expect(ui.description).toBeUndefined(); | ||
| expect(ui.lang).toBeUndefined(); | ||
| }); | ||
|
|
||
| it('drops the footer when no confidence labels are supplied', () => { | ||
| expect(mapPatternToUi(makePattern(), '003').footer).toBeUndefined(); | ||
| }); | ||
| }); | ||
|
|
||
| describe('aggregatePatterns', () => { | ||
| it('flattens per-spec patterns and dedupes identical texts, first wins', () => { | ||
| const shared = makePattern({ text: 'Branches stay local until pushed' }); | ||
| const result = aggregatePatterns( | ||
| [ | ||
| { specId: '001', patterns: [makePattern({ index: 1, text: 'A' }), shared] }, | ||
| { specId: '002', patterns: [{ ...shared, index: 5 }, makePattern({ index: 2, text: 'B' })] }, | ||
| ], | ||
| LABELS, | ||
| ); | ||
| expect(result.map((p) => p.title)).toEqual([ | ||
| 'A', | ||
| 'Branches stay local until pushed', | ||
| 'B', | ||
| ]); | ||
| // The deduped entry keeps the first spec's id. | ||
| expect(result[1].id).toBe('001#1'); | ||
| }); | ||
|
|
||
| it('returns an empty list for no specs', () => { | ||
| expect(aggregatePatterns([])).toEqual([]); | ||
| expect(aggregatePatterns([{ specId: '001', patterns: [] }])).toEqual([]); | ||
| }); | ||
|
|
||
| it('is case- and whitespace-insensitive when deduping', () => { | ||
| const result = aggregatePatterns([ | ||
| { specId: '001', patterns: [makePattern({ text: 'Same Rule' })] }, | ||
| { specId: '002', patterns: [makePattern({ text: ' same rule ' })] }, | ||
| ]); | ||
| expect(result).toHaveLength(1); | ||
| }); | ||
| }); | ||
|
|
||
| describe('filterPatterns', () => { | ||
| const patterns = [ | ||
| mapPatternToUi(makePattern({ index: 1, text: 'Typed nav config', category: 'code-organization' }), 's1', LABELS), | ||
| mapPatternToUi(makePattern({ index: 2, text: 'Retry with backoff', category: 'error-handling', reasoning: 'exponential jitter' }), 's1', LABELS), | ||
| ]; | ||
|
|
||
| it('matches title, lang, and description, case-insensitively', () => { | ||
| expect(filterPatterns(patterns, 'NAV').map((p) => p.title)).toEqual(['Typed nav config']); | ||
| expect(filterPatterns(patterns, 'error-handling').map((p) => p.title)).toEqual(['Retry with backoff']); | ||
| expect(filterPatterns(patterns, 'jitter').map((p) => p.title)).toEqual(['Retry with backoff']); | ||
| expect(filterPatterns(patterns, 'zzz')).toEqual([]); | ||
| }); | ||
|
|
||
| it('returns everything for a blank query', () => { | ||
| expect(filterPatterns(patterns, ' ')).toHaveLength(2); | ||
| }); | ||
| }); |
185 changes: 185 additions & 0 deletions
185
apps/frontend/src/renderer/components/PatternsPilotView.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,185 @@ | ||
| /** | ||
| * Patterns library pilot on the shared design system (U5 B1). | ||
| * | ||
| * The backend stores learned patterns per spec, so this pilot aggregates | ||
| * across the project's known specs (from the task store) via the existing | ||
| * `window.electronAPI.pattern.listPatterns` IPC and renders them through | ||
| * `libs/ui`'s PatternLibrary. Read-only — the approve/override/delete | ||
| * flows stay on the legacy Patterns view. Reachable via the "Patterns | ||
| * (new UI)" sidebar item. | ||
| */ | ||
|
|
||
| import { useCallback, useEffect, useMemo, useState } from 'react'; | ||
| import { useTranslation } from 'react-i18next'; | ||
| import { PatternLibrary } from '@auto-code/ui'; | ||
| import type { UiMetaSection, UiPattern } from '@auto-code/ui'; | ||
| import { useTaskStore } from '../stores/task-store'; | ||
| import { | ||
| aggregatePatterns, | ||
| filterPatterns, | ||
| type SpecPatterns, | ||
| } from '../lib/patterns-ui'; | ||
|
|
||
| interface PatternsPilotViewProps { | ||
| projectId: string; | ||
| } | ||
|
|
||
| interface PilotState { | ||
| patterns: UiPattern[] | null; | ||
| specCount: number; | ||
| loading: boolean; | ||
| error: Error | null; | ||
| } | ||
|
|
||
| export function PatternsPilotView({ | ||
| projectId, | ||
| }: Readonly<PatternsPilotViewProps>) { | ||
| const { t } = useTranslation(['patterns']); | ||
| const [state, setState] = useState<PilotState>({ | ||
| patterns: null, | ||
| specCount: 0, | ||
| loading: true, | ||
| error: null, | ||
| }); | ||
| const [reloadKey, setReloadKey] = useState(0); | ||
| const [query, setQuery] = useState(''); | ||
|
|
||
| // Subscribe reactively so patterns refetch when the project's tasks load | ||
| // after mount (reading getState() once would strand the empty state). The | ||
| // effect depends on a stable string key, not the fresh-per-render array, | ||
| // so unrelated task updates don't trigger a refetch. | ||
| const tasks = useTaskStore((store) => store.tasks); | ||
| const specKey = useMemo( | ||
| () => | ||
| Array.from( | ||
| new Set( | ||
| tasks | ||
| .filter((task) => task.projectId === projectId) | ||
| .map((task) => task.specId), | ||
| ), | ||
| ).join('|'), | ||
| [tasks, projectId], | ||
| ); | ||
|
|
||
| const confidenceLabels = useMemo( | ||
| () => ({ | ||
| high: t('patterns:patternsPilot.confidence.high'), | ||
| medium: t('patterns:patternsPilot.confidence.medium'), | ||
| low: t('patterns:patternsPilot.confidence.low'), | ||
| }), | ||
| [t], | ||
| ); | ||
|
|
||
| useEffect(() => { | ||
| let active = true; | ||
| setState({ patterns: null, specCount: 0, loading: true, error: null }); | ||
|
|
||
| // Patterns are stored per spec; load each spec's patterns, tolerating | ||
| // per-spec failures (a spec may have none / an unreadable file). | ||
| const specIds = specKey === '' ? [] : specKey.split('|'); | ||
| Promise.all( | ||
| specIds.map((specId) => | ||
| window.electronAPI.pattern | ||
| .listPatterns(projectId, specId) | ||
| .then((result): SpecPatterns => ({ | ||
| specId, | ||
| patterns: result.success ? (result.data ?? []) : [], | ||
| })) | ||
| .catch((err: unknown): SpecPatterns => { | ||
| console.warn( | ||
| '[PatternsPilotView] Failed to load patterns for spec', | ||
| specId, | ||
| err, | ||
| ); | ||
| return { specId, patterns: [] }; | ||
| }), | ||
| ), | ||
| ) | ||
| .then((perSpec) => { | ||
| if (!active) return; | ||
| setState({ | ||
| patterns: aggregatePatterns(perSpec, confidenceLabels), | ||
| specCount: specIds.length, | ||
| loading: false, | ||
| error: null, | ||
| }); | ||
| }) | ||
| .catch((err: unknown) => { | ||
| if (!active) return; | ||
| console.error('[PatternsPilotView] Failed to load patterns:', err); | ||
| setState({ | ||
| patterns: null, | ||
| specCount: 0, | ||
| loading: false, | ||
| error: new Error(t('patterns:patternsPilot.error')), | ||
| }); | ||
| }); | ||
| return () => { | ||
| active = false; | ||
| }; | ||
| }, [projectId, specKey, reloadKey, confidenceLabels, t]); | ||
|
|
||
| const reload = useCallback(() => setReloadKey((key) => key + 1), []); | ||
|
|
||
| const visible = useMemo( | ||
| () => | ||
| state.patterns == null ? null : filterPatterns(state.patterns, query), | ||
| [state.patterns, query], | ||
| ); | ||
|
|
||
| const metaSections = useMemo<UiMetaSection[] | undefined>(() => { | ||
| if (state.patterns == null) return undefined; | ||
| return [ | ||
| { | ||
| title: t('patterns:patternsPilot.meta.title'), | ||
| rows: [ | ||
| { | ||
| label: t('patterns:patternsPilot.meta.total'), | ||
| value: String(state.patterns.length), | ||
| }, | ||
| { | ||
| label: t('patterns:patternsPilot.meta.specs'), | ||
| value: String(state.specCount), | ||
| }, | ||
| ], | ||
| }, | ||
| ]; | ||
| }, [state.patterns, state.specCount, t]); | ||
|
|
||
| const stateLabels = useMemo( | ||
| () => ({ | ||
| loading: t('patterns:patternsPilot.states.loading'), | ||
| retry: t('patterns:patternsPilot.states.retry'), | ||
| empty: t('patterns:patternsPilot.states.empty'), | ||
| }), | ||
| [t], | ||
| ); | ||
|
|
||
| const kindLabels = useMemo( | ||
| () => ({ | ||
| pattern: t('patterns:patternsPilot.kinds.pattern'), | ||
| gotcha: t('patterns:patternsPilot.kinds.gotcha'), | ||
| decision: t('patterns:patternsPilot.kinds.decision'), | ||
| rule: t('patterns:patternsPilot.kinds.rule'), | ||
| }), | ||
| [t], | ||
| ); | ||
|
|
||
| return ( | ||
| <div className="h-full overflow-hidden"> | ||
| <PatternLibrary | ||
| patterns={visible} | ||
| loading={state.loading} | ||
| error={state.error} | ||
| onRetry={reload} | ||
| searchValue={query} | ||
| onSearchChange={setQuery} | ||
| searchPlaceholder={t('patterns:patternsPilot.searchPlaceholder')} | ||
| searchLabel={t('patterns:patternsPilot.searchLabel')} | ||
| stateLabels={stateLabels} | ||
| kindLabels={kindLabels} | ||
| metaSections={metaSections} | ||
| /> | ||
| </div> | ||
| ); | ||
| } | ||
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
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.