@softr/sdk 0.0.81 → 0.0.82

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.
@@ -1,178 +0,0 @@
1
- ---
2
- name: comments
3
- description: >-
4
- Comment threads on records: defineComments, the comments calls from
5
- @softr/sdk/server to list, write, react to and mention in one record's thread,
6
- and useCommentComposer and useFileUpload from @softr/sdk/client for the
7
- composer. Bodies come back as sanitised bodyHtml.
8
- metadata:
9
- type: sub-skill
10
- library: softr-sdk
11
- library_version: 0.0.81
12
- ---
13
-
14
- **Boundary.** `comments` lives on `@softr/sdk/server` and runs only inside a `createServerFn` handler. Importing it into a component fails the build. `useCommentComposer` lives on `@softr/sdk/client` and runs in components. `defineComments` comes from `@softr/sdk` and can be imported anywhere.
15
-
16
- # Comments on records
17
-
18
- Each record of a commented datasource has one thread. The SDK reads and writes it. The app owns what you see: layout, copy, order, dates.
19
-
20
- ## Setup
21
-
22
- ```ts
23
- import { createServerFn } from '@tanstack/react-start'
24
- import { defineComments, type CommentMention, type CommentAttachmentInput } from '@softr/sdk'
25
- import { comments } from '@softr/sdk/server'
26
- import { useCommentComposer, useFileUpload } from '@softr/sdk/client'
27
- ```
28
-
29
- ## Declare the comments action
30
-
31
- ```ts
32
- export const orderComments = defineComments({
33
- id: 'orderComments',
34
- datasourceId: '8f14e45f-ceea-467a-9d0b-2c1a0e3b5d77',
35
- })
36
- ```
37
-
38
- - `id` is your name for it, unique in the app. `datasourceId` is the table that gets threads.
39
- - No `permissions`. Who can read, post and delete is set in Settings → Comments, shared with native blocks.
40
- - A viewer sees a thread only if a `read` action (`defineQuery`) on the same table lets them read it. Without one, `list` returns `null` for them.
41
- - One declaration per datasource. Two on the same table are the **same** threads, so "internal notes" plus "customer comments" on one table isn't possible.
42
- - Write the object inline. A variable hides it from the build.
43
-
44
- ## Read a thread
45
-
46
- ```ts
47
- const getThread = createServerFn({ method: 'GET' })
48
- .validator((data: { recordId: string; offset?: number | null }) => data)
49
- .handler(({ data }) => comments.list(orderComments, { recordId: data.recordId, offset: data.offset }))
50
- ```
51
-
52
- `comments.list` returns `null` when this visitor can't see comments on the record (comments are off, the table isn't set up, or their group can't read). Render nothing for `null`, not an error.
53
-
54
- Otherwise it returns:
55
-
56
- | Field | What it is |
57
- | --- | --- |
58
- | `items` | This page's comments, newest first. Each is a `CommentView` |
59
- | `canPost` | Whether the visitor can post. Hide the composer when false |
60
- | `allowMentions` | Whether `@` opens a mention menu. Don't call `searchMentions` when false |
61
- | `allowAttachments` | Whether the composer can attach files |
62
- | `notificationsEnabled` | Whether to show the notifications switch at all |
63
- | `quickReactions` | The five `{ emoji, label }` reactions. There are no others |
64
- | `total`, `nextOffset` | Paging. Pass `nextOffset` back as `offset` to get the next, older page. `null` on the last page |
65
-
66
- A `CommentView` is `{ id, bodyHtml, attachments, author: { name, avatar, isDeactivated }, createdAt, canDelete, canEdit, edited, source, reactions }`.
67
-
68
- - `createdAt` is ISO 8601. Show it with `<FormattedDate value={comment.createdAt} time />`, or `useFormatting` for a title; see the `formatting` guidance.
69
- - `attachments` are `{ url, filename, type }`. `type` is `null` when unknown.
70
- - `edited` is true once the comment changed after posting.
71
- - `source` is `{ text, mentions }` for the edit box, or `null` when `canEdit` is false.
72
- - Each reaction is `{ emoji, label, count, isSelected }`.
73
-
74
- `bodyHtml` is already sanitised. Render it with `dangerouslySetInnerHTML` and nothing else:
75
-
76
- ```tsx
77
- <div className="comment-body" dangerouslySetInnerHTML={{ __html: comment.bodyHtml }} />
78
- ```
79
-
80
- It holds `p`, `br`, `strong`, `em`, `u`, `s`, `del`, `a`, `ul`, `ol`, `li`, `code`, `pre`, `blockquote`, `h1`–`h3` and mentions (`span[data-type="mention"]`). Links open in a new tab. Style these tags under the body's class: the reset strips bullets, link colour and spacing.
81
-
82
- ## Post a comment
83
-
84
- ```ts
85
- const postComment = createServerFn({ method: 'POST' })
86
- .validator(
87
- (data: {
88
- recordId: string
89
- body: string
90
- mentions: Array<CommentMention>
91
- attachments?: Array<CommentAttachmentInput>
92
- }) => data,
93
- )
94
- .handler(({ data }) => comments.create(orderComments, data))
95
- ```
96
-
97
- Pass the composer's `text` and `mentions` as they are. Mentioned people get an email. A mention whose `@name` was edited posts as plain text.
98
-
99
- `attachments` are the finished uploads: `useFileUpload(orderComments).files.filter((f) => f.status === 'done')`. `useFileUpload` takes the comments declaration like a mutation. Send files only when `allowAttachments` is true. A comment needs text or at least one file.
100
-
101
- ## The composer
102
-
103
- ```tsx
104
- const composer = useCommentComposer()
105
- const { files, isUploading, upload, remove } = useFileUpload(orderComments)
106
- const done = files.filter((f) => f.status === 'done')
107
-
108
- <textarea {...composer.textareaProps} placeholder="Write a comment" />
109
- {/* While composer.query is not null, list findPeople's answer for it. Picking a person calls composer.pick(person). */}
110
- <button
111
- disabled={(composer.text.trim() === '' && done.length === 0) || isUploading}
112
- onClick={async () => {
113
- await postComment({ data: { recordId, body: composer.text, mentions: composer.mentions, attachments: done } })
114
- composer.reset()
115
- done.forEach((f) => remove(f.id))
116
- }}
117
- >
118
- Post
119
- </button>
120
- ```
121
-
122
- - Spread `textareaProps` onto the `<textarea>` and don't override its handlers.
123
- - `query` is the text after `@` before the caret, or `null`. It's `""` right after `@` is typed.
124
- - `pick({ name, email })` writes `@name`, records the mention and moves the caret after it.
125
- - `reset()` empties the box. Call it only after the post succeeds.
126
-
127
- ## Editing a comment
128
-
129
- Offer Edit only where `canEdit` is true. Mount the edit box only while editing.
130
-
131
- - Text: `useCommentComposer({ initial: comment.source })`.
132
- - Files: `useFileUpload(orderComments, { initialAttachments: comment.attachments.map((a) => ({ ...a, id: a.url })) })`. Attachments have no `id`, so the url stands in.
133
- - Save with `comments.update`, passing the full file list: the kept files plus new uploads. With `allowAttachments` false the server keeps the old files whatever you send.
134
-
135
- ## Mentions
136
-
137
- ```ts
138
- const findPeople = createServerFn({ method: 'GET' })
139
- .validator((data: { query: string }) => data)
140
- .handler(({ data }) => comments.searchMentions(orderComments, data))
141
- ```
142
-
143
- It returns `[{ name, email, avatar }]`: people the visitor can mention whose name or email matches. Who's mentionable is set per user group in Settings → Comments.
144
-
145
- ## Delete, update, react, subscribe
146
-
147
- ```ts
148
- await comments.delete(orderComments, { recordId, commentId })
149
- await comments.update(orderComments, { recordId, commentId, body, mentions, attachments })
150
- await comments.react(orderComments, { recordId, commentId, emoji: '👍', selected: !reaction.isSelected })
151
- const subscribed = await comments.subscription(orderComments, { recordId })
152
- await comments.setSubscription(orderComments, { recordId, subscribed: !subscribed })
153
- ```
154
-
155
- Each runs in its own server function, like `list`. Writes return nothing, so re-read the thread after each one.
156
-
157
- - Offer delete only where `canDelete` is true.
158
- - `react` adds the reaction when `selected` is true and removes it when false. `emoji` must be one of `quickReactions`.
159
- - A subscription means the visitor gets an email about new comments on the record. It needs a signed-in visitor. Posting subscribes the author; a mention subscribes the person mentioned.
160
- - Mails go out a few minutes after a comment, never to its author, and only while notifications are on in Settings → Comments.
161
-
162
- ## Failure modes
163
-
164
- **Keying the thread on the URL.** Pass the fetched `record.id`, never a route or search param. A param can be a slug or stale, and the thread shows another record's comments.
165
-
166
- **Treating `null` as an error.** Render nothing. An error box shows every such visitor a broken page.
167
-
168
- **Rendering a body as text, or sanitising it again.** Use `bodyHtml` with `dangerouslySetInnerHTML`. As text it shows raw tags; a second sanitiser strips the mentions.
169
-
170
- **Inventing reactions.** Only `quickReactions` can be stored. `react` throws for anything else.
171
-
172
- **Sending only new files on edit.** `update` replaces the whole list, so kept files you leave out get deleted.
173
-
174
- **Calling `comments.*` from a component.** It's server only. Wrap each call in a `createServerFn`.
175
-
176
- ## References
177
-
178
- - [record-queries/SKILL.md](../record-queries/SKILL.md): fetching the record whose `id` keys the thread