Skip to content

Commit

Permalink
fix(ai-help): refetch quota after an ai error (#10615)
Browse files Browse the repository at this point in the history
  • Loading branch information
argl authored Mar 1, 2024
1 parent 2e06a1a commit aa6b141
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 12 deletions.
23 changes: 11 additions & 12 deletions client/src/plus/ai-help/use-ai.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ import { useCallback, useEffect, useReducer, useRef, useState } from "react";
import { useSearchParams } from "react-router-dom";

import { SSE } from "sse.js";
import useSWR from "swr";
import useSWR, { mutate } from "swr";
import { AIHelpLog } from "./rust-types";
import { useGleanClick } from "../../telemetry/glean-context";
import { AI_HELP } from "../../telemetry/constants";
import { useAIHelpSettings } from "./utils";
import { EVENT_TIMEOUT } from "./constants";
import { AI_HELP_QUOTA_PATH } from "../common/api";

const RETRY_INTERVAL = 10000;
const ERROR_TIMEOUT = 60000;
Expand Down Expand Up @@ -384,6 +385,7 @@ export function useAiChat({
eventSourceRef.current?.close();
eventSourceRef.current = undefined;
setLoadingState("failed");
mutate(AI_HELP_QUOTA_PATH);
console.error(err);
},
[gleanClick]
Expand Down Expand Up @@ -694,18 +696,15 @@ export function useAiChat({
}, [state, path, setMessages]);

function useRemoteQuota() {
const { data } = useSWR<Quota>(
"/api/v1/plus/ai/help/quota",
async (url) => {
const response = await fetch(url);
if (!response.ok) {
const text = await response.text();
throw new Error(`${response.status} on ${url}: ${text}`);
}
const data = await response.json();
return data.quota;
const { data } = useSWR<Quota>(AI_HELP_QUOTA_PATH, async (url) => {
const response = await fetch(url);
if (!response.ok) {
const text = await response.text();
throw new Error(`${response.status} on ${url}: ${text}`);
}
);
const data = await response.json();
return data.quota;
});

return data;
}
Expand Down
1 change: 1 addition & 0 deletions client/src/plus/common/api.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export const STRIPE_PLANS_PATH = "/api/v1/stripe/plans";
export const SETTINGS_BASE_PATH = "/api/v1/plus/settings/";
export const NEWSLETTER_BASE_PATH = "/api/v1/plus/newsletter/";
export const AI_HELP_QUOTA_PATH = "/api/v1/plus/ai/help/quota";

export type PLUS_SETTINGS = {
col_in_search: boolean;
Expand Down

0 comments on commit aa6b141

Please sign in to comment.