diff --git a/src/components/dashboard/PagesManager.tsx b/src/components/dashboard/PagesManager.tsx index 43578af6..d66a864a 100644 --- a/src/components/dashboard/PagesManager.tsx +++ b/src/components/dashboard/PagesManager.tsx @@ -48,6 +48,7 @@ export const PagesManager: React.FC<{ characterId: site.data?.characterId, limit: 100, visibility, + handle: subdomain, }) // Batch selections diff --git a/src/lib/storage.ts b/src/lib/storage.ts index 9f9c639d..f1e281ff 100644 --- a/src/lib/storage.ts +++ b/src/lib/storage.ts @@ -7,8 +7,14 @@ try { data = JSON.parse(localStorage.getItem(namespace) || "{}") } catch (error) {} -export const getKeys = (key: string) => { - return Object.keys(data).filter((k) => k.startsWith(key)) +export const getKeys = (key: string | string[]) => { + return Object.keys(data).filter((k) => { + if (typeof key === "string") { + return k.startsWith(key) + } else { + return key.some((item) => k.startsWith(item)) + } + }) } export const getStorage = (key: string, noCache?: boolean) => { diff --git a/src/models/page.model.ts b/src/models/page.model.ts index 99929961..c639207b 100644 --- a/src/models/page.model.ts +++ b/src/models/page.model.ts @@ -146,63 +146,71 @@ export async function postNotes( ) } -const getLocalPages = (input: { characterId: number; isPost?: boolean }) => { +const getLocalPages = (input: { + characterId: number + isPost?: boolean + handle?: string +}) => { const pages: ExpandedNote[] = [] - getKeys(`draft-${input.characterId}-`).forEach((key) => { - const page = getStorage(key) - if (input.isPost === undefined || page.isPost === input.isPost) { - const note: ExpandedNote = { - characterId: input.characterId, - noteId: 0, - draftKey: key.replace(`draft-${input.characterId}-`, ""), - linkItemType: null, - linkKey: "", - toCharacterId: null, - toAddress: null, - toNoteId: null, - toHeadCharacterId: null, - toHeadNoteId: null, - toContractAddress: null, - toTokenId: null, - toLinklistId: null, - toUri: null, - deleted: false, - locked: false, - contractAddress: null, - uri: null, - operator: "", - owner: "", - createdAt: new Date(page.date).toISOString(), - updatedAt: new Date(page.date).toISOString(), - deletedAt: null, - transactionHash: "", - blockNumber: 0, - logIndex: 0, - updatedTransactionHash: "", - updatedBlockNumber: 0, - updatedLogIndex: 0, - metadata: { - content: { - title: page.values?.title, - content: page.values?.content, - date_published: page.values?.publishedAt, - summary: page.values?.excerpt, - tags: [ - page.isPost ? "post" : "page", - ...(page.values?.tags - ?.split(",") - .map((tag: string) => tag.trim()) - .filter((tag: string) => tag) || []), - ], - slug: page.values?.slug, - sources: ["xlog"], + getKeys([`draft-${input.characterId}-`, `draft-${input.handle}-`]).forEach( + (key) => { + const page = getStorage(key) + if (input.isPost === undefined || page.isPost === input.isPost) { + const note: ExpandedNote = { + characterId: input.characterId, + noteId: 0, + draftKey: key + .replace(`draft-${input.characterId}-`, "") + .replace(`draft-${input.handle}-`, ""), // In order to be compatible with old drafts + linkItemType: null, + linkKey: "", + toCharacterId: null, + toAddress: null, + toNoteId: null, + toHeadCharacterId: null, + toHeadNoteId: null, + toContractAddress: null, + toTokenId: null, + toLinklistId: null, + toUri: null, + deleted: false, + locked: false, + contractAddress: null, + uri: null, + operator: "", + owner: "", + createdAt: new Date(page.date).toISOString(), + updatedAt: new Date(page.date).toISOString(), + deletedAt: null, + transactionHash: "", + blockNumber: 0, + logIndex: 0, + updatedTransactionHash: "", + updatedBlockNumber: 0, + updatedLogIndex: 0, + metadata: { + content: { + title: page.values?.title, + content: page.values?.content, + date_published: page.values?.publishedAt, + summary: page.values?.excerpt, + tags: [ + page.isPost ? "post" : "page", + ...(page.values?.tags + ?.split(",") + .map((tag: string) => tag.trim()) + .filter((tag: string) => tag) || []), + ], + slug: page.values?.slug, + sources: ["xlog"], + }, }, - }, - local: true, + local: true, + } + pages.push(note) } - pages.push(note) - } - }) + }, + ) return pages } @@ -215,6 +223,7 @@ export async function getPagesBySite(input: { tags?: string[] useStat?: boolean keepBody?: boolean + handle?: string // In order to be compatible with old drafts }) { if (!input.characterId) { return { @@ -256,6 +265,7 @@ export async function getPagesBySite(input: { const local = getLocalPages({ characterId: input.characterId, isPost: input.type === "post", + handle: input.handle, }) local.forEach((localPage) => { @@ -342,6 +352,7 @@ export async function getPage(input: { characterId: number useStat?: boolean noteId?: number + handle?: string // In order to be compatible with old drafts }) { const mustLocal = input.slug?.startsWith("local-") @@ -365,6 +376,7 @@ export async function getPage(input: { // local page const local = getLocalPages({ characterId: input.characterId, + handle: input.handle, }) const localPage = local.find( (page) => diff --git a/src/pages/_site/[site]/preview/[page].tsx b/src/pages/_site/[site]/preview/[page].tsx index dd1c7d53..6c9f191b 100644 --- a/src/pages/_site/[site]/preview/[page].tsx +++ b/src/pages/_site/[site]/preview/[page].tsx @@ -46,6 +46,7 @@ function SitePagePage() { const page = useGetPage({ characterId: site.data?.characterId, slug: pageSlug, + handle: domainOrSubdomain, }) if (userRole.isSuccess && !userRole.data && page.isSuccess) { diff --git a/src/pages/dashboard/[subdomain]/editor.tsx b/src/pages/dashboard/[subdomain]/editor.tsx index b3e0fa03..f7084960 100644 --- a/src/pages/dashboard/[subdomain]/editor.tsx +++ b/src/pages/dashboard/[subdomain]/editor.tsx @@ -134,6 +134,7 @@ export default function SubdomainEditor() { characterId: site.data?.characterId, noteId: pageId && /\d+/.test(pageId) ? +pageId : undefined, slug: pageId || draftKey.replace(`draft-${site.data?.characterId}-`, ""), + handle: subdomain, }) const [visibility, setVisibility] = useState() diff --git a/src/queries/page.ts b/src/queries/page.ts index bccc9ce0..0e54db32 100644 --- a/src/queries/page.ts +++ b/src/queries/page.ts @@ -83,6 +83,7 @@ export const useGetPage = ( slug: input.slug, noteId: input.noteId, useStat: input.useStat, + handle: input.handle, }) }) }