Sitesmith modal: load assets once; clearer oversize-image message
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
304850fcd3
commit
cfa8ab2e40
3 files changed
+5
-2
No files matched your search
@@ -142,7 +142,10 @@ export const SitesmithModal: React.FC<Props> = ({ onClose, target }) => {
|
|||||||
const useAssetsFlag = !(assetsLoaded && assets.length > 0 && includedAssets.size === 0);
|
const useAssetsFlag = !(assetsLoaded && assets.length > 0 && includedAssets.size === 0);
|
||||||
const assetFilter = assetsLoaded && excluded.size > 0 && includedAssets.size > 0 ? [...includedAssets] : undefined;
|
const assetFilter = assetsLoaded && excluded.size > 0 && includedAssets.size > 0 ? [...includedAssets] : undefined;
|
||||||
|
|
||||||
|
const assetsRequested = useRef(false);
|
||||||
const loadAssets = async () => {
|
const loadAssets = async () => {
|
||||||
|
if (assetsRequested.current) return;
|
||||||
|
assetsRequested.current = true;
|
||||||
const r = await ss.listAssets();
|
const r = await ss.listAssets();
|
||||||
if (r.ok) { setAssets(r.assets ?? []); setAssetsError(null); } else setAssetsError(r.message);
|
if (r.ok) { setAssets(r.assets ?? []); setAssetsError(null); } else setAssetsError(r.message);
|
||||||
setAssetsLoaded(true);
|
setAssetsLoaded(true);
|
||||||
|
|||||||
@@ -26,7 +26,7 @@ export function formatBytes(n: number): string {
|
|||||||
export async function validateAttachment(file: File, maxBytes = DEFAULT_MAX_UPLOAD_BYTES): Promise<string | null> {
|
export async function validateAttachment(file: File, maxBytes = DEFAULT_MAX_UPLOAD_BYTES): Promise<string | null> {
|
||||||
const label = file.name ? `"${file.name.slice(0, 60)}"` : 'That file';
|
const label = file.name ? `"${file.name.slice(0, 60)}"` : 'That file';
|
||||||
if (file.size === 0) return `${label} is empty.`;
|
if (file.size === 0) return `${label} is empty.`;
|
||||||
if (file.size > maxBytes) return `${label} is ${formatBytes(file.size)}; the limit is ${formatBytes(maxBytes)}. Use a smaller image.`;
|
if (file.size > maxBytes) return `${label} is ${formatBytes(file.size)}, which is over the ${formatBytes(maxBytes)} limit. Use a smaller image.`;
|
||||||
const t = await sniffImageType(file);
|
const t = await sniffImageType(file);
|
||||||
if (!t) return `${label} is not a JPEG, PNG, WebP or GIF image (SVG, PDF and other formats are not accepted).`;
|
if (!t) return `${label} is not a JPEG, PNG, WebP or GIF image (SVG, PDF and other formats are not accepted).`;
|
||||||
return null;
|
return null;
|
||||||
|
|||||||
@@ -46,7 +46,7 @@ describe('attachments validation', () => {
|
|||||||
test('rejects wrong type, empty, and oversize with plain messages', async () => {
|
test('rejects wrong type, empty, and oversize with plain messages', async () => {
|
||||||
expect(await validateAttachment(file([60, 115, 118, 103, 62, 0, 0, 0], 'logo.png'))).toMatch(/not a JPEG, PNG, WebP or GIF/);
|
expect(await validateAttachment(file([60, 115, 118, 103, 62, 0, 0, 0], 'logo.png'))).toMatch(/not a JPEG, PNG, WebP or GIF/);
|
||||||
expect(await validateAttachment(file([], 'e.png'))).toMatch(/empty/);
|
expect(await validateAttachment(file([], 'e.png'))).toMatch(/empty/);
|
||||||
expect(await validateAttachment(file(PNG, 'big.png', 9 * 1024 * 1024))).toMatch(/9\.0 MB; the limit is 8\.0 MB/);
|
expect(await validateAttachment(file(PNG, 'big.png', 9 * 1024 * 1024))).toMatch(/9\.0 MB, which is over the 8\.0 MB limit/);
|
||||||
expect(await validateAttachment(file(PNG, 'ok.png'))).toBeNull();
|
expect(await validateAttachment(file(PNG, 'ok.png'))).toBeNull();
|
||||||
expect(formatBytes(2048)).toBe('2 KB');
|
expect(formatBytes(2048)).toBe('2 KB');
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in new issue
Block a user