-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(web-scraping): switch to the server-side web page content diff
- Loading branch information
Showing
3 changed files
with
17 additions
and
37 deletions.
There are no files selected for viewing
32 changes: 12 additions & 20 deletions
32
src/pages/workspace/utils/web_scraping/web_page_content_tracker_revision.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 |
---|---|---|
@@ -1,34 +1,26 @@ | ||
import { EuiCodeBlock, useEuiTextDiff } from '@elastic/eui'; | ||
import { EuiCodeBlock } from '@elastic/eui'; | ||
|
||
import type { WebPageContentRevision } from './web_page_data_revision'; | ||
|
||
export interface WebPageContentTrackerRevisionProps { | ||
revision: WebPageContentRevision; | ||
previousRevision?: WebPageContentRevision; | ||
showDiff?: boolean; | ||
} | ||
|
||
function getTextToRender(text: string): [string, string | undefined] { | ||
const parsedData = JSON.parse(text) as string | object; | ||
if (parsedData && typeof parsedData === 'object') { | ||
return [JSON.stringify(parsedData, null, 2), 'json']; | ||
export function WebPageContentTrackerRevision({ revision, showDiff }: WebPageContentTrackerRevisionProps) { | ||
let dataToRender; | ||
try { | ||
dataToRender = JSON.parse(revision.data) as string | object; | ||
if (typeof dataToRender !== 'string') { | ||
dataToRender = JSON.stringify(dataToRender, null, 2); | ||
} | ||
} catch { | ||
dataToRender = revision.data; | ||
} | ||
|
||
return [parsedData, undefined]; | ||
} | ||
|
||
export function WebPageContentTrackerRevision({ | ||
revision, | ||
previousRevision, | ||
showDiff, | ||
}: WebPageContentTrackerRevisionProps) { | ||
const [afterText, language] = getTextToRender(revision.data); | ||
const [beforeText] = previousRevision && showDiff ? getTextToRender(previousRevision.data) : [afterText]; | ||
|
||
const [textToRender] = useEuiTextDiff({ beforeText, afterText }); | ||
return ( | ||
<EuiCodeBlock fontSize={'l'} language={language} isCopyable> | ||
{!showDiff || afterText === beforeText ? afterText : textToRender} | ||
<EuiCodeBlock fontSize={'m'} language={showDiff && dataToRender.startsWith('@@') ? 'diff' : 'json'} isCopyable> | ||
{dataToRender} | ||
</EuiCodeBlock> | ||
); | ||
} |
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