Better defaults if prompt becomes invalid

This commit is contained in:
Caleb Owens 2024-05-22 18:05:36 +02:00
parent d80475e563
commit 53abdffb5c
2 changed files with 17 additions and 4 deletions

View File

@ -30,7 +30,9 @@ export class PromptService {
selectedBranchPrompt(projectId: string): Prompt | undefined {
const id = get(this.selectedBranchPromptId(projectId));
return get(this.branchPrompts.userPrompts).find((userPrompt) => userPrompt.id == id)?.prompt;
if (!id) return;
return this.findPrompt(get(this.branchPrompts.userPrompts), id);
}
selectedCommitPromptId(projectId: string): Persisted<string | undefined> {
@ -39,9 +41,19 @@ export class PromptService {
selectedCommitPrompt(projectId: string): Prompt | undefined {
const id = get(this.selectedCommitPromptId(projectId));
console.log(id);
return get(this.commitPrompts.userPrompts).find((userPrompt) => userPrompt.id == id)?.prompt;
if (!id) return;
return this.findPrompt(get(this.commitPrompts.userPrompts), id);
}
findPrompt(prompts: UserPrompt[], promptId: string) {
const prompt = prompts.find((userPrompt) => userPrompt.id == promptId)?.prompt;
if (!prompt) return;
if (this.promptMissingContent(prompt)) return;
return prompt;
}
promptEquals(prompt1: Prompt, prompt2: Prompt) {

View File

@ -41,7 +41,8 @@
}
function initializeForm(form: HTMLFormElement) {
if ($selectedPromptId) {
// If the selectedPromptId is present and cooresponds to a valid prompt
if ($selectedPromptId && promptService.findPrompt($userPrompts, $selectedPromptId)) {
form.prompt.value = $selectedPromptId;
} else {
form.prompt.value = defaultId;