Skip to content

Every analytics tool wants a subscription. And your data. I built Twiligent instead. YouTube stats. Instagram stats. One dashboard. Runs locally on your machine.

refactor(tracked): keep the original fetch upload for screenshots

spacesdrive85560021961e
diff --git a/docs/architecture/frontend/API_LAYER.md b/docs/architecture/frontend/API_LAYER.md
index e9df384..c31ff8c 100644
--- a/docs/architecture/frontend/API_LAYER.md
+++ b/docs/architecture/frontend/API_LAYER.md
@@ -117,16 +117,24 @@ keys are `label`, `category`, `accountId`, `manualViews`, and `imageUrl`. `manua
clear a previously set value.
`imageUrl` must already be a hosted `https:` URL. The Worker never accepts binary uploads, so
-the browser posts the file straight to Cloudinary with `api.uploadToCloudinary()` (below) and
+the browser posts the file straight to Cloudinary with `api.uploadImage()` (below) and
sends only the returned `secure_url`.
## Cloudinary Uploads
+These are the only calls in `api.js` that do not go to the Worker.
+
+```js
+uploadImage(file, cloudConfig)
+```
+
+Used for tracked item screenshots. It posts the image with `fetch` to `https://api.cloudinary.com/v1_1/<cloudName>/image/upload` and resolves with `secure_url`. It throws an `Error` with Cloudinary's own message, or `'Cloudinary did not return an image URL'`, and network or JSON failures reject with the browser's own error. This is the exact behaviour the tracked content page had before the refactor.
+
```js
uploadToCloudinary(file, cloudConfig, { onProgress, resourceType })
```
-The one call in `api.js` that does not go to the Worker. It posts `file` and the unsigned
+Used by publishing. It posts `file` and the unsigned
`upload_preset` from `getCloudinaryConfig()` to
`https://api.cloudinary.com/v1_1/<cloudName>/<resourceType>/upload` with `XMLHttpRequest`, so
upload progress is available, and resolves with the response's `secure_url`.
diff --git a/frontend/src/features/analytics/tracked/trackedList.js b/frontend/src/features/analytics/tracked/trackedList.js
index 3fa49ad..d676de0 100644
--- a/frontend/src/features/analytics/tracked/trackedList.js
+++ b/frontend/src/features/analytics/tracked/trackedList.js
@@ -78,11 +78,3 @@ export function imageFileError(file, hasCloudConfig) {
if (file.size > MAX_IMAGE_BYTES) return `That image is too large. Choose one under ${MAX_IMAGE_BYTES / 1024 / 1024}MB.`;
return '';
}
-
-// matches the wording the page showed when it uploaded with fetch
-export function imageUploadMessage(err) {
- if (err.reason === 'network') return 'Failed to fetch';
- if (err.reason !== 'rejected') return err.message;
- if (err.parseMessage) return err.parseMessage;
- return err.cloudinaryMessage || 'Cloudinary did not return an image URL';
-}
diff --git a/frontend/src/features/analytics/tracked/trackedList.test.js b/frontend/src/features/analytics/tracked/trackedList.test.js
index 692a093..8c6cf44 100644
--- a/frontend/src/features/analytics/tracked/trackedList.test.js
+++ b/frontend/src/features/analytics/tracked/trackedList.test.js
@@ -2,7 +2,7 @@ import { test } from 'node:test';
import assert from 'node:assert/strict';
import {
SORT_OPTIONS, filterAndSortTracked, categoriesOf, trackedStatsOf, typeChipsOf, pluralize, countLabel,
- bulkCategoryMessage, parseManualViews, imageFileError, imageUploadMessage, MAX_IMAGE_BYTES,
+ bulkCategoryMessage, parseManualViews, imageFileError, MAX_IMAGE_BYTES,
} from './trackedList.js';
const items = [
@@ -75,10 +75,3 @@ test('image checks run in the original order', () => {
assert.equal(imageFileError(image, true), '');
});
-test('image upload errors keep the page wording', () => {
- assert.equal(imageUploadMessage({ reason: 'network' }), 'Failed to fetch');
- assert.equal(imageUploadMessage({ reason: 'rejected', cloudinaryMessage: 'Bad preset' }), 'Bad preset');
- assert.equal(imageUploadMessage({ reason: 'rejected' }), 'Cloudinary did not return an image URL');
- assert.equal(imageUploadMessage({ reason: 'rejected', parseMessage: 'Unexpected end of JSON input' }), 'Unexpected end of JSON input');
- assert.equal(imageUploadMessage(new Error('Other')), 'Other');
-});
diff --git a/frontend/src/features/analytics/tracked/useTrackedEditor.js b/frontend/src/features/analytics/tracked/useTrackedEditor.js
index 099611c..3cdcc63 100644
--- a/frontend/src/features/analytics/tracked/useTrackedEditor.js
+++ b/frontend/src/features/analytics/tracked/useTrackedEditor.js
@@ -1,24 +1,12 @@
import { useRef, useState } from 'react';
import { api } from '../../../services/api';
import { isRedditItem } from '../../../utils/trackedContent';
-import { imageFileError, imageUploadMessage, parseManualViews } from './trackedList.js';
+import { imageFileError, parseManualViews } from './trackedList.js';
function clearInput(inputRef) {
if (inputRef.current) inputRef.current.value = '';
}
-// the Worker never handles binary uploads, so only the returned URL is saved on the item
-async function uploadTrackedImage(file, cloudConfig) {
- let url;
- try {
- url = await api.uploadToCloudinary(file, cloudConfig, { resourceType: 'image' });
- } catch (err) {
- throw new Error(imageUploadMessage(err));
- }
- if (!url) throw new Error('Cloudinary did not return an image URL');
- return url;
-}
-
function useTrackedImage({ cloudConfig, showToast }) {
const [imageUrl, setImageUrl] = useState(null);
const [uploading, setUploading] = useState(false);
@@ -31,7 +19,8 @@ function useTrackedImage({ cloudConfig, showToast }) {
if (error) { showToast(error, 'error'); return; }
setUploading(true);
try {
- setImageUrl(await uploadTrackedImage(file, cloudConfig));
+ // the Worker never handles binary uploads, so only the returned url is saved on the item
+ setImageUrl(await api.uploadImage(file, cloudConfig));
showToast('Image uploaded');
} catch (err) {
showToast(`Image upload failed. ${err.message}`, 'error');
diff --git a/frontend/src/features/publishing/UploadContent.jsx b/frontend/src/features/publishing/UploadContent.jsx
index d18fb7b..6122745 100644
--- a/frontend/src/features/publishing/UploadContent.jsx
+++ b/frontend/src/features/publishing/UploadContent.jsx
@@ -40,7 +40,7 @@ export default function UploadContent() {
useEffect(() => {
const instagramAccounts = (accounts || []).filter(account => account.platform === 'instagram');
- // eslint-disable-next-line react-hooks/set-state-in-effect -- keeps the original first render
+ // eslint-disable-next-line react-hooks/set-state-in-effect
setIgAccounts(instagramAccounts);
if (instagramAccounts.length === 1) setSelectedAccount(instagramAccounts[0].id);
loadScheduledPosts();
diff --git a/frontend/src/services/api.js b/frontend/src/services/api.js
index 54c7d5a..f8a7ff8 100644
--- a/frontend/src/services/api.js
+++ b/frontend/src/services/api.js
@@ -64,6 +64,20 @@ function uploadToCloudinary(file, cloudConfig, { onProgress, resourceType } = {}
});
}
+// tracked item screenshots use fetch so failures surface exactly as before
+async function uploadImage(file, cloudConfig) {
+ const formData = new FormData();
+ formData.append('file', file);
+ formData.append('upload_preset', cloudConfig.uploadPreset);
+
+ const res = await fetch(`${CLOUDINARY_API}/${cloudConfig.cloudName}/image/upload`, { method: 'POST', body: formData });
+ const json = await res.json();
+ if (!res.ok || !json.secure_url) {
+ throw new Error(json.error?.message || 'Cloudinary did not return an image URL');
+ }
+ return json.secure_url;
+}
+
export const api = {
health: () => request('/health'),
@@ -95,6 +109,7 @@ export const api = {
getCloudinaryConfig: () => request('/cloudinary-config'),
uploadToCloudinary,
+ uploadImage,
// Instagram Publishing
getIGPublishingLimit: (id) => request(`/accounts/${id}/ig-publishing-limit`),