feat: saving on toggle

This commit is contained in:
Julien Calixte
2026-01-20 23:02:33 +01:00
parent 4a53a17de4
commit e316d8080b
2 changed files with 204 additions and 9 deletions

View File

@@ -1,8 +1,10 @@
<script setup lang="ts">
import { computed, defineAsyncComponent, watch } from "vue"
import { computed, defineAsyncComponent, nextTick, ref, watch } from "vue"
import { useUserRepoStore } from "@/modules/repo/store/userRepo.store"
import { useFile } from "@/hooks/useFile.hook"
import { computedAsync } from "@vueuse/core"
import { useCheckboxCommit } from "@/hooks/useCheckboxCommit.hook"
import { useMarkdown } from "@/hooks/useMarkdown.hook"
import { queryFileContent } from "@/modules/repo/services/repo"
import { decodeBase64ToUTF8 } from "@/utils/decodeBase64ToUTF8"
const FluxNote = defineAsyncComponent(() => import("@/components/FluxNote.vue"))
const props = defineProps<{ user: string; repo: string }>()
@@ -15,14 +17,60 @@ const todoNote = computed(() =>
store.files.find((file) => file.path?.endsWith("_todo/todo.md")),
)
const content = computedAsync(() => {
if (!todoNote.value) {
const sha = computed(() => todoNote.value?.sha ?? "")
const path = computed(() => todoNote.value?.path)
const { toHTML } = useMarkdown(repo)
// Setup checkbox commit handler
const {
pendingContent,
syncContent,
listenToCheckboxes,
hasPendingChanges,
} = useCheckboxCommit({
user: props.user,
repo: props.repo,
path,
initialContent: "",
initialSha: sha,
containerSelector: ".todo-notes .note-display",
debounceMs: 1000,
})
// Render pending content to HTML for display
const renderedContent = computed(() => {
if (!pendingContent.value) {
return ""
}
const { getContent } = useFile(todoNote.value?.sha ?? "", false)
return getContent()
return toHTML(pendingContent.value)
})
// Fetch raw content when sha changes
watch(
sha,
async (newSha) => {
if (!newSha || hasPendingChanges.value) {
return
}
const base64Content = await queryFileContent(props.user, props.repo, newSha)
if (base64Content) {
const rawContent = decodeBase64ToUTF8(base64Content)
syncContent(rawContent, newSha)
}
},
{ immediate: true },
)
// Setup checkbox listeners when content renders
watch(
renderedContent,
async () => {
await nextTick()
listenToCheckboxes()
},
{ immediate: true },
)
</script>
<template>
@@ -31,7 +79,7 @@ const content = computedAsync(() => {
key="todo-notes"
:user="user"
:repo="repo"
:content="content"
:content="renderedContent"
:parse-content="false"
/>
</div>