fix(social): stop 'Error fetching User Profiles' alert loop

fetchUserProfiles used a raw fetch without the bearer token, so managed
users always got a 400 from /api/social/user, and the mount effect fired
an alert() on every page load. Use apiFetch (auth attached), let managed
users fetch without a local key, and make the background auto-fetch
silent — only the manual Settings button alerts now.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
victormanuel.caverogracia 2026-07-17 00:24:16 +02:00
parent 0311b6143d
commit 54bb701b83

View file

@ -19,7 +19,6 @@ import HistoryTab from './components/HistoryTab';
import ProfileMenu from './components/ProfileMenu';
import { useAuth } from './contexts/AuthContext';
import { apiFetch, apiJson, QuotaError } from './lib/api';
import { getApiUrl } from './config';
// Enhanced "Encryption" using XOR + Base64 with a Salt
// This is better than plain Base64 but still client-side.
@ -278,10 +277,10 @@ function App() {
}, [falKey]);
useEffect(() => {
if (uploadPostKey && userProfiles.length === 0) {
fetchUserProfiles();
if ((uploadPostKey || isManaged) && userProfiles.length === 0) {
fetchUserProfiles({ silent: true });
}
}, [uploadPostKey]);
}, [uploadPostKey, isManaged]);
// For managed users, fetch the durable R2 URLs of the current job's clips so the
// preview can fall back to them when the local files have been cleaned up.
@ -335,11 +334,13 @@ function App() {
}, [status, jobId]);
const fetchUserProfiles = async () => {
if (!uploadPostKey) return;
// silent: background auto-fetch — never alert(), just log. Managed users need
// no local key (the server resolves its own); BYOK sends the header.
const fetchUserProfiles = async ({ silent = false } = {}) => {
if (!uploadPostKey && !isManaged) return;
try {
const res = await fetch(getApiUrl('/api/social/user'), {
headers: { 'X-Upload-Post-Key': uploadPostKey }
const res = await apiFetch('/api/social/user', {
headers: uploadPostKey ? { 'X-Upload-Post-Key': uploadPostKey } : {}
});
if (!res.ok) throw new Error("Failed to fetch");
const data = await res.json();
@ -349,11 +350,11 @@ function App() {
if (!uploadUserId) {
setUploadUserId(data.profiles[0].username);
}
} else {
} else if (!silent) {
alert("No profiles found for this API Key.");
}
} catch (e) {
alert("Error fetching User Profiles. Please check key.");
if (!silent) alert("Error fetching User Profiles. Please check key.");
console.error(e);
}
};