@bevel-software/platform-core-frontend 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +202 -0
- package/package.json +70 -0
- package/src/core/CoreAppShell.tsx +444 -0
- package/src/core/__tests__/ShellRoutes.test.tsx +95 -0
- package/src/core/events.ts +29 -0
- package/src/core/registry.ts +336 -0
- package/src/css-side-effect-imports.d.ts +7 -0
- package/src/html-extensions.ts +12 -0
- package/src/index.css +49 -0
- package/src/index.ts +23 -0
- package/src/lib/api.ts +57 -0
- package/src/lib/utils.ts +138 -0
- package/src/modules/access/__tests__/ManageAccessDialog.test.tsx +130 -0
- package/src/modules/access/__tests__/useFileAccess.test.tsx +253 -0
- package/src/modules/access/api.ts +205 -0
- package/src/modules/access/components/AccessRestrictedBanner.tsx +31 -0
- package/src/modules/access/components/ManageAccessDialog.tsx +1232 -0
- package/src/modules/access/components/NodeOwnersBanner.tsx +30 -0
- package/src/modules/access/hooks/useFileAccess.ts +151 -0
- package/src/modules/admin/components/AdminRolesPage.tsx +943 -0
- package/src/modules/admin/components/RolesCorruptedBanner.tsx +74 -0
- package/src/modules/admin/services/admin.api.ts +92 -0
- package/src/modules/admin/services/roles.api.ts +136 -0
- package/src/modules/admin/state/admin.context.tsx +223 -0
- package/src/modules/auth/components/LoginScreen.tsx +135 -0
- package/src/modules/auth/hooks/useAuthState.ts +59 -0
- package/src/modules/auth/services/auth.api.ts +24 -0
- package/src/modules/auth/services/microsoft-oauth.ts +57 -0
- package/src/modules/auth/state/auth.context.ts +18 -0
- package/src/modules/git/__tests__/BranchSwitcher.test.tsx +347 -0
- package/src/modules/git/__tests__/FileHistoryPanel.test.tsx +201 -0
- package/src/modules/git/__tests__/ProtectedBranchBanner.test.tsx +82 -0
- package/src/modules/git/__tests__/PullNeededBanner.test.tsx +441 -0
- package/src/modules/git/__tests__/git.api.test.ts +113 -0
- package/src/modules/git/__tests__/pr.api.test.ts +44 -0
- package/src/modules/git/__tests__/useAutoPullUpdates.test.tsx +212 -0
- package/src/modules/git/components/BranchSwitcher.tsx +527 -0
- package/src/modules/git/components/DiffView.tsx +30 -0
- package/src/modules/git/components/FileComparisonPanel.tsx +377 -0
- package/src/modules/git/components/FileHistoryPanel.tsx +320 -0
- package/src/modules/git/components/ProtectedBranchBanner.tsx +42 -0
- package/src/modules/git/components/PullNeededBanner.tsx +204 -0
- package/src/modules/git/components/PullRequestsForMe.tsx +244 -0
- package/src/modules/git/components/UnifiedDiffView.tsx +37 -0
- package/src/modules/git/hooks/__tests__/useGitState.deleteBranch.test.tsx +99 -0
- package/src/modules/git/hooks/useAutoPullUpdates.ts +121 -0
- package/src/modules/git/hooks/useGitState.ts +335 -0
- package/src/modules/git/services/__tests__/error-messages.test.ts +134 -0
- package/src/modules/git/services/error-messages.ts +257 -0
- package/src/modules/git/services/git.api.ts +163 -0
- package/src/modules/git/services/pr-prompts.ts +159 -0
- package/src/modules/git/services/pr.api.ts +39 -0
- package/src/modules/git/state/auto-update.context.ts +21 -0
- package/src/modules/git/state/git.context.ts +82 -0
- package/src/modules/layout/components/AppLayout.tsx +40 -0
- package/src/modules/layout/components/DemoBanner.tsx +57 -0
- package/src/modules/layout/components/MaintenanceOverlay.tsx +104 -0
- package/src/modules/layout/components/MobileChatLayout.tsx +153 -0
- package/src/modules/layout/components/ResizableThreePaneLayout.tsx +203 -0
- package/src/modules/layout/components/SlideOverlay.tsx +224 -0
- package/src/modules/layout/components/__tests__/MobileChatLayout.test.tsx +77 -0
- package/src/modules/layout/components/__tests__/ResizableThreePaneLayout.test.tsx +57 -0
- package/src/modules/layout/hooks/useMediaQuery.ts +23 -0
- package/src/modules/layout/state/__tests__/layout.context.test.tsx +39 -0
- package/src/modules/layout/state/layout.context.ts +46 -0
- package/src/modules/library/__tests__/DetailDialog.test.tsx +200 -0
- package/src/modules/library/__tests__/diff.test.ts +62 -0
- package/src/modules/library/__tests__/loadout.test.tsx +72 -0
- package/src/modules/library/__tests__/status.test.ts +117 -0
- package/src/modules/library/components/ChangeRequestDock.tsx +47 -0
- package/src/modules/library/components/CompareView.tsx +357 -0
- package/src/modules/library/components/DetailDialog.tsx +566 -0
- package/src/modules/library/components/GearIcon.tsx +12 -0
- package/src/modules/library/components/LibraryCard.tsx +132 -0
- package/src/modules/library/components/LibraryPage.tsx +254 -0
- package/src/modules/library/components/LoadoutSidebar.tsx +156 -0
- package/src/modules/library/components/StatusGem.tsx +21 -0
- package/src/modules/library/components/SuggestChange.tsx +171 -0
- package/src/modules/library/components/particles.ts +50 -0
- package/src/modules/library/hooks/useLibraryData.ts +119 -0
- package/src/modules/library/hooks/useSkillDetail.ts +74 -0
- package/src/modules/library/library.css +320 -0
- package/src/modules/library/services/library.api.ts +164 -0
- package/src/modules/library/state/loadout.tsx +129 -0
- package/src/modules/library/state/toast.tsx +42 -0
- package/src/modules/library/utils/diff.ts +102 -0
- package/src/modules/library/utils/status.ts +116 -0
- package/src/modules/pr/__tests__/PrApprovalBadge.test.tsx +93 -0
- package/src/modules/pr/__tests__/PrCancelButton.test.tsx +158 -0
- package/src/modules/pr/__tests__/PrCancelConfirmDialog.test.tsx +44 -0
- package/src/modules/pr/__tests__/PrMergeButton.test.tsx +360 -0
- package/src/modules/pr/__tests__/bucketize-comments.test.ts +85 -0
- package/src/modules/pr/__tests__/parsePatch.test.ts +104 -0
- package/src/modules/pr/__tests__/usePrViewerState.test.ts +72 -0
- package/src/modules/pr/components/OpenChangeRequestDialog.tsx +193 -0
- package/src/modules/pr/components/PrApprovalBadge.tsx +80 -0
- package/src/modules/pr/components/PrCancelButton.tsx +99 -0
- package/src/modules/pr/components/PrCancelConfirmDialog.tsx +119 -0
- package/src/modules/pr/components/PrCommentComposer.tsx +92 -0
- package/src/modules/pr/components/PrCommentThread.tsx +168 -0
- package/src/modules/pr/components/PrCommentsPanel.tsx +143 -0
- package/src/modules/pr/components/PrDiffPane.tsx +425 -0
- package/src/modules/pr/components/PrFileRow.tsx +164 -0
- package/src/modules/pr/components/PrFilesList.tsx +53 -0
- package/src/modules/pr/components/PrHeader.tsx +45 -0
- package/src/modules/pr/components/PrHeaderOverflowMenu.tsx +58 -0
- package/src/modules/pr/components/PrMergeButton.tsx +268 -0
- package/src/modules/pr/components/PrMergeConfirmDialog.tsx +112 -0
- package/src/modules/pr/components/PrRefreshFromTargetButton.tsx +110 -0
- package/src/modules/pr/components/PrViewer.tsx +364 -0
- package/src/modules/pr/hooks/usePrViewerState.ts +150 -0
- package/src/modules/pr/services/pr-approvals.api.ts +31 -0
- package/src/modules/pr/services/pr-cancel.api.ts +19 -0
- package/src/modules/pr/services/pr-comments.api.ts +45 -0
- package/src/modules/pr/services/pr-detail.api.ts +17 -0
- package/src/modules/pr/services/pr-merge.api.ts +53 -0
- package/src/modules/pr/services/pr-open.api.ts +30 -0
- package/src/modules/pr/state/pr-viewer.context.ts +34 -0
- package/src/modules/pr/utils/bucketize-comments.ts +55 -0
- package/src/modules/pr/utils/parsePatch.ts +57 -0
- package/src/modules/review/__tests__/review.api.test.ts +121 -0
- package/src/modules/review/components/BinaryChangePlaceholder.tsx +15 -0
- package/src/modules/review/components/DiffViewer.tsx +78 -0
- package/src/modules/review/components/MarkdownDiffViewer.tsx +263 -0
- package/src/modules/review/components/ReviewFileRow.tsx +114 -0
- package/src/modules/review/components/ReviewPanel.tsx +260 -0
- package/src/modules/review/components/ReviewPanelSurface.tsx +52 -0
- package/src/modules/review/components/__tests__/ReviewPanel.test.tsx +106 -0
- package/src/modules/review/hooks/__tests__/useReviewState.test.ts +88 -0
- package/src/modules/review/hooks/useReviewState.ts +256 -0
- package/src/modules/review/services/review.api.ts +72 -0
- package/src/modules/review/state/ReviewProvider.tsx +28 -0
- package/src/modules/review/state/review.context.ts +29 -0
- package/src/modules/secrets-vault/components/ConnectToolsPage.tsx +488 -0
- package/src/modules/secrets-vault/components/SecretsPage.tsx +276 -0
- package/src/modules/secrets-vault/components/ToolSecretsPanel.tsx +362 -0
- package/src/modules/secrets-vault/components/__tests__/SecretsPage.test.tsx +51 -0
- package/src/modules/secrets-vault/services/connect.api.ts +113 -0
- package/src/modules/secrets-vault/services/secrets.api.ts +82 -0
- package/src/modules/secrets-vault/services/tool-secrets.api.ts +116 -0
- package/src/modules/toolbar/components/AdminMenu.tsx +250 -0
- package/src/modules/toolbar/components/AppSwitcher.tsx +108 -0
- package/src/modules/toolbar/components/ExternalAgentAccessPage.tsx +548 -0
- package/src/modules/toolbar/components/Toolbar.tsx +123 -0
- package/src/modules/toolbar/components/__tests__/AppSwitcher.test.tsx +122 -0
- package/src/modules/toolbar/components/__tests__/Toolbar.test.tsx +353 -0
- package/src/modules/toolbar/services/external-api-keys.api.ts +65 -0
- package/src/modules/tools/ToolsExplorerPage.tsx +380 -0
- package/src/modules/workflow/hooks/useEventBusSubscription.ts +24 -0
- package/src/modules/workflow/hooks/useFileLock.ts +430 -0
- package/src/modules/workflow/services/lock.api.ts +139 -0
- package/src/modules/workflow/state/EventBusFocusBinder.tsx +25 -0
- package/src/modules/workflow/state/EventBusProvider.tsx +232 -0
- package/src/modules/workflow/state/event-bus.context.ts +75 -0
- package/src/modules/workspace/__tests__/testFixtures.ts +54 -0
- package/src/modules/workspace/__tests__/useWorkspaceState.test.ts +91 -0
- package/src/modules/workspace/components/EditorTabs.tsx +384 -0
- package/src/modules/workspace/components/FileExplorer.tsx +1068 -0
- package/src/modules/workspace/components/FileRoute.tsx +392 -0
- package/src/modules/workspace/components/FileViewer.tsx +906 -0
- package/src/modules/workspace/components/__tests__/EditorTabs.test.tsx +241 -0
- package/src/modules/workspace/components/__tests__/FileExplorer.test.tsx +666 -0
- package/src/modules/workspace/components/__tests__/FileRoute.test.tsx +330 -0
- package/src/modules/workspace/components/__tests__/FileViewer.test.tsx +492 -0
- package/src/modules/workspace/components/renderers/CsvRenderer.tsx +174 -0
- package/src/modules/workspace/components/renderers/DocxRenderer.tsx +87 -0
- package/src/modules/workspace/components/renderers/HtmlRenderer.tsx +231 -0
- package/src/modules/workspace/components/renderers/ImageRenderer.tsx +50 -0
- package/src/modules/workspace/components/renderers/KbMarkdownView.tsx +287 -0
- package/src/modules/workspace/components/renderers/MarkdownRenderer.tsx +252 -0
- package/src/modules/workspace/components/renderers/MermaidDiagram.tsx +268 -0
- package/src/modules/workspace/components/renderers/PdfRenderer.tsx +81 -0
- package/src/modules/workspace/components/renderers/TextRenderer.tsx +86 -0
- package/src/modules/workspace/components/renderers/ToolForm.tsx +404 -0
- package/src/modules/workspace/components/renderers/ToolRenderer.tsx +417 -0
- package/src/modules/workspace/components/renderers/XlsxRenderer.tsx +157 -0
- package/src/modules/workspace/components/renderers/__tests__/CsvRenderer.test.ts +57 -0
- package/src/modules/workspace/components/renderers/__tests__/KbMarkdownView.test.tsx +95 -0
- package/src/modules/workspace/components/renderers/__tests__/MarkdownRenderer.test.tsx +328 -0
- package/src/modules/workspace/components/renderers/__tests__/PdfRenderer.test.tsx +167 -0
- package/src/modules/workspace/components/renderers/__tests__/htmlSandbox.test.ts +318 -0
- package/src/modules/workspace/components/renderers/csvUtils.ts +76 -0
- package/src/modules/workspace/components/renderers/htmlSandbox.ts +332 -0
- package/src/modules/workspace/components/renderers/index.ts +39 -0
- package/src/modules/workspace/components/renderers/types.ts +27 -0
- package/src/modules/workspace/hooks/__tests__/useWorkspaceState.test.tsx +298 -0
- package/src/modules/workspace/hooks/useWorkspaceState.ts +1315 -0
- package/src/modules/workspace/routing/__tests__/kb-routes.test.tsx +99 -0
- package/src/modules/workspace/routing/kb-routes.ts +266 -0
- package/src/modules/workspace/services/__tests__/workspace.api.test.ts +72 -0
- package/src/modules/workspace/services/workspace.api.ts +185 -0
- package/src/modules/workspace/state/workspace.context.ts +235 -0
- package/src/modules/workspace/utils/__tests__/frontmatter.test.ts +107 -0
- package/src/modules/workspace/utils/diff.ts +53 -0
- package/src/modules/workspace/utils/fileTree.ts +179 -0
- package/src/modules/workspace/utils/frontmatter.ts +80 -0
- package/src/modules/workspace/utils/readDroppedEntries.ts +119 -0
- package/src/modules/workspace/utils/tab-persistence.ts +23 -0
- package/src/shared/components/Dialog.tsx +198 -0
- package/src/shared/components/PageShell.tsx +51 -0
- package/src/shared/components/useModalLayer.ts +35 -0
- package/src/shared/markdown/Markdown.tsx +62 -0
- package/src/test-setup.ts +59 -0
|
@@ -0,0 +1,252 @@
|
|
|
1
|
+
import { useState, useCallback, useEffect, useRef } from 'react';
|
|
2
|
+
import { useLocation } from 'react-router-dom';
|
|
3
|
+
import { KbMarkdownView } from './KbMarkdownView';
|
|
4
|
+
import {
|
|
5
|
+
useFileNav,
|
|
6
|
+
useNodeIdNav,
|
|
7
|
+
useCanonicalFileUrl,
|
|
8
|
+
KB_ROUTE_PREFIX,
|
|
9
|
+
resolveRelativePath,
|
|
10
|
+
} from '../../routing/kb-routes';
|
|
11
|
+
import type { FileRendererProps, RendererSaveState } from './types';
|
|
12
|
+
|
|
13
|
+
/** Decode a URL hash (`#some%20slug`) to its bare slug, tolerating bad escapes. */
|
|
14
|
+
function safeDecodeHash(hash: string): string {
|
|
15
|
+
const raw = hash.replace(/^#/, '');
|
|
16
|
+
try {
|
|
17
|
+
return decodeURIComponent(raw);
|
|
18
|
+
} catch {
|
|
19
|
+
return raw;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/** Escape a slug for safe interpolation into an attribute-selector string. */
|
|
24
|
+
function escapeForSelector(value: string): string {
|
|
25
|
+
return (
|
|
26
|
+
value
|
|
27
|
+
.replace(/["\\]/g, '\\$&')
|
|
28
|
+
// CSS-hex-escape control characters (newline, CR, …): a raw control char in
|
|
29
|
+
// a CSS string is a syntax error that would make querySelector throw. The
|
|
30
|
+
// trailing space terminates the hex escape.
|
|
31
|
+
// eslint-disable-next-line no-control-regex
|
|
32
|
+
.replace(/[\x00-\x1f\x7f]/g, (c) => `\\${c.charCodeAt(0).toString(16)} `)
|
|
33
|
+
);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Markdown view/edit renderer driven entirely by the parent's `readOnly`
|
|
38
|
+
* prop:
|
|
39
|
+
*
|
|
40
|
+
* - `readOnly === true` → rendered preview (view mode, via `KbMarkdownView`)
|
|
41
|
+
* - `readOnly === false` → source textarea (edit mode)
|
|
42
|
+
*
|
|
43
|
+
* There is no internal mode toggle. The FileViewer's top-level "Edit" /
|
|
44
|
+
* "Done" button is the single mode switch. The dirty/save state still works
|
|
45
|
+
* the same: typing in edit mode emits `onValueChange`, Ctrl/Cmd+S triggers
|
|
46
|
+
* `onSave`, and the parent owns when to flip `readOnly` back to true.
|
|
47
|
+
*
|
|
48
|
+
* The read view itself lives in `KbMarkdownView`, shared with the Atlassian
|
|
49
|
+
* embed; this component owns the edit textarea, save lifecycle, deep-link
|
|
50
|
+
* scroll, and in-workspace link navigation.
|
|
51
|
+
*/
|
|
52
|
+
export function MarkdownRenderer({
|
|
53
|
+
content,
|
|
54
|
+
savedContent,
|
|
55
|
+
filePath,
|
|
56
|
+
onSave,
|
|
57
|
+
onDirtyChange,
|
|
58
|
+
onValueChange,
|
|
59
|
+
onSaveStateChange,
|
|
60
|
+
readOnly = false,
|
|
61
|
+
}: FileRendererProps) {
|
|
62
|
+
// openFile here is the navigating version: clicking a markdown link to a file
|
|
63
|
+
// updates the URL so the route reflects what's on screen.
|
|
64
|
+
const { openFile } = useFileNav();
|
|
65
|
+
// Shared id-link resolver (resolve-id → openFile, heading preserved) — same
|
|
66
|
+
// implementation the chat citation renderer uses.
|
|
67
|
+
const { openNodeId } = useNodeIdNav();
|
|
68
|
+
// The URL hash carries a heading slug for citation deep-links
|
|
69
|
+
// (`…/Node.md#goal`); we scroll the matching heading into view once the
|
|
70
|
+
// rendered content is in the DOM.
|
|
71
|
+
const location = useLocation();
|
|
72
|
+
const scrollContainerRef = useRef<HTMLDivElement>(null);
|
|
73
|
+
const [value, setValue] = useState(content);
|
|
74
|
+
// savedContent is the on-disk baseline; falls back to content when the
|
|
75
|
+
// caller doesn't track a separate saved version (single-file legacy).
|
|
76
|
+
const [savedValue, setSavedValue] = useState(savedContent ?? content);
|
|
77
|
+
const [saveState, setSaveState] = useState<RendererSaveState>('idle');
|
|
78
|
+
const [saveError, setSaveError] = useState<string | null>(null);
|
|
79
|
+
|
|
80
|
+
const dirty = !readOnly && value !== savedValue;
|
|
81
|
+
|
|
82
|
+
// On-disk baseline for the dirty flag. Runs even mid-edit so an autosave
|
|
83
|
+
// checkpoint that advances `savedContent` clears the "Unsaved" indicator.
|
|
84
|
+
//
|
|
85
|
+
// The editable `value` is only re-derived from `content` in read-only preview
|
|
86
|
+
// mode — in EDIT mode the textarea owns `value`. Re-seeding `value` from
|
|
87
|
+
// `content` during an edit is what made a background autosave visibly jump the
|
|
88
|
+
// UI: the checkpoint round-trips the buffer back through the `content` prop
|
|
89
|
+
// (and commit can line-ending-normalize it), so `setValue(content)` re-assigned
|
|
90
|
+
// the controlled textarea, throwing the caret to the end and scrolling the
|
|
91
|
+
// document to the top. File switches and Edit/View flips remount this component
|
|
92
|
+
// via the FileViewer `key`, so mount-time `useState(content)` already loads the
|
|
93
|
+
// correct buffer for a new file or when (re-)entering edit mode.
|
|
94
|
+
useEffect(() => {
|
|
95
|
+
// Intentional prop→state sync (same pattern useWorkspaceState uses):
|
|
96
|
+
// mirror the on-disk baseline, and the buffer in preview mode only.
|
|
97
|
+
// eslint-disable-next-line react-hooks/set-state-in-effect
|
|
98
|
+
setSavedValue(savedContent ?? content);
|
|
99
|
+
if (readOnly) setValue(content);
|
|
100
|
+
}, [content, savedContent, readOnly]);
|
|
101
|
+
|
|
102
|
+
// Reset transient save UI when the file or its content identity changes.
|
|
103
|
+
// A background autosave echo can advance `content`/`savedContent` while a
|
|
104
|
+
// manual save is still in flight; don't clobber the 'saving' strip in that
|
|
105
|
+
// window — `save()` owns the transition out of 'saving' (→ idle on success,
|
|
106
|
+
// → error on failure). Only clear idle/error state on a genuine reset.
|
|
107
|
+
useEffect(() => {
|
|
108
|
+
// eslint-disable-next-line react-hooks/set-state-in-effect
|
|
109
|
+
setSaveState((current) => (current === 'saving' ? current : 'idle'));
|
|
110
|
+
setSaveError(null);
|
|
111
|
+
}, [content, savedContent, filePath]);
|
|
112
|
+
|
|
113
|
+
useEffect(() => {
|
|
114
|
+
onDirtyChange?.(dirty);
|
|
115
|
+
}, [dirty, onDirtyChange]);
|
|
116
|
+
|
|
117
|
+
useEffect(() => {
|
|
118
|
+
onSaveStateChange?.(saveState);
|
|
119
|
+
}, [saveState, onSaveStateChange]);
|
|
120
|
+
|
|
121
|
+
// Deep-link scroll: when the URL has a `#heading-slug` fragment, scroll the
|
|
122
|
+
// heading with that id (added by rehype-slug) into view. Re-runs when the
|
|
123
|
+
// hash, the file, or the rendered value changes — the latter because the
|
|
124
|
+
// content may still be loading when the route first resolves.
|
|
125
|
+
useEffect(() => {
|
|
126
|
+
if (!readOnly) return;
|
|
127
|
+
const slug = location.hash ? safeDecodeHash(location.hash) : '';
|
|
128
|
+
if (!slug) return;
|
|
129
|
+
const raf = requestAnimationFrame(() => {
|
|
130
|
+
const container = scrollContainerRef.current;
|
|
131
|
+
if (!container) return;
|
|
132
|
+
const target = container.querySelector(`[id="${escapeForSelector(slug)}"]`);
|
|
133
|
+
target?.scrollIntoView({ block: 'start' });
|
|
134
|
+
});
|
|
135
|
+
return () => cancelAnimationFrame(raf);
|
|
136
|
+
}, [location.hash, filePath, value, readOnly]);
|
|
137
|
+
|
|
138
|
+
// Navigate to a workspace file referenced by a relative link. react-markdown
|
|
139
|
+
// percent-encodes spaces in hrefs (`Some%20File.md`), so decode before
|
|
140
|
+
// resolving or the path won't match a real file.
|
|
141
|
+
// Citation deep-link for a heading: the node's canonical URL (its id URL when
|
|
142
|
+
// it's a node, else the path URL) plus the heading's `#slug`. Matches the "copy
|
|
143
|
+
// link to this file" affordance, scoped to a section. Falls back to the current
|
|
144
|
+
// location while the id resolves / for non-node files.
|
|
145
|
+
const canonicalFileUrl = useCanonicalFileUrl(filePath);
|
|
146
|
+
const headingLink = useCallback(
|
|
147
|
+
(slug: string) =>
|
|
148
|
+
`${canonicalFileUrl ?? `${window.location.origin}${location.pathname}`}#${slug}`,
|
|
149
|
+
[canonicalFileUrl, location.pathname],
|
|
150
|
+
);
|
|
151
|
+
|
|
152
|
+
const handleFileLink = useCallback(
|
|
153
|
+
(href: string) => {
|
|
154
|
+
// Absolute workspace citation URLs (`/workspace/<branch>/<path>`, e.g. the
|
|
155
|
+
// links the agent emits) carry their own branch and are resolved by
|
|
156
|
+
// `openFile` directly. Never run them through `resolveRelativePath` — that
|
|
157
|
+
// would treat them as relative to the current file and mangle the path.
|
|
158
|
+
if (href.startsWith(`${KB_ROUTE_PREFIX}/`)) {
|
|
159
|
+
openFile(href);
|
|
160
|
+
return;
|
|
161
|
+
}
|
|
162
|
+
let decoded = href;
|
|
163
|
+
try { decoded = decodeURIComponent(href); } catch { /* leave as-is */ }
|
|
164
|
+
openFile(resolveRelativePath(filePath, decoded));
|
|
165
|
+
},
|
|
166
|
+
[openFile, filePath],
|
|
167
|
+
);
|
|
168
|
+
|
|
169
|
+
const save = useCallback(async (): Promise<boolean> => {
|
|
170
|
+
if (readOnly || value === savedValue) return true;
|
|
171
|
+
setSaveState('saving');
|
|
172
|
+
setSaveError(null);
|
|
173
|
+
try {
|
|
174
|
+
await onSave(value);
|
|
175
|
+
setSavedValue(value);
|
|
176
|
+
setSaveState('idle');
|
|
177
|
+
return true;
|
|
178
|
+
} catch (err) {
|
|
179
|
+
// Keep the raw error for console/telemetry — never render it. The DOM
|
|
180
|
+
// gets a stable, user-friendly string so we don't leak backend internals.
|
|
181
|
+
console.error('[MarkdownRenderer] save failed:', err);
|
|
182
|
+
setSaveState('error');
|
|
183
|
+
setSaveError(err instanceof Error ? err.message : String(err));
|
|
184
|
+
return false;
|
|
185
|
+
}
|
|
186
|
+
}, [readOnly, value, savedValue, onSave]);
|
|
187
|
+
|
|
188
|
+
const handleChange = useCallback((e: React.ChangeEvent<HTMLTextAreaElement>) => {
|
|
189
|
+
const next = e.target.value;
|
|
190
|
+
setValue(next);
|
|
191
|
+
onValueChange?.(next);
|
|
192
|
+
}, [onValueChange]);
|
|
193
|
+
|
|
194
|
+
const handleKeyDown = useCallback((e: React.KeyboardEvent) => {
|
|
195
|
+
if ((e.ctrlKey || e.metaKey) && e.key === 's') {
|
|
196
|
+
e.preventDefault();
|
|
197
|
+
void save();
|
|
198
|
+
}
|
|
199
|
+
}, [save]);
|
|
200
|
+
|
|
201
|
+
// Optional save-state strip — only shown when there's actually something to
|
|
202
|
+
// surface (mid-save spinner or an error message), so view mode stays clean.
|
|
203
|
+
const showStatusStrip = saveState === 'saving' || (saveState === 'error' && saveError);
|
|
204
|
+
|
|
205
|
+
return (
|
|
206
|
+
<div className="flex flex-col h-full">
|
|
207
|
+
{showStatusStrip && (
|
|
208
|
+
<div className="flex items-center gap-2 pb-2 mb-2 border-b border-slate-200 shrink-0">
|
|
209
|
+
{saveState === 'saving' && (
|
|
210
|
+
<span
|
|
211
|
+
role="status"
|
|
212
|
+
aria-live="polite"
|
|
213
|
+
aria-atomic="true"
|
|
214
|
+
className="text-xs text-slate-600"
|
|
215
|
+
>
|
|
216
|
+
Saving…
|
|
217
|
+
</span>
|
|
218
|
+
)}
|
|
219
|
+
{saveState === 'error' && saveError && (
|
|
220
|
+
<span
|
|
221
|
+
role="alert"
|
|
222
|
+
aria-live="assertive"
|
|
223
|
+
aria-atomic="true"
|
|
224
|
+
className="text-xs text-red-600"
|
|
225
|
+
>
|
|
226
|
+
Couldn't save your changes. Try again in a moment.
|
|
227
|
+
</span>
|
|
228
|
+
)}
|
|
229
|
+
</div>
|
|
230
|
+
)}
|
|
231
|
+
|
|
232
|
+
{readOnly ? (
|
|
233
|
+
<KbMarkdownView
|
|
234
|
+
source={value}
|
|
235
|
+
onOpenFile={handleFileLink}
|
|
236
|
+
onOpenNodeId={openNodeId}
|
|
237
|
+
headingLink={headingLink}
|
|
238
|
+
containerRef={scrollContainerRef}
|
|
239
|
+
/>
|
|
240
|
+
) : (
|
|
241
|
+
<textarea
|
|
242
|
+
className="flex-1 w-full bg-transparent text-sm text-slate-700 font-mono whitespace-pre-wrap break-words leading-relaxed resize-none outline-none"
|
|
243
|
+
value={value}
|
|
244
|
+
onChange={handleChange}
|
|
245
|
+
onKeyDown={handleKeyDown}
|
|
246
|
+
spellCheck={false}
|
|
247
|
+
autoFocus
|
|
248
|
+
/>
|
|
249
|
+
)}
|
|
250
|
+
</div>
|
|
251
|
+
);
|
|
252
|
+
}
|
|
@@ -0,0 +1,268 @@
|
|
|
1
|
+
import { memo, useEffect, useRef, useState, useCallback } from 'react';
|
|
2
|
+
import mermaid from 'mermaid';
|
|
3
|
+
import { ZoomIn, ZoomOut, Maximize2 } from 'lucide-react';
|
|
4
|
+
|
|
5
|
+
mermaid.initialize({
|
|
6
|
+
startOnLoad: false,
|
|
7
|
+
theme: 'dark',
|
|
8
|
+
darkMode: true,
|
|
9
|
+
fontFamily: 'ui-sans-serif, system-ui, sans-serif',
|
|
10
|
+
themeVariables: {
|
|
11
|
+
darkMode: true,
|
|
12
|
+
background: '#171717',
|
|
13
|
+
primaryColor: '#3b82f6',
|
|
14
|
+
primaryTextColor: '#e5e5e5',
|
|
15
|
+
primaryBorderColor: '#525252',
|
|
16
|
+
lineColor: '#737373',
|
|
17
|
+
secondaryColor: '#1e3a5f',
|
|
18
|
+
tertiaryColor: '#262626',
|
|
19
|
+
},
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
let diagramCounter = 0;
|
|
23
|
+
|
|
24
|
+
interface MermaidDiagramProps {
|
|
25
|
+
code: string;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
const MIN_SCALE = 0.1;
|
|
29
|
+
const MAX_SCALE = 20;
|
|
30
|
+
const ZOOM_FACTOR = 0.15;
|
|
31
|
+
|
|
32
|
+
export const MermaidDiagram = memo(function MermaidDiagram({ code }: MermaidDiagramProps) {
|
|
33
|
+
const viewportRef = useRef<HTMLDivElement>(null);
|
|
34
|
+
const svgContainerRef = useRef<HTMLDivElement>(null);
|
|
35
|
+
const [error, setError] = useState<string | null>(null);
|
|
36
|
+
|
|
37
|
+
// Use refs for transform state so the wheel handler always sees current values
|
|
38
|
+
const scaleRef = useRef(1);
|
|
39
|
+
const translateRef = useRef({ x: 0, y: 0 });
|
|
40
|
+
const [, forceRender] = useState(0);
|
|
41
|
+
const rerender = useCallback(() => forceRender(n => n + 1), []);
|
|
42
|
+
|
|
43
|
+
const [isPanning, setIsPanning] = useState(false);
|
|
44
|
+
const panStart = useRef({ x: 0, y: 0 });
|
|
45
|
+
const translateStart = useRef({ x: 0, y: 0 });
|
|
46
|
+
|
|
47
|
+
// Render mermaid SVG
|
|
48
|
+
useEffect(() => {
|
|
49
|
+
const el = svgContainerRef.current;
|
|
50
|
+
if (!el) return;
|
|
51
|
+
|
|
52
|
+
let cancelled = false;
|
|
53
|
+
const id = `mermaid-diagram-${++diagramCounter}`;
|
|
54
|
+
|
|
55
|
+
(async () => {
|
|
56
|
+
try {
|
|
57
|
+
const { svg } = await mermaid.render(id, code);
|
|
58
|
+
if (cancelled) return;
|
|
59
|
+
el.innerHTML = svg;
|
|
60
|
+
setError(null);
|
|
61
|
+
|
|
62
|
+
const svgEl = el.querySelector('svg');
|
|
63
|
+
if (svgEl) {
|
|
64
|
+
svgEl.style.maxWidth = 'none';
|
|
65
|
+
svgEl.style.height = 'auto';
|
|
66
|
+
}
|
|
67
|
+
} catch (err) {
|
|
68
|
+
if (cancelled) return;
|
|
69
|
+
setError(err instanceof Error ? err.message : 'Failed to render diagram');
|
|
70
|
+
el.innerHTML = '';
|
|
71
|
+
}
|
|
72
|
+
})();
|
|
73
|
+
|
|
74
|
+
return () => { cancelled = true; };
|
|
75
|
+
}, [code]);
|
|
76
|
+
|
|
77
|
+
// Reset view when code changes
|
|
78
|
+
useEffect(() => {
|
|
79
|
+
scaleRef.current = 1;
|
|
80
|
+
translateRef.current = { x: 0, y: 0 };
|
|
81
|
+
rerender();
|
|
82
|
+
}, [code, rerender]);
|
|
83
|
+
|
|
84
|
+
// Native wheel listener (non-passive so we can preventDefault)
|
|
85
|
+
useEffect(() => {
|
|
86
|
+
const viewport = viewportRef.current;
|
|
87
|
+
if (!viewport) return;
|
|
88
|
+
|
|
89
|
+
function onWheel(e: WheelEvent) {
|
|
90
|
+
e.preventDefault();
|
|
91
|
+
|
|
92
|
+
const oldScale = scaleRef.current;
|
|
93
|
+
const factor = e.deltaY > 0 ? 1 - ZOOM_FACTOR : 1 + ZOOM_FACTOR;
|
|
94
|
+
const newScale = Math.min(MAX_SCALE, Math.max(MIN_SCALE, oldScale * factor));
|
|
95
|
+
|
|
96
|
+
// Zoom toward cursor position
|
|
97
|
+
const rect = viewport!.getBoundingClientRect();
|
|
98
|
+
const cursorX = e.clientX - rect.left;
|
|
99
|
+
const cursorY = e.clientY - rect.top;
|
|
100
|
+
|
|
101
|
+
const t = translateRef.current;
|
|
102
|
+
const ratio = newScale / oldScale;
|
|
103
|
+
translateRef.current = {
|
|
104
|
+
x: cursorX - ratio * (cursorX - t.x),
|
|
105
|
+
y: cursorY - ratio * (cursorY - t.y),
|
|
106
|
+
};
|
|
107
|
+
scaleRef.current = newScale;
|
|
108
|
+
rerender();
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
viewport.addEventListener('wheel', onWheel, { passive: false });
|
|
112
|
+
return () => viewport.removeEventListener('wheel', onWheel);
|
|
113
|
+
}, [rerender]);
|
|
114
|
+
|
|
115
|
+
// Track the active pointer so a stray second touch (e.g. user resting a
|
|
116
|
+
// thumb mid-drag) doesn't hijack the pan. Single-pointer pan only; pinch
|
|
117
|
+
// zoom is intentionally left to the visible +/- buttons for now.
|
|
118
|
+
const activePointerId = useRef<number | null>(null);
|
|
119
|
+
|
|
120
|
+
const handlePointerDown = useCallback((e: React.PointerEvent) => {
|
|
121
|
+
// For mouse pointers, only react to the primary button.
|
|
122
|
+
if (e.pointerType === 'mouse' && e.button !== 0) return;
|
|
123
|
+
if (activePointerId.current !== null) return;
|
|
124
|
+
activePointerId.current = e.pointerId;
|
|
125
|
+
// setPointerCapture keeps move/up events flowing to this element even if
|
|
126
|
+
// the pointer drifts outside its bounds — without it, releasing a touch
|
|
127
|
+
// outside the diagram never fires pointerup and the diagram stays "stuck"
|
|
128
|
+
// in panning mode.
|
|
129
|
+
e.currentTarget.setPointerCapture(e.pointerId);
|
|
130
|
+
setIsPanning(true);
|
|
131
|
+
panStart.current = { x: e.clientX, y: e.clientY };
|
|
132
|
+
translateStart.current = { ...translateRef.current };
|
|
133
|
+
}, []);
|
|
134
|
+
|
|
135
|
+
const handlePointerMove = useCallback((e: React.PointerEvent) => {
|
|
136
|
+
if (activePointerId.current !== e.pointerId) return;
|
|
137
|
+
translateRef.current = {
|
|
138
|
+
x: translateStart.current.x + (e.clientX - panStart.current.x),
|
|
139
|
+
y: translateStart.current.y + (e.clientY - panStart.current.y),
|
|
140
|
+
};
|
|
141
|
+
rerender();
|
|
142
|
+
}, [rerender]);
|
|
143
|
+
|
|
144
|
+
const handlePointerUp = useCallback((e: React.PointerEvent) => {
|
|
145
|
+
if (activePointerId.current !== e.pointerId) return;
|
|
146
|
+
// Symmetric with the setPointerCapture in handlePointerDown. Browsers
|
|
147
|
+
// typically release capture implicitly on pointerup, but pointercancel
|
|
148
|
+
// semantics vary across engines and `releasePointerCapture` is a no-op
|
|
149
|
+
// when the pointer is no longer captured, so calling it unconditionally
|
|
150
|
+
// here keeps both code paths consistent.
|
|
151
|
+
if (e.currentTarget.hasPointerCapture(e.pointerId)) {
|
|
152
|
+
e.currentTarget.releasePointerCapture(e.pointerId);
|
|
153
|
+
}
|
|
154
|
+
activePointerId.current = null;
|
|
155
|
+
setIsPanning(false);
|
|
156
|
+
}, []);
|
|
157
|
+
|
|
158
|
+
const zoomIn = useCallback(() => {
|
|
159
|
+
const viewport = viewportRef.current;
|
|
160
|
+
if (!viewport) return;
|
|
161
|
+
const rect = viewport.getBoundingClientRect();
|
|
162
|
+
const cx = rect.width / 2;
|
|
163
|
+
const cy = rect.height / 2;
|
|
164
|
+
|
|
165
|
+
const oldScale = scaleRef.current;
|
|
166
|
+
const newScale = Math.min(MAX_SCALE, oldScale * (1 + ZOOM_FACTOR));
|
|
167
|
+
const ratio = newScale / oldScale;
|
|
168
|
+
const t = translateRef.current;
|
|
169
|
+
translateRef.current = { x: cx - ratio * (cx - t.x), y: cy - ratio * (cy - t.y) };
|
|
170
|
+
scaleRef.current = newScale;
|
|
171
|
+
rerender();
|
|
172
|
+
}, [rerender]);
|
|
173
|
+
|
|
174
|
+
const zoomOut = useCallback(() => {
|
|
175
|
+
const viewport = viewportRef.current;
|
|
176
|
+
if (!viewport) return;
|
|
177
|
+
const rect = viewport.getBoundingClientRect();
|
|
178
|
+
const cx = rect.width / 2;
|
|
179
|
+
const cy = rect.height / 2;
|
|
180
|
+
|
|
181
|
+
const oldScale = scaleRef.current;
|
|
182
|
+
const newScale = Math.max(MIN_SCALE, oldScale * (1 - ZOOM_FACTOR));
|
|
183
|
+
const ratio = newScale / oldScale;
|
|
184
|
+
const t = translateRef.current;
|
|
185
|
+
translateRef.current = { x: cx - ratio * (cx - t.x), y: cy - ratio * (cy - t.y) };
|
|
186
|
+
scaleRef.current = newScale;
|
|
187
|
+
rerender();
|
|
188
|
+
}, [rerender]);
|
|
189
|
+
|
|
190
|
+
const resetView = useCallback(() => {
|
|
191
|
+
scaleRef.current = 1;
|
|
192
|
+
translateRef.current = { x: 0, y: 0 };
|
|
193
|
+
rerender();
|
|
194
|
+
}, [rerender]);
|
|
195
|
+
|
|
196
|
+
if (error) {
|
|
197
|
+
return (
|
|
198
|
+
<div className="rounded-lg border border-red-200/50 bg-red-50 p-4 my-4">
|
|
199
|
+
<p className="text-xs text-red-600 font-medium mb-1">Mermaid diagram error</p>
|
|
200
|
+
<pre className="text-xs text-red-700/70 whitespace-pre-wrap">{error}</pre>
|
|
201
|
+
<pre className="mt-2 text-xs text-slate-600 whitespace-pre-wrap">{code}</pre>
|
|
202
|
+
</div>
|
|
203
|
+
);
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
const scale = scaleRef.current;
|
|
207
|
+
const translate = translateRef.current;
|
|
208
|
+
|
|
209
|
+
return (
|
|
210
|
+
<div className="relative my-4 rounded-lg border border-slate-300 bg-white overflow-hidden group">
|
|
211
|
+
{/* Zoom controls */}
|
|
212
|
+
<div className="absolute top-2 right-2 z-10 flex items-center gap-1 opacity-0 group-hover:opacity-100 transition-opacity">
|
|
213
|
+
<button
|
|
214
|
+
onClick={zoomOut}
|
|
215
|
+
className="p-1.5 rounded bg-slate-100/90 hover:bg-slate-200 text-slate-600 hover:text-slate-900 transition-colors"
|
|
216
|
+
title="Zoom out"
|
|
217
|
+
>
|
|
218
|
+
<ZoomOut size={14} />
|
|
219
|
+
</button>
|
|
220
|
+
<span className="text-xs text-slate-600 min-w-[3rem] text-center tabular-nums">
|
|
221
|
+
{Math.round(scale * 100)}%
|
|
222
|
+
</span>
|
|
223
|
+
<button
|
|
224
|
+
onClick={zoomIn}
|
|
225
|
+
className="p-1.5 rounded bg-slate-100/90 hover:bg-slate-200 text-slate-600 hover:text-slate-900 transition-colors"
|
|
226
|
+
title="Zoom in"
|
|
227
|
+
>
|
|
228
|
+
<ZoomIn size={14} />
|
|
229
|
+
</button>
|
|
230
|
+
<button
|
|
231
|
+
onClick={resetView}
|
|
232
|
+
className="p-1.5 rounded bg-slate-100/90 hover:bg-slate-200 text-slate-600 hover:text-slate-900 transition-colors"
|
|
233
|
+
title="Reset view"
|
|
234
|
+
>
|
|
235
|
+
<Maximize2 size={14} />
|
|
236
|
+
</button>
|
|
237
|
+
</div>
|
|
238
|
+
|
|
239
|
+
{/* Pannable / zoomable diagram area */}
|
|
240
|
+
<div
|
|
241
|
+
ref={viewportRef}
|
|
242
|
+
className="overflow-hidden"
|
|
243
|
+
// touchAction: 'none' stops the browser from claiming the gesture for
|
|
244
|
+
// page scrolling, so a finger drag inside the diagram pans the SVG
|
|
245
|
+
// instead of scrolling the chat behind it.
|
|
246
|
+
style={{
|
|
247
|
+
cursor: isPanning ? 'grabbing' : 'grab',
|
|
248
|
+
minHeight: 200,
|
|
249
|
+
touchAction: 'none',
|
|
250
|
+
}}
|
|
251
|
+
onPointerDown={handlePointerDown}
|
|
252
|
+
onPointerMove={handlePointerMove}
|
|
253
|
+
onPointerUp={handlePointerUp}
|
|
254
|
+
onPointerCancel={handlePointerUp}
|
|
255
|
+
>
|
|
256
|
+
<div
|
|
257
|
+
ref={svgContainerRef}
|
|
258
|
+
className="p-4 [&_svg]:mx-auto"
|
|
259
|
+
style={{
|
|
260
|
+
transform: `translate(${translate.x}px, ${translate.y}px) scale(${scale})`,
|
|
261
|
+
transformOrigin: '0 0',
|
|
262
|
+
transition: isPanning ? 'none' : 'transform 0.15s ease-out',
|
|
263
|
+
}}
|
|
264
|
+
/>
|
|
265
|
+
</div>
|
|
266
|
+
</div>
|
|
267
|
+
);
|
|
268
|
+
});
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import { useState, useEffect } from 'react';
|
|
2
|
+
import { useWorkspace } from '../../state/workspace.context';
|
|
3
|
+
import { authFetch } from '../../../../lib/api';
|
|
4
|
+
import type { FileRendererProps } from './types';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Inline PDF viewer. Fetches the binary from `/api/workspace/:id/file/raw`,
|
|
8
|
+
* wraps it in a blob URL, and hands it to the browser's native PDF.js viewer
|
|
9
|
+
* via `<iframe>`. Zero JS PDF dependencies — toolbar, zoom, page navigation,
|
|
10
|
+
* search and print all come from Chromium/Firefox. The MIME map entry for
|
|
11
|
+
* `.pdf` in `workspace.routes.ts` is what makes the iframe render inline
|
|
12
|
+
* instead of triggering a download.
|
|
13
|
+
*/
|
|
14
|
+
export function PdfRenderer({ filePath }: FileRendererProps) {
|
|
15
|
+
const { workspaceId } = useWorkspace();
|
|
16
|
+
const [objectUrl, setObjectUrl] = useState<string | null>(null);
|
|
17
|
+
const [error, setError] = useState<string | null>(null);
|
|
18
|
+
|
|
19
|
+
useEffect(() => {
|
|
20
|
+
// Reset stale state from a prior filePath / workspaceId before starting
|
|
21
|
+
// the new fetch — including the case where the workspace is being torn
|
|
22
|
+
// down (workspaceId → null). Without resetting on the null branch, the
|
|
23
|
+
// previous effect's cleanup revokes the blob URL but `objectUrl` is
|
|
24
|
+
// still in state, leaving the iframe pointing at a revoked URL.
|
|
25
|
+
setError(null);
|
|
26
|
+
setObjectUrl(null);
|
|
27
|
+
|
|
28
|
+
if (!workspaceId) return;
|
|
29
|
+
|
|
30
|
+
let revoked = false;
|
|
31
|
+
let createdUrl: string | null = null;
|
|
32
|
+
(async () => {
|
|
33
|
+
try {
|
|
34
|
+
const res = await authFetch(
|
|
35
|
+
`/api/workspace/${workspaceId}/file/raw?path=${encodeURIComponent(filePath)}`,
|
|
36
|
+
);
|
|
37
|
+
if (revoked) return;
|
|
38
|
+
if (!res.ok) {
|
|
39
|
+
setError(`Failed to load PDF (HTTP ${res.status})`);
|
|
40
|
+
return;
|
|
41
|
+
}
|
|
42
|
+
const blob = await res.blob();
|
|
43
|
+
if (revoked) return;
|
|
44
|
+
createdUrl = URL.createObjectURL(blob);
|
|
45
|
+
setObjectUrl(createdUrl);
|
|
46
|
+
} catch (e) {
|
|
47
|
+
if (revoked) return;
|
|
48
|
+
setError(e instanceof Error ? e.message : String(e));
|
|
49
|
+
}
|
|
50
|
+
})();
|
|
51
|
+
|
|
52
|
+
return () => {
|
|
53
|
+
revoked = true;
|
|
54
|
+
if (createdUrl) URL.revokeObjectURL(createdUrl);
|
|
55
|
+
};
|
|
56
|
+
}, [workspaceId, filePath]);
|
|
57
|
+
|
|
58
|
+
if (error) {
|
|
59
|
+
return (
|
|
60
|
+
<div className="flex items-center justify-center h-full text-red-600 text-sm">
|
|
61
|
+
{error}
|
|
62
|
+
</div>
|
|
63
|
+
);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
if (!objectUrl) {
|
|
67
|
+
return (
|
|
68
|
+
<div className="flex items-center justify-center h-full text-slate-600 text-sm">
|
|
69
|
+
Loading PDF...
|
|
70
|
+
</div>
|
|
71
|
+
);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
return (
|
|
75
|
+
<iframe
|
|
76
|
+
src={objectUrl}
|
|
77
|
+
title={filePath}
|
|
78
|
+
className="w-full h-full border-0"
|
|
79
|
+
/>
|
|
80
|
+
);
|
|
81
|
+
}
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import { useState, useCallback, useEffect } from 'react';
|
|
2
|
+
import type { FileRendererProps, RendererSaveState } from './types';
|
|
3
|
+
|
|
4
|
+
export function TextRenderer({
|
|
5
|
+
content,
|
|
6
|
+
savedContent,
|
|
7
|
+
filePath,
|
|
8
|
+
onSave,
|
|
9
|
+
onDirtyChange,
|
|
10
|
+
onValueChange,
|
|
11
|
+
onSaveStateChange,
|
|
12
|
+
readOnly = false,
|
|
13
|
+
}: FileRendererProps) {
|
|
14
|
+
const [value, setValue] = useState(content);
|
|
15
|
+
const [savedValue, setSavedValue] = useState(savedContent ?? content);
|
|
16
|
+
const [saveState, setSaveState] = useState<RendererSaveState>('idle');
|
|
17
|
+
const [saveError, setSaveError] = useState<string | null>(null);
|
|
18
|
+
|
|
19
|
+
const dirty = !readOnly && value !== savedValue;
|
|
20
|
+
|
|
21
|
+
// Reset editor state whenever EITHER the content or the filePath changes.
|
|
22
|
+
// File identity matters: switching between two files with identical content
|
|
23
|
+
// must still reset so a prior save/dirty state doesn't leak across files.
|
|
24
|
+
useEffect(() => {
|
|
25
|
+
setValue(content);
|
|
26
|
+
setSavedValue(savedContent ?? content);
|
|
27
|
+
setSaveState('idle');
|
|
28
|
+
setSaveError(null);
|
|
29
|
+
}, [content, savedContent, filePath]);
|
|
30
|
+
|
|
31
|
+
useEffect(() => {
|
|
32
|
+
onDirtyChange?.(dirty);
|
|
33
|
+
}, [dirty, onDirtyChange]);
|
|
34
|
+
|
|
35
|
+
useEffect(() => {
|
|
36
|
+
onSaveStateChange?.(saveState);
|
|
37
|
+
}, [saveState, onSaveStateChange]);
|
|
38
|
+
|
|
39
|
+
const save = useCallback(async (): Promise<boolean> => {
|
|
40
|
+
if (readOnly || value === savedValue) return true;
|
|
41
|
+
setSaveState('saving');
|
|
42
|
+
setSaveError(null);
|
|
43
|
+
try {
|
|
44
|
+
await onSave(value);
|
|
45
|
+
setSavedValue(value);
|
|
46
|
+
setSaveState('idle');
|
|
47
|
+
return true;
|
|
48
|
+
} catch (err) {
|
|
49
|
+
setSaveState('error');
|
|
50
|
+
setSaveError(err instanceof Error ? err.message : 'Failed to save');
|
|
51
|
+
return false;
|
|
52
|
+
}
|
|
53
|
+
}, [readOnly, value, savedValue, onSave]);
|
|
54
|
+
|
|
55
|
+
const handleChange = useCallback((e: React.ChangeEvent<HTMLTextAreaElement>) => {
|
|
56
|
+
const next = e.target.value;
|
|
57
|
+
setValue(next);
|
|
58
|
+
onValueChange?.(next);
|
|
59
|
+
}, [onValueChange]);
|
|
60
|
+
|
|
61
|
+
const handleKeyDown = useCallback((e: React.KeyboardEvent) => {
|
|
62
|
+
if ((e.ctrlKey || e.metaKey) && e.key === 's') {
|
|
63
|
+
e.preventDefault();
|
|
64
|
+
void save();
|
|
65
|
+
}
|
|
66
|
+
}, [save]);
|
|
67
|
+
|
|
68
|
+
return (
|
|
69
|
+
<div className="flex flex-col h-full min-h-0">
|
|
70
|
+
<textarea
|
|
71
|
+
className="w-full flex-1 min-h-0 bg-transparent text-sm text-slate-700 font-mono whitespace-pre-wrap break-words leading-relaxed resize-none outline-none"
|
|
72
|
+
value={value}
|
|
73
|
+
onChange={readOnly ? undefined : handleChange}
|
|
74
|
+
onKeyDown={readOnly ? undefined : handleKeyDown}
|
|
75
|
+
readOnly={readOnly}
|
|
76
|
+
spellCheck={false}
|
|
77
|
+
/>
|
|
78
|
+
{!readOnly && (
|
|
79
|
+
<div className="pt-2 text-xs min-h-5">
|
|
80
|
+
{saveState === 'saving' && <span className="text-slate-600">Saving…</span>}
|
|
81
|
+
{saveState === 'error' && saveError && <span className="text-red-600">{saveError}</span>}
|
|
82
|
+
</div>
|
|
83
|
+
)}
|
|
84
|
+
</div>
|
|
85
|
+
);
|
|
86
|
+
}
|