feat(builder): shared asset upload/list util

Extract uploadAsset/listAssets into utils/assets.ts, lifting the exact
uploadToWhp body and list_assets fetch pattern already duplicated across
shared.tsx/ImageBlock/Logo/Navbar/etc. shared.tsx's uploadToWhp is now a
thin re-export (`export const uploadToWhp = uploadAsset`) so its ~6
existing callers are unaffected.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-07-12 12:43:59 -07:00
parent b46b6915a5
commit 296c10d019
4 changed files with 211 additions and 17 deletions
+6 -17
View File
@@ -3,6 +3,7 @@ import { useEditor } from '@craftjs/core';
import {
GRADIENTS,
} from '../../../constants/presets';
import { uploadAsset } from '../../../utils/assets';
/* ---------- Helper: auto text color for bg ---------- */
export function autoTextColor(bg: string): string {
@@ -17,23 +18,11 @@ export function autoTextColor(bg: string): string {
return '#ffffff';
}
/* ---------- Helper: upload to WHP ---------- */
export async function uploadToWhp(file: File): Promise<string | null> {
const cfg = (window as any).WHP_CONFIG;
if (!cfg) return URL.createObjectURL(file);
const formData = new FormData();
formData.append('file', file);
try {
const resp = await fetch(`${cfg.apiUrl}?action=upload_asset&site_id=${cfg.siteId}`, {
method: 'POST',
headers: { 'X-CSRF-Token': cfg.csrfToken },
body: formData,
});
const data = await resp.json();
if (data.success && data.url) return data.url;
return null;
} catch { return null; }
}
/* ---------- Helper: upload to WHP ----------
Thin re-export over the shared `uploadAsset` util (`@/utils/assets`) so
the existing callers importing `uploadToWhp` from here keep working
unchanged. */
export const uploadToWhp = uploadAsset;
/* ---------- Shared inline styles ---------- */
export const labelStyle: CSSProperties = { fontSize: 11, color: '#a1a1aa', display: 'block', marginBottom: 4, textTransform: 'capitalize' };
@@ -0,0 +1,21 @@
import { describe, test, expect, vi, afterEach } from 'vitest';
import { uploadToWhp } from './shared';
import { uploadAsset } from '../../../utils/assets';
describe('shared.uploadToWhp', () => {
afterEach(() => {
vi.unstubAllGlobals();
delete (window as any).WHP_CONFIG;
});
test('is a thin re-export of uploadAsset (same function reference)', () => {
expect(uploadToWhp).toBe(uploadAsset);
});
test('still works standalone (no WHP_CONFIG -> blob: URL) for existing callers', async () => {
(URL as any).createObjectURL = vi.fn(() => 'blob:mock-url');
const file = new File(['x'], 'photo.png', { type: 'image/png' });
const url = await uploadToWhp(file);
expect(url).toBe('blob:mock-url');
});
});