Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion frontend/app/auth/__tests__/loginRedirect.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { describe, expect, it } from '@jest/globals';
import { getLoginEntryAction } from '../loginRedirect';
import { getLoginEntryAction } from '../../../lib/auth/loginRedirect';

describe('getLoginEntryAction', () => {
it('keeps waiting while the profile query is unresolved', () => {
Expand Down
2 changes: 1 addition & 1 deletion frontend/app/auth/login.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import { getEmailValidationError, PROFILE_BOUNDS } from '@polybuys/shared';
import { useEntranceAnimation } from '../../hooks/useEntranceAnimation';
import { useAuth } from '../../hooks/useAuth';
import { requestPermissionAndSyncToken } from '../../hooks/usePushNotifications';
import { getLoginEntryAction, type LoginStep } from './loginRedirect';
import { getLoginEntryAction, type LoginStep } from '../../lib/auth/loginRedirect';
import { colors, typography, spacing, borderRadius } from '../../theme/tokens';

const APP_REVIEW_EMAIL = (process.env.EXPO_PUBLIC_APP_REVIEW_EMAIL ?? '').toLowerCase().trim();
Expand Down
117 changes: 57 additions & 60 deletions frontend/app/profile/edit.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { useRouter } from 'expo-router';
import { useMutation, useQuery } from 'convex/react';
import { api } from 'convex/_generated/api';
import { Id } from 'convex/_generated/dataModel';
import * as FileSystem from 'expo-file-system/legacy';
import * as ImagePicker from 'expo-image-picker';
import { SaveFormat, manipulateAsync } from 'expo-image-manipulator';
import { getEmailValidationError } from '@polybuys/shared';
Expand Down Expand Up @@ -40,74 +41,68 @@ type UploadOptions = {

async function uploadImageToConvex(
uploadUrl: string,
blob: Blob,
fileUri: string,
{ signal, timeoutMs = PROFILE_IMAGE_BOUNDS.UPLOAD_TIMEOUT_MS }: UploadOptions = {}
): Promise<Id<'_storage'>> {
return await new Promise((resolve, reject) => {
const xhr = new XMLHttpRequest();
let settled = false;

const rejectOnce = (error: Error) => {
if (settled) return;
settled = true;
cleanup();
reject(error);
};
const resolveOnce = (value: Id<'_storage'>) => {
if (settled) return;
settled = true;
cleanup();
resolve(value);
};
const task = FileSystem.createUploadTask(uploadUrl, fileUri, {
headers: {
'Content-Type': 'image/jpeg',
},
httpMethod: 'POST',
uploadType: FileSystem.FileSystemUploadType.BINARY_CONTENT,
});
const uploadPromise = task.uploadAsync();

let timeoutHandle: ReturnType<typeof setTimeout> | null = null;
const abortPromise = new Promise<never>((_, reject) => {
const onAbort = () => {
try {
xhr.abort();
} catch {
// no-op
}
rejectOnce(new Error('Image upload was cancelled.'));
};
const cleanup = () => {
xhr.onerror = null;
xhr.onload = null;
xhr.onabort = null;
xhr.ontimeout = null;
signal?.removeEventListener('abort', onAbort);
void task.cancelAsync().catch(() => undefined);
reject(new Error('Image upload was cancelled.'));
};

if (signal?.aborted) {
rejectOnce(new Error('Image upload was cancelled.'));
onAbort();
return;
}

xhr.open('POST', uploadUrl);
xhr.timeout = timeoutMs;
xhr.setRequestHeader('Content-Type', blob.type || 'image/jpeg');
signal?.addEventListener('abort', onAbort, { once: true });
});

xhr.onerror = () => rejectOnce(new Error('Network error during image upload.'));
xhr.onabort = () => rejectOnce(new Error('Image upload was cancelled.'));
xhr.ontimeout = () => rejectOnce(new Error('Image upload timed out. Please try again.'));
xhr.onload = () => {
if (xhr.status < 200 || xhr.status >= 300) {
rejectOnce(new Error(`Image upload failed (${xhr.status}).`));
return;
}
const timeoutPromise = new Promise<never>((_, reject) => {
timeoutHandle = setTimeout(() => {
void task.cancelAsync().catch(() => undefined);
reject(new Error('Image upload timed out. Please try again.'));
}, timeoutMs);
});

try {
const parsed = JSON.parse(xhr.responseText) as { storageId?: Id<'_storage'> };
if (!parsed.storageId) {
rejectOnce(new Error('Upload response missing storage ID.'));
return;
}
resolveOnce(parsed.storageId);
} catch {
rejectOnce(new Error('Upload response could not be parsed.'));
}
};
let result: Awaited<typeof uploadPromise>;
try {
result = await Promise.race([uploadPromise, abortPromise, timeoutPromise]);
} finally {
if (timeoutHandle !== null) {
clearTimeout(timeoutHandle);
}
}

signal?.addEventListener('abort', onAbort, { once: true });
xhr.send(blob);
});
if (!result) {
throw new Error('Image upload was cancelled.');
}
if (result.status < 200 || result.status >= 300) {
throw new Error(`Image upload failed (${result.status}).`);
}

try {
const parsed = JSON.parse(result.body) as { storageId?: Id<'_storage'> };
if (!parsed.storageId) {
throw new Error('Upload response missing storage ID.');
}
return parsed.storageId;
} catch (error) {
if (error instanceof Error && error.message === 'Upload response missing storage ID.') {
throw error;
}
throw new Error('Upload response could not be parsed.');
}
}

export default function ProfileEditScreen() {
Expand Down Expand Up @@ -210,9 +205,12 @@ export default function ProfileEditScreen() {
format: SaveFormat.JPEG,
});

const blob = await (await fetch(manipulated.uri)).blob();
const fileInfo = await FileSystem.getInfoAsync(manipulated.uri);
const maxBytes = PROFILE_IMAGE_BOUNDS.MAX_FILE_SIZE_MB * 1024 * 1024;
if (blob.size > maxBytes) {
if (!fileInfo.exists || fileInfo.isDirectory) {
throw new Error('Prepared profile image file is missing.');
}
if (fileInfo.size > maxBytes) {
throw new Error(
`Profile image is too large after compression (max ${PROFILE_IMAGE_BOUNDS.MAX_FILE_SIZE_MB} MB).`
);
Expand Down Expand Up @@ -268,12 +266,11 @@ export default function ProfileEditScreen() {
let nextPicture: Id<'_storage'> | null = picture;

if (pendingPictureUri) {
const blob = await (await fetch(pendingPictureUri)).blob();
const uploadUrl = await generateUploadUrl({});
uploadAbortRef.current?.abort();
const abortController = new AbortController();
uploadAbortRef.current = abortController;
nextPicture = await uploadImageToConvex(uploadUrl, blob, {
nextPicture = await uploadImageToConvex(uploadUrl, pendingPictureUri, {
signal: abortController.signal,
});
uploadAbortRef.current = null;
Expand Down
Loading
Loading