-
Notifications
You must be signed in to change notification settings - Fork 159
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: enhance AI assistant ui/x (#583)
- Loading branch information
Showing
8 changed files
with
397 additions
and
393 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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 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,92 @@ | ||
import { useRef, useState } from 'react'; | ||
import Editor from '@monaco-editor/react'; | ||
import { Box, IconButton, Stack, useTheme } from '@mui/material'; | ||
import { ContentCopy } from '@mui/icons-material'; | ||
import { TooltipHint } from './TooltipHint'; | ||
import { codeEditorBaseStyles } from '../menus/CodeEditor/styles'; | ||
|
||
interface Props { | ||
code: string; | ||
language?: string; | ||
} | ||
|
||
export function CodeSnippet({ code, language = 'plaintext' }: Props) { | ||
const [tooltipMsg, setTooltipMsg] = useState<string>('Click to copy'); | ||
const editorRef = useRef(null); | ||
const theme = useTheme(); | ||
|
||
const handleClick = (e: any) => { | ||
if (editorRef.current) { | ||
navigator.clipboard.writeText(code); | ||
setTooltipMsg('Copied!'); | ||
setTimeout(() => { | ||
setTooltipMsg('Click to copy'); | ||
}, 2000); | ||
} | ||
}; | ||
|
||
const handleEditorDidMount = (editor: any) => { | ||
editorRef.current = editor; | ||
}; | ||
|
||
return ( | ||
<Box style={codeEditorBaseStyles}> | ||
<Stack | ||
direction="row" | ||
justifyContent="space-between" | ||
alignItems="center" | ||
spacing={1} | ||
sx={{ | ||
backgroundColor: theme.palette.grey['100'], | ||
pt: theme.spacing(0.5), | ||
pb: theme.spacing(0.5), | ||
// 10px on Monaco + 2px border | ||
pr: '12px', | ||
pl: '12px', | ||
}} | ||
> | ||
<Box sx={{ color: 'text.secondary' }}>{language}</Box> | ||
|
||
<TooltipHint title={tooltipMsg}> | ||
<IconButton onClick={handleClick} size="small"> | ||
<ContentCopy fontSize="inherit" /> | ||
</IconButton> | ||
</TooltipHint> | ||
</Stack> | ||
<div | ||
style={{ | ||
// calculate height based on number of lines | ||
height: `${Math.ceil(code.split('\n').length) * 19}px`, | ||
position: 'relative', | ||
border: `2px solid ${theme.palette.grey['100']}`, | ||
borderTop: 'none', | ||
}} | ||
> | ||
<Editor | ||
language={language} | ||
value={code} | ||
height="100%" | ||
width="100%" | ||
options={{ | ||
readOnly: true, | ||
minimap: { enabled: false }, | ||
overviewRulerLanes: 0, | ||
hideCursorInOverviewRuler: true, | ||
overviewRulerBorder: false, | ||
scrollbar: { | ||
vertical: 'hidden', | ||
handleMouseWheel: false, | ||
}, | ||
scrollBeyondLastLine: false, | ||
wordWrap: 'off', | ||
lineNumbers: 'off', | ||
automaticLayout: true, | ||
folding: false, | ||
renderLineHighlightOnlyWhenFocus: true, | ||
}} | ||
onMount={handleEditorDidMount} | ||
/> | ||
</div> | ||
</Box> | ||
); | ||
} |
This file contains 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,47 @@ | ||
import { parseCodeBlocks } from './AICodeBlockParser'; | ||
|
||
describe('parseCodeBlocks()', () => { | ||
test('A string without code blocks returns one item', () => { | ||
const result = parseCodeBlocks(`This is a string from AI.`); | ||
expect(result).toHaveLength(1); | ||
expect(typeof result[0]).toBe('string'); | ||
}); | ||
|
||
test('A string + 1 unclosed code block returns two items', () => { | ||
const result = parseCodeBlocks(` | ||
A string of text with an open code block. | ||
\`\`\`js | ||
const foo = 'foo';`); | ||
expect(result).toHaveLength(2); | ||
expect(typeof result[0]).toBe('string'); | ||
expect(typeof result[1]).toBe('object'); | ||
}); | ||
|
||
test('A string + 1 code block returns two items', () => { | ||
const result = parseCodeBlocks(` | ||
A string of text with a full code block. | ||
\`\`\`js | ||
const foo = 'foo'; | ||
\`\`\``); | ||
expect(result).toHaveLength(2); | ||
}); | ||
|
||
test('A string + 1 full code block + a string returns three items', () => { | ||
const result = parseCodeBlocks(` | ||
A string of text with a full code block. | ||
\`\`\`js | ||
const foo = 'foo'; | ||
\`\`\` | ||
And another piece of text | ||
And more text here`); | ||
expect(result).toHaveLength(3); | ||
expect(typeof result[0]).toBe('string'); | ||
expect(typeof result[1]).toBe('object'); | ||
expect(typeof result[2]).toBe('string'); | ||
}); | ||
}); |
This file contains 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.
ad68415
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.
Successfully deployed to the following URLs:
quadratic – ./
quadratic-nu.vercel.app
quadratic-quadratic.vercel.app
quadratic-git-main-quadratic.vercel.app