-
Notifications
You must be signed in to change notification settings - Fork 15
feat: migrate account update to dedicated api #1649
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
c12eed5
eba5cf8
df82665
dbe539f
3a0051b
a894f5c
94299b4
61bb297
e60d74b
d2cb78b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -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; | ||||||||||||||||||||||||||||||||||||||||
|
|
@@ -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(""); | ||||||||||||||||||||||||||||||||||||||||
|
|
@@ -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({ | ||||||||||||||||||||||||||||||||||||||||
| accessToken, | ||||||||||||||||||||||||||||||||||||||||
| accountId: orgId, | ||||||||||||||||||||||||||||||||||||||||
| name, | ||||||||||||||||||||||||||||||||||||||||
| image, | ||||||||||||||||||||||||||||||||||||||||
| instruction, | ||||||||||||||||||||||||||||||||||||||||
| knowledges, | ||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+152
to
+159
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. P2: Handle Prompt for AI agents
Suggested change
|
||||||||||||||||||||||||||||||||||||||||
| 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, | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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(); | ||
|
|
@@ -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; | ||
| } | ||
cubic-dev-ai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| 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
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 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 |
||
|
|
||
| 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; | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
save()is used as a boolean-returning action (const success = await save()inOrgSettings), but this call now propagates exceptions when the API returns a non-2xx response (e.g., expired token/401 or validation/400). Becausesave()no longer converts that failure intofalse,handleSavehas notry/catchpath and the rejection becomes unhandled instead of a normal failed save state, which regresses error handling compared with the previous implementation.Useful? React with 👍 / 👎.