create today fleeting note
This commit is contained in:
52
src/hooks/useGitHubContent.hook.ts
Normal file
52
src/hooks/useGitHubContent.hook.ts
Normal file
@@ -0,0 +1,52 @@
|
||||
import { getOctokit } from '@/modules/repo/services/octo'
|
||||
import { encodeUTF8ToBase64 } from '@/utils/decodeBase64ToUTF8'
|
||||
import { confirmMessage, errorMessage } from '@/utils/notif'
|
||||
|
||||
export const useGitHubContent = ({
|
||||
user,
|
||||
repo
|
||||
}: {
|
||||
user: string
|
||||
repo: string
|
||||
}) => {
|
||||
const putFile = async ({
|
||||
content,
|
||||
path,
|
||||
sha
|
||||
}: {
|
||||
content: string
|
||||
path: string
|
||||
sha?: 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.content?.sha ?? null
|
||||
} catch (error) {
|
||||
errorMessage('File could not be saved')
|
||||
}
|
||||
|
||||
return null
|
||||
}
|
||||
|
||||
return {
|
||||
updateFile: async (props: { content: string; path: string; sha: string }) =>
|
||||
putFile(props),
|
||||
createFile: async (props: { content: string; path: string }) =>
|
||||
putFile(props)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user