@oxyhq/services
Version:
88 lines (81 loc) • 3.57 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.refreshAvatarInStore = refreshAvatarInStore;
exports.updateAvatarVisibility = updateAvatarVisibility;
exports.updateProfileWithAvatar = updateProfileWithAvatar;
var _core = require("@oxyhq/core");
var _accountStore = require("../stores/accountStore.js");
var _authStore = require("../stores/authStore.js");
var _queryKeys = require("../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)
*/
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
*/
function refreshAvatarInStore(sessionId, avatarFileId, oxyServices) {
const {
updateAccount
} = _accountStore.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
*/
async function updateProfileWithAvatar(updates, oxyServices, activeSessionId, queryClient, syncSession) {
const data = await (0, _core.authenticatedApiCall)(oxyServices, activeSessionId, () => oxyServices.updateProfile(updates), syncSession);
// Update cache with server response
queryClient.setQueryData(_queryKeys.queryKeys.accounts.current(), data);
if (activeSessionId) {
queryClient.setQueryData(_queryKeys.queryKeys.users.profile(activeSessionId), data);
}
// Update authStore so frontend components see the changes immediately
_authStore.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
(0, _queryKeys.invalidateUserQueries)(queryClient);
(0, _queryKeys.invalidateAccountQueries)(queryClient);
return data;
}
//# sourceMappingURL=avatarUtils.js.map