feat(notes): share an always-latest link, not just a snapshot
Some checks failed
CI / verify (push) Has been cancelled
Some checks failed
CI / verify (push) Has been cancelled
The Share button now offers two links: the pinned snapshot (default) and an "always latest" link that carries note paths in `?liveNotes` instead of blob shas. On open they resolve to the current shas against the HEAD file list and the URL is rewritten to the ordinary pinned form, so freshness, editing and the snapshot banner keep working unchanged.
This commit is contained in:
62
src/modules/note/liveNotes.spec.ts
Normal file
62
src/modules/note/liveNotes.spec.ts
Normal file
@@ -0,0 +1,62 @@
|
||||
import { describe, expect, it } from "vitest"
|
||||
|
||||
import { resolveLivePathsToShas, stackToLivePaths } from "./liveNotes"
|
||||
|
||||
const files = [
|
||||
{ path: "README.md", sha: "a".repeat(40) },
|
||||
{ path: "notes/one.md", sha: "b".repeat(40) },
|
||||
{ path: "notes/two.md", sha: "c".repeat(40) }
|
||||
]
|
||||
|
||||
describe("stackToLivePaths", () => {
|
||||
it("maps each stacked sha to its current file path", () => {
|
||||
expect(stackToLivePaths(["b".repeat(40), "c".repeat(40)], files)).toEqual([
|
||||
"notes/one.md",
|
||||
"notes/two.md"
|
||||
])
|
||||
})
|
||||
|
||||
it("keeps a sha verbatim when no path resolves (drifted snapshot)", () => {
|
||||
const orphan = "d".repeat(40)
|
||||
expect(stackToLivePaths(["b".repeat(40), orphan], files)).toEqual([
|
||||
"notes/one.md",
|
||||
orphan
|
||||
])
|
||||
})
|
||||
|
||||
it("preserves order", () => {
|
||||
expect(stackToLivePaths(["c".repeat(40), "a".repeat(40)], files)).toEqual([
|
||||
"notes/two.md",
|
||||
"README.md"
|
||||
])
|
||||
})
|
||||
})
|
||||
|
||||
describe("resolveLivePathsToShas", () => {
|
||||
it("resolves each path to its latest sha", () => {
|
||||
expect(
|
||||
resolveLivePathsToShas(["notes/one.md", "notes/two.md"], files)
|
||||
).toEqual(["b".repeat(40), "c".repeat(40)])
|
||||
})
|
||||
|
||||
it("passes a sha-shaped entry through as a pinned fallback", () => {
|
||||
const orphan = "d".repeat(40)
|
||||
expect(resolveLivePathsToShas(["notes/one.md", orphan], files)).toEqual([
|
||||
"b".repeat(40),
|
||||
orphan
|
||||
])
|
||||
})
|
||||
|
||||
it("drops a renamed or deleted path so the view degrades gracefully", () => {
|
||||
expect(
|
||||
resolveLivePathsToShas(["notes/gone.md", "notes/two.md"], files)
|
||||
).toEqual(["c".repeat(40)])
|
||||
})
|
||||
|
||||
it("round-trips a live-shared stack back to the same shas", () => {
|
||||
const shas = ["a".repeat(40), "b".repeat(40)]
|
||||
expect(resolveLivePathsToShas(stackToLivePaths(shas, files), files)).toEqual(
|
||||
shas
|
||||
)
|
||||
})
|
||||
})
|
||||
38
src/modules/note/liveNotes.ts
Normal file
38
src/modules/note/liveNotes.ts
Normal file
@@ -0,0 +1,38 @@
|
||||
// A shared link pins each stacked note to a blob sha, so the recipient sees the
|
||||
// exact snapshot forever. A "living" link trades that pin for freshness: notes
|
||||
// are referenced by their file path instead, and re-resolved to the latest blob
|
||||
// sha against the repo's HEAD file list every time the link is opened.
|
||||
|
||||
const SHA_PATTERN = /^[0-9a-f]{40}$/i
|
||||
|
||||
/**
|
||||
* Encode the current stack (blob shas) as a living reference list. Each note
|
||||
* becomes its file path when one resolves from the HEAD file list, so the link
|
||||
* re-resolves to the latest version on open. A sha with no known path (e.g. an
|
||||
* already-drifted snapshot the sharer never pulled) is kept verbatim, staying
|
||||
* pinned — order is preserved either way.
|
||||
*/
|
||||
export const stackToLivePaths = (
|
||||
shas: ReadonlyArray<string>,
|
||||
files: ReadonlyArray<{ path?: string; sha?: string }>
|
||||
): string[] =>
|
||||
shas.map((sha) => files.find((file) => file.sha === sha)?.path ?? sha)
|
||||
|
||||
/**
|
||||
* Resolve a living reference list back to current blob shas against the HEAD
|
||||
* file list: a path maps to its latest sha; an entry already shaped like a sha
|
||||
* is passed through (the pinned fallback above); anything else — a renamed or
|
||||
* deleted path — is dropped so the view degrades gracefully instead of trying
|
||||
* to fetch a blob that no longer exists.
|
||||
*/
|
||||
export const resolveLivePathsToShas = (
|
||||
entries: ReadonlyArray<string>,
|
||||
files: ReadonlyArray<{ path?: string; sha?: string }>
|
||||
): string[] =>
|
||||
entries
|
||||
.map((entry) => {
|
||||
const latestSha = files.find((file) => file.path === entry)?.sha
|
||||
if (latestSha) return latestSha
|
||||
return SHA_PATTERN.test(entry) ? entry : null
|
||||
})
|
||||
.filter((sha): sha is string => sha !== null)
|
||||
Reference in New Issue
Block a user