Skip to content
Merged
78 changes: 0 additions & 78 deletions app/api/account/update/route.tsx

This file was deleted.

39 changes: 19 additions & 20 deletions hooks/useOrgSettings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import { uploadFile } from "@/lib/arweave/uploadFile";
import { getFileMimeType } from "@/utils/getFileMimeType";
import { getClientApiBaseUrl } from "@/lib/api/getClientApiBaseUrl";
import useAccountOrganizations from "./useAccountOrganizations";
import { usePrivy } from "@privy-io/react-auth";
import { updateAccountProfile } from "@/lib/accounts/updateAccountProfile";

interface KnowledgeItem {
name: string;
Expand All @@ -21,6 +23,7 @@ interface OrgData {

const useOrgSettings = (orgId: string | null) => {
const { data: organizations } = useAccountOrganizations();
const { getAccessToken } = usePrivy();
const queryClient = useQueryClient();
const [orgData, setOrgData] = useState<OrgData | null>(null);
const [name, setName] = useState("");
Expand Down Expand Up @@ -141,30 +144,26 @@ const useOrgSettings = (orgId: string | null) => {

setIsSaving(true);
try {
const response = await fetch("/api/account/update", {
method: "POST",
body: JSON.stringify({
accountId: orgId,
name,
image,
instruction,
knowledges,
}),
headers: { "Content-Type": "application/json" },
});

if (response.ok) {
const data = await response.json();
setOrgData(data.data);
// Invalidate org list cache so sidebar shows updated image/name immediately
await queryClient.invalidateQueries({ queryKey: ["accountOrganizations"] });
return true;
const accessToken = await getAccessToken();
if (!accessToken) {
return false;
}
return false;

const data = await updateAccountProfile({
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Catch account update errors in org settings save

save() is used as a boolean-returning action (const success = await save() in OrgSettings), but this call now propagates exceptions when the API returns a non-2xx response (e.g., expired token/401 or validation/400). Because save() no longer converts that failure into false, handleSave has no try/catch path and the rejection becomes unhandled instead of a normal failed save state, which regresses error handling compared with the previous implementation.

Useful? React with 👍 / 👎.

accessToken,
accountId: orgId,
name,
image,
instruction,
knowledges,
});
Comment on lines +152 to +159
Copy link
Copy Markdown

@cubic-dev-ai cubic-dev-ai bot Apr 6, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2: Handle updateAccountProfile errors in save() and return false on failure instead of letting the promise reject.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At hooks/useOrgSettings.ts, line 177:

<comment>Handle `updateAccountProfile` errors in `save()` and return `false` on failure instead of letting the promise reject.</comment>

<file context>
@@ -141,30 +169,32 @@ const useOrgSettings = (orgId: string | null) => {
       }
-      return false;
+
+      const data = await updateAccountProfile({
+        accessToken,
+        accountId: orgId,
</file context>
Suggested change
const data = await updateAccountProfile({
accessToken,
accountId: orgId,
name,
image,
instruction,
knowledges,
});
const data = await updateAccountProfile({
accessToken,
accountId: orgId,
name,
image,
instruction,
knowledges,
}).catch(() => null);
if (!data) {
return false;
}
Fix with Cubic

setOrgData(data);
await queryClient.invalidateQueries({ queryKey: ["accountOrganizations"] });
return true;
} finally {
setIsSaving(false);
}
}, [orgId, name, image, instruction, knowledges, queryClient]);
}, [orgId, name, image, instruction, knowledges, queryClient, getAccessToken]);

return {
orgData,
Expand Down
35 changes: 15 additions & 20 deletions hooks/useUser.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { useAccount } from "wagmi";
import { toast } from "sonner";
import { AccountWithDetails } from "@/lib/supabase/accounts/getAccountWithDetails";
import { fetchOrCreateAccount } from "@/lib/accounts/fetchOrCreateAccount";
import { updateAccountProfile } from "@/lib/accounts/updateAccountProfile";

const useUser = () => {
const { login, user, logout, getAccessToken } = usePrivy();
Expand Down Expand Up @@ -64,29 +65,23 @@ const useUser = () => {

setUpdating(true);
try {
const response = await fetch("/api/account/update", {
method: "POST",
body: JSON.stringify({
instruction,
organization,
name,
image,
jobTitle,
roleType,
companyName,
accountId: userData.account_id,
}),
headers: {
"Content-Type": "application/json",
},
});

if (!response.ok) {
const accessToken = await getAccessToken();
if (!accessToken) {
return;
}

const data = await response.json();
setUserData(data.data);
const data = await updateAccountProfile({
accessToken,
accountId: userData.account_id,
instruction,
organization,
name,
image,
jobTitle,
roleType,
companyName,
});
setUserData(data);
setIsModalOpen(false);
} catch {
// Error handled silently
Comment on lines 86 to 87
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

Empty catch block swallows errors without feedback.

The comment "Error handled silently" describes what is happening but not why this is the desired behavior. Silent error swallowing makes debugging difficult. Consider at minimum logging the error, or providing user feedback via toast (which is already imported).

🛡️ Proposed fix with user feedback
-    } catch {
-      // Error handled silently
+    } catch (error) {
+      console.error("Failed to save profile:", error);
+      toast.error("Failed to save profile. Please try again.");
     } finally {
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@hooks/useUser.tsx` around lines 85 - 86, The empty catch block in the useUser
hook is swallowing errors silently; update the catch in the try/catch around the
asynchronous user logic (the catch in hooks/useUser.tsx inside the useUser hook)
to at minimum log the error via console.error or processLogger and show user
feedback using the imported toast (e.g., toast.error) so errors are visible
during debugging and the user gets notified; ensure the catch accepts an error
parameter (e) and include that error in the log/toast message for context.

Expand Down
44 changes: 44 additions & 0 deletions lib/accounts/updateAccountProfile.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { getClientApiBaseUrl } from "@/lib/api/getClientApiBaseUrl";

type KnowledgeItem = {
name: string;
url: string;
type: string;
};

type UpdateAccountProfileArgs = {
accountId: string;
accessToken: string;
name?: string;
instruction?: string;
organization?: string;
image?: string;
jobTitle?: string;
roleType?: string;
companyName?: string;
knowledges?: KnowledgeItem[];
};

/**
* Updates an account profile using the dedicated accounts API.
*/
export async function updateAccountProfile({
accessToken,
...body
}: UpdateAccountProfileArgs) {
const response = await fetch(`${getClientApiBaseUrl()}/api/accounts`, {
method: "PATCH",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${accessToken}`,
},
body: JSON.stringify(body),
});

if (!response.ok) {
throw new Error(`Failed to update account: ${response.status}`);
}

const data = await response.json();
return data.data;
}
Loading