forked from BloopAI/bloop
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
File explanation bug fixes and improvements (BloopAI#848)
* fix headers sizes * file highlights fixes * highlight lines of code when hovering over file chip * open file modal instead of navigation for folder chip in article response * fix repo status in repo cards * disable continue button is email is invalid
- Loading branch information
1 parent
1210678
commit 2a00e63
Showing
19 changed files
with
412 additions
and
146 deletions.
There are no files selected for viewing
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
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
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
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
142 changes: 142 additions & 0 deletions
142
client/src/components/MarkdownWithCode/CodeRenderer.tsx
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,142 @@ | ||
import { | ||
Dispatch, | ||
memo, | ||
MutableRefObject, | ||
ReactNode, | ||
SetStateAction, | ||
useCallback, | ||
useMemo, | ||
} from 'react'; | ||
import FileChip from '../Chat/ConversationMessage/FileChip'; | ||
import CodeWithBreadcrumbs from '../../pages/ArticleResponse/CodeWithBreadcrumbs'; | ||
import { FileHighlightsType } from '../../types/general'; | ||
import NewCode from './NewCode'; | ||
|
||
type Props = { | ||
children: ReactNode[]; | ||
repoName: string; | ||
fileChips: MutableRefObject<never[]>; | ||
hideCode?: boolean; | ||
updateScrollToIndex: (lines: string) => void; | ||
openFileModal: ( | ||
path: string, | ||
scrollToLine?: string | undefined, | ||
highlightColor?: string | undefined, | ||
) => void; | ||
setFileHighlights: Dispatch<SetStateAction<FileHighlightsType>>; | ||
setHoveredLines: Dispatch<SetStateAction<[number, number] | null>>; | ||
className?: string; | ||
propsJSON: string; | ||
inline?: boolean; | ||
}; | ||
|
||
const CodeRenderer = ({ | ||
className, | ||
children, | ||
inline, | ||
hideCode, | ||
updateScrollToIndex, | ||
fileChips, | ||
setFileHighlights, | ||
setHoveredLines, | ||
openFileModal, | ||
repoName, | ||
propsJSON, | ||
}: Props) => { | ||
const matchLang = useMemo( | ||
() => | ||
/lang:(\w+)/.exec(className || '') || | ||
/language-(\w+)/.exec(className || ''), | ||
[className], | ||
); | ||
const matchType = useMemo( | ||
() => /language-type:(\w+)/.exec(className || ''), | ||
[className], | ||
); | ||
const matchPath = useMemo( | ||
() => /path:(.+),/.exec(className || ''), | ||
[className], | ||
); | ||
const matchLines = useMemo( | ||
() => /lines:(.+)/.exec(className || ''), | ||
[className], | ||
); | ||
const code = useMemo( | ||
() => | ||
typeof children[0] === 'string' ? children[0].replace(/\n$/, '') : '', | ||
[children], | ||
); | ||
const lines = useMemo( | ||
() => matchLines?.[1].split('-').map((l) => Number(l)) || [], | ||
[matchLines], | ||
); | ||
const colorPreview = useMemo( | ||
() => | ||
children[0] && | ||
children.length === 1 && | ||
typeof children[0] === 'string' && | ||
/(^#[0-9A-F]{6}$)|(^#[0-9A-F]{3}$)/i.test(children[0]) ? ( | ||
<span | ||
className="w-3 h-3 inline-block" | ||
style={{ backgroundColor: children[0] }} | ||
/> | ||
) : null, | ||
[children], | ||
); | ||
|
||
const linesToUse: [number, number] | undefined = useMemo( | ||
() => [lines[0] - 1, (lines[1] ?? lines[0]) - 1], | ||
[lines], | ||
); | ||
|
||
const handleChipClick = useCallback(() => { | ||
updateScrollToIndex(`${lines[0] - 1}_${(lines[1] ?? lines[0]) - 1}`); | ||
}, [updateScrollToIndex, lines]); | ||
|
||
return ( | ||
<> | ||
{!inline && | ||
(matchType?.[1] || matchLang?.[1]) && | ||
typeof children[0] === 'string' ? ( | ||
matchType?.[1] === 'Quoted' ? ( | ||
hideCode ? ( | ||
<FileChip | ||
fileName={matchPath?.[1] || ''} | ||
filePath={matchPath?.[1] || ''} | ||
skipIcon={false} | ||
onClick={handleChipClick} | ||
lines={linesToUse} | ||
fileChips={fileChips} | ||
setFileHighlights={setFileHighlights} | ||
setHoveredLines={setHoveredLines} | ||
/> | ||
) : ( | ||
<CodeWithBreadcrumbs | ||
code={code} | ||
language={matchLang?.[1] || ''} | ||
filePath={matchPath?.[1] || ''} | ||
onResultClick={openFileModal} | ||
startLine={lines[0] ? lines[0] - 1 : null} | ||
repoName={repoName} | ||
/> | ||
) | ||
) : ( | ||
<NewCode code={code} language={matchLang?.[1] || ''} /> | ||
) | ||
) : colorPreview ? ( | ||
<span className="inline-flex gap-1.5 items-center"> | ||
{colorPreview} | ||
<code {...JSON.parse(propsJSON)} className={className}> | ||
{children} | ||
</code> | ||
</span> | ||
) : ( | ||
<code {...JSON.parse(propsJSON)} className={className}> | ||
{children} | ||
</code> | ||
)} | ||
</> | ||
); | ||
}; | ||
|
||
export default memo(CodeRenderer); |
Oops, something went wrong.