Skip to content

Commit aa50ed4

Browse files
committed
feat(download): enhance download functionality with dynamic URL generation
Signed-off-by: Amit Amrutiya <amitamrutiya2210@gmail.com>
1 parent 9b8c44a commit aa50ed4

3 files changed

Lines changed: 31 additions & 10 deletions

File tree

src/custom/CatalogDetail/ActionButton.tsx

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { CopyIcon, DeleteIcon, EditIcon, KanvasIcon, PublishIcon } from '../../i
44
import Download from '../../icons/Download/Download';
55
import { charcoal, useTheme } from '../../theme';
66
import { Pattern } from '../CustomCatalog/CustomCard';
7-
import { downloadFilter, downloadYaml } from './helper';
7+
import { downloadPattern, downloadYaml, getValidSorceType } from './helper';
88
import { ActionButton, StyledActionWrapper, UnpublishAction } from './style';
99
import { RESOURCE_TYPES } from './types';
1010

@@ -13,6 +13,7 @@ interface ActionButtonsProps {
1313
details: Pattern;
1414
type: string;
1515
isCloneLoading: boolean;
16+
getDownloadUrl: (sorceType: string, id: string) => string;
1617
handleClone: (name: string, id: string) => void;
1718
handleUnpublish: () => void;
1819
isCloneDisabled: boolean;
@@ -34,6 +35,7 @@ const ActionButtons: React.FC<ActionButtonsProps> = ({
3435
isCloneDisabled,
3536
showUnpublishAction,
3637
handleUnpublish,
38+
getDownloadUrl,
3739
showOpenPlaygroundAction,
3840
onOpenPlaygroundClick,
3941
showInfoAction,
@@ -42,6 +44,7 @@ const ActionButtons: React.FC<ActionButtonsProps> = ({
4244
handleDelete
4345
}) => {
4446
const cleanedType = type.replace('my-', '').replace(/s$/, '');
47+
const sorceType = getValidSorceType(type);
4548
const theme = useTheme();
4649
return (
4750
<StyledActionWrapper>
@@ -83,9 +86,9 @@ const ActionButtons: React.FC<ActionButtonsProps> = ({
8386
color: theme.palette.text.default
8487
}}
8588
onClick={() =>
86-
cleanedType === RESOURCE_TYPES.FILTERS
87-
? downloadFilter(details.id, details.name)
88-
: downloadYaml(details.pattern_file, details.name)
89+
cleanedType === RESOURCE_TYPES.VIEWS
90+
? downloadYaml(details.pattern_file, details.name)
91+
: downloadPattern(details.id, details.name, sorceType, getDownloadUrl)
8992
}
9093
>
9194
<Download width={24} height={24} fill={theme.palette.icon.default} />

src/custom/CatalogDetail/helper.ts

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,17 @@ export function slugify(str: string): string {
3434
return str;
3535
}
3636

37-
export const downloadFilter = (id: string, name: string): void => {
38-
const dataUri = `${process.env.API_ENDPOINT_PREFIX}/api/content/filters/download/${id}`;
39-
40-
// Add the .wasm extension to the filename
41-
const fileNameWithExtension = name + '.wasm';
37+
export const downloadPattern = (
38+
id: string,
39+
name: string,
40+
sorceType: string,
41+
getDownloadUrl: (sorceType: string, id: string) => string
42+
): void => {
43+
const downloadUrl = getDownloadUrl(sorceType, id);
4244

45+
const fileNameWithExtension = `${name}.yaml`;
4346
const linkElement = document.createElement('a');
44-
linkElement.setAttribute('href', dataUri);
47+
linkElement.setAttribute('href', downloadUrl);
4548
linkElement.setAttribute('download', fileNameWithExtension);
4649
linkElement.click();
4750
linkElement.remove();
@@ -59,3 +62,16 @@ export const formatDate = (date: Date) => {
5962
const formattedDate = new Date(date).toLocaleDateString('en-US', options);
6063
return formattedDate;
6164
};
65+
66+
export const getValidSorceType = (type: string): string => {
67+
if (type === 'my-designs' || type === 'catalog') {
68+
return 'patterns';
69+
}
70+
if (type === 'my-filters') {
71+
return 'filters';
72+
}
73+
if (type === 'my-views') {
74+
return 'views';
75+
}
76+
return '';
77+
};

src/custom/CatalogDetail/types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ export const RESOURCE_TYPES = {
3737
VIEWS: 'view'
3838
};
3939

40+
export const PATTERNS = 'patterns';
41+
4042
export type ContentClassType = {
4143
community: {
4244
icon: React.ComponentType;

0 commit comments

Comments
 (0)