@scalemule/discussions 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +34 -0
- package/dist/chunk-BMUVJJXD.js +83 -0
- package/dist/index.cjs +85 -0
- package/dist/index.d.cts +64 -0
- package/dist/index.d.ts +64 -0
- package/dist/index.js +1 -0
- package/dist/react.cjs +359 -0
- package/dist/react.d.cts +60 -0
- package/dist/react.d.ts +60 -0
- package/dist/react.js +271 -0
- package/package.json +68 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 ScaleMule Inc.
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
# @scalemule/discussions
|
|
2
|
+
|
|
3
|
+
Threaded comments for any ScaleMule object: blog posts, news articles, photos, videos, listings, or pages.
|
|
4
|
+
|
|
5
|
+
```bash
|
|
6
|
+
npm install @scalemule/discussions
|
|
7
|
+
```
|
|
8
|
+
|
|
9
|
+
Talks to `scalemule-discussions` at `/v1/discussions`. Do not put an internal service token in the browser. Customer apps use `SCALEMULE_API_KEY` (or a same-origin BFF that holds that key).
|
|
10
|
+
|
|
11
|
+
```tsx
|
|
12
|
+
import { DiscussionsClient } from '@scalemule/discussions';
|
|
13
|
+
import { DiscussionThread, useDiscussion } from '@scalemule/discussions/react';
|
|
14
|
+
|
|
15
|
+
const client = new DiscussionsClient({
|
|
16
|
+
apiKey: process.env.NEXT_PUBLIC_SCALEMULE_API_KEY,
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
function Comments({ articleId }: { articleId: string }) {
|
|
20
|
+
const { comments, addComment } = useDiscussion(client, 'blog_post', articleId);
|
|
21
|
+
return (
|
|
22
|
+
<DiscussionThread
|
|
23
|
+
comments={comments}
|
|
24
|
+
onAddComment={(body, parentId, guest) =>
|
|
25
|
+
addComment(body, { parentCommentId: parentId, ...guest })
|
|
26
|
+
}
|
|
27
|
+
/>
|
|
28
|
+
);
|
|
29
|
+
}
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
Same-origin proxy (Napsite news sites): set `apiBaseUrl` to `''` and `pathPrefix` to `/api/discussions` so the browser never sees the API key.
|
|
33
|
+
|
|
34
|
+
Target types: `blog_post`, `photo`, `video`, `audio`, `listing`, `page`, `social_post`.
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
// src/client.ts
|
|
2
|
+
var DEFAULT_BASE = "https://api.scalemule.com";
|
|
3
|
+
var DEFAULT_PREFIX = "/v1/discussions";
|
|
4
|
+
var DiscussionsClient = class {
|
|
5
|
+
constructor(config = {}) {
|
|
6
|
+
this.apiKey = config.apiKey;
|
|
7
|
+
this.baseUrl = (config.apiBaseUrl ?? DEFAULT_BASE).replace(/\/$/, "");
|
|
8
|
+
this.prefix = (config.pathPrefix ?? DEFAULT_PREFIX).replace(/\/$/, "") || "";
|
|
9
|
+
this.getToken = config.getToken ?? (config.sessionToken ? async () => config.sessionToken ?? null : void 0);
|
|
10
|
+
}
|
|
11
|
+
threadPath(targetType, targetId) {
|
|
12
|
+
return `${this.prefix}/threads/${encodeURIComponent(targetType)}/${encodeURIComponent(targetId)}/comments`;
|
|
13
|
+
}
|
|
14
|
+
async listComments(targetType, targetId, options) {
|
|
15
|
+
const params = new URLSearchParams();
|
|
16
|
+
if (options?.page) params.set("page", String(options.page));
|
|
17
|
+
if (options?.per_page) params.set("per_page", String(options.per_page));
|
|
18
|
+
if (options?.since) params.set("since", options.since);
|
|
19
|
+
const qs = params.toString();
|
|
20
|
+
return this.request(
|
|
21
|
+
"GET",
|
|
22
|
+
`${this.threadPath(targetType, targetId)}${qs ? `?${qs}` : ""}`
|
|
23
|
+
);
|
|
24
|
+
}
|
|
25
|
+
async addComment(targetType, targetId, options) {
|
|
26
|
+
return this.request("POST", this.threadPath(targetType, targetId), {
|
|
27
|
+
content: options.body,
|
|
28
|
+
body: options.body,
|
|
29
|
+
parent_comment_id: options.parent_comment_id,
|
|
30
|
+
author_name: options.author_name,
|
|
31
|
+
author_email: options.author_email
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
async editComment(commentId, body) {
|
|
35
|
+
return this.request(
|
|
36
|
+
"PATCH",
|
|
37
|
+
`${this.prefix}/comments/${encodeURIComponent(commentId)}`,
|
|
38
|
+
{ content: body, body }
|
|
39
|
+
);
|
|
40
|
+
}
|
|
41
|
+
async deleteComment(commentId) {
|
|
42
|
+
return this.request("DELETE", `${this.prefix}/comments/${encodeURIComponent(commentId)}`);
|
|
43
|
+
}
|
|
44
|
+
async request(method, path, body) {
|
|
45
|
+
const headers = { "Content-Type": "application/json" };
|
|
46
|
+
if (this.apiKey) headers["x-api-key"] = this.apiKey;
|
|
47
|
+
if (this.getToken) {
|
|
48
|
+
const token = await this.getToken();
|
|
49
|
+
if (token) headers.Authorization = `Bearer ${token}`;
|
|
50
|
+
}
|
|
51
|
+
try {
|
|
52
|
+
const response = await fetch(`${this.baseUrl}${path}`, {
|
|
53
|
+
method,
|
|
54
|
+
headers,
|
|
55
|
+
body: body === void 0 ? void 0 : JSON.stringify(body),
|
|
56
|
+
credentials: !this.baseUrl || this.baseUrl.startsWith("/") ? "same-origin" : "omit"
|
|
57
|
+
});
|
|
58
|
+
if (response.status === 204) return { data: null, error: null };
|
|
59
|
+
const json = await response.json().catch(() => null);
|
|
60
|
+
if (!response.ok) {
|
|
61
|
+
const error = {
|
|
62
|
+
code: json?.error?.code ?? "unknown",
|
|
63
|
+
message: json?.error?.message ?? json?.error ?? json?.message ?? response.statusText,
|
|
64
|
+
status: response.status
|
|
65
|
+
};
|
|
66
|
+
return { data: null, error };
|
|
67
|
+
}
|
|
68
|
+
const data = json?.data !== void 0 ? json.data : json;
|
|
69
|
+
return { data, error: null };
|
|
70
|
+
} catch (err) {
|
|
71
|
+
return {
|
|
72
|
+
data: null,
|
|
73
|
+
error: {
|
|
74
|
+
code: "network_error",
|
|
75
|
+
message: err instanceof Error ? err.message : "Network error",
|
|
76
|
+
status: 0
|
|
77
|
+
}
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
export { DiscussionsClient };
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
// src/client.ts
|
|
4
|
+
var DEFAULT_BASE = "https://api.scalemule.com";
|
|
5
|
+
var DEFAULT_PREFIX = "/v1/discussions";
|
|
6
|
+
var DiscussionsClient = class {
|
|
7
|
+
constructor(config = {}) {
|
|
8
|
+
this.apiKey = config.apiKey;
|
|
9
|
+
this.baseUrl = (config.apiBaseUrl ?? DEFAULT_BASE).replace(/\/$/, "");
|
|
10
|
+
this.prefix = (config.pathPrefix ?? DEFAULT_PREFIX).replace(/\/$/, "") || "";
|
|
11
|
+
this.getToken = config.getToken ?? (config.sessionToken ? async () => config.sessionToken ?? null : void 0);
|
|
12
|
+
}
|
|
13
|
+
threadPath(targetType, targetId) {
|
|
14
|
+
return `${this.prefix}/threads/${encodeURIComponent(targetType)}/${encodeURIComponent(targetId)}/comments`;
|
|
15
|
+
}
|
|
16
|
+
async listComments(targetType, targetId, options) {
|
|
17
|
+
const params = new URLSearchParams();
|
|
18
|
+
if (options?.page) params.set("page", String(options.page));
|
|
19
|
+
if (options?.per_page) params.set("per_page", String(options.per_page));
|
|
20
|
+
if (options?.since) params.set("since", options.since);
|
|
21
|
+
const qs = params.toString();
|
|
22
|
+
return this.request(
|
|
23
|
+
"GET",
|
|
24
|
+
`${this.threadPath(targetType, targetId)}${qs ? `?${qs}` : ""}`
|
|
25
|
+
);
|
|
26
|
+
}
|
|
27
|
+
async addComment(targetType, targetId, options) {
|
|
28
|
+
return this.request("POST", this.threadPath(targetType, targetId), {
|
|
29
|
+
content: options.body,
|
|
30
|
+
body: options.body,
|
|
31
|
+
parent_comment_id: options.parent_comment_id,
|
|
32
|
+
author_name: options.author_name,
|
|
33
|
+
author_email: options.author_email
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
async editComment(commentId, body) {
|
|
37
|
+
return this.request(
|
|
38
|
+
"PATCH",
|
|
39
|
+
`${this.prefix}/comments/${encodeURIComponent(commentId)}`,
|
|
40
|
+
{ content: body, body }
|
|
41
|
+
);
|
|
42
|
+
}
|
|
43
|
+
async deleteComment(commentId) {
|
|
44
|
+
return this.request("DELETE", `${this.prefix}/comments/${encodeURIComponent(commentId)}`);
|
|
45
|
+
}
|
|
46
|
+
async request(method, path, body) {
|
|
47
|
+
const headers = { "Content-Type": "application/json" };
|
|
48
|
+
if (this.apiKey) headers["x-api-key"] = this.apiKey;
|
|
49
|
+
if (this.getToken) {
|
|
50
|
+
const token = await this.getToken();
|
|
51
|
+
if (token) headers.Authorization = `Bearer ${token}`;
|
|
52
|
+
}
|
|
53
|
+
try {
|
|
54
|
+
const response = await fetch(`${this.baseUrl}${path}`, {
|
|
55
|
+
method,
|
|
56
|
+
headers,
|
|
57
|
+
body: body === void 0 ? void 0 : JSON.stringify(body),
|
|
58
|
+
credentials: !this.baseUrl || this.baseUrl.startsWith("/") ? "same-origin" : "omit"
|
|
59
|
+
});
|
|
60
|
+
if (response.status === 204) return { data: null, error: null };
|
|
61
|
+
const json = await response.json().catch(() => null);
|
|
62
|
+
if (!response.ok) {
|
|
63
|
+
const error = {
|
|
64
|
+
code: json?.error?.code ?? "unknown",
|
|
65
|
+
message: json?.error?.message ?? json?.error ?? json?.message ?? response.statusText,
|
|
66
|
+
status: response.status
|
|
67
|
+
};
|
|
68
|
+
return { data: null, error };
|
|
69
|
+
}
|
|
70
|
+
const data = json?.data !== void 0 ? json.data : json;
|
|
71
|
+
return { data, error: null };
|
|
72
|
+
} catch (err) {
|
|
73
|
+
return {
|
|
74
|
+
data: null,
|
|
75
|
+
error: {
|
|
76
|
+
code: "network_error",
|
|
77
|
+
message: err instanceof Error ? err.message : "Network error",
|
|
78
|
+
status: 0
|
|
79
|
+
}
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
exports.DiscussionsClient = DiscussionsClient;
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
type TargetType = 'blog_post' | 'photo' | 'video' | 'audio' | 'listing' | 'page' | 'social_post';
|
|
2
|
+
interface DiscussionsConfig {
|
|
3
|
+
apiKey?: string;
|
|
4
|
+
/** Origin of the API, no trailing slash. Default https://api.scalemule.com */
|
|
5
|
+
apiBaseUrl?: string;
|
|
6
|
+
/** Path before /threads/.... Default /v1/discussions */
|
|
7
|
+
pathPrefix?: string;
|
|
8
|
+
getToken?: () => Promise<string | null>;
|
|
9
|
+
sessionToken?: string;
|
|
10
|
+
userId?: string;
|
|
11
|
+
}
|
|
12
|
+
interface ApiError {
|
|
13
|
+
code: string;
|
|
14
|
+
message: string;
|
|
15
|
+
status?: number;
|
|
16
|
+
}
|
|
17
|
+
interface ApiResponse<T> {
|
|
18
|
+
data: T | null;
|
|
19
|
+
error: ApiError | null;
|
|
20
|
+
}
|
|
21
|
+
type CommentStatus = 'pending' | 'approved' | 'spam' | 'deleted';
|
|
22
|
+
interface DiscussionComment {
|
|
23
|
+
id: string;
|
|
24
|
+
thread_id?: string;
|
|
25
|
+
target_type?: string;
|
|
26
|
+
target_id?: string;
|
|
27
|
+
author_user_id?: string | null;
|
|
28
|
+
author_name?: string | null;
|
|
29
|
+
author_email?: string | null;
|
|
30
|
+
parent_comment_id?: string | null;
|
|
31
|
+
body: string;
|
|
32
|
+
status: CommentStatus | string;
|
|
33
|
+
depth?: number;
|
|
34
|
+
created_at: string;
|
|
35
|
+
updated_at: string;
|
|
36
|
+
replies?: DiscussionComment[];
|
|
37
|
+
}
|
|
38
|
+
interface AddCommentOptions {
|
|
39
|
+
body: string;
|
|
40
|
+
parent_comment_id?: string;
|
|
41
|
+
author_name?: string;
|
|
42
|
+
author_email?: string;
|
|
43
|
+
}
|
|
44
|
+
interface ListCommentsOptions {
|
|
45
|
+
page?: number;
|
|
46
|
+
per_page?: number;
|
|
47
|
+
since?: string;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
declare class DiscussionsClient {
|
|
51
|
+
private readonly apiKey?;
|
|
52
|
+
private readonly baseUrl;
|
|
53
|
+
private readonly prefix;
|
|
54
|
+
private readonly getToken?;
|
|
55
|
+
constructor(config?: DiscussionsConfig);
|
|
56
|
+
threadPath(targetType: TargetType | string, targetId: string): string;
|
|
57
|
+
listComments(targetType: TargetType | string, targetId: string, options?: ListCommentsOptions): Promise<ApiResponse<DiscussionComment[]>>;
|
|
58
|
+
addComment(targetType: TargetType | string, targetId: string, options: AddCommentOptions): Promise<ApiResponse<DiscussionComment>>;
|
|
59
|
+
editComment(commentId: string, body: string): Promise<ApiResponse<DiscussionComment>>;
|
|
60
|
+
deleteComment(commentId: string): Promise<ApiResponse<void>>;
|
|
61
|
+
private request;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export { type AddCommentOptions, type ApiError, type ApiResponse, type CommentStatus, type DiscussionComment, DiscussionsClient, type DiscussionsConfig, type ListCommentsOptions, type TargetType };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
type TargetType = 'blog_post' | 'photo' | 'video' | 'audio' | 'listing' | 'page' | 'social_post';
|
|
2
|
+
interface DiscussionsConfig {
|
|
3
|
+
apiKey?: string;
|
|
4
|
+
/** Origin of the API, no trailing slash. Default https://api.scalemule.com */
|
|
5
|
+
apiBaseUrl?: string;
|
|
6
|
+
/** Path before /threads/.... Default /v1/discussions */
|
|
7
|
+
pathPrefix?: string;
|
|
8
|
+
getToken?: () => Promise<string | null>;
|
|
9
|
+
sessionToken?: string;
|
|
10
|
+
userId?: string;
|
|
11
|
+
}
|
|
12
|
+
interface ApiError {
|
|
13
|
+
code: string;
|
|
14
|
+
message: string;
|
|
15
|
+
status?: number;
|
|
16
|
+
}
|
|
17
|
+
interface ApiResponse<T> {
|
|
18
|
+
data: T | null;
|
|
19
|
+
error: ApiError | null;
|
|
20
|
+
}
|
|
21
|
+
type CommentStatus = 'pending' | 'approved' | 'spam' | 'deleted';
|
|
22
|
+
interface DiscussionComment {
|
|
23
|
+
id: string;
|
|
24
|
+
thread_id?: string;
|
|
25
|
+
target_type?: string;
|
|
26
|
+
target_id?: string;
|
|
27
|
+
author_user_id?: string | null;
|
|
28
|
+
author_name?: string | null;
|
|
29
|
+
author_email?: string | null;
|
|
30
|
+
parent_comment_id?: string | null;
|
|
31
|
+
body: string;
|
|
32
|
+
status: CommentStatus | string;
|
|
33
|
+
depth?: number;
|
|
34
|
+
created_at: string;
|
|
35
|
+
updated_at: string;
|
|
36
|
+
replies?: DiscussionComment[];
|
|
37
|
+
}
|
|
38
|
+
interface AddCommentOptions {
|
|
39
|
+
body: string;
|
|
40
|
+
parent_comment_id?: string;
|
|
41
|
+
author_name?: string;
|
|
42
|
+
author_email?: string;
|
|
43
|
+
}
|
|
44
|
+
interface ListCommentsOptions {
|
|
45
|
+
page?: number;
|
|
46
|
+
per_page?: number;
|
|
47
|
+
since?: string;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
declare class DiscussionsClient {
|
|
51
|
+
private readonly apiKey?;
|
|
52
|
+
private readonly baseUrl;
|
|
53
|
+
private readonly prefix;
|
|
54
|
+
private readonly getToken?;
|
|
55
|
+
constructor(config?: DiscussionsConfig);
|
|
56
|
+
threadPath(targetType: TargetType | string, targetId: string): string;
|
|
57
|
+
listComments(targetType: TargetType | string, targetId: string, options?: ListCommentsOptions): Promise<ApiResponse<DiscussionComment[]>>;
|
|
58
|
+
addComment(targetType: TargetType | string, targetId: string, options: AddCommentOptions): Promise<ApiResponse<DiscussionComment>>;
|
|
59
|
+
editComment(commentId: string, body: string): Promise<ApiResponse<DiscussionComment>>;
|
|
60
|
+
deleteComment(commentId: string): Promise<ApiResponse<void>>;
|
|
61
|
+
private request;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export { type AddCommentOptions, type ApiError, type ApiResponse, type CommentStatus, type DiscussionComment, DiscussionsClient, type DiscussionsConfig, type ListCommentsOptions, type TargetType };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { DiscussionsClient } from './chunk-BMUVJJXD.js';
|
package/dist/react.cjs
ADDED
|
@@ -0,0 +1,359 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var react = require('react');
|
|
4
|
+
var jsxRuntime = require('react/jsx-runtime');
|
|
5
|
+
|
|
6
|
+
// src/react.tsx
|
|
7
|
+
|
|
8
|
+
// src/client.ts
|
|
9
|
+
var DEFAULT_BASE = "https://api.scalemule.com";
|
|
10
|
+
var DEFAULT_PREFIX = "/v1/discussions";
|
|
11
|
+
var DiscussionsClient = class {
|
|
12
|
+
constructor(config = {}) {
|
|
13
|
+
this.apiKey = config.apiKey;
|
|
14
|
+
this.baseUrl = (config.apiBaseUrl ?? DEFAULT_BASE).replace(/\/$/, "");
|
|
15
|
+
this.prefix = (config.pathPrefix ?? DEFAULT_PREFIX).replace(/\/$/, "") || "";
|
|
16
|
+
this.getToken = config.getToken ?? (config.sessionToken ? async () => config.sessionToken ?? null : void 0);
|
|
17
|
+
}
|
|
18
|
+
threadPath(targetType, targetId) {
|
|
19
|
+
return `${this.prefix}/threads/${encodeURIComponent(targetType)}/${encodeURIComponent(targetId)}/comments`;
|
|
20
|
+
}
|
|
21
|
+
async listComments(targetType, targetId, options) {
|
|
22
|
+
const params = new URLSearchParams();
|
|
23
|
+
if (options?.page) params.set("page", String(options.page));
|
|
24
|
+
if (options?.per_page) params.set("per_page", String(options.per_page));
|
|
25
|
+
if (options?.since) params.set("since", options.since);
|
|
26
|
+
const qs = params.toString();
|
|
27
|
+
return this.request(
|
|
28
|
+
"GET",
|
|
29
|
+
`${this.threadPath(targetType, targetId)}${qs ? `?${qs}` : ""}`
|
|
30
|
+
);
|
|
31
|
+
}
|
|
32
|
+
async addComment(targetType, targetId, options) {
|
|
33
|
+
return this.request("POST", this.threadPath(targetType, targetId), {
|
|
34
|
+
content: options.body,
|
|
35
|
+
body: options.body,
|
|
36
|
+
parent_comment_id: options.parent_comment_id,
|
|
37
|
+
author_name: options.author_name,
|
|
38
|
+
author_email: options.author_email
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
async editComment(commentId, body) {
|
|
42
|
+
return this.request(
|
|
43
|
+
"PATCH",
|
|
44
|
+
`${this.prefix}/comments/${encodeURIComponent(commentId)}`,
|
|
45
|
+
{ content: body, body }
|
|
46
|
+
);
|
|
47
|
+
}
|
|
48
|
+
async deleteComment(commentId) {
|
|
49
|
+
return this.request("DELETE", `${this.prefix}/comments/${encodeURIComponent(commentId)}`);
|
|
50
|
+
}
|
|
51
|
+
async request(method, path, body) {
|
|
52
|
+
const headers = { "Content-Type": "application/json" };
|
|
53
|
+
if (this.apiKey) headers["x-api-key"] = this.apiKey;
|
|
54
|
+
if (this.getToken) {
|
|
55
|
+
const token = await this.getToken();
|
|
56
|
+
if (token) headers.Authorization = `Bearer ${token}`;
|
|
57
|
+
}
|
|
58
|
+
try {
|
|
59
|
+
const response = await fetch(`${this.baseUrl}${path}`, {
|
|
60
|
+
method,
|
|
61
|
+
headers,
|
|
62
|
+
body: body === void 0 ? void 0 : JSON.stringify(body),
|
|
63
|
+
credentials: !this.baseUrl || this.baseUrl.startsWith("/") ? "same-origin" : "omit"
|
|
64
|
+
});
|
|
65
|
+
if (response.status === 204) return { data: null, error: null };
|
|
66
|
+
const json = await response.json().catch(() => null);
|
|
67
|
+
if (!response.ok) {
|
|
68
|
+
const error = {
|
|
69
|
+
code: json?.error?.code ?? "unknown",
|
|
70
|
+
message: json?.error?.message ?? json?.error ?? json?.message ?? response.statusText,
|
|
71
|
+
status: response.status
|
|
72
|
+
};
|
|
73
|
+
return { data: null, error };
|
|
74
|
+
}
|
|
75
|
+
const data = json?.data !== void 0 ? json.data : json;
|
|
76
|
+
return { data, error: null };
|
|
77
|
+
} catch (err) {
|
|
78
|
+
return {
|
|
79
|
+
data: null,
|
|
80
|
+
error: {
|
|
81
|
+
code: "network_error",
|
|
82
|
+
message: err instanceof Error ? err.message : "Network error",
|
|
83
|
+
status: 0
|
|
84
|
+
}
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
};
|
|
89
|
+
|
|
90
|
+
// src/react-components/theme.ts
|
|
91
|
+
function themeToStyle(theme) {
|
|
92
|
+
return {
|
|
93
|
+
"--sm-d-primary": theme?.primary ?? "#bb171d",
|
|
94
|
+
"--sm-d-primary-text": theme?.primaryText ?? "#ffffff",
|
|
95
|
+
"--sm-d-surface": theme?.surface ?? "#ffffff",
|
|
96
|
+
"--sm-d-border": theme?.borderColor ?? "#d7dcd7",
|
|
97
|
+
"--sm-d-text": theme?.textColor ?? "#171917",
|
|
98
|
+
"--sm-d-muted": theme?.mutedText ?? "#5c635c",
|
|
99
|
+
"--sm-d-heading": theme?.headingColor ?? "#171917",
|
|
100
|
+
"--sm-d-radius": theme?.borderRadius ?? "0",
|
|
101
|
+
"--sm-d-font": theme?.fontFamily ?? "Arial, sans-serif",
|
|
102
|
+
"--sm-d-heading-font": theme?.headingFontFamily ?? "Georgia, serif"
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
var MAX_INDENT = 4;
|
|
106
|
+
function CommentItem({ comment, depth = 0, onReply, theme }) {
|
|
107
|
+
const indent = Math.min(depth, MAX_INDENT);
|
|
108
|
+
const replies = comment.replies ?? [];
|
|
109
|
+
return /* @__PURE__ */ jsxRuntime.jsxs(
|
|
110
|
+
"div",
|
|
111
|
+
{
|
|
112
|
+
style: {
|
|
113
|
+
...themeToStyle(theme),
|
|
114
|
+
marginLeft: indent * 24,
|
|
115
|
+
paddingLeft: depth > 0 ? 16 : 0,
|
|
116
|
+
borderLeft: depth > 0 ? "2px solid var(--sm-d-border, #d7dcd7)" : "none",
|
|
117
|
+
marginTop: 16,
|
|
118
|
+
fontFamily: "var(--sm-d-font, Arial, sans-serif)"
|
|
119
|
+
},
|
|
120
|
+
children: [
|
|
121
|
+
/* @__PURE__ */ jsxRuntime.jsxs("div", { style: { display: "flex", alignItems: "center", gap: 8, marginBottom: 6 }, children: [
|
|
122
|
+
/* @__PURE__ */ jsxRuntime.jsx("span", { style: { fontWeight: 700, fontSize: 14, color: "var(--sm-d-text)" }, children: comment.author_name ?? "A reader" }),
|
|
123
|
+
/* @__PURE__ */ jsxRuntime.jsx("time", { dateTime: comment.created_at, style: { fontSize: 12, color: "var(--sm-d-muted)" }, children: new Date(comment.created_at).toLocaleDateString(void 0, { month: "short", day: "numeric", year: "numeric" }) }),
|
|
124
|
+
comment.status === "pending" && /* @__PURE__ */ jsxRuntime.jsx("span", { style: { fontSize: 11, padding: "1px 6px", background: "#fef3c7", color: "#92400e" }, children: "Pending review" })
|
|
125
|
+
] }),
|
|
126
|
+
/* @__PURE__ */ jsxRuntime.jsx("div", { style: { fontSize: 16, lineHeight: 1.6, color: "var(--sm-d-text)", whiteSpace: "pre-wrap", wordBreak: "break-word" }, children: comment.body }),
|
|
127
|
+
onReply && comment.status !== "pending" && /* @__PURE__ */ jsxRuntime.jsx(
|
|
128
|
+
"button",
|
|
129
|
+
{
|
|
130
|
+
type: "button",
|
|
131
|
+
onClick: () => onReply(comment.id),
|
|
132
|
+
style: { border: "none", background: "transparent", color: "var(--sm-d-muted)", fontSize: 13, fontWeight: 600, cursor: "pointer", padding: "8px 0", fontFamily: "inherit" },
|
|
133
|
+
children: "Reply"
|
|
134
|
+
}
|
|
135
|
+
),
|
|
136
|
+
replies.map((child) => /* @__PURE__ */ jsxRuntime.jsx(CommentItem, { comment: child, depth: depth + 1, onReply, theme }, child.id))
|
|
137
|
+
]
|
|
138
|
+
}
|
|
139
|
+
);
|
|
140
|
+
}
|
|
141
|
+
function CommentInput({
|
|
142
|
+
onSubmit,
|
|
143
|
+
showGuestFields = false,
|
|
144
|
+
replyingTo,
|
|
145
|
+
onCancelReply,
|
|
146
|
+
theme
|
|
147
|
+
}) {
|
|
148
|
+
const [content, setContent] = react.useState("");
|
|
149
|
+
const [guestName, setGuestName] = react.useState("");
|
|
150
|
+
const [guestEmail, setGuestEmail] = react.useState("");
|
|
151
|
+
const [isSubmitting, setIsSubmitting] = react.useState(false);
|
|
152
|
+
const handleSubmit = react.useCallback(async () => {
|
|
153
|
+
const trimmed = content.trim();
|
|
154
|
+
if (!trimmed || isSubmitting) return;
|
|
155
|
+
setIsSubmitting(true);
|
|
156
|
+
try {
|
|
157
|
+
await onSubmit(
|
|
158
|
+
trimmed,
|
|
159
|
+
showGuestFields ? guestName.trim() || void 0 : void 0,
|
|
160
|
+
showGuestFields ? guestEmail.trim() || void 0 : void 0
|
|
161
|
+
);
|
|
162
|
+
setContent("");
|
|
163
|
+
} finally {
|
|
164
|
+
setIsSubmitting(false);
|
|
165
|
+
}
|
|
166
|
+
}, [content, guestName, guestEmail, showGuestFields, isSubmitting, onSubmit]);
|
|
167
|
+
const inputStyle = {
|
|
168
|
+
width: "100%",
|
|
169
|
+
fontSize: 16,
|
|
170
|
+
border: "1px solid var(--sm-d-border, #d7dcd7)",
|
|
171
|
+
borderRadius: "var(--sm-d-radius, 0)",
|
|
172
|
+
padding: "10px 12px",
|
|
173
|
+
fontFamily: "var(--sm-d-font, Arial, sans-serif)",
|
|
174
|
+
color: "var(--sm-d-text, #171917)",
|
|
175
|
+
background: "var(--sm-d-surface, #ffffff)",
|
|
176
|
+
boxSizing: "border-box"
|
|
177
|
+
};
|
|
178
|
+
return /* @__PURE__ */ jsxRuntime.jsxs("div", { style: { ...themeToStyle(theme), fontFamily: "var(--sm-d-font, Arial, sans-serif)" }, children: [
|
|
179
|
+
replyingTo && /* @__PURE__ */ jsxRuntime.jsxs("div", { style: { display: "flex", gap: 8, fontSize: 13, color: "var(--sm-d-muted)", marginBottom: 8 }, children: [
|
|
180
|
+
/* @__PURE__ */ jsxRuntime.jsx("span", { children: "Replying" }),
|
|
181
|
+
onCancelReply && /* @__PURE__ */ jsxRuntime.jsx("button", { type: "button", onClick: onCancelReply, style: { border: "none", background: "transparent", color: "var(--sm-d-primary)", cursor: "pointer", padding: 0 }, children: "Cancel" })
|
|
182
|
+
] }),
|
|
183
|
+
showGuestFields && /* @__PURE__ */ jsxRuntime.jsxs("div", { style: { display: "flex", gap: 8, marginBottom: 8 }, children: [
|
|
184
|
+
/* @__PURE__ */ jsxRuntime.jsx("input", { type: "text", placeholder: "Name", value: guestName, onChange: (e) => setGuestName(e.target.value), style: { ...inputStyle, flex: 1 }, "aria-label": "Your name" }),
|
|
185
|
+
/* @__PURE__ */ jsxRuntime.jsx("input", { type: "email", placeholder: "Email (optional)", value: guestEmail, onChange: (e) => setGuestEmail(e.target.value), style: { ...inputStyle, flex: 1 }, "aria-label": "Your email" })
|
|
186
|
+
] }),
|
|
187
|
+
/* @__PURE__ */ jsxRuntime.jsxs("div", { style: { display: "flex", gap: 8, alignItems: "flex-end" }, children: [
|
|
188
|
+
/* @__PURE__ */ jsxRuntime.jsx(
|
|
189
|
+
"textarea",
|
|
190
|
+
{
|
|
191
|
+
value: content,
|
|
192
|
+
onChange: (e) => setContent(e.target.value),
|
|
193
|
+
placeholder: "Write a comment...",
|
|
194
|
+
rows: 3,
|
|
195
|
+
style: { ...inputStyle, flex: 1, resize: "vertical", minHeight: 80 },
|
|
196
|
+
"aria-label": "Comment"
|
|
197
|
+
}
|
|
198
|
+
),
|
|
199
|
+
/* @__PURE__ */ jsxRuntime.jsx(
|
|
200
|
+
"button",
|
|
201
|
+
{
|
|
202
|
+
type: "button",
|
|
203
|
+
onClick: () => void handleSubmit(),
|
|
204
|
+
disabled: !content.trim() || isSubmitting || showGuestFields && !guestName.trim(),
|
|
205
|
+
style: {
|
|
206
|
+
border: "1px solid var(--sm-d-primary, #bb171d)",
|
|
207
|
+
padding: "10px 20px",
|
|
208
|
+
minHeight: 44,
|
|
209
|
+
background: "var(--sm-d-primary, #bb171d)",
|
|
210
|
+
color: "var(--sm-d-primary-text, #ffffff)",
|
|
211
|
+
fontSize: 14,
|
|
212
|
+
fontWeight: 700,
|
|
213
|
+
cursor: "pointer",
|
|
214
|
+
opacity: !content.trim() || isSubmitting ? 0.5 : 1,
|
|
215
|
+
fontFamily: "inherit"
|
|
216
|
+
},
|
|
217
|
+
children: isSubmitting ? "Posting..." : "Post"
|
|
218
|
+
}
|
|
219
|
+
)
|
|
220
|
+
] })
|
|
221
|
+
] });
|
|
222
|
+
}
|
|
223
|
+
function buildTree(comments) {
|
|
224
|
+
const byId = /* @__PURE__ */ new Map();
|
|
225
|
+
const roots = [];
|
|
226
|
+
for (const comment of comments) {
|
|
227
|
+
byId.set(comment.id, { ...comment, replies: [] });
|
|
228
|
+
}
|
|
229
|
+
for (const comment of comments) {
|
|
230
|
+
const node = byId.get(comment.id);
|
|
231
|
+
if (comment.parent_comment_id && byId.has(comment.parent_comment_id)) {
|
|
232
|
+
byId.get(comment.parent_comment_id).replies.push(node);
|
|
233
|
+
} else {
|
|
234
|
+
roots.push(node);
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
return roots;
|
|
238
|
+
}
|
|
239
|
+
function DiscussionThread({
|
|
240
|
+
comments,
|
|
241
|
+
onAddComment,
|
|
242
|
+
currentUserId,
|
|
243
|
+
allowGuestComments = true,
|
|
244
|
+
theme,
|
|
245
|
+
title = "Comments",
|
|
246
|
+
error
|
|
247
|
+
}) {
|
|
248
|
+
const [replyingTo, setReplyingTo] = react.useState();
|
|
249
|
+
const tree = buildTree(comments);
|
|
250
|
+
const isGuest = !currentUserId;
|
|
251
|
+
const handleAdd = react.useCallback(
|
|
252
|
+
async (content, guestName, guestEmail) => {
|
|
253
|
+
if (!onAddComment) return;
|
|
254
|
+
await onAddComment(content, replyingTo, { authorName: guestName, authorEmail: guestEmail });
|
|
255
|
+
setReplyingTo(void 0);
|
|
256
|
+
},
|
|
257
|
+
[onAddComment, replyingTo]
|
|
258
|
+
);
|
|
259
|
+
return /* @__PURE__ */ jsxRuntime.jsxs(
|
|
260
|
+
"section",
|
|
261
|
+
{
|
|
262
|
+
"aria-label": title,
|
|
263
|
+
style: { ...themeToStyle(theme), fontFamily: "var(--sm-d-font, Arial, sans-serif)", marginTop: 36 },
|
|
264
|
+
children: [
|
|
265
|
+
/* @__PURE__ */ jsxRuntime.jsxs(
|
|
266
|
+
"h2",
|
|
267
|
+
{
|
|
268
|
+
style: {
|
|
269
|
+
margin: "0 0 16px",
|
|
270
|
+
fontSize: 28,
|
|
271
|
+
fontWeight: 700,
|
|
272
|
+
fontFamily: "var(--sm-d-heading-font, Georgia, serif)",
|
|
273
|
+
color: "var(--sm-d-heading)"
|
|
274
|
+
},
|
|
275
|
+
children: [
|
|
276
|
+
title,
|
|
277
|
+
" (",
|
|
278
|
+
comments.length,
|
|
279
|
+
")"
|
|
280
|
+
]
|
|
281
|
+
}
|
|
282
|
+
),
|
|
283
|
+
error && /* @__PURE__ */ jsxRuntime.jsx("p", { style: { color: "var(--sm-d-primary)", fontSize: 14 }, children: error }),
|
|
284
|
+
tree.length === 0 ? /* @__PURE__ */ jsxRuntime.jsx("p", { style: { color: "var(--sm-d-muted)", fontSize: 15, padding: "8px 0 16px" }, children: "No comments yet. Be the first to share a thought on this story." }) : tree.map((comment) => /* @__PURE__ */ jsxRuntime.jsx(
|
|
285
|
+
CommentItem,
|
|
286
|
+
{
|
|
287
|
+
comment,
|
|
288
|
+
depth: 0,
|
|
289
|
+
onReply: onAddComment ? setReplyingTo : void 0,
|
|
290
|
+
theme
|
|
291
|
+
},
|
|
292
|
+
comment.id
|
|
293
|
+
)),
|
|
294
|
+
onAddComment && /* @__PURE__ */ jsxRuntime.jsx("div", { style: { marginTop: 24 }, children: /* @__PURE__ */ jsxRuntime.jsx(
|
|
295
|
+
CommentInput,
|
|
296
|
+
{
|
|
297
|
+
onSubmit: handleAdd,
|
|
298
|
+
showGuestFields: isGuest && allowGuestComments,
|
|
299
|
+
replyingTo,
|
|
300
|
+
onCancelReply: replyingTo ? () => setReplyingTo(void 0) : void 0,
|
|
301
|
+
theme
|
|
302
|
+
}
|
|
303
|
+
) })
|
|
304
|
+
]
|
|
305
|
+
}
|
|
306
|
+
);
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
// src/react.tsx
|
|
310
|
+
function useDiscussion(clientOrConfig, targetType, targetId) {
|
|
311
|
+
const client = react.useMemo(
|
|
312
|
+
() => clientOrConfig instanceof DiscussionsClient ? clientOrConfig : new DiscussionsClient(clientOrConfig),
|
|
313
|
+
[clientOrConfig]
|
|
314
|
+
);
|
|
315
|
+
const [comments, setComments] = react.useState([]);
|
|
316
|
+
const [isLoading, setIsLoading] = react.useState(false);
|
|
317
|
+
const [error, setError] = react.useState(null);
|
|
318
|
+
react.useEffect(() => {
|
|
319
|
+
if (!targetId) return;
|
|
320
|
+
let cancelled = false;
|
|
321
|
+
setIsLoading(true);
|
|
322
|
+
setError(null);
|
|
323
|
+
void client.listComments(targetType, targetId).then((result) => {
|
|
324
|
+
if (cancelled) return;
|
|
325
|
+
if (result.data) setComments(result.data);
|
|
326
|
+
if (result.error) setError(result.error.message);
|
|
327
|
+
setIsLoading(false);
|
|
328
|
+
});
|
|
329
|
+
return () => {
|
|
330
|
+
cancelled = true;
|
|
331
|
+
};
|
|
332
|
+
}, [client, targetType, targetId]);
|
|
333
|
+
const addComment = react.useCallback(
|
|
334
|
+
async (body, options) => {
|
|
335
|
+
if (!targetId) return;
|
|
336
|
+
const result = await client.addComment(targetType, targetId, {
|
|
337
|
+
body,
|
|
338
|
+
parent_comment_id: options?.parentCommentId,
|
|
339
|
+
author_name: options?.authorName,
|
|
340
|
+
author_email: options?.authorEmail
|
|
341
|
+
});
|
|
342
|
+
if (result.error) {
|
|
343
|
+
setError(result.error.message);
|
|
344
|
+
return result;
|
|
345
|
+
}
|
|
346
|
+
if (result.data) {
|
|
347
|
+
setComments((prev) => prev.some((c) => c.id === result.data.id) ? prev : [...prev, result.data]);
|
|
348
|
+
}
|
|
349
|
+
return result;
|
|
350
|
+
},
|
|
351
|
+
[client, targetType, targetId]
|
|
352
|
+
);
|
|
353
|
+
return { comments, isLoading, error, addComment, client };
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
exports.CommentInput = CommentInput;
|
|
357
|
+
exports.CommentItem = CommentItem;
|
|
358
|
+
exports.DiscussionThread = DiscussionThread;
|
|
359
|
+
exports.useDiscussion = useDiscussion;
|
package/dist/react.d.cts
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { DiscussionComment, DiscussionsClient, DiscussionsConfig, TargetType, ApiResponse } from './index.cjs';
|
|
2
|
+
import React from 'react';
|
|
3
|
+
|
|
4
|
+
interface DiscussionTheme {
|
|
5
|
+
primary?: string;
|
|
6
|
+
primaryText?: string;
|
|
7
|
+
surface?: string;
|
|
8
|
+
borderColor?: string;
|
|
9
|
+
textColor?: string;
|
|
10
|
+
mutedText?: string;
|
|
11
|
+
headingColor?: string;
|
|
12
|
+
fontFamily?: string;
|
|
13
|
+
headingFontFamily?: string;
|
|
14
|
+
borderRadius?: string;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
interface DiscussionThreadProps {
|
|
18
|
+
comments: DiscussionComment[];
|
|
19
|
+
onAddComment?: (content: string, parentId?: string, guest?: {
|
|
20
|
+
authorName?: string;
|
|
21
|
+
authorEmail?: string;
|
|
22
|
+
}) => void | Promise<void>;
|
|
23
|
+
currentUserId?: string;
|
|
24
|
+
allowGuestComments?: boolean;
|
|
25
|
+
theme?: DiscussionTheme;
|
|
26
|
+
title?: string;
|
|
27
|
+
error?: string | null;
|
|
28
|
+
}
|
|
29
|
+
declare function DiscussionThread({ comments, onAddComment, currentUserId, allowGuestComments, theme, title, error, }: DiscussionThreadProps): React.JSX.Element;
|
|
30
|
+
|
|
31
|
+
interface CommentItemProps {
|
|
32
|
+
comment: DiscussionComment;
|
|
33
|
+
depth?: number;
|
|
34
|
+
onReply?: (parentId: string) => void;
|
|
35
|
+
theme?: DiscussionTheme;
|
|
36
|
+
}
|
|
37
|
+
declare function CommentItem({ comment, depth, onReply, theme }: CommentItemProps): React.JSX.Element;
|
|
38
|
+
|
|
39
|
+
interface CommentInputProps {
|
|
40
|
+
onSubmit: (content: string, guestName?: string, guestEmail?: string) => void | Promise<void>;
|
|
41
|
+
showGuestFields?: boolean;
|
|
42
|
+
replyingTo?: string;
|
|
43
|
+
onCancelReply?: () => void;
|
|
44
|
+
theme?: DiscussionTheme;
|
|
45
|
+
}
|
|
46
|
+
declare function CommentInput({ onSubmit, showGuestFields, replyingTo, onCancelReply, theme, }: CommentInputProps): React.JSX.Element;
|
|
47
|
+
|
|
48
|
+
declare function useDiscussion(clientOrConfig: DiscussionsClient | DiscussionsConfig, targetType: TargetType | string, targetId?: string): {
|
|
49
|
+
comments: DiscussionComment[];
|
|
50
|
+
isLoading: boolean;
|
|
51
|
+
error: string | null;
|
|
52
|
+
addComment: (body: string, options?: {
|
|
53
|
+
parentCommentId?: string;
|
|
54
|
+
authorName?: string;
|
|
55
|
+
authorEmail?: string;
|
|
56
|
+
}) => Promise<ApiResponse<DiscussionComment> | undefined>;
|
|
57
|
+
client: DiscussionsClient;
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
export { CommentInput, CommentItem, type DiscussionTheme, DiscussionThread, useDiscussion };
|
package/dist/react.d.ts
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { DiscussionComment, DiscussionsClient, DiscussionsConfig, TargetType, ApiResponse } from './index.js';
|
|
2
|
+
import React from 'react';
|
|
3
|
+
|
|
4
|
+
interface DiscussionTheme {
|
|
5
|
+
primary?: string;
|
|
6
|
+
primaryText?: string;
|
|
7
|
+
surface?: string;
|
|
8
|
+
borderColor?: string;
|
|
9
|
+
textColor?: string;
|
|
10
|
+
mutedText?: string;
|
|
11
|
+
headingColor?: string;
|
|
12
|
+
fontFamily?: string;
|
|
13
|
+
headingFontFamily?: string;
|
|
14
|
+
borderRadius?: string;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
interface DiscussionThreadProps {
|
|
18
|
+
comments: DiscussionComment[];
|
|
19
|
+
onAddComment?: (content: string, parentId?: string, guest?: {
|
|
20
|
+
authorName?: string;
|
|
21
|
+
authorEmail?: string;
|
|
22
|
+
}) => void | Promise<void>;
|
|
23
|
+
currentUserId?: string;
|
|
24
|
+
allowGuestComments?: boolean;
|
|
25
|
+
theme?: DiscussionTheme;
|
|
26
|
+
title?: string;
|
|
27
|
+
error?: string | null;
|
|
28
|
+
}
|
|
29
|
+
declare function DiscussionThread({ comments, onAddComment, currentUserId, allowGuestComments, theme, title, error, }: DiscussionThreadProps): React.JSX.Element;
|
|
30
|
+
|
|
31
|
+
interface CommentItemProps {
|
|
32
|
+
comment: DiscussionComment;
|
|
33
|
+
depth?: number;
|
|
34
|
+
onReply?: (parentId: string) => void;
|
|
35
|
+
theme?: DiscussionTheme;
|
|
36
|
+
}
|
|
37
|
+
declare function CommentItem({ comment, depth, onReply, theme }: CommentItemProps): React.JSX.Element;
|
|
38
|
+
|
|
39
|
+
interface CommentInputProps {
|
|
40
|
+
onSubmit: (content: string, guestName?: string, guestEmail?: string) => void | Promise<void>;
|
|
41
|
+
showGuestFields?: boolean;
|
|
42
|
+
replyingTo?: string;
|
|
43
|
+
onCancelReply?: () => void;
|
|
44
|
+
theme?: DiscussionTheme;
|
|
45
|
+
}
|
|
46
|
+
declare function CommentInput({ onSubmit, showGuestFields, replyingTo, onCancelReply, theme, }: CommentInputProps): React.JSX.Element;
|
|
47
|
+
|
|
48
|
+
declare function useDiscussion(clientOrConfig: DiscussionsClient | DiscussionsConfig, targetType: TargetType | string, targetId?: string): {
|
|
49
|
+
comments: DiscussionComment[];
|
|
50
|
+
isLoading: boolean;
|
|
51
|
+
error: string | null;
|
|
52
|
+
addComment: (body: string, options?: {
|
|
53
|
+
parentCommentId?: string;
|
|
54
|
+
authorName?: string;
|
|
55
|
+
authorEmail?: string;
|
|
56
|
+
}) => Promise<ApiResponse<DiscussionComment> | undefined>;
|
|
57
|
+
client: DiscussionsClient;
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
export { CommentInput, CommentItem, type DiscussionTheme, DiscussionThread, useDiscussion };
|
package/dist/react.js
ADDED
|
@@ -0,0 +1,271 @@
|
|
|
1
|
+
import { DiscussionsClient } from './chunk-BMUVJJXD.js';
|
|
2
|
+
import { useState, useCallback, useMemo, useEffect } from 'react';
|
|
3
|
+
import { jsxs, jsx } from 'react/jsx-runtime';
|
|
4
|
+
|
|
5
|
+
// src/react-components/theme.ts
|
|
6
|
+
function themeToStyle(theme) {
|
|
7
|
+
return {
|
|
8
|
+
"--sm-d-primary": theme?.primary ?? "#bb171d",
|
|
9
|
+
"--sm-d-primary-text": theme?.primaryText ?? "#ffffff",
|
|
10
|
+
"--sm-d-surface": theme?.surface ?? "#ffffff",
|
|
11
|
+
"--sm-d-border": theme?.borderColor ?? "#d7dcd7",
|
|
12
|
+
"--sm-d-text": theme?.textColor ?? "#171917",
|
|
13
|
+
"--sm-d-muted": theme?.mutedText ?? "#5c635c",
|
|
14
|
+
"--sm-d-heading": theme?.headingColor ?? "#171917",
|
|
15
|
+
"--sm-d-radius": theme?.borderRadius ?? "0",
|
|
16
|
+
"--sm-d-font": theme?.fontFamily ?? "Arial, sans-serif",
|
|
17
|
+
"--sm-d-heading-font": theme?.headingFontFamily ?? "Georgia, serif"
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
var MAX_INDENT = 4;
|
|
21
|
+
function CommentItem({ comment, depth = 0, onReply, theme }) {
|
|
22
|
+
const indent = Math.min(depth, MAX_INDENT);
|
|
23
|
+
const replies = comment.replies ?? [];
|
|
24
|
+
return /* @__PURE__ */ jsxs(
|
|
25
|
+
"div",
|
|
26
|
+
{
|
|
27
|
+
style: {
|
|
28
|
+
...themeToStyle(theme),
|
|
29
|
+
marginLeft: indent * 24,
|
|
30
|
+
paddingLeft: depth > 0 ? 16 : 0,
|
|
31
|
+
borderLeft: depth > 0 ? "2px solid var(--sm-d-border, #d7dcd7)" : "none",
|
|
32
|
+
marginTop: 16,
|
|
33
|
+
fontFamily: "var(--sm-d-font, Arial, sans-serif)"
|
|
34
|
+
},
|
|
35
|
+
children: [
|
|
36
|
+
/* @__PURE__ */ jsxs("div", { style: { display: "flex", alignItems: "center", gap: 8, marginBottom: 6 }, children: [
|
|
37
|
+
/* @__PURE__ */ jsx("span", { style: { fontWeight: 700, fontSize: 14, color: "var(--sm-d-text)" }, children: comment.author_name ?? "A reader" }),
|
|
38
|
+
/* @__PURE__ */ jsx("time", { dateTime: comment.created_at, style: { fontSize: 12, color: "var(--sm-d-muted)" }, children: new Date(comment.created_at).toLocaleDateString(void 0, { month: "short", day: "numeric", year: "numeric" }) }),
|
|
39
|
+
comment.status === "pending" && /* @__PURE__ */ jsx("span", { style: { fontSize: 11, padding: "1px 6px", background: "#fef3c7", color: "#92400e" }, children: "Pending review" })
|
|
40
|
+
] }),
|
|
41
|
+
/* @__PURE__ */ jsx("div", { style: { fontSize: 16, lineHeight: 1.6, color: "var(--sm-d-text)", whiteSpace: "pre-wrap", wordBreak: "break-word" }, children: comment.body }),
|
|
42
|
+
onReply && comment.status !== "pending" && /* @__PURE__ */ jsx(
|
|
43
|
+
"button",
|
|
44
|
+
{
|
|
45
|
+
type: "button",
|
|
46
|
+
onClick: () => onReply(comment.id),
|
|
47
|
+
style: { border: "none", background: "transparent", color: "var(--sm-d-muted)", fontSize: 13, fontWeight: 600, cursor: "pointer", padding: "8px 0", fontFamily: "inherit" },
|
|
48
|
+
children: "Reply"
|
|
49
|
+
}
|
|
50
|
+
),
|
|
51
|
+
replies.map((child) => /* @__PURE__ */ jsx(CommentItem, { comment: child, depth: depth + 1, onReply, theme }, child.id))
|
|
52
|
+
]
|
|
53
|
+
}
|
|
54
|
+
);
|
|
55
|
+
}
|
|
56
|
+
function CommentInput({
|
|
57
|
+
onSubmit,
|
|
58
|
+
showGuestFields = false,
|
|
59
|
+
replyingTo,
|
|
60
|
+
onCancelReply,
|
|
61
|
+
theme
|
|
62
|
+
}) {
|
|
63
|
+
const [content, setContent] = useState("");
|
|
64
|
+
const [guestName, setGuestName] = useState("");
|
|
65
|
+
const [guestEmail, setGuestEmail] = useState("");
|
|
66
|
+
const [isSubmitting, setIsSubmitting] = useState(false);
|
|
67
|
+
const handleSubmit = useCallback(async () => {
|
|
68
|
+
const trimmed = content.trim();
|
|
69
|
+
if (!trimmed || isSubmitting) return;
|
|
70
|
+
setIsSubmitting(true);
|
|
71
|
+
try {
|
|
72
|
+
await onSubmit(
|
|
73
|
+
trimmed,
|
|
74
|
+
showGuestFields ? guestName.trim() || void 0 : void 0,
|
|
75
|
+
showGuestFields ? guestEmail.trim() || void 0 : void 0
|
|
76
|
+
);
|
|
77
|
+
setContent("");
|
|
78
|
+
} finally {
|
|
79
|
+
setIsSubmitting(false);
|
|
80
|
+
}
|
|
81
|
+
}, [content, guestName, guestEmail, showGuestFields, isSubmitting, onSubmit]);
|
|
82
|
+
const inputStyle = {
|
|
83
|
+
width: "100%",
|
|
84
|
+
fontSize: 16,
|
|
85
|
+
border: "1px solid var(--sm-d-border, #d7dcd7)",
|
|
86
|
+
borderRadius: "var(--sm-d-radius, 0)",
|
|
87
|
+
padding: "10px 12px",
|
|
88
|
+
fontFamily: "var(--sm-d-font, Arial, sans-serif)",
|
|
89
|
+
color: "var(--sm-d-text, #171917)",
|
|
90
|
+
background: "var(--sm-d-surface, #ffffff)",
|
|
91
|
+
boxSizing: "border-box"
|
|
92
|
+
};
|
|
93
|
+
return /* @__PURE__ */ jsxs("div", { style: { ...themeToStyle(theme), fontFamily: "var(--sm-d-font, Arial, sans-serif)" }, children: [
|
|
94
|
+
replyingTo && /* @__PURE__ */ jsxs("div", { style: { display: "flex", gap: 8, fontSize: 13, color: "var(--sm-d-muted)", marginBottom: 8 }, children: [
|
|
95
|
+
/* @__PURE__ */ jsx("span", { children: "Replying" }),
|
|
96
|
+
onCancelReply && /* @__PURE__ */ jsx("button", { type: "button", onClick: onCancelReply, style: { border: "none", background: "transparent", color: "var(--sm-d-primary)", cursor: "pointer", padding: 0 }, children: "Cancel" })
|
|
97
|
+
] }),
|
|
98
|
+
showGuestFields && /* @__PURE__ */ jsxs("div", { style: { display: "flex", gap: 8, marginBottom: 8 }, children: [
|
|
99
|
+
/* @__PURE__ */ jsx("input", { type: "text", placeholder: "Name", value: guestName, onChange: (e) => setGuestName(e.target.value), style: { ...inputStyle, flex: 1 }, "aria-label": "Your name" }),
|
|
100
|
+
/* @__PURE__ */ jsx("input", { type: "email", placeholder: "Email (optional)", value: guestEmail, onChange: (e) => setGuestEmail(e.target.value), style: { ...inputStyle, flex: 1 }, "aria-label": "Your email" })
|
|
101
|
+
] }),
|
|
102
|
+
/* @__PURE__ */ jsxs("div", { style: { display: "flex", gap: 8, alignItems: "flex-end" }, children: [
|
|
103
|
+
/* @__PURE__ */ jsx(
|
|
104
|
+
"textarea",
|
|
105
|
+
{
|
|
106
|
+
value: content,
|
|
107
|
+
onChange: (e) => setContent(e.target.value),
|
|
108
|
+
placeholder: "Write a comment...",
|
|
109
|
+
rows: 3,
|
|
110
|
+
style: { ...inputStyle, flex: 1, resize: "vertical", minHeight: 80 },
|
|
111
|
+
"aria-label": "Comment"
|
|
112
|
+
}
|
|
113
|
+
),
|
|
114
|
+
/* @__PURE__ */ jsx(
|
|
115
|
+
"button",
|
|
116
|
+
{
|
|
117
|
+
type: "button",
|
|
118
|
+
onClick: () => void handleSubmit(),
|
|
119
|
+
disabled: !content.trim() || isSubmitting || showGuestFields && !guestName.trim(),
|
|
120
|
+
style: {
|
|
121
|
+
border: "1px solid var(--sm-d-primary, #bb171d)",
|
|
122
|
+
padding: "10px 20px",
|
|
123
|
+
minHeight: 44,
|
|
124
|
+
background: "var(--sm-d-primary, #bb171d)",
|
|
125
|
+
color: "var(--sm-d-primary-text, #ffffff)",
|
|
126
|
+
fontSize: 14,
|
|
127
|
+
fontWeight: 700,
|
|
128
|
+
cursor: "pointer",
|
|
129
|
+
opacity: !content.trim() || isSubmitting ? 0.5 : 1,
|
|
130
|
+
fontFamily: "inherit"
|
|
131
|
+
},
|
|
132
|
+
children: isSubmitting ? "Posting..." : "Post"
|
|
133
|
+
}
|
|
134
|
+
)
|
|
135
|
+
] })
|
|
136
|
+
] });
|
|
137
|
+
}
|
|
138
|
+
function buildTree(comments) {
|
|
139
|
+
const byId = /* @__PURE__ */ new Map();
|
|
140
|
+
const roots = [];
|
|
141
|
+
for (const comment of comments) {
|
|
142
|
+
byId.set(comment.id, { ...comment, replies: [] });
|
|
143
|
+
}
|
|
144
|
+
for (const comment of comments) {
|
|
145
|
+
const node = byId.get(comment.id);
|
|
146
|
+
if (comment.parent_comment_id && byId.has(comment.parent_comment_id)) {
|
|
147
|
+
byId.get(comment.parent_comment_id).replies.push(node);
|
|
148
|
+
} else {
|
|
149
|
+
roots.push(node);
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
return roots;
|
|
153
|
+
}
|
|
154
|
+
function DiscussionThread({
|
|
155
|
+
comments,
|
|
156
|
+
onAddComment,
|
|
157
|
+
currentUserId,
|
|
158
|
+
allowGuestComments = true,
|
|
159
|
+
theme,
|
|
160
|
+
title = "Comments",
|
|
161
|
+
error
|
|
162
|
+
}) {
|
|
163
|
+
const [replyingTo, setReplyingTo] = useState();
|
|
164
|
+
const tree = buildTree(comments);
|
|
165
|
+
const isGuest = !currentUserId;
|
|
166
|
+
const handleAdd = useCallback(
|
|
167
|
+
async (content, guestName, guestEmail) => {
|
|
168
|
+
if (!onAddComment) return;
|
|
169
|
+
await onAddComment(content, replyingTo, { authorName: guestName, authorEmail: guestEmail });
|
|
170
|
+
setReplyingTo(void 0);
|
|
171
|
+
},
|
|
172
|
+
[onAddComment, replyingTo]
|
|
173
|
+
);
|
|
174
|
+
return /* @__PURE__ */ jsxs(
|
|
175
|
+
"section",
|
|
176
|
+
{
|
|
177
|
+
"aria-label": title,
|
|
178
|
+
style: { ...themeToStyle(theme), fontFamily: "var(--sm-d-font, Arial, sans-serif)", marginTop: 36 },
|
|
179
|
+
children: [
|
|
180
|
+
/* @__PURE__ */ jsxs(
|
|
181
|
+
"h2",
|
|
182
|
+
{
|
|
183
|
+
style: {
|
|
184
|
+
margin: "0 0 16px",
|
|
185
|
+
fontSize: 28,
|
|
186
|
+
fontWeight: 700,
|
|
187
|
+
fontFamily: "var(--sm-d-heading-font, Georgia, serif)",
|
|
188
|
+
color: "var(--sm-d-heading)"
|
|
189
|
+
},
|
|
190
|
+
children: [
|
|
191
|
+
title,
|
|
192
|
+
" (",
|
|
193
|
+
comments.length,
|
|
194
|
+
")"
|
|
195
|
+
]
|
|
196
|
+
}
|
|
197
|
+
),
|
|
198
|
+
error && /* @__PURE__ */ jsx("p", { style: { color: "var(--sm-d-primary)", fontSize: 14 }, children: error }),
|
|
199
|
+
tree.length === 0 ? /* @__PURE__ */ jsx("p", { style: { color: "var(--sm-d-muted)", fontSize: 15, padding: "8px 0 16px" }, children: "No comments yet. Be the first to share a thought on this story." }) : tree.map((comment) => /* @__PURE__ */ jsx(
|
|
200
|
+
CommentItem,
|
|
201
|
+
{
|
|
202
|
+
comment,
|
|
203
|
+
depth: 0,
|
|
204
|
+
onReply: onAddComment ? setReplyingTo : void 0,
|
|
205
|
+
theme
|
|
206
|
+
},
|
|
207
|
+
comment.id
|
|
208
|
+
)),
|
|
209
|
+
onAddComment && /* @__PURE__ */ jsx("div", { style: { marginTop: 24 }, children: /* @__PURE__ */ jsx(
|
|
210
|
+
CommentInput,
|
|
211
|
+
{
|
|
212
|
+
onSubmit: handleAdd,
|
|
213
|
+
showGuestFields: isGuest && allowGuestComments,
|
|
214
|
+
replyingTo,
|
|
215
|
+
onCancelReply: replyingTo ? () => setReplyingTo(void 0) : void 0,
|
|
216
|
+
theme
|
|
217
|
+
}
|
|
218
|
+
) })
|
|
219
|
+
]
|
|
220
|
+
}
|
|
221
|
+
);
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
// src/react.tsx
|
|
225
|
+
function useDiscussion(clientOrConfig, targetType, targetId) {
|
|
226
|
+
const client = useMemo(
|
|
227
|
+
() => clientOrConfig instanceof DiscussionsClient ? clientOrConfig : new DiscussionsClient(clientOrConfig),
|
|
228
|
+
[clientOrConfig]
|
|
229
|
+
);
|
|
230
|
+
const [comments, setComments] = useState([]);
|
|
231
|
+
const [isLoading, setIsLoading] = useState(false);
|
|
232
|
+
const [error, setError] = useState(null);
|
|
233
|
+
useEffect(() => {
|
|
234
|
+
if (!targetId) return;
|
|
235
|
+
let cancelled = false;
|
|
236
|
+
setIsLoading(true);
|
|
237
|
+
setError(null);
|
|
238
|
+
void client.listComments(targetType, targetId).then((result) => {
|
|
239
|
+
if (cancelled) return;
|
|
240
|
+
if (result.data) setComments(result.data);
|
|
241
|
+
if (result.error) setError(result.error.message);
|
|
242
|
+
setIsLoading(false);
|
|
243
|
+
});
|
|
244
|
+
return () => {
|
|
245
|
+
cancelled = true;
|
|
246
|
+
};
|
|
247
|
+
}, [client, targetType, targetId]);
|
|
248
|
+
const addComment = useCallback(
|
|
249
|
+
async (body, options) => {
|
|
250
|
+
if (!targetId) return;
|
|
251
|
+
const result = await client.addComment(targetType, targetId, {
|
|
252
|
+
body,
|
|
253
|
+
parent_comment_id: options?.parentCommentId,
|
|
254
|
+
author_name: options?.authorName,
|
|
255
|
+
author_email: options?.authorEmail
|
|
256
|
+
});
|
|
257
|
+
if (result.error) {
|
|
258
|
+
setError(result.error.message);
|
|
259
|
+
return result;
|
|
260
|
+
}
|
|
261
|
+
if (result.data) {
|
|
262
|
+
setComments((prev) => prev.some((c) => c.id === result.data.id) ? prev : [...prev, result.data]);
|
|
263
|
+
}
|
|
264
|
+
return result;
|
|
265
|
+
},
|
|
266
|
+
[client, targetType, targetId]
|
|
267
|
+
);
|
|
268
|
+
return { comments, isLoading, error, addComment, client };
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
export { CommentInput, CommentItem, DiscussionThread, useDiscussion };
|
package/package.json
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@scalemule/discussions",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"description": "ScaleMule Discussions SDK — threaded comments on any content object",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./dist/index.cjs",
|
|
7
|
+
"module": "./dist/index.js",
|
|
8
|
+
"types": "./dist/index.d.ts",
|
|
9
|
+
"exports": {
|
|
10
|
+
".": {
|
|
11
|
+
"types": "./dist/index.d.ts",
|
|
12
|
+
"import": "./dist/index.js",
|
|
13
|
+
"require": "./dist/index.cjs"
|
|
14
|
+
},
|
|
15
|
+
"./react": {
|
|
16
|
+
"types": "./dist/react.d.ts",
|
|
17
|
+
"import": "./dist/react.js",
|
|
18
|
+
"require": "./dist/react.cjs"
|
|
19
|
+
}
|
|
20
|
+
},
|
|
21
|
+
"files": [
|
|
22
|
+
"dist",
|
|
23
|
+
"README.md",
|
|
24
|
+
"LICENSE"
|
|
25
|
+
],
|
|
26
|
+
"scripts": {
|
|
27
|
+
"build": "tsup",
|
|
28
|
+
"dev": "tsup --watch",
|
|
29
|
+
"test": "vitest run",
|
|
30
|
+
"lint": "tsc --noEmit",
|
|
31
|
+
"prepublishOnly": "node -e \"const v=require('./package.json').version; if(!/^0\\.(0|1)\\.\\d+$/.test(v)){console.error('ERR: SDK '+v+' violates patch-only policy (0.0.x or 0.1.x only). See CONTRIBUTING.md SDK versioning section.');process.exit(1)}\" && npm run lint && npm test && npm run build"
|
|
32
|
+
},
|
|
33
|
+
"peerDependencies": {
|
|
34
|
+
"react": ">=18.0.0",
|
|
35
|
+
"react-dom": ">=18.0.0"
|
|
36
|
+
},
|
|
37
|
+
"peerDependenciesMeta": {
|
|
38
|
+
"react": { "optional": true },
|
|
39
|
+
"react-dom": { "optional": true }
|
|
40
|
+
},
|
|
41
|
+
"devDependencies": {
|
|
42
|
+
"@types/react": "^18.3.0",
|
|
43
|
+
"react": "^18.3.0",
|
|
44
|
+
"react-dom": "^18.3.0",
|
|
45
|
+
"tsup": "^8.0.0",
|
|
46
|
+
"typescript": "^5.4.0",
|
|
47
|
+
"vitest": "^1.0.0"
|
|
48
|
+
},
|
|
49
|
+
"keywords": [
|
|
50
|
+
"scalemule",
|
|
51
|
+
"comments",
|
|
52
|
+
"discussions",
|
|
53
|
+
"sdk"
|
|
54
|
+
],
|
|
55
|
+
"license": "MIT",
|
|
56
|
+
"repository": {
|
|
57
|
+
"type": "git",
|
|
58
|
+
"url": "git+https://github.com/scalemule/discussions.git"
|
|
59
|
+
},
|
|
60
|
+
"author": "ScaleMule Inc. <support@scalemule.com>",
|
|
61
|
+
"engines": {
|
|
62
|
+
"node": ">=18.0.0"
|
|
63
|
+
},
|
|
64
|
+
"publishConfig": {
|
|
65
|
+
"access": "public",
|
|
66
|
+
"registry": "https://registry.npmjs.org/"
|
|
67
|
+
}
|
|
68
|
+
}
|