generated from amazon-archives/__template_Apache-2.0
-
Notifications
You must be signed in to change notification settings - Fork 40
[Transformation Playground] Transformation UI #1489
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
mikaylathompson
merged 26 commits into
opensearch-project:main
from
mikaylathompson:playground-transformations
May 2, 2025
Merged
Changes from 19 commits
Commits
Show all changes
26 commits
Select commit
Hold shift + click to select a range
7c08eef
Basic file uploading
mikaylathompson f518b71
Improve error checking/display
mikaylathompson b06d1b9
Add code view popover
mikaylathompson 35eba9d
format content before displaying
mikaylathompson 60ab771
Add eslint rule and fixes for relative imports
mikaylathompson 2e2bdbc
Refactor
mikaylathompson 769b07c
Add delete option
mikaylathompson b3fa40e
Add tests
mikaylathompson 9b52d58
Change ndjson handling (separate docs), update tests
mikaylathompson deed872
Deal with size limits for local storage
mikaylathompson 592f33c
Add editing feature
mikaylathompson 140748c
rough draft of transformation panel
mikaylathompson 0e446ad
mostly working version of editors
mikaylathompson 35f0be2
Fix the twitchiness and invisible highlighting
mikaylathompson 51214ec
Formatting, save status, etc.
mikaylathompson 7c5f98d
Handle BoardItem resizing for the editor
mikaylathompson 91529e3
Default content, cleanup, sizing
mikaylathompson 491ed39
Merge branch 'main' into playground-transformations
mikaylathompson 264d068
Sonarqube fixes
mikaylathompson 308127a
Fix editor bugs (erasing changes) and clearer error display
mikaylathompson 89ca8a6
Linter fixes
mikaylathompson 4ac6a2e
Add missing files
mikaylathompson 28d66cd
sonarqube fixes
mikaylathompson 1b3aefc
Incorporate review feedback
mikaylathompson 2ee8b96
Remove all custom styles in favor of Cloudspace components
mikaylathompson 1a60b63
Add SaveStatusIndicator tests
mikaylathompson 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
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
218 changes: 218 additions & 0 deletions
218
frontend/src/components/playground/AceEditorComponent.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,218 @@ | ||
"use client"; | ||
|
||
import { useState, useEffect, useRef, useCallback } from "react"; | ||
import AceEditor, { IAnnotation } from "react-ace"; | ||
import { usePlayground } from "@/context/PlaygroundContext"; | ||
import { usePlaygroundActions } from "@/hooks/usePlaygroundActions"; | ||
|
||
// Import ace-builds core | ||
import ace from "ace-builds"; | ||
import beautify from "ace-builds/src-noconflict/ext-beautify"; | ||
|
||
// Import modes | ||
import "ace-builds/src-noconflict/mode-json"; | ||
import "ace-builds/src-noconflict/mode-javascript"; | ||
import "ace-builds/src-noconflict/theme-github"; | ||
import "ace-builds/src-noconflict/ext-language_tools"; | ||
// This seems like it should be imported from the line above, but there are missing values (like showing highlighted text) without this | ||
import "ace-builds/css/theme/github.css"; | ||
|
||
// Import workers | ||
import jsonWorkerUrl from "ace-builds/src-noconflict/worker-json"; | ||
import javascriptWorkerUrl from "ace-builds/src-noconflict/worker-javascript"; | ||
|
||
// Configure Ace to use the imported workers | ||
ace.config.setModuleUrl("ace/mode/json_worker", jsonWorkerUrl); | ||
ace.config.setModuleUrl("ace/mode/javascript_worker", javascriptWorkerUrl); | ||
|
||
interface AceEditorComponentProps { | ||
peternied marked this conversation as resolved.
Show resolved
Hide resolved
|
||
itemId: string; | ||
mode?: "json" | "javascript"; | ||
formatRef?: React.RefObject<(() => void) | null>; | ||
onSaveStatusChange?: (isSaved: boolean) => void; | ||
} | ||
|
||
const defaultContent: string = ` | ||
function main(context) { | ||
return (document) => { | ||
// Your transformation logic here | ||
return document; | ||
}; | ||
} | ||
// Entrypoint function | ||
(() => main)(); | ||
`; | ||
|
||
export default function AceEditorComponent({ | ||
itemId, | ||
mode = "json", | ||
formatRef, | ||
onSaveStatusChange, | ||
}: Readonly<AceEditorComponentProps>) { | ||
const { state } = usePlayground(); | ||
const { updateTransformation } = usePlaygroundActions(); | ||
const [content, setContent] = useState(""); | ||
peternied marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// Use a ref instead of state for validation errors to prevent re-renders | ||
const validationErrorsRef = useRef<IAnnotation[]>([]); | ||
const editorRef = useRef<AceEditor>(null); | ||
const containerRef = useRef<HTMLDivElement>(null); | ||
const [dimensions, setDimensions] = useState({ width: 500, height: 300 }); // Default fallback values | ||
peternied marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
||
// Find the transformation by ID | ||
const transformation = state.transformations.find((t) => t.id === itemId); | ||
|
||
// Set up ResizeObserver to monitor container size | ||
useEffect(() => { | ||
if (!containerRef.current) return; | ||
|
||
const resizeObserver = new ResizeObserver((entries) => { | ||
const { width, height } = entries[0].contentRect; | ||
// Subtract some padding for better appearance | ||
setDimensions({ | ||
width: Math.max(width - 20, 100), // Ensure minimum width | ||
height: Math.max(height - 20, 100), // Ensure minimum height | ||
}); | ||
}); | ||
|
||
resizeObserver.observe(containerRef.current); | ||
return () => resizeObserver.disconnect(); | ||
}, []); | ||
|
||
// Initialize content from the transformation | ||
useEffect(() => { | ||
if (transformation) { | ||
setContent(transformation.content || defaultContent); | ||
if (onSaveStatusChange) { | ||
onSaveStatusChange(true); | ||
} | ||
} | ||
}, [transformation, onSaveStatusChange]); | ||
|
||
// Save the current content to local storage | ||
const saveContent = useCallback(() => { | ||
if (!transformation || content === transformation.content) return; | ||
if (validationErrorsRef.current.length > 0) { | ||
console.log("Validation errors:", validationErrorsRef.current); | ||
return; | ||
} | ||
|
||
updateTransformation(itemId, transformation.name, content); | ||
if (onSaveStatusChange) { | ||
onSaveStatusChange(true); | ||
} | ||
}, [ | ||
content, | ||
itemId, | ||
transformation, | ||
updateTransformation, | ||
onSaveStatusChange, | ||
]); | ||
|
||
// Format the code based on the mode | ||
const formatCode = useCallback(() => { | ||
if (!content) return; | ||
try { | ||
console.log("Formatting code..."); | ||
if (editorRef.current) { | ||
beautify.beautify(editorRef.current.editor.session); | ||
} | ||
} catch (error) { | ||
console.error("Error formatting code:", error); | ||
} | ||
saveContent(); | ||
}, [content, saveContent]); | ||
|
||
// Handle keyboard shortcuts | ||
const handleKeyDown = useCallback( | ||
(event: KeyboardEvent) => { | ||
// Check for Ctrl+S or Cmd+S | ||
if ((event.ctrlKey || event.metaKey) && event.key === "s") { | ||
event.preventDefault(); | ||
saveContent(); | ||
} | ||
}, | ||
[saveContent], | ||
); | ||
|
||
// Add keyboard event listener | ||
useEffect(() => { | ||
const editor = editorRef.current?.editor; | ||
if (editor) { | ||
editor.container.addEventListener("keydown", handleKeyDown); | ||
} | ||
|
||
return () => { | ||
if (editor) { | ||
editor.container.removeEventListener("keydown", handleKeyDown); | ||
} | ||
}; | ||
}, [handleKeyDown]); | ||
|
||
// Handle content change and save (debounce is handled internally by AceEditor) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Perfect comment to mention de-bouncing :D |
||
const handleChange = (newContent: string) => { | ||
setContent(newContent); | ||
|
||
// Skip update if transformation doesn't exist | ||
if (!transformation) { | ||
return; | ||
} | ||
|
||
// Mark as unsaved if content is different from saved content | ||
const savedStatus = transformation.content === newContent; | ||
if (onSaveStatusChange) { | ||
onSaveStatusChange(savedStatus); | ||
} | ||
|
||
// Auto-save after debounce period (handled by AceEditor) | ||
console.log("Updating transformation:", transformation.name); | ||
saveContent(); | ||
}; | ||
|
||
// Expose formatCode function to parent component via ref | ||
useEffect(() => { | ||
if (formatRef) { | ||
formatRef.current = formatCode; | ||
} | ||
}, [formatCode, formatRef]); | ||
|
||
return ( | ||
<div | ||
ref={containerRef} | ||
style={{ width: "100%", height: "100%", minHeight: "200px" }} | ||
> | ||
<AceEditor | ||
ref={editorRef} | ||
mode={mode} | ||
theme="github" | ||
value={content} | ||
onChange={handleChange} | ||
onValidate={(errors) => { | ||
// The UI gets "twitchy" if we set state (and therefore re-render) on every validation | ||
// So we're using a ref to store the errors instead | ||
validationErrorsRef.current = errors as IAnnotation[]; | ||
}} | ||
name={itemId} | ||
debounceChangePeriod={500} | ||
width={`${dimensions.width}px`} | ||
height={`${dimensions.height}px`} | ||
editorProps={{ $blockScrolling: false }} | ||
setOptions={{ | ||
enableBasicAutocompletion: true, | ||
}} | ||
showGutter={true} | ||
showPrintMargin={false} | ||
highlightActiveLine={true} | ||
minLines={10} | ||
tabSize={2} | ||
commands={beautify.commands.map((command) => ({ | ||
name: command.name, | ||
bindKey: | ||
typeof command.bindKey === "string" | ||
? { win: command.bindKey, mac: command.bindKey } | ||
: command.bindKey, | ||
exec: command.exec, | ||
}))} | ||
/> | ||
</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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm curious if it is easy to see this break as you've been dev'ing on this component, trying to get an understanding of if we should have UX tests to verify callback/intervals being triggered correctly.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oof. It's not a bad idea, but I'm truly dreading the implementation of that and a lot of this is in flux as I work on the execution step, so it feels like work that's going to change pretty dramatically. Can we postpone for now? I can create a task to track this work.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
https://opensearch.atlassian.net/browse/MIGRATIONS-2520