Skip to content

Commit

Permalink
fix: race condition on get selected text logic
Browse files Browse the repository at this point in the history
  • Loading branch information
vineus committed Dec 17, 2023
1 parent 6edef45 commit 9c9fbd8
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 9 deletions.
11 changes: 8 additions & 3 deletions src/ask.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { LaunchProps } from "@raycast/api";
import { Detail, LaunchProps } from "@raycast/api";
import { AskDustQuestion } from "./answerQuestion";
import { AgentType } from "./dust_api/agent";
import { AskAgentQuestionForm, useGetSelectedText } from "./askAgent";
Expand All @@ -7,11 +7,16 @@ import { DUST_AGENT } from "./agents";
export default function AskDustCommand(props: LaunchProps<{ arguments: { search: string; agent?: AgentType } }>) {
const question = props.arguments.search;
const agent = props.arguments.agent || DUST_AGENT;
const { selectedText, isLoading } = useGetSelectedText();

const selectedText = question ? undefined : useGetSelectedText();
if (isLoading) {
return <Detail isLoading={true} />;
}

const initialQuestion = question ? undefined : selectedText;
return question ? (
<AskDustQuestion question={question} agent={agent} />
) : (
<AskAgentQuestionForm initialQuestion={selectedText} agent={agent} />
<AskAgentQuestionForm initialQuestion={initialQuestion} agent={agent} />
);
}
15 changes: 9 additions & 6 deletions src/askAgent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ interface AskAgentQuestionFormValues {

export function useGetSelectedText() {
const [selectedText, setSelectedText] = useState<string | undefined>(undefined);
const [isLoading, setIsLoading] = useState(true);
useEffect(() => {
async function fetchHighlightedText() {
let text: string | undefined;
Expand All @@ -36,10 +37,11 @@ export function useGetSelectedText() {
await showToast(Toast.Style.Success, "Used highlighted text as question.");
setSelectedText(text);
}
setIsLoading(false);
}
fetchHighlightedText();
}, []);
return selectedText;
return { selectedText: selectedText, isLoading: isLoading };
}

export function AskAgentQuestionForm({ agent, initialQuestion }: { agent: AgentType; initialQuestion?: string }) {
Expand Down Expand Up @@ -83,10 +85,10 @@ export function AskAgentQuestionForm({ agent, initialQuestion }: { agent: AgentT
const QUICK_AGENT_KEY = "dust_favorite_agent";

export default function AskDustAgentCommand() {
const { agents, isLoading, error } = useAgents();
const { agents, error, isLoading: isLoadingAgents } = useAgents();
const [favoriteAgentId, setFavoriteAgentId] = useState<string | undefined>(undefined);
const [isLoadingFavorite, setIsLoadingFavorite] = useState(true);
const selectedText = useGetSelectedText();
const { selectedText, isLoading: isLoadingSelectedText } = useGetSelectedText();

useEffect(() => {
async function fetchFavoriteAgent() {
Expand All @@ -107,7 +109,7 @@ export default function AskDustAgentCommand() {
title: `Could not refresh agents list: ${error}`,
});
}
if (!isLoading && agents) {
if (!isLoadingAgents && agents) {
showToast({
style: Toast.Style.Success,
title: `Loaded ${Object.values(agents).length} agents`,
Expand Down Expand Up @@ -152,9 +154,10 @@ export default function AskDustAgentCommand() {
})
: undefined;

const isLoading = isLoadingAgents || isLoadingSelectedText || isLoadingFavorite;
return (
<List isLoading={isLoading && isLoadingFavorite}>
{!agents && <List.EmptyView icon={Icon.XMarkCircle} title="No Dust agents loaded" />}
<List isLoading={isLoading}>
{(!agents || isLoading) && <List.EmptyView icon={Icon.XMarkCircle} title="No Dust agents loaded" />}
{agents && favoriteAgentId && (
<AgentListItem
agent={agents[favoriteAgentId]}
Expand Down

0 comments on commit 9c9fbd8

Please # to comment.