✨ (user settings) init user settings with .litenote.json
This commit is contained in:
@@ -2,6 +2,7 @@ import { useGitHubLogin } from '@/hooks/useGitHubLogin.hook'
|
||||
import { useMarkdown } from '@/hooks/useMarkdown.hook'
|
||||
import { useNoteCache } from '@/modules/note/hooks/useNoteCache'
|
||||
import { RepoFile } from '@/modules/repo/interfaces/RepoFile'
|
||||
import { UserSettings } from '@/modules/repo/interfaces/UserSettings'
|
||||
import { Octokit } from '@octokit/rest'
|
||||
|
||||
export const getFiles = async (
|
||||
@@ -75,3 +76,49 @@ export const getMainReadme = async (owner: string, repo: string) => {
|
||||
|
||||
return null
|
||||
}
|
||||
|
||||
export const getUserSettingsContent = async (
|
||||
user: string,
|
||||
repo: string,
|
||||
files: RepoFile[]
|
||||
): Promise<UserSettings | null> => {
|
||||
const configFile = files.find((file) => file.path === '.litenote.json')
|
||||
|
||||
if (!configFile?.sha) {
|
||||
return null
|
||||
}
|
||||
const content = await getFileContent(user, repo, configFile.sha)
|
||||
|
||||
if (!content) {
|
||||
return null
|
||||
}
|
||||
|
||||
return JSON.parse(atob(content)) as UserSettings
|
||||
}
|
||||
|
||||
export const getFileContent = async (
|
||||
user: string,
|
||||
repo: string,
|
||||
sha: string
|
||||
) => {
|
||||
const { accessToken } = useGitHubLogin()
|
||||
|
||||
const octokit = new Octokit({
|
||||
auth: accessToken.value
|
||||
})
|
||||
|
||||
if (!user || !repo) {
|
||||
null
|
||||
}
|
||||
|
||||
const file = await octokit.request(
|
||||
'GET /repos/{owner}/{repo}/git/blobs/{file_sha}',
|
||||
{
|
||||
owner: user,
|
||||
repo: repo,
|
||||
file_sha: sha
|
||||
}
|
||||
)
|
||||
|
||||
return file?.data.content ?? null
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user