Skip to content

Commit 8b769cc

Browse files
committed
core: support monograph.slug field
Signed-off-by: 01zulfi <85733202+01zulfi@users.noreply.github.com>
1 parent bdd78df commit 8b769cc

File tree

5 files changed

+45
-8
lines changed

5 files changed

+45
-8
lines changed

apps/web/src/components/publish-view/index.tsx

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import { Loading, Refresh } from "../icons";
2323
import { db } from "../../common/db";
2424
import { writeText } from "clipboard-polyfill";
2525
import { showToast } from "../../utils/toast";
26-
import { EV, EVENTS, hosts, MonographAnalytics } from "@notesnook/core";
26+
import { EV, EVENTS, hosts } from "@notesnook/core";
2727
import { useStore } from "../../stores/monograph-store";
2828
import { Note } from "@notesnook/core";
2929
import { strings } from "@notesnook/intl";
@@ -64,6 +64,9 @@ function PublishView(props: PublishViewProps) {
6464
if (!monographAnalytics?.isAllowed || !monograph) return { totalViews: 0 };
6565
return await db.monographs.analytics(monograph?.id);
6666
}, [monograph?.id, monographAnalytics]);
67+
const publishUrl = usePromise(async () => {
68+
return await db.monographs.publishUrl(note.id);
69+
}, [monograph?.id, monograph?.publishUrl]);
6770

6871
useEffect(() => {
6972
const fileDownloadedEvent = EV.subscribe(
@@ -95,23 +98,27 @@ function PublishView(props: PublishViewProps) {
9598
variant="text.body"
9699
as="a"
97100
target="_blank"
98-
href={`${hosts.MONOGRAPH_HOST}/${monograph?.id}`}
101+
href={publishUrl.status === "fulfilled" ? publishUrl.value : "#"}
99102
sx={{
100103
textOverflow: "ellipsis",
101104
whiteSpace: "nowrap",
102105
textDecoration: "none",
103106
overflow: "hidden",
104-
px: 1
107+
px: 1,
108+
opacity: publishUrl.status === "fulfilled" ? 1 : 0.8
105109
}}
106110
>
107-
{`${hosts.MONOGRAPH_HOST}/${monograph?.id}`}
111+
{publishUrl.status === "fulfilled" ? publishUrl.value : ""}
108112
</Link>
109113
<Button
110114
variant="secondary"
111115
className="copyPublishLink"
112116
sx={{ flexShrink: 0, m: 0 }}
117+
disabled={publishUrl.status !== "fulfilled"}
113118
onClick={() => {
114-
writeText(`${hosts.MONOGRAPH_HOST}/${monograph?.id}`);
119+
if (publishUrl.status !== "fulfilled") return;
120+
121+
writeText(publishUrl.value);
115122
}}
116123
>
117124
{strings.copy()}
@@ -425,6 +432,7 @@ type ResolvedMonograph = {
425432
selfDestruct: boolean;
426433
publishedAt?: number;
427434
password?: string;
435+
publishUrl?: string;
428436
};
429437

430438
async function resolveMonograph(
@@ -435,6 +443,7 @@ async function resolveMonograph(
435443
return {
436444
id: monographId,
437445
selfDestruct: !!monograph.selfDestruct,
446+
publishUrl: monograph.publishUrl,
438447
publishedAt: monograph.datePublished,
439448
password: monograph.password
440449
? await db.monographs.decryptPassword(monograph.password)

packages/core/src/api/monographs.ts

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,10 @@ export type MonographAnalytics = {
4141
totalViews: number;
4242
};
4343

44-
export type PublishOptions = { password?: string; selfDestruct?: boolean };
44+
export type PublishOptions = {
45+
password?: string;
46+
selfDestruct?: boolean;
47+
};
4548
export class Monographs {
4649
monographs: string[] = [];
4750
constructor(private readonly db: Database) {}
@@ -130,7 +133,7 @@ export class Monographs {
130133

131134
const method = update ? http.patch.json : http.post.json;
132135
const deviceId = await this.db.kv().read("deviceId");
133-
const { id, datePublished } = await method(
136+
const { id, datePublished, publishUrl } = await method(
134137
`${Constants.API_HOST}/monographs?deviceId=${deviceId}`,
135138
monograph,
136139
token
@@ -142,7 +145,8 @@ export class Monographs {
142145
title: monograph.title,
143146
selfDestruct: monograph.selfDestruct,
144147
datePublished: datePublished,
145-
password: monograph.password
148+
password: monograph.password,
149+
publishUrl: publishUrl
146150
});
147151
return id;
148152
}
@@ -203,4 +207,18 @@ export class Monographs {
203207
return { totalViews: 0 };
204208
}
205209
}
210+
211+
async publishUrl(monographId: string): Promise<string> {
212+
try {
213+
const token = await this.db.tokenManager.getAccessToken();
214+
const { publishUrl } = (await http.get(
215+
`${Constants.API_HOST}/monographs/${monographId}/publish-url`,
216+
token
217+
)) as { publishUrl: string };
218+
return publishUrl;
219+
} catch {
220+
const monograph = await this.get(monographId);
221+
return monograph?.publishUrl || "";
222+
}
223+
}
206224
}

packages/core/src/collections/monographs.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ export class Monographs implements ICollection {
6262
datePublished: merged.datePublished,
6363
selfDestruct: merged.selfDestruct,
6464
password: merged.password,
65+
publishUrl: merged.publishUrl,
6566
type: "monograph"
6667
});
6768
}

packages/core/src/database/migrations.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,14 @@ export class NNMigrationProvider implements MigrationProvider {
404404
.addColumn("password", "text")
405405
.execute();
406406
}
407+
},
408+
"a-2025-11-26": {
409+
async up(db) {
410+
await db.schema
411+
.alterTable("monographs")
412+
.addColumn("publishUrl", "text", COLLATE_NOCASE)
413+
.execute();
414+
}
407415
}
408416
};
409417
}

packages/core/src/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -498,6 +498,7 @@ export interface Monograph extends BaseItem<"monograph"> {
498498
datePublished: number;
499499
selfDestruct: boolean;
500500
password?: Cipher<"base64">;
501+
publishUrl?: string;
501502
}
502503

503504
export type Match = {

0 commit comments

Comments
 (0)