Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2339 from botpress/ya-studio-components
feat(studio): added util components & change to cms
- Loading branch information
Showing
7 changed files
with
102 additions
and
1 deletion.
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
35 changes: 35 additions & 0 deletions
35
src/bp/ui-studio/src/web/components/Shared/Utils/Downloader.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,35 @@ | ||
import axios from 'axios' | ||
import React, { FC, useEffect, useRef, useState } from 'react' | ||
|
||
import { DownloaderProps } from './typings' | ||
|
||
export const Downloader: FC<DownloaderProps> = props => { | ||
const downloadLink = useRef(null) | ||
const [content, setContent] = useState<string>() | ||
const [filename, setFilename] = useState<string>() | ||
|
||
const startDownload = async (url: string, filename?: string, method: string = 'get') => { | ||
const { data, headers } = await axios({ method, url, responseType: 'blob' }) | ||
|
||
if (!filename) { | ||
const extractName = /filename=(.*\.)/.exec(headers['content-disposition']) | ||
filename = extractName && extractName[1] | ||
} | ||
|
||
setContent(window.URL.createObjectURL(new Blob([data]))) | ||
setFilename(filename) | ||
|
||
// @ts-ignore | ||
downloadLink.current!.click() | ||
props.onDownloadCompleted && props.onDownloadCompleted() | ||
} | ||
|
||
useEffect(() => { | ||
if (props.url) { | ||
// tslint:disable-next-line: no-floating-promises | ||
startDownload(props.url, props.filename) | ||
} | ||
}, [props.url]) | ||
|
||
return <a ref={downloadLink} href={content} download={filename} /> | ||
} |
27 changes: 27 additions & 0 deletions
27
src/bp/ui-studio/src/web/components/Shared/Utils/ElementPreview.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,27 @@ | ||
import React from 'react' | ||
import { connect } from 'react-redux' | ||
import { fetchContentItem } from '~/actions' | ||
import store from '~/store' | ||
|
||
import withLanguage from '../../Util/withLanguage' | ||
|
||
const ElementPreview = props => { | ||
const { itemId, contentItem, contentLang } = props | ||
if (!itemId) { | ||
return null | ||
} | ||
|
||
if (!contentItem) { | ||
props.fetchContentItem(props.itemId.replace('#!', '')) | ||
} | ||
|
||
return contentItem ? `${contentItem.schema.title} | ${contentItem.previews[contentLang]}` : '' | ||
} | ||
|
||
const mapStateToProps = ({ content: { itemsById } }, { itemId }) => ({ contentItem: itemsById[itemId] }) | ||
const ConnectedElementPreview = connect( | ||
mapStateToProps, | ||
{ fetchContentItem } | ||
)(withLanguage(ElementPreview)) | ||
|
||
export default props => <ConnectedElementPreview {...props} store={store} /> |
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,2 @@ | ||
export { default as ElementPreview } from './ElementPreview' | ||
export { Downloader } from './Downloader' |
24 changes: 24 additions & 0 deletions
24
src/bp/ui-studio/src/web/components/Shared/Utils/typings.d.ts
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,24 @@ | ||
import { IconName, MaybeElement, Position } from '@blueprintjs/core' | ||
import React from 'react' | ||
|
||
declare module 'botpress/utils' { | ||
export function ElementPreview(props: ElementPreviewProps): JSX.Element | ||
export function Downloader(props: DownloaderProps): JSX.Element | ||
} | ||
|
||
export interface DownloaderProps { | ||
/** When the URL is set, the backend is called and the download is started. */ | ||
url: string | ||
/** If the filename is not set, it will be extracted from headers */ | ||
filename?: string | ||
/** Trigger an action after the download is done */ | ||
onDownloadCompleted?: () => void | ||
} | ||
|
||
export interface ElementPreviewProps { | ||
/** The ID of the content element to display */ | ||
itemId: string | ||
readonly contentLang?: string | ||
readonly fetchContentItem?: (itemId: string) => void | ||
readonly contentItem?: any | ||
} |
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