refactor(notes): return failure status from pullLatest

Callers had to read back the side-effected status ref to know why a
pull failed, which also broke TS narrowing on the freshness badge
handler. Return { raw, failureStatus } so the reason flows through
the return value.
This commit is contained in:
Julien Calixte
2026-05-25 21:25:54 +02:00
parent 60e3849c20
commit 54c52feeba
2 changed files with 17 additions and 14 deletions

View File

@@ -236,10 +236,10 @@ watch(mode, async (newMode) => {
}) })
const onConflictDiscard = async () => { const onConflictDiscard = async () => {
const newRaw = await pullLatest() const { raw } = await pullLatest()
if (newRaw !== null) { if (raw !== null) {
rawContent.value = newRaw rawContent.value = raw
initialRawContent.value = newRaw initialRawContent.value = raw
} }
} }
@@ -269,13 +269,13 @@ const onBadgeClick = async () => {
return return
} }
const newRaw = await pullLatest() const { raw, failureStatus } = await pullLatest()
if (newRaw !== null) { if (raw !== null) {
rawContent.value = newRaw rawContent.value = raw
initialRawContent.value = newRaw initialRawContent.value = raw
return return
} }
if (freshnessStatus.value === "unauthorized") { if (failureStatus === "unauthorized") {
errorMessage("🔐 GitHub auth expired — please sign in again") errorMessage("🔐 GitHub auth expired — please sign in again")
} }
} catch (error) { } catch (error) {

View File

@@ -77,14 +77,17 @@ export const useNoteFreshness = ({
return { sha: result.sha, failureStatus: null } return { sha: result.sha, failureStatus: null }
} }
const pullLatest = async (): Promise<string | null> => { const pullLatest = async (): Promise<{
if (!path.value) return null raw: string | null
failureStatus: FreshnessStatus | null
}> => {
if (!path.value) return { raw: null, failureStatus: null }
const usedCachedSha = latestSha.value !== null const usedCachedSha = latestSha.value !== null
const { sha: remoteSha, failureStatus } = await resolveRemoteSha(path.value) const { sha: remoteSha, failureStatus } = await resolveRemoteSha(path.value)
if (!remoteSha) { if (!remoteSha) {
console.warn("pullLatest: could not resolve remote sha", { path: path.value }) console.warn("pullLatest: could not resolve remote sha", { path: path.value })
if (failureStatus) status.value = failureStatus if (failureStatus) status.value = failureStatus
return null return { raw: null, failureStatus }
} }
const fileContent = await queryFileContent(user, repo, remoteSha) const fileContent = await queryFileContent(user, repo, remoteSha)
if (!fileContent) { if (!fileContent) {
@@ -96,7 +99,7 @@ export const useNoteFreshness = ({
// Cached SHA may be stale — clear so the next click re-resolves it. // Cached SHA may be stale — clear so the next click re-resolves it.
if (usedCachedSha) latestSha.value = null if (usedCachedSha) latestSha.value = null
status.value = "offline" status.value = "offline"
return null return { raw: null, failureStatus: "offline" }
} }
const { saveCacheNote } = prepareNoteCache(sha.value, path.value) const { saveCacheNote } = prepareNoteCache(sha.value, path.value)
await saveCacheNote(fileContent, { await saveCacheNote(fileContent, {
@@ -108,7 +111,7 @@ export const useNoteFreshness = ({
lastCheckedAt.value = new Date() lastCheckedAt.value = new Date()
status.value = "verified" status.value = "verified"
const { getRawContent } = markdownBuilder(sha.value) const { getRawContent } = markdownBuilder(sha.value)
return getRawContent(fileContent) return { raw: getRawContent(fileContent), failureStatus: null }
} }
return { return {