Skip to content

Commit 89a2962

Browse files
authored
Merge pull request #18 from stacklok/issue-398
fix: properly parse title and prompt when there are snippets
2 parents d1e56ee + b64a0a6 commit 89a2962

File tree

3 files changed

+25
-7
lines changed

3 files changed

+25
-7
lines changed

src/components/PromptList.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ export function PromptList({ prompts }: { prompts: Prompt[] }) {
1212
const { currentPromptId, setCurrentPromptId } = usePromptsStore();
1313

1414
const groupedPrompts = groupPromptsByRelativeDate(prompts);
15+
1516
return (
1617
<div className="mx-2">
1718
{Object.entries(groupedPrompts).map(([group, prompts]) => (

src/hooks/useBreadcrumb.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { extractTitleFromMessage } from "@/lib/utils";
1+
import { extractTitleFromMessage, sanitizeQuestionPrompt } from "@/lib/utils";
22
import { useLocation } from "react-router-dom";
33
import { usePromptsStore } from "./usePromptsStore";
44

@@ -20,7 +20,12 @@ export function useBreadcrumb() {
2020
try {
2121
const chat = prompts.find((prompt) => prompt.chat_id === currentPromptId);
2222
const title = chat?.question_answers?.[0].question.message ?? "";
23-
return extractTitleFromMessage(title) ?? "";
23+
24+
const sanitized = sanitizeQuestionPrompt({
25+
question: title,
26+
answer: chat?.question_answers?.[0]?.answer?.message ?? "",
27+
});
28+
return extractTitleFromMessage(sanitized) ?? "";
2429
} catch {
2530
return "";
2631
}

src/lib/utils.ts

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ export function extractTitleFromMessage(message: string) {
2020
if (match) {
2121
const beforeMarkdown = match[1].trim();
2222
const afterMarkdown = match[2].trim();
23-
2423
const title = beforeMarkdown || afterMarkdown;
2524
return title;
2625
}
@@ -121,12 +120,25 @@ export function sanitizeQuestionPrompt({
121120
answer: string;
122121
}) {
123122
try {
123+
// Check if 'answer' is truthy; if so, try to find and return the text after "Query:"
124124
if (answer) {
125-
return question.split("Query:").pop() ?? "";
125+
const index = question.indexOf("Query:");
126+
if (index !== -1) {
127+
// Return the substring starting right after the first occurrence of "Query:"
128+
// Adding the length of "Query:" to the index to start after it
129+
return question.substring(index + "Query:".length).trim();
130+
} else {
131+
// If there is no "Query:" in the string, log the condition and return an empty string
132+
console.log("No 'Query:' found in the question.");
133+
return "";
134+
}
135+
} else {
136+
// If 'answer' is not provided or falsy, return the original question
137+
return question;
126138
}
127-
128-
return question;
129-
} catch {
139+
} catch (error) {
140+
// Log the error and return the original question as a fallback
141+
console.error("Error processing the question:", error);
130142
return question;
131143
}
132144
}

0 commit comments

Comments
 (0)