Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: explicit firstComment argument in createDiscussion() #3292

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions .changeset/fast-jars-love.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
"@lix-js/sdk": patch
---

refactor: explicit `firstComment` argument in `createDiscussion()` https://github.com/opral/lix-sdk/issues/164

It was unclear that the `content` argument in `createDiscussion()` was meant to be the first comment. This change makes it explicit by renaming the argument to `firstComment`.

```diff
- createDiscussion({ discussion, content: "first comment" })
+ createDiscussion({ discussion, firstComment: { content: "first commment" } }})
```
2 changes: 1 addition & 1 deletion packages/csv-app/src/components/ChangeSet.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ const ConfirmChangesBox = () => {
await createDiscussion({
lix,
changeSet,
content: description,
firstComment: { content: description },
});
await saveLixToOpfs({ lix });
}
Expand Down
2 changes: 1 addition & 1 deletion packages/lix-file-manager/src/components/FilterSelect.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ const FilterSelect = () => {
return await createDiscussion({
lix: { ...lix, db: trx },
changeSet,
content: discussionValue,
firstComment: { content: discussionValue },
});
}
);
Expand Down
41 changes: 8 additions & 33 deletions packages/lix-sdk/src/discussion/create-discussion.test.ts
Original file line number Diff line number Diff line change
@@ -1,45 +1,20 @@
import { expect, test } from "vitest";
import { openLixInMemory } from "../lix/open-lix-in-memory.js";
import { newLixFile } from "../lix/new-lix.js";
import type { DetectedChange, LixPlugin } from "../plugin/lix-plugin.js";
import { createDiscussion } from "./create-discussion.js";
import { createChangeSet } from "../change-set/create-change-set.js";
import { fileQueueSettled } from "../file-queue/file-queue-settled.js";

const mockPlugin: LixPlugin = {
key: "mock-plugin",
detectChangesGlob: "*",
detectChanges: async ({ after }) => {
return [
{
schema: {
key: "text",
type: "json",
},
entity_id: "test",
snapshot: {
text: after ? new TextDecoder().decode(after.data) : undefined,
},
} satisfies DetectedChange,
];
},
};

test("should be able to start a discussion on changes", async () => {
const lix = await openLixInMemory({
blob: await newLixFile(),
providePlugins: [mockPlugin],
});

const enc = new TextEncoder();
const lix = await openLixInMemory({});

// produce a change
await lix.db
.insertInto("file")
.values({ id: "test", path: "/test.txt", data: enc.encode("test") })
.insertInto("key_value")
.values({
key: "mock_key",
value: "mock_value",
})
.execute();

await fileQueueSettled({ lix });

const changes = await lix.db
.selectFrom("change")
.selectAll("change")
Expand All @@ -48,8 +23,8 @@ test("should be able to start a discussion on changes", async () => {
await lix.db.transaction().execute(async (trx) => {
await createDiscussion({
lix: { db: trx },
firstComment: { content: "Hello, world!" },
changeSet: await createChangeSet({ lix: { db: trx }, changes }),
content: "comment on a change",
});
});

Expand Down
12 changes: 6 additions & 6 deletions packages/lix-sdk/src/discussion/create-discussion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@ import type { Lix } from "../lix/open-lix.js";
* @example
* ```ts
* const changeSet = await createChangeSet({ lix, changes: ["change1", "change2"] });
* const discussion = await createDiscussion({ lix, changeSet, body: "first comment" });
* const discussion = await createDiscussion({ lix, changeSet, firstComment: { content: "first comment" } });
* ```
*
* @returns the created discussion
*/
export async function createDiscussion(args: {
lix: Pick<Lix, "db">;
changeSet: Pick<ChangeSet, "id">;
content: Comment["content"];
}): Promise<Discussion & { comment: Comment }> {
firstComment: Pick<Comment, "content">;
}): Promise<Discussion & { firstComment: Comment }> {
const executeInTransaction = async (trx: Lix["db"]) => {
const discussion = await trx
.insertInto("discussion")
Expand All @@ -26,17 +26,17 @@ export async function createDiscussion(args: {
.returningAll()
.executeTakeFirstOrThrow();

const comment = await trx
const firstComment = await trx
.insertInto("comment")
.values({
parent_id: null,
discussion_id: discussion.id,
content: args.content,
content: args.firstComment.content,
})
.returningAll()
.executeTakeFirstOrThrow();

return { ...discussion, comment };
return { ...discussion, firstComment };
};

// user provided an open transaction
Expand Down
2 changes: 1 addition & 1 deletion packages/lix-sdk/src/lix/merge.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -724,7 +724,7 @@ test("it should copy discussion and related comments and mappings", async () =>
await createDiscussion({
lix: lix1,
changeSet: await createChangeSet({ lix: lix1, changes: [changes[0]!] }),
content: "comment on a change",
firstComment: { content: "comment on a change" },
});

await merge({ sourceLix: lix1, targetLix: lix2 });
Expand Down
Loading