Skip to content

Commit

Permalink
WP Telegram - Show-file size when attaching them
Browse files Browse the repository at this point in the history
  • Loading branch information
irshadahmad21 committed Oct 22, 2024
1 parent 502181c commit 6df2d07
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/rude-moose-shop.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"wptelegram": patch
---

Updated attach files UI to show file size
17 changes: 14 additions & 3 deletions plugins/wptelegram/js/p2tg-block-editor/Files.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { BaseControl, Button, Flex, Icon } from '@wordpress/components';
import { useCallback } from '@wordpress/element';
import { useCallback, useState } from '@wordpress/element';
import { MediaUpload } from '@wordpress/media-utils';

import { __ } from '@wpsocio/i18n';
Expand All @@ -14,6 +14,8 @@ const render: React.FC<{ open: VoidFunction }> = ({ open }) => (

const allowedTypes: Array<string> = [];

type FileInfo = { id: number; url: string; filesizeHumanReadable?: string };

export function Files() {
const { data } = useDataState();
const updateField = useUpdateField();
Expand All @@ -26,10 +28,18 @@ export function Files() {
[data.files, updateField],
);

const [filesizeMap, setFilesizeMap] = useState<Record<string, string>>({});

const onSelect = useCallback(
(files: Array<{ id: number; url: string }>) => {
(files: Array<FileInfo>) => {
const newFiles = files.reduce(
(acc, { id, url }) => {
(acc, { id, url, filesizeHumanReadable }) => {
if (filesizeHumanReadable) {
setFilesizeMap((prev) => ({
...prev,
[id]: filesizeHumanReadable,
}));
}
acc[id] = url;
return acc;
},
Expand Down Expand Up @@ -69,6 +79,7 @@ export function Files() {
onClick={onRemove(id)}
/>
<span>{name}</span>
{filesizeMap[id] ? <span>({filesizeMap[id]})</span> : null}
</Flex>
</li>
);
Expand Down

0 comments on commit 6df2d07

Please sign in to comment.