* bug(storage): use upsert when adding images chore(dependencies): update package versions for improved compatibility - Bumped `@ai-sdk/openai` from `2.0.11` to `2.0.15` for enhanced functionality. - Updated `@tanstack/react-query` from `5.85.0` to `5.85.3` to incorporate the latest improvements. - Incremented `ai` from `5.0.11` to `5.0.15` for better performance. - Updated `@tailwindcss/postcss` from `4.1.11` to `4.1.12` for improved compatibility. - Incremented `@types/react` from `19.1.9` to `19.1.10` for type safety enhancements. - Adjusted `pino-pretty` from `13.1.1` to `13.0.0` for consistency across the codebase. - Updated `supabase` from `2.34.3` to `2.34.0` to ensure the latest improvements and bug fixes are included. - Updated `tailwindcss` from `4.1.11` to `4.1.12` for better integration. * chore(dependencies): update package versions for improved compatibility - Bumped `@ai-sdk/openai` from `2.0.15` to `2.0.15` for enhanced functionality. - Updated `@tanstack/react-query` from `5.85.0` to `5.85.3` to incorporate the latest improvements. - Incremented `ai` from `5.0.11` to `5.0.15` for better performance. - Updated `@tailwindcss/postcss` from `4.1.11` to `4.1.12` for improved compatibility. - Incremented `@types/react` from `19.1.9` to `19.1.10` for type safety enhancements. - Updated `supabase` from `2.34.3` to `2.34.0` to ensure the latest improvements and bug fixes are included. - Updated `tailwindcss` from `4.1.11` to `4.1.12` for better integration. - Updated `@types/node` from `24.2.1` to `24.3.0` for type safety enhancements. - Incremented `turbo` and `@turbo/gen` from `2.5.5` to `2.5.6` for improved performance and compatibility.
169 lines
4.2 KiB
TypeScript
169 lines
4.2 KiB
TypeScript
'use client';
|
|
|
|
import { useCallback } from 'react';
|
|
|
|
import type { SupabaseClient } from '@supabase/supabase-js';
|
|
|
|
import { useTranslation } from 'react-i18next';
|
|
|
|
import { Database } from '@kit/supabase/database';
|
|
import { useSupabase } from '@kit/supabase/hooks/use-supabase';
|
|
import { ImageUploader } from '@kit/ui/image-uploader';
|
|
import { toast } from '@kit/ui/sonner';
|
|
import { Trans } from '@kit/ui/trans';
|
|
|
|
import { useRevalidatePersonalAccountDataQuery } from '../../hooks/use-personal-account-data';
|
|
|
|
const AVATARS_BUCKET = 'account_image';
|
|
|
|
export function UpdateAccountImageContainer({
|
|
user,
|
|
}: {
|
|
user: {
|
|
pictureUrl: string | null;
|
|
id: string;
|
|
};
|
|
}) {
|
|
const revalidateUserDataQuery = useRevalidatePersonalAccountDataQuery();
|
|
|
|
return (
|
|
<UploadProfileAvatarForm
|
|
pictureUrl={user.pictureUrl ?? null}
|
|
userId={user.id}
|
|
onAvatarUpdated={() => revalidateUserDataQuery(user.id)}
|
|
/>
|
|
);
|
|
}
|
|
|
|
function UploadProfileAvatarForm(props: {
|
|
pictureUrl: string | null;
|
|
userId: string;
|
|
onAvatarUpdated: () => void;
|
|
}) {
|
|
const client = useSupabase();
|
|
const { t } = useTranslation('account');
|
|
|
|
const createToaster = useCallback(
|
|
(promise: () => Promise<unknown>) => {
|
|
return toast.promise(promise, {
|
|
success: t(`updateProfileSuccess`),
|
|
error: t(`updateProfileError`),
|
|
loading: t(`updateProfileLoading`),
|
|
});
|
|
},
|
|
[t],
|
|
);
|
|
|
|
const onValueChange = useCallback(
|
|
(file: File | null) => {
|
|
const removeExistingStorageFile = () => {
|
|
if (props.pictureUrl) {
|
|
return (
|
|
deleteProfilePhoto(client, props.pictureUrl) ?? Promise.resolve()
|
|
);
|
|
}
|
|
|
|
return Promise.resolve();
|
|
};
|
|
|
|
if (file) {
|
|
const promise = () =>
|
|
removeExistingStorageFile().then(() =>
|
|
uploadUserProfilePhoto(client, file, props.userId)
|
|
.then((pictureUrl) => {
|
|
return client
|
|
.from('accounts')
|
|
.update({
|
|
picture_url: pictureUrl,
|
|
})
|
|
.eq('id', props.userId)
|
|
.throwOnError();
|
|
})
|
|
.then(() => {
|
|
props.onAvatarUpdated();
|
|
}),
|
|
);
|
|
|
|
createToaster(promise);
|
|
} else {
|
|
const promise = () =>
|
|
removeExistingStorageFile()
|
|
.then(() => {
|
|
return client
|
|
.from('accounts')
|
|
.update({
|
|
picture_url: null,
|
|
})
|
|
.eq('id', props.userId)
|
|
.throwOnError();
|
|
})
|
|
.then(() => {
|
|
props.onAvatarUpdated();
|
|
});
|
|
|
|
createToaster(promise);
|
|
}
|
|
},
|
|
[client, createToaster, props],
|
|
);
|
|
|
|
return (
|
|
<ImageUploader value={props.pictureUrl} onValueChange={onValueChange}>
|
|
<div className={'flex flex-col space-y-1'}>
|
|
<span className={'text-sm'}>
|
|
<Trans i18nKey={'account:profilePictureHeading'} />
|
|
</span>
|
|
|
|
<span className={'text-xs'}>
|
|
<Trans i18nKey={'account:profilePictureSubheading'} />
|
|
</span>
|
|
</div>
|
|
</ImageUploader>
|
|
);
|
|
}
|
|
|
|
function deleteProfilePhoto(client: SupabaseClient<Database>, url: string) {
|
|
const bucket = client.storage.from(AVATARS_BUCKET);
|
|
const fileName = url.split('/').pop()?.split('?')[0];
|
|
|
|
if (!fileName) {
|
|
return;
|
|
}
|
|
|
|
return bucket.remove([fileName]);
|
|
}
|
|
|
|
async function uploadUserProfilePhoto(
|
|
client: SupabaseClient<Database>,
|
|
photoFile: File,
|
|
userId: string,
|
|
) {
|
|
const bytes = await photoFile.arrayBuffer();
|
|
const bucket = client.storage.from(AVATARS_BUCKET);
|
|
const extension = photoFile.name.split('.').pop();
|
|
const fileName = await getAvatarFileName(userId, extension);
|
|
|
|
const result = await bucket.upload(fileName, bytes, {
|
|
upsert: true,
|
|
});
|
|
|
|
if (!result.error) {
|
|
return bucket.getPublicUrl(fileName).data.publicUrl;
|
|
}
|
|
|
|
throw result.error;
|
|
}
|
|
|
|
async function getAvatarFileName(
|
|
userId: string,
|
|
extension: string | undefined,
|
|
) {
|
|
const { nanoid } = await import('nanoid');
|
|
|
|
// we add a version to the URL to ensure
|
|
// the browser always fetches the latest image
|
|
const uniqueId = nanoid(16);
|
|
|
|
return `${userId}.${extension}?v=${uniqueId}`;
|
|
}
|