-
Notifications
You must be signed in to change notification settings - Fork 0
feat: add in-browser text file editor modal #87
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
4 commits
Select commit
Hold shift + click to select a range
7b3e915
docs: start work on text editor modal todo
FSM1 8ecd4c3
feat: add in-browser text file editor with full crypto round-trip
FSM1 9b64e0c
fix: address PR review feedback for text editor modal
FSM1 f166716
fix(e2e): increase test timeout for page reload test
FSM1 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
File renamed without changes.
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
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
212 changes: 212 additions & 0 deletions
212
apps/web/src/components/file-browser/TextEditorDialog.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,212 @@ | ||
| import { useState, useEffect, useCallback, useRef } from 'react'; | ||
| import type { FileEntry } from '@cipherbox/crypto'; | ||
| import { Modal } from '../ui/Modal'; | ||
| import { useAuthStore } from '../../stores/auth.store'; | ||
| import { useFolder } from '../../hooks/useFolder'; | ||
| import { downloadFile } from '../../services/download.service'; | ||
| import { encryptFile } from '../../services/file-crypto.service'; | ||
| import { addToIpfs } from '../../lib/api/ipfs'; | ||
| import '../../styles/text-editor-dialog.css'; | ||
|
|
||
| type TextEditorDialogProps = { | ||
| open: boolean; | ||
| onClose: () => void; | ||
| item: FileEntry | null; | ||
| parentFolderId: string; | ||
| }; | ||
|
|
||
| /** | ||
| * Modal dialog for editing text files in-browser. | ||
| * | ||
| * Full crypto round-trip: download -> decrypt -> edit -> encrypt -> re-upload | ||
| * -> update folder metadata -> unpin old CID. | ||
| */ | ||
| export function TextEditorDialog({ open, onClose, item, parentFolderId }: TextEditorDialogProps) { | ||
| const [loading, setLoading] = useState(false); | ||
| const [saving, setSaving] = useState(false); | ||
| const [content, setContent] = useState(''); | ||
| const [originalContent, setOriginalContent] = useState(''); | ||
| const [error, setError] = useState<string | null>(null); | ||
| const textareaRef = useRef<HTMLTextAreaElement>(null); | ||
|
|
||
| const { updateFile } = useFolder(); | ||
|
|
||
| const isDirty = content !== originalContent; | ||
| const lineCount = content.split('\n').length; | ||
|
|
||
| // Load file content when dialog opens | ||
| useEffect(() => { | ||
| if (!open || !item) { | ||
| setContent(''); | ||
| setOriginalContent(''); | ||
| setError(null); | ||
| setLoading(false); | ||
| setSaving(false); | ||
| return; | ||
| } | ||
|
|
||
| let cancelled = false; | ||
| setLoading(true); | ||
| setError(null); | ||
|
|
||
| (async () => { | ||
| try { | ||
| const auth = useAuthStore.getState(); | ||
| if (!auth.derivedKeypair) { | ||
| throw new Error('No keypair available - please log in again'); | ||
| } | ||
|
|
||
| const plaintext = await downloadFile( | ||
| { | ||
| cid: item.cid, | ||
| iv: item.fileIv, | ||
| wrappedKey: item.fileKeyEncrypted, | ||
| originalName: item.name, | ||
| }, | ||
| auth.derivedKeypair.privateKey | ||
| ); | ||
|
|
||
| if (cancelled) return; | ||
|
|
||
| const text = new TextDecoder().decode(plaintext); | ||
| setContent(text); | ||
| setOriginalContent(text); | ||
| setLoading(false); | ||
|
|
||
| // Focus textarea after content loads | ||
| requestAnimationFrame(() => { | ||
| textareaRef.current?.focus(); | ||
| }); | ||
| } catch (err) { | ||
| if (cancelled) return; | ||
| setError(err instanceof Error ? err.message : 'Failed to load file'); | ||
| setLoading(false); | ||
| } | ||
| })(); | ||
|
|
||
| return () => { | ||
| cancelled = true; | ||
| }; | ||
| }, [open, item]); | ||
|
|
||
| const handleSave = useCallback(async () => { | ||
| if (!item || !isDirty) return; | ||
|
|
||
| setSaving(true); | ||
| setError(null); | ||
|
|
||
| try { | ||
| const auth = useAuthStore.getState(); | ||
| if (!auth.derivedKeypair) { | ||
| throw new Error('No keypair available - please log in again'); | ||
| } | ||
|
|
||
| // 1. Encode content | ||
| const encoded = new TextEncoder().encode(content); | ||
| const file = new File([encoded], item.name); | ||
|
|
||
| // 2. Encrypt with new key/IV | ||
| const encrypted = await encryptFile(file, auth.derivedKeypair.publicKey); | ||
|
|
||
| // 3. Upload to IPFS | ||
| // Use .slice() to get a clean copy with its own ArrayBuffer, | ||
| // avoiding sub-view issues if ciphertext is an offset view. | ||
| const ciphertextBytes = encrypted.ciphertext.slice(); | ||
| const blob = new Blob([ciphertextBytes.buffer as ArrayBuffer]); | ||
| const { cid } = await addToIpfs(blob); | ||
|
|
||
| // 4. Update folder metadata | ||
| await updateFile(parentFolderId, { | ||
| fileId: item.id, | ||
| newCid: cid, | ||
| newFileKeyEncrypted: encrypted.wrappedKey, | ||
| newFileIv: encrypted.iv, | ||
| newSize: encrypted.originalSize, | ||
| }); | ||
|
|
||
| onClose(); | ||
| } catch (err) { | ||
| setError(err instanceof Error ? err.message : 'Failed to save file'); | ||
| setSaving(false); | ||
| } | ||
| }, [item, content, isDirty, parentFolderId, updateFile, onClose]); | ||
|
|
||
| // Handle Ctrl/Cmd+S to save | ||
| useEffect(() => { | ||
| if (!open) return; | ||
|
|
||
| const handleKeyDown = (e: KeyboardEvent) => { | ||
| if ((e.metaKey || e.ctrlKey) && e.key === 's') { | ||
| e.preventDefault(); | ||
| if (isDirty && !saving && !loading) { | ||
| handleSave(); | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| document.addEventListener('keydown', handleKeyDown); | ||
| return () => document.removeEventListener('keydown', handleKeyDown); | ||
| }, [open, isDirty, saving, loading, handleSave]); | ||
|
|
||
| if (!item) return null; | ||
|
|
||
| const title = `Edit: ${item.name}`; | ||
| const isDisabled = saving || loading; | ||
|
|
||
| return ( | ||
| <Modal | ||
| open={open} | ||
| onClose={isDisabled ? undefined : onClose} | ||
| title={title} | ||
| className="text-editor-modal" | ||
| > | ||
| {loading ? ( | ||
| <div className="text-editor-loading">decrypting...</div> | ||
| ) : ( | ||
| <div className="text-editor-body"> | ||
| <textarea | ||
| ref={textareaRef} | ||
| className="text-editor-textarea" | ||
| value={content} | ||
| onChange={(e) => setContent(e.target.value)} | ||
| disabled={isDisabled} | ||
| spellCheck={false} | ||
| aria-label={`Content of ${item.name}`} | ||
| /> | ||
| <div className={`text-editor-status${isDirty ? ' text-editor-status--modified' : ''}`}> | ||
| <span> | ||
| {'// '} | ||
| {lineCount} {lineCount === 1 ? 'line' : 'lines'} | ||
| {' | utf-8'} | ||
| {isDirty ? ' | modified' : ''} | ||
| </span> | ||
| </div> | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| {error && ( | ||
| <div className="text-editor-error"> | ||
| {'> '} | ||
| {error} | ||
| </div> | ||
| )} | ||
| <div className="text-editor-footer"> | ||
| <button | ||
| type="button" | ||
| className="dialog-button dialog-button--secondary" | ||
| onClick={onClose} | ||
| disabled={isDisabled} | ||
| > | ||
| cancel | ||
| </button> | ||
| <button | ||
| type="button" | ||
| className="dialog-button dialog-button--primary" | ||
| onClick={handleSave} | ||
| disabled={isDisabled || !isDirty} | ||
| > | ||
| {saving ? 'encrypting...' : '--save'} | ||
| </button> | ||
| </div> | ||
| </div> | ||
| )} | ||
| </Modal> | ||
| ); | ||
| } | ||
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.