mirror of
https://github.com/umami-software/umami.git
synced 2026-02-05 05:07:15 +01:00
Added download functionality.
This commit is contained in:
parent
0debe89d05
commit
7670ec4136
17 changed files with 216 additions and 5 deletions
41
src/components/input/DownloadButton.tsx
Normal file
41
src/components/input/DownloadButton.tsx
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
import Papa from 'papaparse';
|
||||
import { Button, Icon, TooltipPopup } from 'react-basics';
|
||||
import Icons from '@/components/icons';
|
||||
import { useMessages } from '@/components/hooks';
|
||||
|
||||
export function DownloadButton({
|
||||
filename = 'data',
|
||||
data,
|
||||
}: {
|
||||
filename?: string;
|
||||
data?: any;
|
||||
onClick?: () => void;
|
||||
}) {
|
||||
const { formatMessage, labels } = useMessages();
|
||||
|
||||
const handleClick = async () => {
|
||||
downloadCsv(`${filename}.csv`, Papa.unparse(data));
|
||||
};
|
||||
|
||||
return (
|
||||
<TooltipPopup label={formatMessage(labels.download)} position="top">
|
||||
<Button variant="quiet" onClick={handleClick} disabled={!data}>
|
||||
<Icon>
|
||||
<Icons.Download />
|
||||
</Icon>
|
||||
</Button>
|
||||
</TooltipPopup>
|
||||
);
|
||||
}
|
||||
|
||||
function downloadCsv(filename: string, data: any) {
|
||||
const blob = new Blob([data], { type: 'text/csv' });
|
||||
const url = URL.createObjectURL(blob);
|
||||
|
||||
const a = document.createElement('a');
|
||||
a.href = url;
|
||||
a.download = filename;
|
||||
a.click();
|
||||
|
||||
URL.revokeObjectURL(url);
|
||||
}
|
||||
47
src/components/input/ExportButton.tsx
Normal file
47
src/components/input/ExportButton.tsx
Normal file
|
|
@ -0,0 +1,47 @@
|
|||
import { useState } from 'react';
|
||||
import { Icon, TooltipPopup, LoadingButton } from 'react-basics';
|
||||
import Icons from '@/components/icons';
|
||||
import { useMessages, useApi } from '@/components/hooks';
|
||||
import { useFilterParams } from '@/components/hooks/useFilterParams';
|
||||
import { useSearchParams } from 'next/navigation';
|
||||
|
||||
export function ExportButton({ websiteId }: { websiteId: string }) {
|
||||
const { formatMessage, labels } = useMessages();
|
||||
const [isLoading, setIsLoading] = useState(false);
|
||||
const params = useFilterParams(websiteId);
|
||||
const searchParams = useSearchParams();
|
||||
const { get } = useApi();
|
||||
|
||||
const handleClick = async () => {
|
||||
setIsLoading(true);
|
||||
|
||||
const { zip } = await get(`/websites/${websiteId}/export`, { ...params, ...searchParams });
|
||||
|
||||
const binary = atob(zip);
|
||||
const bytes = new Uint8Array(binary.length);
|
||||
for (let i = 0; i < binary.length; i++) {
|
||||
bytes[i] = binary.charCodeAt(i);
|
||||
}
|
||||
|
||||
const blob = new Blob([bytes], { type: 'application/zip' });
|
||||
const url = URL.createObjectURL(blob);
|
||||
|
||||
const a = document.createElement('a');
|
||||
a.href = url;
|
||||
a.download = 'download.zip';
|
||||
a.click();
|
||||
URL.revokeObjectURL(url);
|
||||
|
||||
setIsLoading(false);
|
||||
};
|
||||
|
||||
return (
|
||||
<TooltipPopup label={formatMessage(labels.download)} position="top">
|
||||
<LoadingButton variant="quiet" isLoading={isLoading} onClick={handleClick}>
|
||||
<Icon>
|
||||
<Icons.Download />
|
||||
</Icon>
|
||||
</LoadingButton>
|
||||
</TooltipPopup>
|
||||
);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue