Skip to content

Commit

Permalink
fix: show qr code also in the tool and use translations
Browse files Browse the repository at this point in the history
  • Loading branch information
drodil committed Nov 5, 2024
1 parent 143cfad commit bace909
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 35 deletions.
99 changes: 64 additions & 35 deletions plugins/toolbox/src/components/Generators/QRCode.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ import MenuItem from '@mui/material/MenuItem';
import InputLabel from '@mui/material/InputLabel';
import FormControl from '@mui/material/FormControl';
import TextField from '@mui/material/TextField';
import { DefaultEditor } from '@drodil/backstage-plugin-toolbox';
import {
DefaultEditor,
useToolboxTranslation,
} from '@drodil/backstage-plugin-toolbox';

import { configApiRef, useApi } from '@backstage/core-plugin-api';
import Select, { SelectChangeEvent } from '@mui/material/Select';
Expand Down Expand Up @@ -50,6 +53,7 @@ const ConfigSelect = (props: {
}),
[props],
);
const { t } = useToolboxTranslation();

const labelId = `label-for-${props.settingKey}`;
const between = '3px';
Expand Down Expand Up @@ -86,7 +90,7 @@ const ConfigSelect = (props: {
key={`selectFor${props.name}menu${value}`}
value={value}
>
{value}
{t(`tool.qr-code-generate.${value}`)}
</MenuItem>
))
: null}
Expand All @@ -95,7 +99,7 @@ const ConfigSelect = (props: {
<TextField
defaultValue={props.settings[colorSetting]}
id="input"
label={`${props.name} color`}
label={`${props.name} ${t('tool.qr-code-generate.color')}`}
name="input"
onChange={onChangeColor}
sx={{ width: 140, margin: `10px ${between} 0 ${between}` }}
Expand All @@ -111,7 +115,9 @@ export const QRCodeGenerator = () => {
const sample = faker.internet.url();

const [fileExt, setFileExt] = useState<FileExtension>('png');
const [image, setImage] = useState<string | null>(null);
const ref = useRef(null);
const { t } = useToolboxTranslation();

const config = useApi(configApiRef).getOptionalConfig('app.toolbox.qrCode');

Expand Down Expand Up @@ -194,7 +200,24 @@ export const QRCodeGenerator = () => {
},
shape: settings.shape,
});
}, [input, qrCode, settings]);
qrCode
.getRawData()
.then(data => {
if (!data) {
setImage(null);
return;
}

if (Buffer.isBuffer(data)) {
const blob = new Blob([data], { type: 'image/png' });
setImage(window.URL.createObjectURL(blob));
return;
}

setImage(window.URL.createObjectURL(data));
})
.catch(() => setImage(null));
}, [qrCode, settings, input]);

const onExtensionChange = useCallback(
(event: SelectChangeEvent) => {
Expand All @@ -210,31 +233,26 @@ export const QRCodeGenerator = () => {
}, [fileExt, qrCode]);

const DownloadOptions = (
<span>
{'File type: '}
<Select
defaultValue="png"
key="selectExtensionChanger"
label="file extension"
onChange={onExtensionChange}
placeholder="file extension"
variant="standard"
>
<MenuItem key="png" value="png">
png
</MenuItem>
<MenuItem key="webp" value="webp">
webp
</MenuItem>
<MenuItem key="jpeg" value="jpeg">
jpeg
</MenuItem>
<MenuItem key="svg" value="svg">
svg
</MenuItem>
</Select>
<Button key="downloadbutton" onClick={onDownloadClick}>
Download
<span style={{ marginLeft: '2rem' }}>
<FormControl>
<InputLabel id="file-type">
{t('tool.qr-code-generate.downloadAs')}
</InputLabel>
<Select
defaultValue="png"
labelId="file-type"
onChange={onExtensionChange}
label={t('tool.qr-code-generate.downloadAs')}
variant="outlined"
>
<MenuItem value="png">png</MenuItem>
<MenuItem value="webp">webp</MenuItem>
<MenuItem value="jpeg">jpeg</MenuItem>
<MenuItem value="svg">svg</MenuItem>
</Select>
</FormControl>
<Button key="downloadbutton" onClick={onDownloadClick} disabled={!input}>
{t('tool.qr-code-generate.download')}
</Button>
</span>
);
Expand All @@ -247,7 +265,7 @@ export const QRCodeGenerator = () => {
additionalTools={[
<ConfigSelect
key="dotSelect"
name="Dot"
name={t('tool.qr-code-generate.dotType')}
setSettings={setSettings}
settingKey={'dotType' as const}
settings={settings}
Expand All @@ -264,33 +282,44 @@ export const QRCodeGenerator = () => {
/>,
<ConfigSelect
key="cornerSquareSelect"
name="Corner Square"
name={t('tool.qr-code-generate.cornerSquareType')}
setSettings={setSettings}
settingKey={'cornerSquareType' as const}
settings={settings}
types={['square', 'dot', 'extra-rounded'] as CornerSquareType[]}
/>,
<ConfigSelect
key="cornerDotSelect"
name="Corner Dot"
name={t('tool.qr-code-generate.cornerDotType')}
setSettings={setSettings}
settingKey={'cornerDotType' as const}
settings={settings}
types={['dot', 'square'] as CornerDotType[]}
/>,
<ConfigSelect
key="shapeSelect"
name="Shape"
name={t('tool.qr-code-generate.shapeType')}
setSettings={setSettings}
settingKey={'shape' as const}
settings={settings}
types={['circle', 'square'] as ShapeType[]}
/>,
DownloadOptions,
]}
extraRightContent={DownloadOptions}
input={input}
inputProps={{ maxLength: 2048 }}
rightContent={<div ref={ref} />}
rightContent={
image ? (
<img
ref={ref}
className="blob-to-image"
src={image}
alt="QR code"
/>
) : (
<div ref={ref} />
)
}
sample={sample}
setInput={useCallback(
value => {
Expand Down
16 changes: 16 additions & 0 deletions plugins/toolbox/src/translation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,22 @@ export const toolboxTranslationRef = createTranslationRef({
'qr-code-generate': {
name: 'QR Code',
description: 'Generate QR code from text',
fileType: 'File type:',
dotType: 'Dot',
cornerSquareType: 'Corner square',
cornerDotType: 'Corner dot',
shapeType: 'Shape',
color: 'color',
download: 'Download',
square: 'Square',
circle: 'Circle',
classy: 'Classy',
dots: 'Dots',
downloadAs: 'File type',
'classy-rounded': 'Classy rounded',
'extra-rounded': 'Extra rounded',
rounded: 'Rounded',
dot: 'Dot',
},
'bar-code-generate': {
name: 'Barcode',
Expand Down

0 comments on commit bace909

Please sign in to comment.