Skip to content

Commit 8f68d60

Browse files
committed
refactor: streamline cloud model disabling logic in authentication flow
- Extracted the logic for disabling cloud models into a separate function, `disableCloudModelsIfEnabled`, to improve code readability and maintainability. - Updated the `handleInactiveAccount` and sign-out processes to utilize the new function, ensuring consistent behavior when clearing sessions. - Enhanced error handling in the CDP form detection process by adding logging for AX tree retrieval attempts, improving debugging capabilities.
1 parent 06a2a50 commit 8f68d60

5 files changed

Lines changed: 36 additions & 23 deletions

File tree

src/components/ui/combobox.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,9 @@ export interface ComboboxProps {
3434
className?: string;
3535
id?: string;
3636
name?: string;
37-
portalContainer?: React.ComponentProps<typeof PopoverContent>["portalContainer"];
37+
portalContainer?: React.ComponentProps<
38+
typeof PopoverContent
39+
>["portalContainer"];
3840
"aria-invalid"?: boolean;
3941
}
4042

src/components/ui/popover.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@ function PopoverContent({
2222
portalContainer,
2323
...props
2424
}: React.ComponentProps<typeof PopoverPrimitive.Content> & {
25-
portalContainer?: React.ComponentProps<typeof PopoverPrimitive.Portal>["container"];
25+
portalContainer?: React.ComponentProps<
26+
typeof PopoverPrimitive.Portal
27+
>["container"];
2628
}) {
2729
return (
2830
<PopoverPrimitive.Portal container={portalContainer}>

src/hooks/use-auth.ts

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -45,26 +45,31 @@ export function useAuth(): AuthState & AuthActions {
4545
setInactiveMessage(null);
4646
}, []);
4747

48+
const disableCloudModelsIfEnabled = useCallback(async () => {
49+
const currentSettings = await storage.aiSettings.getValue();
50+
51+
if (currentSettings.cloudModelsEnabled) {
52+
await storage.aiSettings.setValue({
53+
...currentSettings,
54+
cloudModelsEnabled: false,
55+
});
56+
}
57+
}, []);
58+
4859
const handleInactiveAccount = useCallback(
4960
async (message?: string) => {
5061
const authService = getAuthService();
5162
await authService.clearSession();
5263
queryClient.setQueryData(["auth", "session"], null);
5364

54-
const currentSettings = await storage.aiSettings.getValue();
55-
if (currentSettings.cloudModelsEnabled) {
56-
await storage.aiSettings.setValue({
57-
...currentSettings,
58-
cloudModelsEnabled: false,
59-
});
60-
}
65+
await disableCloudModelsIfEnabled();
6166

6267
setPendingApproval(true);
6368
setInactiveMessage(
6469
message ?? "Account pending approval. Please contact support.",
6570
);
6671
},
67-
[queryClient],
72+
[queryClient, disableCloudModelsIfEnabled],
6873
);
6974

7075
const fetchUserStatus = useCallback(
@@ -215,15 +220,9 @@ export function useAuth(): AuthState & AuthActions {
215220
const signOutMutation = useMutation({
216221
mutationFn: async () => {
217222
const authService = getAuthService();
218-
await authService.clearSession();
219223

220-
const currentSettings = await storage.aiSettings.getValue();
221-
if (currentSettings.cloudModelsEnabled) {
222-
await storage.aiSettings.setValue({
223-
...currentSettings,
224-
cloudModelsEnabled: false,
225-
});
226-
}
224+
await authService.clearSession();
225+
await disableCloudModelsIfEnabled();
227226

228227
logger.debug("Signed out successfully");
229228
},

src/lib/cdp/cdp-fill-verifier.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,10 @@ function normalize(value: unknown): string {
3535
function valuesMatch(actual: string, expected: string): boolean {
3636
const actualNorm = normalize(actual);
3737
const expectedNorm = normalize(expected);
38+
3839
if (!expectedNorm) return actualNorm.length === 0;
3940
if (actualNorm === expectedNorm) return true;
41+
4042
return actualNorm.includes(expectedNorm) || expectedNorm.includes(actualNorm);
4143
}
4244

src/lib/cdp/cdp-form-detector.ts

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -73,12 +73,20 @@ export async function detectFormFields(
7373
let axTree: AXTreeResponse | null = null;
7474

7575
for (let attempt = 0; attempt < MAX_RETRIES; attempt++) {
76-
axTree = await sendCommand<AXTreeResponse>(
77-
tabId,
78-
"Accessibility.getFullAXTree",
79-
);
76+
try {
77+
axTree = await sendCommand<AXTreeResponse>(
78+
tabId,
79+
"Accessibility.getFullAXTree",
80+
);
81+
} catch (error) {
82+
logger.error("Failed to get AX tree:", {
83+
error,
84+
attempt: attempt + 1,
85+
});
86+
axTree = null;
87+
}
8088

81-
if (axTree?.nodes?.length > 0) break;
89+
if (axTree?.nodes && axTree.nodes.length > 0) break;
8290

8391
logger.warn(
8492
`AX tree empty on attempt ${attempt + 1}, retrying in ${RETRY_DELAY_MS}ms`,

0 commit comments

Comments
 (0)