@oxyhq/services
Version:
83 lines (75 loc) • 3.34 kB
JavaScript
;
import { authenticatedApiCall } from '@oxyhq/core';
import { useAccountStore } from "../stores/accountStore.js";
import { useAuthStore } from "../stores/authStore.js";
import { queryKeys, invalidateUserQueries, invalidateAccountQueries } from "../hooks/queries/queryKeys.js";
/**
* Updates file visibility to public for avatar use.
* Handles errors gracefully, only logging non-404 errors.
*
* @param fileId - The file ID to update visibility for
* @param oxyServices - OxyServices instance
* @param contextName - Optional context name for logging
* @returns Promise that resolves when visibility is updated (or skipped)
*/
export async function updateAvatarVisibility(fileId, oxyServices, contextName = 'AvatarUtils') {
// Skip if temporary asset ID or no file ID
if (!fileId || fileId.startsWith('temp-')) {
return;
}
try {
await oxyServices.assetUpdateVisibility(fileId, 'public');
// Visibility update is logged by the API
} catch (visError) {
// Silently handle errors - 404 means asset doesn't exist yet (which is OK)
// Other errors are logged by the API, so no need to log here
// Function continues gracefully regardless of visibility update success
}
}
/**
* Refreshes avatar in accountStore with cache-busted URL to force image reload.
*
* @param sessionId - The session ID for the account to update
* @param avatarFileId - The new avatar file ID
* @param oxyServices - OxyServices instance to generate download URL
*/
export function refreshAvatarInStore(sessionId, avatarFileId, oxyServices) {
const {
updateAccount
} = useAccountStore.getState();
const cacheBustedUrl = oxyServices.getFileDownloadUrl(avatarFileId, 'thumb') + `?t=${Date.now()}`;
updateAccount(sessionId, {
avatar: avatarFileId,
avatarUrl: cacheBustedUrl
});
}
/**
* Updates user profile with avatar and handles all side effects (query invalidation, accountStore update).
* This function can be used from within OxyContext provider without requiring useOxy hook.
*
* @param updates - Profile updates including avatar
* @param oxyServices - OxyServices instance
* @param activeSessionId - Active session ID
* @param queryClient - TanStack Query client
* @param syncSession - Optional function to sync session/refresh token when auth errors occur
* @returns Promise that resolves with updated user data
*/
export async function updateProfileWithAvatar(updates, oxyServices, activeSessionId, queryClient, syncSession) {
const data = await authenticatedApiCall(oxyServices, activeSessionId, () => oxyServices.updateProfile(updates), syncSession);
// Update cache with server response
queryClient.setQueryData(queryKeys.accounts.current(), data);
if (activeSessionId) {
queryClient.setQueryData(queryKeys.users.profile(activeSessionId), data);
}
// Update authStore so frontend components see the changes immediately
useAuthStore.getState().setUser(data);
// If avatar was updated, refresh accountStore with cache-busted URL
if (updates.avatar && activeSessionId) {
refreshAvatarInStore(activeSessionId, updates.avatar, oxyServices);
}
// Invalidate all related queries to refresh everywhere
invalidateUserQueries(queryClient);
invalidateAccountQueries(queryClient);
return data;
}
//# sourceMappingURL=avatarUtils.js.map