progress for file update inapp

This commit is contained in:
Julien Calixte
2023-08-20 23:55:25 +02:00
parent 767e238848
commit ccb486a0b6
6 changed files with 118 additions and 24 deletions

View File

@@ -1,4 +1,4 @@
import { Ref, ref, toValue } from 'vue'
import { computed, Ref, ref, toValue } from 'vue'
import { useMarkdown } from '@/hooks/useMarkdown.hook'
import { prepareNoteCache } from '@/modules/note/cache/prepareNoteCache'
@@ -6,10 +6,16 @@ import { getFileContent } from '@/modules/repo/services/repo'
import { useUserRepoStore } from '@/modules/repo/store/userRepo.store'
export const useFile = (sha: Ref<string> | string, retrieveContent = true) => {
const store = useUserRepoStore()
const path = computed(() => {
const file = store.files.find((file) => file.sha === toValue(sha))
return file?.path ?? ''
})
const { render, getRawContent: getRawContentFromFile } = useMarkdown(
toValue(sha)
)
const store = useUserRepoStore()
const { getCachedNote, saveCacheNote } = prepareNoteCache(toValue(sha))
const fromCache = ref(false)
@@ -41,28 +47,37 @@ export const useFile = (sha: Ref<string> | string, retrieveContent = true) => {
const getRawContent = async () => {
const fileContent = await getCachedFileContent()
if (!fileContent) {
return
return null
}
rawContent.value = getRawContentFromFile(fileContent)
return rawContent.value
return getRawContentFromFile(fileContent)
}
const getContent = async () => {
const fileContent = await getCachedFileContent()
if (!fileContent) {
return
return null
}
content.value = render(fileContent)
return content.value
return render(fileContent)
}
if (retrieveContent) {
getContent()
getRawContent()
getCachedFileContent().then((fileContent) => {
if (!fileContent) {
return
}
rawContent.value = getRawContentFromFile(fileContent)
content.value = render(fileContent)
})
}
return {
path,
content,
rawContent,
getRawContent,

View File

@@ -0,0 +1,49 @@
import { getOctokit } from '@/modules/repo/services/octo'
import { encodeUTF8ToBase64 } from '@/utils/decodeBase64ToUTF8'
import { confirmMessage, errorMessage } from '@/utils/notif'
export const useGitHubUpdate = ({
user,
repo,
sha
}: {
user: string
repo: string
sha: string
}) => {
const updateFile = async ({
content,
path
}: {
content: string
path: string
}) => {
try {
const octokit = await getOctokit()
const response = await octokit.request(
`PUT /repos/{owner}/{repo}/contents/{path}`,
{
owner: user,
repo,
path,
message: `Updating ${path} from Lite Note`,
content: encodeUTF8ToBase64(content),
sha
}
)
confirmMessage('file saved on GitHub')
return response?.data.commit.sha ?? null
} catch (error) {
errorMessage('File could not be saved')
}
return null
}
return {
updateFile
}
}