|
| 1 | +# Function Edit Page Implementation Plan |
| 2 | + |
| 3 | +**Status:** COMPLETED |
| 4 | + |
| 5 | +**Goal:** Build the Function Edit Page with a TreeView file browser and CodeEditor for editing and deploying function code to GitHub. |
| 6 | + |
| 7 | +**Architecture:** The page follows the project's layered architecture. A `useFunctionEditPage` hook (co-located in the view file, unexported) handles data loading, state management, and save logic. A `FileTreeView` component with a co-located `useFileTreeView` hook renders the file tree. An `EditToolbar` component with `useEditToolbar` hook manages save flow and navigation. The `SourceControlService` interface is extended with `fetch()` and `updateRepo()`. |
| 8 | + |
| 9 | +**Tech Stack:** React, PatternFly 6 (TreeView, Toolbar, Modal, Button, Alert, EmptyState), OCP Dynamic Plugin SDK (CodeEditor, DocumentTitle, ListPageHeader), Vitest + React Testing Library + MSW |
| 10 | + |
| 11 | +**Testing approach:** MSW for all GitHub API interactions. `vi.mock` only for `react-i18next` and OCP SDK components. |
| 12 | + |
| 13 | +--- |
| 14 | + |
| 15 | +## Completed Tasks |
| 16 | + |
| 17 | +### Task 1: Add fetch() to SourceControlService and GithubService |
| 18 | + |
| 19 | +- Added `fetch(repo)` using `git.getTree({ recursive: '1' })` + per-file `git.getBlob` |
| 20 | +- Migrated all GithubService tests from `vi.mock('@octokit/rest')` to MSW handlers |
| 21 | +- Renamed test file to `GithubService.test.ts` |
| 22 | +- Merged `RepoInfo` + `SourceRepo` into single `RepoMetadata` type |
| 23 | +- Added test for `fetchFileContent` error path |
| 24 | + |
| 25 | +### Task 2: Decompose push() into push() and updateRepo() |
| 26 | + |
| 27 | +- `push()` for initial commits (createRef, no parents) |
| 28 | +- `updateRepo()` for subsequent commits with local commit SHA cache |
| 29 | +- Cache handles GitHub's eventually consistent ref storage (stale getRef on rapid saves) |
| 30 | +- Cache clears only on "not a fast forward" errors (external push), not on network errors |
| 31 | +- 5 updateRepo tests: first push, second from cache, third from cache, stale cache recovery, network error preserves cache |
| 32 | + |
| 33 | +### Task 3: Add getLanguageFromPath utility |
| 34 | + |
| 35 | +- Maps file extensions and special filenames (Dockerfile, Makefile) to Monaco Language enum |
| 36 | +- Extracted to shared `src/utils/utils.ts` along with `parseNamespaceAndRuntime` and `handlerMap` |
| 37 | + |
| 38 | +### Tasks 4+5: FileTreeView component |
| 39 | + |
| 40 | +- Builds `TreeViewDataItem[]` directly from flat `FileEntry[]` paths (no intermediate type) |
| 41 | +- Separate handling for root files and nested files |
| 42 | +- Directories render before files, sorted alphabetically |
| 43 | +- Loading state with spinner and "Loading source..." text |
| 44 | +- Empty state with "No files" placeholder (not clickable) |
| 45 | +- Dirty indicator after filename (`func.yaml ●`) |
| 46 | +- `React.memo` with `useMemo` (justified: parent re-renders on every CodeEditor keystroke) |
| 47 | +- Fixed width (16rem) with horizontal scroll, vertical scroll for long file lists |
| 48 | +- 11 test cases using realistic Node function file structure from knative/func templates |
| 49 | + |
| 50 | +### Task 6: FunctionEditPage view and hooks |
| 51 | + |
| 52 | +- Page component: toolbar + flex layout (FileTreeView left, SDK CodeEditor right) |
| 53 | +- `Flex` with `direction: row`, `flexWrap: nowrap`, `alignItems: stretch` (fixes baseline alignment stacking issue) |
| 54 | +- SDK CodeEditor with `height="70vh"`, empty state (code icon + "Start editing"), language label visible |
| 55 | +- Handler file auto-selected based on runtime from func.yaml (function.go for Go, index.js for Node, function/func.py for Python) |
| 56 | +- Each page loads its own data from URL params (no navigation state passing between pages) |
| 57 | +- `resolveRepoContent` extracts API calls from state management |
| 58 | +- Repo metadata stored in state after load for save without re-fetching |
| 59 | + |
| 60 | +### EditToolbar component |
| 61 | + |
| 62 | +- Back link (left), success/danger Alert (center), Save & Deploy button (right) |
| 63 | +- `useEditToolbar` hook: save flow with isSaving/error/success state |
| 64 | +- Success alert "Pushed to GitHub. Deployment running..." with 2-second auto-dismiss (tested with `vi.useFakeTimers`) |
| 65 | +- Unsaved changes confirmation modal (LeaveModal) on back navigation when dirty |
| 66 | +- Save & Deploy disabled when no changes |
| 67 | + |
| 68 | +### Infrastructure changes |
| 69 | + |
| 70 | +- Migrated from Jest to Vitest with MSW 2.x |
| 71 | +- Custom jsdom environment removed (Vitest handles globals natively) |
| 72 | +- CSP connect-src for GitHub API added to start-console.sh for dev mode |
| 73 | +- Added CSS, co-location, React performance rules to style guide and architecture docs |
| 74 | +- Added communication rules and draft PR step to workflow docs |
| 75 | + |
| 76 | +--- |
| 77 | + |
| 78 | +## Deviations from original plan |
| 79 | + |
| 80 | +- Used SDK CodeEditor instead of PatternFly CodeEditor (CSP blob URL issues with Monaco workers in OCP Console) |
| 81 | +- Removed navigation state passing between list and edit page (each page is self-contained) |
| 82 | +- Split `push()` into `push()` + `updateRepo()` (clearer separation of initial vs subsequent commits) |
| 83 | +- Added local commit SHA cache (GitHub eventual consistency workaround) |
| 84 | +- Added success alert after save (not in original plan) |
| 85 | +- Editor empty state with code icon (not in original plan) |
| 86 | +- `autoSelectHandler` renamed to `determineHandler` (pure function, returns path instead of calling setState) |
0 commit comments