From 21b59887998ed042aff2f6ccbbfbbd327f49eee0 Mon Sep 17 00:00:00 2001
From: omagdy7 <omar.professional8777@gmail.com>
Date: Thu, 23 Nov 2023 20:33:10 +0200
Subject: Applying DRY principle and removing duplicate code

---
 src/ollama.tsx | 78 +++++++++++++++++++++-------------------------------------
 1 file changed, 28 insertions(+), 50 deletions(-)

(limited to 'src/ollama.tsx')

diff --git a/src/ollama.tsx b/src/ollama.tsx
index 4931fda..f5eaae7 100644
--- a/src/ollama.tsx
+++ b/src/ollama.tsx
@@ -109,7 +109,7 @@ export async function askWithContext(prompt: string) {
     for (const block of currentBlocksTree) {
       blocksContent += await getTreeContent(block)
     }
-    askAI(prompt, `With the Context of : ${blocksContent}`)
+    askAI(prompt, `Context: ${blocksContent}`)
   } catch (e: any) {
     logseq.App.showMsg(e.toString(), 'warning')
     console.error(e)
@@ -179,23 +179,6 @@ export async function summarizeBlockFromEvent(b: IHookEvent) {
   }
 }
 
-export async function convertToFlashCardFromEvent(b: IHookEvent) {
-  const currentBlock = await logseq.Editor.getBlock(b.uuid)
-  if (!currentBlock) {
-    throw new Error("Block not found");
-  }
-  await convertToFlashCard(currentBlock.uuid, currentBlock.content)
-}
-
-export async function convertToFlashCardCurrentBlock() {
-  const currentBlock = await logseq.Editor.getCurrentBlock()
-  if (!currentBlock) {
-    throw new Error("Block not found");
-  }
-  await convertToFlashCard(currentBlock.uuid, currentBlock.content)
-}
-
-
 export async function convertToFlashCard(uuid: string, blockContent: string) {
   try {
     const questionBlock = await logseq.Editor.insertBlock(uuid, "Genearting question....", { before: false })
@@ -211,42 +194,28 @@ export async function convertToFlashCard(uuid: string, blockContent: string) {
   }
 }
 
-export async function DivideTaskIntoSubTasksFromEvent(b: IHookEvent) {
-  try {
-    const currentBlock = await logseq.Editor.getBlock(b.uuid)
-    if (!currentBlock) {
-      throw new Error("Block not found");
-    }
-    const block = await logseq.Editor.insertBlock(currentBlock.uuid, "Genearting todos....", { before: false })
-    if (!block) {
-      throw new Error("Block not found");
-    }
-    if (currentBlock) {
-      let i = 0;
-      const response = await promptLLM(`Divide this task into subtasks with numbers: ${currentBlock.content} `)
-      for (const todo of response.split("\n")) {
-        if (i == 0) {
-          await logseq.Editor.updateBlock(block.uuid, `TODO ${todo.slice(3)} `)
-        } else {
-          await logseq.Editor.insertBlock(currentBlock.uuid, `TODO ${todo.slice(3)} `, { before: false })
-        }
-        i++;
-      }
-    }
-  } catch (e: any) {
-    logseq.App.showMsg(e.toString(), 'warning')
-    console.error(e)
-  }
+export async function convertToFlashCardFromEvent(b: IHookEvent) {
+  const currentBlock = await logseq.Editor.getBlock(b.uuid)
+  await convertToFlashCard(currentBlock!.uuid, currentBlock!.content)
 }
 
-export async function DivideTaskIntoSubTasks() {
+export async function convertToFlashCardCurrentBlock() {
+  const currentBlock = await logseq.Editor.getCurrentBlock()
+  await convertToFlashCard(currentBlock!.uuid, currentBlock!.content)
+}
+
+export async function DivideTaskIntoSubTasks(uuid: string, content: string) {
   try {
-    const currentBlock = await logseq.Editor.getCurrentBlock()
-    if (currentBlock) {
-      const response = await promptLLM(`Divide this task into subtasks with numbers: ${currentBlock.content} `)
-      for (const todo of response.split("\n")) {
-        await logseq.Editor.insertBlock(currentBlock.uuid, `TODO ${todo.slice(3)} `, { before: false })
+    const block = await logseq.Editor.insertBlock(uuid, "Genearting todos....", { before: false })
+    let i = 0;
+    const response = await promptLLM(`Divide this task into subtasks with numbers: ${content} `)
+    for (const todo of response.split("\n")) {
+      if (i == 0) {
+        await logseq.Editor.updateBlock(block!.uuid, `TODO ${todo.slice(3)} `)
+      } else {
+        await logseq.Editor.insertBlock(uuid, `TODO ${todo.slice(3)} `, { before: false })
       }
+      i++;
     }
   } catch (e: any) {
     logseq.App.showMsg(e.toString(), 'warning')
@@ -254,4 +223,13 @@ export async function DivideTaskIntoSubTasks() {
   }
 }
 
+export async function DivideTaskIntoSubTasksFromEvent(b: IHookEvent) {
+  const currentBlock = await logseq.Editor.getBlock(b.uuid)
+  DivideTaskIntoSubTasks(currentBlock!.uuid, currentBlock!.content)
+}
+
+export async function DivideTaskIntoSubTasksCurrentBlock() {
+  const currentBlock = await logseq.Editor.getCurrentBlock()
+  DivideTaskIntoSubTasks(currentBlock!.uuid, currentBlock!.content)
+}
 
-- 
cgit v1.2.3

