feat: upload org logos through r2 route
This commit is contained in:
@@ -1,3 +0,0 @@
|
|||||||
export const getOrgLogoUrl = (orgId: string) => {
|
|
||||||
return `${import.meta.env.VITE_SUPABASE_URL}/storage/v1/object/public/autumn/logo/${orgId}`;
|
|
||||||
};
|
|
||||||
@@ -1,5 +1,4 @@
|
|||||||
import { ImageIcon } from "lucide-react";
|
import { ImageIcon } from "lucide-react";
|
||||||
import axios from "axios";
|
|
||||||
import type React from "react";
|
import type React from "react";
|
||||||
import { useRef, useState } from "react";
|
import { useRef, useState } from "react";
|
||||||
import { toast } from "sonner";
|
import { toast } from "sonner";
|
||||||
@@ -9,7 +8,6 @@ import { useOrg } from "@/hooks/common/useOrg";
|
|||||||
import { authClient } from "@/lib/auth-client";
|
import { authClient } from "@/lib/auth-client";
|
||||||
import { useAxiosInstance } from "@/services/useAxiosInstance";
|
import { useAxiosInstance } from "@/services/useAxiosInstance";
|
||||||
import { getBackendErr } from "@/utils/genUtils";
|
import { getBackendErr } from "@/utils/genUtils";
|
||||||
import { getOrgLogoUrl } from "@/utils/orgUtils";
|
|
||||||
|
|
||||||
const MAX_SIZE_MB = 10;
|
const MAX_SIZE_MB = 10;
|
||||||
|
|
||||||
@@ -25,6 +23,7 @@ const OrgLogoUploader: React.FC = () => {
|
|||||||
const handleRemove = async () => {
|
const handleRemove = async () => {
|
||||||
setRemoving(true);
|
setRemoving(true);
|
||||||
try {
|
try {
|
||||||
|
await axiosInstance.delete("/organization/logo");
|
||||||
const { error } = await authClient.organization.update({
|
const { error } = await authClient.organization.update({
|
||||||
data: { logo: "" },
|
data: { logo: "" },
|
||||||
});
|
});
|
||||||
@@ -44,12 +43,19 @@ const OrgLogoUploader: React.FC = () => {
|
|||||||
inputRef.current?.click();
|
inputRef.current?.click();
|
||||||
};
|
};
|
||||||
|
|
||||||
const uploadToSupabase = async (file: File) => {
|
const uploadToR2 = async (file: File) => {
|
||||||
const { data } = await axiosInstance.get("/organization/upload_url");
|
const formData = new FormData();
|
||||||
const { signedUrl } = data;
|
formData.append("file", file);
|
||||||
await axios.put(signedUrl, file, {
|
|
||||||
headers: { "Content-Type": file.type },
|
const { data } = await axiosInstance.post<{ logoUrl: string }>(
|
||||||
});
|
"/organization/logo",
|
||||||
|
formData,
|
||||||
|
{
|
||||||
|
headers: { "Content-Type": "multipart/form-data" },
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
return data.logoUrl;
|
||||||
};
|
};
|
||||||
|
|
||||||
const handleUploading = async (e: React.ChangeEvent<HTMLInputElement>) => {
|
const handleUploading = async (e: React.ChangeEvent<HTMLInputElement>) => {
|
||||||
@@ -62,9 +68,14 @@ const OrgLogoUploader: React.FC = () => {
|
|||||||
}
|
}
|
||||||
setUploading(true);
|
setUploading(true);
|
||||||
try {
|
try {
|
||||||
await uploadToSupabase(file);
|
if (!org?.id) {
|
||||||
|
toast.error("Organization is not loaded");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const logoUrl = await uploadToR2(file);
|
||||||
const { error } = await authClient.organization.update({
|
const { error } = await authClient.organization.update({
|
||||||
data: { logo: getOrgLogoUrl(org.id) },
|
data: { logo: logoUrl },
|
||||||
});
|
});
|
||||||
if (error) {
|
if (error) {
|
||||||
toast.error(error.message || "Failed to update logo");
|
toast.error(error.message || "Failed to update logo");
|
||||||
|
|||||||
Reference in New Issue
Block a user