create-feedbax 0.1.0 → 0.2.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 +201 -21
- package/README.md +11 -1
- package/dist/cli.js +3283 -22
- package/dist/index.js +3394 -160
- package/package.json +40 -14
- package/template/.feedbax-template.sha256 +1 -0
- package/template/.gitignore.template +6 -0
- package/template/.npmignore.template +5 -0
- package/template/.prettierignore.template +4 -0
- package/template/AGENTS.md +13 -0
- package/template/CONNECTED.md +404 -0
- package/template/LICENSE +201 -0
- package/template/README.md +56 -5
- package/template/components.json +25 -0
- package/template/eslint.config.mjs +48 -0
- package/template/migrations/0001_connected.sql +6 -0
- package/template/migrations/0002_participation.sql +8 -0
- package/template/migrations/0003_images.sql +3 -0
- package/template/migrations/0004_followed_status.sql +9 -0
- package/template/package.json +74 -14
- package/template/public/feedbax-mark.svg +4 -0
- package/template/public/robots.txt +3 -0
- package/template/src/authorized-draft-post.test.tsx +212 -0
- package/template/src/authorized-draft-post.tsx +205 -0
- package/template/src/authorized-draft-query.ts +74 -0
- package/template/src/browser-post-state.test.ts +120 -0
- package/template/src/browser-post-state.ts +134 -0
- package/template/src/browser-vote-state.test.ts +14 -0
- package/template/src/browser-vote-state.ts +27 -0
- package/template/src/capability-authorization-error.ts +3 -0
- package/template/src/changelog-page.test.tsx +139 -0
- package/template/src/changelog-page.tsx +228 -0
- package/template/src/changelog-query.ts +13 -0
- package/template/src/changelog-runtime.ts +27 -0
- package/template/src/cloudflare-turnstile.ts +31 -0
- package/template/src/cloudflare-workers-test.ts +3 -0
- package/template/src/cloudflare-workers.d.ts +24 -0
- package/template/src/comment-creation.test.tsx +311 -0
- package/template/src/comment-profile.ts +5 -0
- package/template/src/comment-thread-page.ts +28 -0
- package/template/src/components/command-palette-context.tsx +71 -0
- package/template/src/components/command-palette.tsx +275 -0
- package/template/src/components/connected-identity-provider.tsx +108 -0
- package/template/src/components/create-post-trigger.tsx +44 -0
- package/template/src/components/device-profile-provider.tsx +324 -0
- package/template/src/components/follow-post-button.tsx +89 -0
- package/template/src/components/header.tsx +97 -0
- package/template/src/components/image-attachments.tsx +187 -0
- package/template/src/components/loader.tsx +53 -0
- package/template/src/components/notification-inbox.tsx +232 -0
- package/template/src/components/portal-footer.tsx +14 -0
- package/template/src/components/profile-menu.tsx +119 -0
- package/template/src/components/ui/components/alert-dialog.tsx +185 -0
- package/template/src/components/ui/components/attachment.tsx +207 -0
- package/template/src/components/ui/components/avatar.tsx +107 -0
- package/template/src/components/ui/components/badge.tsx +52 -0
- package/template/src/components/ui/components/bubble.tsx +128 -0
- package/template/src/components/ui/components/button-group.tsx +87 -0
- package/template/src/components/ui/components/button.tsx +56 -0
- package/template/src/components/ui/components/card.tsx +103 -0
- package/template/src/components/ui/components/checkbox.tsx +26 -0
- package/template/src/components/ui/components/command.tsx +195 -0
- package/template/src/components/ui/components/dialog.tsx +154 -0
- package/template/src/components/ui/components/drawer.tsx +229 -0
- package/template/src/components/ui/components/dropdown-menu.tsx +270 -0
- package/template/src/components/ui/components/empty.tsx +101 -0
- package/template/src/components/ui/components/field.tsx +236 -0
- package/template/src/components/ui/components/input-group.tsx +155 -0
- package/template/src/components/ui/components/input.tsx +20 -0
- package/template/src/components/ui/components/item.tsx +201 -0
- package/template/src/components/ui/components/kbd.tsx +26 -0
- package/template/src/components/ui/components/label.tsx +18 -0
- package/template/src/components/ui/components/marker.tsx +71 -0
- package/template/src/components/ui/components/message-scroller.tsx +128 -0
- package/template/src/components/ui/components/message.tsx +92 -0
- package/template/src/components/ui/components/native-select.tsx +65 -0
- package/template/src/components/ui/components/popover.tsx +88 -0
- package/template/src/components/ui/components/portal-primitives.test.tsx +85 -0
- package/template/src/components/ui/components/radio-group.tsx +36 -0
- package/template/src/components/ui/components/scroll-area.tsx +51 -0
- package/template/src/components/ui/components/secondary-form-actions.test.tsx +23 -0
- package/template/src/components/ui/components/select.tsx +199 -0
- package/template/src/components/ui/components/separator.tsx +23 -0
- package/template/src/components/ui/components/sheet.tsx +135 -0
- package/template/src/components/ui/components/skeleton.tsx +13 -0
- package/template/src/components/ui/components/sonner.tsx +43 -0
- package/template/src/components/ui/components/spinner.tsx +16 -0
- package/template/src/components/ui/components/switch.tsx +29 -0
- package/template/src/components/ui/components/tabs.tsx +80 -0
- package/template/src/components/ui/components/textarea.tsx +18 -0
- package/template/src/components/ui/components/tooltip.tsx +64 -0
- package/template/src/components/ui/hooks/.gitkeep +0 -0
- package/template/src/components/ui/lib/utils.ts +6 -0
- package/template/src/components/ui/styles/globals.css +151 -0
- package/template/src/connected-identity.test.ts +144 -0
- package/template/src/connected-identity.ts +122 -0
- package/template/src/connected-participation.test.tsx +67 -0
- package/template/src/connected-runtime.test.ts +241 -0
- package/template/src/connected-runtime.ts +176 -0
- package/template/src/connected-social.test.tsx +158 -0
- package/template/src/create-post-overlay.tsx +82 -0
- package/template/src/draft-post-list.tsx +39 -0
- package/template/src/feedback-cache-invalidation.test.ts +185 -0
- package/template/src/feedback-cache-invalidation.ts +98 -0
- package/template/src/feedback-runtime.ts +105 -0
- package/template/src/feedbax/changelog/index.test.ts +156 -0
- package/template/src/feedbax/changelog/index.ts +206 -0
- package/template/src/feedbax/changelog/notion-changelog.test.ts +395 -0
- package/template/src/feedbax/changelog/notion-changelog.ts +400 -0
- package/template/src/feedbax/config/index.test.ts +139 -0
- package/template/src/feedbax/config/index.ts +364 -0
- package/template/src/feedbax/feedback/connected-feedback.ts +206 -0
- package/template/src/feedbax/feedback/connected.test.ts +272 -0
- package/template/src/feedbax/feedback/connected.ts +215 -0
- package/template/src/feedbax/feedback/email-delivery.test.ts +65 -0
- package/template/src/feedbax/feedback/email-delivery.ts +66 -0
- package/template/src/feedbax/feedback/feedback-storage.ts +43 -0
- package/template/src/feedbax/feedback/identity-handoff.test.ts +81 -0
- package/template/src/feedbax/feedback/identity-handoff.ts +119 -0
- package/template/src/feedbax/feedback/image-feedback.ts +101 -0
- package/template/src/feedbax/feedback/images.test.ts +223 -0
- package/template/src/feedbax/feedback/images.ts +324 -0
- package/template/src/feedbax/feedback/index.test.ts +900 -0
- package/template/src/feedbax/feedback/index.ts +996 -0
- package/template/src/feedbax/feedback/notion-comments.test.ts +171 -0
- package/template/src/feedbax/feedback/notion-comments.ts +231 -0
- package/template/src/feedbax/feedback/notion-data-source.test.ts +364 -0
- package/template/src/feedbax/feedback/notion-data-source.ts +458 -0
- package/template/src/feedbax/feedback/notion-feedback.test.ts +998 -0
- package/template/src/feedbax/feedback/notion-feedback.ts +651 -0
- package/template/src/feedbax/feedback/notion-image-review.ts +55 -0
- package/template/src/feedbax/feedback/notion-request.ts +74 -0
- package/template/src/feedbax/feedback/participation.test.ts +653 -0
- package/template/src/feedbax/feedback/participation.ts +502 -0
- package/template/src/feedbax.test-fixture.ts +64 -0
- package/template/src/feedbax.ts +69 -0
- package/template/src/images-runtime.test.ts +133 -0
- package/template/src/images-runtime.ts +125 -0
- package/template/src/index.css +1795 -0
- package/template/src/lazy-create-post-overlay.tsx +6 -0
- package/template/src/lazy-post-detail-overlay.tsx +21 -0
- package/template/src/masked-post-link.tsx +27 -0
- package/template/src/overlay-post-detail.test.tsx +181 -0
- package/template/src/overlay-post-detail.tsx +43 -0
- package/template/src/participation-client.ts +63 -0
- package/template/src/participation-runtime.test.ts +35 -0
- package/template/src/participation-runtime.ts +255 -0
- package/template/src/portal-comments.test.ts +375 -0
- package/template/src/portal-comments.ts +403 -0
- package/template/src/portal-features.test.tsx +150 -0
- package/template/src/portal-feedback-form.tsx +530 -0
- package/template/src/portal-feedback-runtime.test.ts +32 -0
- package/template/src/portal-feedback-runtime.ts +58 -0
- package/template/src/portal-feedback-server-function.ts +96 -0
- package/template/src/portal-feedback-submission.test.ts +307 -0
- package/template/src/portal-feedback-submission.ts +224 -0
- package/template/src/portal-head.ts +58 -0
- package/template/src/portal-server-entry.ts +68 -0
- package/template/src/portal-voting.test.ts +106 -0
- package/template/src/portal-voting.ts +149 -0
- package/template/src/post-badges.tsx +27 -0
- package/template/src/post-creation-flow.test.tsx +230 -0
- package/template/src/post-creation-flow.tsx +88 -0
- package/template/src/post-detail-content.test.tsx +233 -0
- package/template/src/post-detail-content.tsx +162 -0
- package/template/src/post-detail-overlay.test.tsx +114 -0
- package/template/src/post-detail-overlay.tsx +67 -0
- package/template/src/post-detail-query.ts +34 -0
- package/template/src/post-feed-controls.test.tsx +239 -0
- package/template/src/post-feed-controls.tsx +298 -0
- package/template/src/post-presentation.ts +37 -0
- package/template/src/post-queries.test.ts +158 -0
- package/template/src/post-queries.ts +26 -0
- package/template/src/post-results.tsx +165 -0
- package/template/src/public-cache-policy.test.ts +148 -0
- package/template/src/public-cache-policy.ts +119 -0
- package/template/src/public-cache-tags.ts +7 -0
- package/template/src/public-changelog-page.ts +15 -0
- package/template/src/public-changelog-server-function.ts +27 -0
- package/template/src/public-date.ts +11 -0
- package/template/src/public-failure-state.test.tsx +69 -0
- package/template/src/public-feedback-index.tsx +94 -0
- package/template/src/public-feedback-page.test.tsx +355 -0
- package/template/src/public-feedback-page.ts +86 -0
- package/template/src/public-feedback-server-function.ts +22 -0
- package/template/src/public-portal-error.tsx +27 -0
- package/template/src/public-post-detail-skeleton.tsx +54 -0
- package/template/src/public-post-detail.tsx +772 -0
- package/template/src/public-post-page.test.tsx +173 -0
- package/template/src/public-post-page.ts +15 -0
- package/template/src/public-post-route.test.tsx +250 -0
- package/template/src/public-post-route.tsx +158 -0
- package/template/src/public-post-server-function.ts +48 -0
- package/template/src/public-post-unavailable.tsx +10 -0
- package/template/src/public-roadmap-page.test.tsx +139 -0
- package/template/src/public-roadmap-page.ts +24 -0
- package/template/src/public-roadmap-route.test.tsx +254 -0
- package/template/src/public-roadmap-route.tsx +50 -0
- package/template/src/public-roadmap-server-function.ts +44 -0
- package/template/src/public-roadmap-view.tsx +411 -0
- package/template/src/public-shell.test.tsx +544 -0
- package/template/src/roadmap-query.ts +21 -0
- package/template/src/root-branding.test.ts +53 -0
- package/template/src/routeTree.gen.ts +177 -0
- package/template/src/routed-post-creation-form.tsx +28 -0
- package/template/src/router.tsx +27 -0
- package/template/src/routes/__root.tsx +114 -0
- package/template/src/routes/changelog.tsx +39 -0
- package/template/src/routes/email-preferences.tsx +77 -0
- package/template/src/routes/index.tsx +61 -0
- package/template/src/routes/p.$slug.tsx +16 -0
- package/template/src/routes/roadmap.tsx +11 -0
- package/template/src/routes/submit.tsx +28 -0
- package/template/src/safe-public-failure.ts +47 -0
- package/template/src/server.ts +151 -0
- package/template/src/similar-posts.tsx +45 -0
- package/template/src/submit-route.test.tsx +134 -0
- package/template/src/trusted-feedback-handler.test.ts +425 -0
- package/template/src/trusted-feedback-handler.ts +181 -0
- package/template/src/trusted-feedback-runtime.ts +17 -0
- package/template/src/turnstile-configuration.test.ts +37 -0
- package/template/src/turnstile-configuration.ts +19 -0
- package/template/src/use-media-query.ts +17 -0
- package/template/src/vote-toggle.test.tsx +171 -0
- package/template/src/vote-toggle.tsx +244 -0
- package/template/tsconfig.json +45 -0
- package/template/vite.config.ts +20 -0
- package/template/vitest.config.ts +4 -0
- package/template/vitest.setup.ts +3 -0
- package/template/wrangler.jsonc +35 -0
- package/dist/cli.d.ts +0 -3
- package/dist/cli.d.ts.map +0 -1
- package/dist/cli.js.map +0 -1
- package/dist/index.d.ts +0 -21
- package/dist/index.d.ts.map +0 -1
- package/dist/index.js.map +0 -1
- package/template/.env.example +0 -4
- package/template/apps/portal/components.json +0 -20
- package/template/apps/portal/package.json +0 -31
- package/template/apps/portal/src/lib/utils.ts +0 -6
- package/template/apps/portal/src/routeTree.gen.ts +0 -59
- package/template/apps/portal/src/router.tsx +0 -10
- package/template/apps/portal/src/routes/__root.tsx +0 -31
- package/template/apps/portal/src/routes/index.tsx +0 -35
- package/template/apps/portal/src/styles.css +0 -91
- package/template/apps/portal/tsconfig.json +0 -15
- package/template/apps/portal/vite.config.ts +0 -13
- package/template/feedbax.config.ts +0 -13
- package/template/feedbax.jsonc +0 -11
- package/template/pnpm-workspace.yaml +0 -3
- package/template/turbo.json +0 -8
|
@@ -0,0 +1,241 @@
|
|
|
1
|
+
import { issueParticipationPass } from "./portal-voting";
|
|
2
|
+
import { expect, it } from "vitest";
|
|
3
|
+
import { Miniflare } from "miniflare";
|
|
4
|
+
import { existsSync } from "node:fs";
|
|
5
|
+
import { readFile } from "node:fs/promises";
|
|
6
|
+
import {
|
|
7
|
+
createConnectedStore,
|
|
8
|
+
createConnectedFeedbackModule,
|
|
9
|
+
createFeedbackModule,
|
|
10
|
+
} from "@feedbax/feedback";
|
|
11
|
+
import { createConnectedRequestHandler } from "./connected-runtime";
|
|
12
|
+
import { sessionHash } from "./connected-identity";
|
|
13
|
+
import feedbax from "./feedbax";
|
|
14
|
+
|
|
15
|
+
it("requires a valid session and same-origin mutations, and ignores browser-supplied Comment identity", async () => {
|
|
16
|
+
const runtime = new Miniflare({
|
|
17
|
+
modules: true,
|
|
18
|
+
script: "export default {fetch(){return new Response('ok')}}",
|
|
19
|
+
compatibilityDate: "2026-07-30",
|
|
20
|
+
d1Databases: ["DB"],
|
|
21
|
+
});
|
|
22
|
+
try {
|
|
23
|
+
const db = await runtime.getD1Database("DB");
|
|
24
|
+
await db.exec(
|
|
25
|
+
(await readFile(
|
|
26
|
+
existsSync("apps/portal/migrations/0001_connected.sql")
|
|
27
|
+
? "apps/portal/migrations/0001_connected.sql"
|
|
28
|
+
: "migrations/0001_connected.sql",
|
|
29
|
+
"utf8",
|
|
30
|
+
)) +
|
|
31
|
+
"\n" +
|
|
32
|
+
(await readFile(
|
|
33
|
+
existsSync("apps/portal/migrations/0002_participation.sql")
|
|
34
|
+
? "apps/portal/migrations/0002_participation.sql"
|
|
35
|
+
: "migrations/0002_participation.sql",
|
|
36
|
+
"utf8",
|
|
37
|
+
)),
|
|
38
|
+
);
|
|
39
|
+
const store = createConnectedStore(db);
|
|
40
|
+
const participant = { subject: "alice", displayName: "Alice" };
|
|
41
|
+
await store.acceptIdentity({
|
|
42
|
+
...participant,
|
|
43
|
+
nonce: "nonce",
|
|
44
|
+
sessionHash: sessionHash("opaque-token"),
|
|
45
|
+
expiresAt: Date.now() + 60000,
|
|
46
|
+
});
|
|
47
|
+
const config = {
|
|
48
|
+
...feedbax,
|
|
49
|
+
product: { ...feedbax.product, siteUrl: "https://feedback.example" },
|
|
50
|
+
connected: {
|
|
51
|
+
issuer: "https://app.example",
|
|
52
|
+
loginUrl: "https://app.example/login",
|
|
53
|
+
},
|
|
54
|
+
};
|
|
55
|
+
const base = createFeedbackModule({
|
|
56
|
+
initialItems: [
|
|
57
|
+
{
|
|
58
|
+
id: "post",
|
|
59
|
+
slug: "post",
|
|
60
|
+
title: "Post",
|
|
61
|
+
description: "Public",
|
|
62
|
+
type: "Feature Request",
|
|
63
|
+
status: "Planned",
|
|
64
|
+
published: true,
|
|
65
|
+
createdAt: new Date(),
|
|
66
|
+
updatedAt: new Date(),
|
|
67
|
+
voteCount: 99,
|
|
68
|
+
},
|
|
69
|
+
],
|
|
70
|
+
});
|
|
71
|
+
const options = {
|
|
72
|
+
config,
|
|
73
|
+
environment: {
|
|
74
|
+
PORTAL_SUBMISSIONS_RATE_LIMITER: {
|
|
75
|
+
limit: async () => ({ success: true }),
|
|
76
|
+
},
|
|
77
|
+
PARTICIPATION_SIGNING_SECRET:
|
|
78
|
+
"test-participation-secret-at-least-32-characters",
|
|
79
|
+
},
|
|
80
|
+
getStore: () => store,
|
|
81
|
+
getFeedback: (identity: typeof participant) =>
|
|
82
|
+
createConnectedFeedbackModule(base, store, identity),
|
|
83
|
+
};
|
|
84
|
+
const handle = createConnectedRequestHandler(options);
|
|
85
|
+
const request = (
|
|
86
|
+
path: string,
|
|
87
|
+
body: unknown,
|
|
88
|
+
cookie = "__Host-feedbax-session=opaque-token",
|
|
89
|
+
origin = "https://feedback.example",
|
|
90
|
+
) =>
|
|
91
|
+
new Request(`https://feedback.example${path}`, {
|
|
92
|
+
method: "POST",
|
|
93
|
+
headers: {
|
|
94
|
+
"Content-Type": "application/json",
|
|
95
|
+
Cookie: cookie,
|
|
96
|
+
Origin: origin,
|
|
97
|
+
},
|
|
98
|
+
body: JSON.stringify(body),
|
|
99
|
+
});
|
|
100
|
+
expect(
|
|
101
|
+
(
|
|
102
|
+
await handle(
|
|
103
|
+
request("/internal/votes", { slug: "post", intention: "add" }, ""),
|
|
104
|
+
)
|
|
105
|
+
)?.status,
|
|
106
|
+
).toBe(401);
|
|
107
|
+
expect(
|
|
108
|
+
(
|
|
109
|
+
await handle(
|
|
110
|
+
request(
|
|
111
|
+
"/internal/votes",
|
|
112
|
+
{ slug: "post", intention: "add" },
|
|
113
|
+
undefined,
|
|
114
|
+
"https://evil.example",
|
|
115
|
+
),
|
|
116
|
+
)
|
|
117
|
+
)?.status,
|
|
118
|
+
).toBe(403);
|
|
119
|
+
for (let i = 0; i < 2; i++)
|
|
120
|
+
expect(
|
|
121
|
+
await (
|
|
122
|
+
await handle(
|
|
123
|
+
request("/internal/votes", { slug: "post", intention: "add" }),
|
|
124
|
+
)
|
|
125
|
+
)?.json(),
|
|
126
|
+
).toMatchObject({ voteCount: 1 });
|
|
127
|
+
const state = await handle(
|
|
128
|
+
new Request(
|
|
129
|
+
"https://feedback.example/internal/connected/votes?slug=post",
|
|
130
|
+
{ headers: { Cookie: "__Host-feedbax-session=opaque-token" } },
|
|
131
|
+
),
|
|
132
|
+
);
|
|
133
|
+
expect(state?.headers.get("cache-control")).toBe("no-store");
|
|
134
|
+
expect(await state?.json()).toEqual({ voteCount: 1, voted: true });
|
|
135
|
+
const comment = await handle(
|
|
136
|
+
request("/internal/comments", {
|
|
137
|
+
slug: "post",
|
|
138
|
+
body: "Hello",
|
|
139
|
+
displayName: "Fake Admin",
|
|
140
|
+
email: "private@example.com",
|
|
141
|
+
subject: "someone-else",
|
|
142
|
+
}),
|
|
143
|
+
);
|
|
144
|
+
expect(comment?.status).toBe(200);
|
|
145
|
+
expect(comment?.headers.get("cache-control")).toBe("no-store");
|
|
146
|
+
const result = await comment?.json();
|
|
147
|
+
expect(result.comment.author).toEqual({
|
|
148
|
+
kind: "participant",
|
|
149
|
+
displayName: "Alice",
|
|
150
|
+
verified: true,
|
|
151
|
+
});
|
|
152
|
+
expect(JSON.stringify(result)).not.toContain("private@example.com");
|
|
153
|
+
expect(JSON.stringify(result)).not.toContain("someone-else");
|
|
154
|
+
const disabled = createConnectedRequestHandler({
|
|
155
|
+
...options,
|
|
156
|
+
config: {
|
|
157
|
+
...config,
|
|
158
|
+
features: { ...config.features, comments: false, voting: false },
|
|
159
|
+
},
|
|
160
|
+
});
|
|
161
|
+
expect(
|
|
162
|
+
(
|
|
163
|
+
await disabled(
|
|
164
|
+
request("/internal/comments", { slug: "post", body: "No" }),
|
|
165
|
+
)
|
|
166
|
+
)?.status,
|
|
167
|
+
).toBe(400);
|
|
168
|
+
expect(
|
|
169
|
+
(
|
|
170
|
+
await disabled(
|
|
171
|
+
request("/internal/votes", { slug: "post", intention: "add" }),
|
|
172
|
+
)
|
|
173
|
+
)?.status,
|
|
174
|
+
).not.toBe(200);
|
|
175
|
+
const verified = createConnectedRequestHandler({
|
|
176
|
+
...options,
|
|
177
|
+
config: {
|
|
178
|
+
...config,
|
|
179
|
+
turnstile: { enabled: true, siteKey: "test-site-key" },
|
|
180
|
+
},
|
|
181
|
+
environment: {
|
|
182
|
+
...options.environment,
|
|
183
|
+
TURNSTILE_SECRET_KEY: "test-secret",
|
|
184
|
+
},
|
|
185
|
+
});
|
|
186
|
+
const expiredPass = issueParticipationPass(
|
|
187
|
+
options.environment.PARTICIPATION_SIGNING_SECRET,
|
|
188
|
+
Date.now() - 31 * 60 * 1000,
|
|
189
|
+
);
|
|
190
|
+
const expiredResponse = await verified(
|
|
191
|
+
request("/internal/votes", {
|
|
192
|
+
slug: "post",
|
|
193
|
+
intention: "add",
|
|
194
|
+
participationPass: expiredPass,
|
|
195
|
+
}),
|
|
196
|
+
);
|
|
197
|
+
expect(expiredResponse?.status).toBe(400);
|
|
198
|
+
expect(await expiredResponse?.json()).toMatchObject({
|
|
199
|
+
code: "verification_required",
|
|
200
|
+
});
|
|
201
|
+
const limited = createConnectedRequestHandler({
|
|
202
|
+
...options,
|
|
203
|
+
environment: {
|
|
204
|
+
...options.environment,
|
|
205
|
+
IDENTITY_HANDOFF_SECRET: "test-identity-secret-at-least-32-characters",
|
|
206
|
+
PORTAL_SUBMISSIONS_RATE_LIMITER: {
|
|
207
|
+
limit: async () => ({ success: false }),
|
|
208
|
+
},
|
|
209
|
+
},
|
|
210
|
+
});
|
|
211
|
+
const limitResponse = await limited(
|
|
212
|
+
request("/internal/votes", { slug: "post", intention: "add" }),
|
|
213
|
+
);
|
|
214
|
+
expect(limitResponse?.status).toBe(429);
|
|
215
|
+
expect(await limitResponse?.json()).toMatchObject({ code: "rate_limited" });
|
|
216
|
+
const sessionRead = await limited(
|
|
217
|
+
new Request("https://feedback.example/internal/identity/session", {
|
|
218
|
+
headers: { Cookie: "__Host-feedbax-session=opaque-token" },
|
|
219
|
+
}),
|
|
220
|
+
);
|
|
221
|
+
expect(sessionRead?.status).toBe(200);
|
|
222
|
+
expect(await sessionRead?.json()).toEqual({
|
|
223
|
+
participant: { displayName: "Alice" },
|
|
224
|
+
});
|
|
225
|
+
const unavailable = createConnectedRequestHandler({
|
|
226
|
+
...options,
|
|
227
|
+
getStore: () => {
|
|
228
|
+
throw new Error("D1 unavailable");
|
|
229
|
+
},
|
|
230
|
+
});
|
|
231
|
+
expect(
|
|
232
|
+
(
|
|
233
|
+
await unavailable(
|
|
234
|
+
request("/internal/votes", { slug: "post", intention: "add" }),
|
|
235
|
+
)
|
|
236
|
+
)?.status,
|
|
237
|
+
).toBe(503);
|
|
238
|
+
} finally {
|
|
239
|
+
await runtime.dispose();
|
|
240
|
+
}
|
|
241
|
+
});
|
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
import { ActionablePortalFailure } from "./safe-public-failure";
|
|
2
|
+
import { VoteEligibilityError } from "@feedbax/feedback";
|
|
3
|
+
import type { FeedbaxConfig } from "@feedbax/config";
|
|
4
|
+
import type {
|
|
5
|
+
ConnectedStore,
|
|
6
|
+
FeedbackModule,
|
|
7
|
+
VerifiedParticipant,
|
|
8
|
+
} from "@feedbax/feedback";
|
|
9
|
+
import { env } from "cloudflare:workers";
|
|
10
|
+
import feedbax from "./feedbax";
|
|
11
|
+
import {
|
|
12
|
+
createConfiguredFeedbackModule,
|
|
13
|
+
getConnectedStore,
|
|
14
|
+
} from "./feedback-runtime";
|
|
15
|
+
import {
|
|
16
|
+
createConnectedIdentityHandler,
|
|
17
|
+
readSessionToken,
|
|
18
|
+
sameOrigin,
|
|
19
|
+
sessionHash,
|
|
20
|
+
} from "./connected-identity";
|
|
21
|
+
import { createPortalVoteHandler } from "./portal-voting";
|
|
22
|
+
import {
|
|
23
|
+
createPortalCommentRequestHandler,
|
|
24
|
+
createPortalCommentMutationRequestHandler,
|
|
25
|
+
} from "./portal-comments";
|
|
26
|
+
import { createPortalSubmissionSecurity } from "./portal-feedback-runtime";
|
|
27
|
+
|
|
28
|
+
type ConnectedEnvironment = Parameters<
|
|
29
|
+
typeof createPortalSubmissionSecurity
|
|
30
|
+
>[0]["environment"] & {
|
|
31
|
+
IDENTITY_HANDOFF_SECRET?: string;
|
|
32
|
+
PARTICIPATION_SIGNING_SECRET?: string;
|
|
33
|
+
};
|
|
34
|
+
export function createConnectedRequestHandler(options: {
|
|
35
|
+
config: FeedbaxConfig;
|
|
36
|
+
environment: ConnectedEnvironment;
|
|
37
|
+
getStore: () => ConnectedStore;
|
|
38
|
+
getFeedback: (participant: VerifiedParticipant) => FeedbackModule;
|
|
39
|
+
}) {
|
|
40
|
+
const { config: feedbax, environment: env } = options;
|
|
41
|
+
return async (request: Request): Promise<Response | undefined> => {
|
|
42
|
+
const url = new URL(request.url);
|
|
43
|
+
const identity = url.pathname.startsWith("/internal/identity/");
|
|
44
|
+
const voteState = url.pathname === "/internal/connected/votes";
|
|
45
|
+
const mutation =
|
|
46
|
+
url.pathname === "/internal/votes" ||
|
|
47
|
+
url.pathname === "/internal/comments";
|
|
48
|
+
if (!identity && !voteState && !mutation) return;
|
|
49
|
+
const json = (value: unknown, status = 200) =>
|
|
50
|
+
Response.json(value, {
|
|
51
|
+
status,
|
|
52
|
+
headers: { "Cache-Control": "no-store" },
|
|
53
|
+
});
|
|
54
|
+
if (!feedbax.connected)
|
|
55
|
+
return identity || voteState
|
|
56
|
+
? json({ error: "Not found." }, 404)
|
|
57
|
+
: undefined;
|
|
58
|
+
try {
|
|
59
|
+
const store = options.getStore();
|
|
60
|
+
const siteUrl = feedbax.product.siteUrl!;
|
|
61
|
+
if (identity) {
|
|
62
|
+
if (
|
|
63
|
+
url.pathname !== "/internal/identity/session" &&
|
|
64
|
+
!(
|
|
65
|
+
await env.PORTAL_SUBMISSIONS_RATE_LIMITER.limit({
|
|
66
|
+
key: `identity:${url.pathname}:${request.headers.get("CF-Connecting-IP") ?? "unknown"}`,
|
|
67
|
+
})
|
|
68
|
+
).success
|
|
69
|
+
)
|
|
70
|
+
return json(
|
|
71
|
+
{ error: "Too many sign-in attempts. Try again shortly." },
|
|
72
|
+
429,
|
|
73
|
+
);
|
|
74
|
+
if (
|
|
75
|
+
!env.IDENTITY_HANDOFF_SECRET ||
|
|
76
|
+
env.IDENTITY_HANDOFF_SECRET.length < 32
|
|
77
|
+
)
|
|
78
|
+
throw new Error("Missing identity secret.");
|
|
79
|
+
return createConnectedIdentityHandler({
|
|
80
|
+
...feedbax.connected,
|
|
81
|
+
siteUrl,
|
|
82
|
+
secret: env.IDENTITY_HANDOFF_SECRET,
|
|
83
|
+
store,
|
|
84
|
+
})(request);
|
|
85
|
+
}
|
|
86
|
+
if (mutation && !sameOrigin(request, siteUrl))
|
|
87
|
+
return json({ error: "Request origin is invalid." }, 403);
|
|
88
|
+
if (mutation && !["POST", "PATCH", "DELETE"].includes(request.method))
|
|
89
|
+
return json({ error: "Method not allowed." }, 405);
|
|
90
|
+
const token = readSessionToken(request);
|
|
91
|
+
const participant = token
|
|
92
|
+
? await store.session(sessionHash(token))
|
|
93
|
+
: undefined;
|
|
94
|
+
if (!participant)
|
|
95
|
+
return json(
|
|
96
|
+
{
|
|
97
|
+
error: "Sign in through the product to participate.",
|
|
98
|
+
code: "sign_in_required",
|
|
99
|
+
},
|
|
100
|
+
401,
|
|
101
|
+
);
|
|
102
|
+
const feedback = options.getFeedback(participant);
|
|
103
|
+
if (voteState && request.method === "GET") {
|
|
104
|
+
const slug = url.searchParams.get("slug") ?? "";
|
|
105
|
+
const post = await feedback.getPublicPost(slug);
|
|
106
|
+
if (!feedbax.features.voting || !post)
|
|
107
|
+
return json({ error: "Post is unavailable." }, 404);
|
|
108
|
+
return json(await store.voteState(slug, participant.subject));
|
|
109
|
+
}
|
|
110
|
+
const security = createPortalSubmissionSecurity({
|
|
111
|
+
turnstile: feedbax.turnstile,
|
|
112
|
+
environment: env,
|
|
113
|
+
rateLimitKey:
|
|
114
|
+
request.headers.get("CF-Connecting-IP") ?? "unknown-participant-ip",
|
|
115
|
+
});
|
|
116
|
+
const mutationOptions = {
|
|
117
|
+
feedback,
|
|
118
|
+
...security,
|
|
119
|
+
participationSigningSecret: env.PARTICIPATION_SIGNING_SECRET,
|
|
120
|
+
};
|
|
121
|
+
if (url.pathname === "/internal/votes" && request.method === "POST")
|
|
122
|
+
return json(
|
|
123
|
+
await createPortalVoteHandler({
|
|
124
|
+
...mutationOptions,
|
|
125
|
+
votingEnabled: feedbax.features.voting,
|
|
126
|
+
})(await request.json()),
|
|
127
|
+
);
|
|
128
|
+
if (url.pathname === "/internal/comments" && request.method === "POST")
|
|
129
|
+
return noStore(
|
|
130
|
+
createPortalCommentRequestHandler({
|
|
131
|
+
...mutationOptions,
|
|
132
|
+
verifiedParticipant: participant,
|
|
133
|
+
commentsEnabled: feedbax.features.comments,
|
|
134
|
+
})(request),
|
|
135
|
+
);
|
|
136
|
+
if (
|
|
137
|
+
url.pathname === "/internal/comments" &&
|
|
138
|
+
(request.method === "PATCH" || request.method === "DELETE")
|
|
139
|
+
)
|
|
140
|
+
return noStore(
|
|
141
|
+
createPortalCommentMutationRequestHandler({
|
|
142
|
+
...mutationOptions,
|
|
143
|
+
commentsEnabled: feedbax.features.comments,
|
|
144
|
+
})(request),
|
|
145
|
+
);
|
|
146
|
+
return json({ error: "Method not allowed." }, 405);
|
|
147
|
+
} catch (error) {
|
|
148
|
+
if (error instanceof ActionablePortalFailure)
|
|
149
|
+
return json(
|
|
150
|
+
{ error: error.message, code: error.code ?? "action_failed" },
|
|
151
|
+
error.code === "rate_limited" ? 429 : 400,
|
|
152
|
+
);
|
|
153
|
+
if (error instanceof VoteEligibilityError)
|
|
154
|
+
return json({ error: error.message, code: "ineligible_post" }, 400);
|
|
155
|
+
return json(
|
|
156
|
+
{
|
|
157
|
+
error:
|
|
158
|
+
"Connected participation is temporarily unavailable. Try again.",
|
|
159
|
+
},
|
|
160
|
+
503,
|
|
161
|
+
);
|
|
162
|
+
}
|
|
163
|
+
};
|
|
164
|
+
}
|
|
165
|
+
export const handleConnectedRequest = createConnectedRequestHandler({
|
|
166
|
+
config: feedbax,
|
|
167
|
+
environment: env,
|
|
168
|
+
getStore: getConnectedStore,
|
|
169
|
+
getFeedback: createConfiguredFeedbackModule,
|
|
170
|
+
});
|
|
171
|
+
|
|
172
|
+
async function noStore(response: Promise<Response>) {
|
|
173
|
+
const result = await response;
|
|
174
|
+
result.headers.set("Cache-Control", "no-store");
|
|
175
|
+
return result;
|
|
176
|
+
}
|
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
// @vitest-environment jsdom
|
|
2
|
+
import { afterEach, expect, it, vi } from "vitest";
|
|
3
|
+
import {
|
|
4
|
+
cleanup,
|
|
5
|
+
fireEvent,
|
|
6
|
+
render,
|
|
7
|
+
screen,
|
|
8
|
+
waitFor,
|
|
9
|
+
} from "@testing-library/react";
|
|
10
|
+
import { useState } from "react";
|
|
11
|
+
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
|
|
12
|
+
import { ConnectedIdentityProvider } from "./components/connected-identity-provider";
|
|
13
|
+
import { FollowPostButton } from "./components/follow-post-button";
|
|
14
|
+
import { NotificationInbox } from "./components/notification-inbox";
|
|
15
|
+
import { ImageAttachments } from "./components/image-attachments";
|
|
16
|
+
import type { ImageAttachment } from "@feedbax/feedback";
|
|
17
|
+
vi.mock("./feedbax", async (importOriginal) => {
|
|
18
|
+
const actual = await importOriginal<typeof import("./feedbax")>();
|
|
19
|
+
return {
|
|
20
|
+
default: {
|
|
21
|
+
...actual.default,
|
|
22
|
+
connected: {
|
|
23
|
+
issuer: "https://app.example",
|
|
24
|
+
loginUrl: "https://app.example/login",
|
|
25
|
+
images: true,
|
|
26
|
+
},
|
|
27
|
+
},
|
|
28
|
+
};
|
|
29
|
+
});
|
|
30
|
+
afterEach(() => {
|
|
31
|
+
cleanup();
|
|
32
|
+
vi.unstubAllGlobals();
|
|
33
|
+
});
|
|
34
|
+
it("follows a Post and manages private read and email preferences from the inbox", async () => {
|
|
35
|
+
let following = false,
|
|
36
|
+
read = false,
|
|
37
|
+
email = false;
|
|
38
|
+
vi.stubGlobal(
|
|
39
|
+
"fetch",
|
|
40
|
+
vi.fn<typeof fetch>().mockImplementation(async (input, init) => {
|
|
41
|
+
const path = String(input);
|
|
42
|
+
const body = init?.body ? JSON.parse(String(init.body)) : undefined;
|
|
43
|
+
if (path === "/internal/identity/session")
|
|
44
|
+
return Response.json({ participant: { displayName: "Alice" } });
|
|
45
|
+
if (path.includes("/follow")) {
|
|
46
|
+
if (body) following = body.following;
|
|
47
|
+
return Response.json({ following });
|
|
48
|
+
}
|
|
49
|
+
if (path.includes("/preferences")) {
|
|
50
|
+
if (body) email = body.emailEnabled;
|
|
51
|
+
return Response.json({ emailAvailable: true, emailEnabled: email });
|
|
52
|
+
}
|
|
53
|
+
if (body) {
|
|
54
|
+
read = true;
|
|
55
|
+
return Response.json({ updated: true });
|
|
56
|
+
}
|
|
57
|
+
return Response.json({
|
|
58
|
+
unreadCount: read ? 0 : 1,
|
|
59
|
+
items: [
|
|
60
|
+
{
|
|
61
|
+
id: "a".repeat(32),
|
|
62
|
+
slug: "post",
|
|
63
|
+
kind: "status",
|
|
64
|
+
title: "Image uploads",
|
|
65
|
+
message: "Status changed to Shipped.",
|
|
66
|
+
createdAt: Date.now(),
|
|
67
|
+
read,
|
|
68
|
+
},
|
|
69
|
+
],
|
|
70
|
+
});
|
|
71
|
+
}),
|
|
72
|
+
);
|
|
73
|
+
const client = new QueryClient({
|
|
74
|
+
defaultOptions: { queries: { retry: false } },
|
|
75
|
+
});
|
|
76
|
+
render(
|
|
77
|
+
<QueryClientProvider client={client}>
|
|
78
|
+
<ConnectedIdentityProvider>
|
|
79
|
+
<FollowPostButton slug="post" />
|
|
80
|
+
<NotificationInbox />
|
|
81
|
+
</ConnectedIdentityProvider>
|
|
82
|
+
</QueryClientProvider>,
|
|
83
|
+
);
|
|
84
|
+
const follow = await screen.findByRole("button", { name: "Follow updates" });
|
|
85
|
+
await waitFor(() => expect(follow.hasAttribute("disabled")).toBe(false));
|
|
86
|
+
fireEvent.click(follow);
|
|
87
|
+
await screen.findByRole("button", { name: "Following" });
|
|
88
|
+
expect(following).toBe(true);
|
|
89
|
+
fireEvent.click(
|
|
90
|
+
await screen.findByRole("button", { name: "Notifications, 1 unread" }),
|
|
91
|
+
);
|
|
92
|
+
await screen.findByText("Status changed to Shipped.");
|
|
93
|
+
fireEvent.click(screen.getByRole("button", { name: "Mark all read" }));
|
|
94
|
+
await screen.findByText("0 unread");
|
|
95
|
+
const toggle = await screen.findByRole("switch", {
|
|
96
|
+
name: "Email notifications",
|
|
97
|
+
});
|
|
98
|
+
fireEvent.click(toggle);
|
|
99
|
+
await waitFor(() => expect(email).toBe(true));
|
|
100
|
+
client.clear();
|
|
101
|
+
});
|
|
102
|
+
it("uploads and removes image previews without submitting the surrounding form", async () => {
|
|
103
|
+
let submitted = false;
|
|
104
|
+
const calls: string[] = [];
|
|
105
|
+
vi.stubGlobal(
|
|
106
|
+
"fetch",
|
|
107
|
+
vi.fn<typeof fetch>().mockImplementation(async (input, init) => {
|
|
108
|
+
if (String(input).includes("identity/session"))
|
|
109
|
+
return Response.json({ participant: { displayName: "Alice" } });
|
|
110
|
+
calls.push(init?.method ?? "GET");
|
|
111
|
+
return Response.json(
|
|
112
|
+
init?.method === "DELETE"
|
|
113
|
+
? { removed: true }
|
|
114
|
+
: {
|
|
115
|
+
id: "11111111-2222-3333-4444-555555555555",
|
|
116
|
+
src: "/internal/images/example",
|
|
117
|
+
contentType: "image/png",
|
|
118
|
+
bytes: 68,
|
|
119
|
+
},
|
|
120
|
+
);
|
|
121
|
+
}),
|
|
122
|
+
);
|
|
123
|
+
function Composer() {
|
|
124
|
+
const [images, setImages] = useState<ImageAttachment[]>([]);
|
|
125
|
+
return (
|
|
126
|
+
<form
|
|
127
|
+
onSubmit={(event) => {
|
|
128
|
+
event.preventDefault();
|
|
129
|
+
submitted = true;
|
|
130
|
+
}}
|
|
131
|
+
>
|
|
132
|
+
<ImageAttachments value={images} onChange={setImages} />
|
|
133
|
+
</form>
|
|
134
|
+
);
|
|
135
|
+
}
|
|
136
|
+
render(
|
|
137
|
+
<ConnectedIdentityProvider>
|
|
138
|
+
<Composer />
|
|
139
|
+
</ConnectedIdentityProvider>,
|
|
140
|
+
);
|
|
141
|
+
const input = await screen.findByLabelText("Choose image attachments");
|
|
142
|
+
fireEvent.change(input, {
|
|
143
|
+
target: {
|
|
144
|
+
files: [
|
|
145
|
+
new File([new Uint8Array([137, 80, 78, 71])], "sample.png", {
|
|
146
|
+
type: "image/png",
|
|
147
|
+
}),
|
|
148
|
+
],
|
|
149
|
+
},
|
|
150
|
+
});
|
|
151
|
+
await screen.findByAltText("Attached image 1");
|
|
152
|
+
fireEvent.click(screen.getByRole("button", { name: "Remove image 1" }));
|
|
153
|
+
await waitFor(() =>
|
|
154
|
+
expect(screen.queryByAltText("Attached image 1")).toBeNull(),
|
|
155
|
+
);
|
|
156
|
+
expect(calls).toEqual(["POST", "DELETE"]);
|
|
157
|
+
expect(submitted).toBe(false);
|
|
158
|
+
});
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import {
|
|
2
|
+
Dialog,
|
|
3
|
+
DialogContent,
|
|
4
|
+
DialogDescription,
|
|
5
|
+
DialogHeader,
|
|
6
|
+
DialogTitle,
|
|
7
|
+
} from "@feedbax/ui/components/dialog";
|
|
8
|
+
import {
|
|
9
|
+
Drawer,
|
|
10
|
+
DrawerContent,
|
|
11
|
+
DrawerDescription,
|
|
12
|
+
DrawerHeader,
|
|
13
|
+
DrawerTitle,
|
|
14
|
+
} from "@feedbax/ui/components/drawer";
|
|
15
|
+
import { useRouter, useRouterState } from "@tanstack/react-router";
|
|
16
|
+
import type { PortalFeedbackMutations } from "./portal-feedback-form";
|
|
17
|
+
import { PostCreationFlow } from "./post-creation-flow";
|
|
18
|
+
import { useMediaQuery } from "./use-media-query";
|
|
19
|
+
|
|
20
|
+
const mobileMediaQuery = "(max-width: 720px)";
|
|
21
|
+
|
|
22
|
+
declare module "@tanstack/react-router" {
|
|
23
|
+
interface HistoryState {
|
|
24
|
+
createPostOverlay?: boolean;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export function CreatePostOverlay({
|
|
29
|
+
mutations,
|
|
30
|
+
}: {
|
|
31
|
+
mutations: PortalFeedbackMutations;
|
|
32
|
+
}) {
|
|
33
|
+
const router = useRouter();
|
|
34
|
+
const overlayRequested = useRouterState({
|
|
35
|
+
select: (state) => state.location.state.createPostOverlay,
|
|
36
|
+
});
|
|
37
|
+
const mobile = useMediaQuery(mobileMediaQuery);
|
|
38
|
+
const open = overlayRequested === true;
|
|
39
|
+
const close = (nextOpen: boolean) => {
|
|
40
|
+
if (!nextOpen) router.history.back();
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
if (!open) return null;
|
|
44
|
+
|
|
45
|
+
if (mobile) {
|
|
46
|
+
return (
|
|
47
|
+
<Drawer open onOpenChange={close}>
|
|
48
|
+
<DrawerContent className="create-post-drawer">
|
|
49
|
+
<DrawerHeader className="sr-only">
|
|
50
|
+
<DrawerTitle>Create a Post</DrawerTitle>
|
|
51
|
+
<DrawerDescription>
|
|
52
|
+
Share a feature request, bug report, or product observation.
|
|
53
|
+
</DrawerDescription>
|
|
54
|
+
</DrawerHeader>
|
|
55
|
+
<PostCreationFlow
|
|
56
|
+
display="overlay"
|
|
57
|
+
onCancel={() => close(false)}
|
|
58
|
+
mutations={mutations}
|
|
59
|
+
/>
|
|
60
|
+
</DrawerContent>
|
|
61
|
+
</Drawer>
|
|
62
|
+
);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
return (
|
|
66
|
+
<Dialog open onOpenChange={close}>
|
|
67
|
+
<DialogContent className="create-post-dialog">
|
|
68
|
+
<DialogHeader className="sr-only">
|
|
69
|
+
<DialogTitle>Create a Post</DialogTitle>
|
|
70
|
+
<DialogDescription>
|
|
71
|
+
Share a feature request, bug report, or product observation.
|
|
72
|
+
</DialogDescription>
|
|
73
|
+
</DialogHeader>
|
|
74
|
+
<PostCreationFlow
|
|
75
|
+
display="overlay"
|
|
76
|
+
onCancel={() => close(false)}
|
|
77
|
+
mutations={mutations}
|
|
78
|
+
/>
|
|
79
|
+
</DialogContent>
|
|
80
|
+
</Dialog>
|
|
81
|
+
);
|
|
82
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import type { PublicPostQuery } from "@feedbax/feedback";
|
|
2
|
+
import { useQuery } from "@tanstack/react-query";
|
|
3
|
+
import { readCapabilities } from "./browser-post-state";
|
|
4
|
+
import { authorizedDraftPostsQuery } from "./authorized-draft-query";
|
|
5
|
+
|
|
6
|
+
const fetchAuthorizedDraft = async (
|
|
7
|
+
input: Parameters<
|
|
8
|
+
typeof import("./portal-feedback-server-function").getPortalDraftPost
|
|
9
|
+
>[0],
|
|
10
|
+
) => {
|
|
11
|
+
const { getPortalDraftPost } =
|
|
12
|
+
await import("./portal-feedback-server-function");
|
|
13
|
+
return getPortalDraftPost(input);
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
export function useAuthorizedDraftPosts(search: PublicPostQuery) {
|
|
17
|
+
const browser = typeof window !== "undefined";
|
|
18
|
+
const capabilities = browser ? readCapabilities(window.localStorage) : {};
|
|
19
|
+
const draftQuery = useQuery({
|
|
20
|
+
...authorizedDraftPostsQuery(
|
|
21
|
+
capabilities,
|
|
22
|
+
fetchAuthorizedDraft,
|
|
23
|
+
browser ? window.localStorage : undefined,
|
|
24
|
+
),
|
|
25
|
+
enabled: browser && Object.keys(capabilities).length > 0,
|
|
26
|
+
});
|
|
27
|
+
const drafts = draftQuery.data ?? [];
|
|
28
|
+
const searchQuery = search.search?.toLocaleLowerCase();
|
|
29
|
+
const visible = drafts.filter(
|
|
30
|
+
(post) =>
|
|
31
|
+
(!searchQuery ||
|
|
32
|
+
`${post.title}\n${post.description}`
|
|
33
|
+
.toLocaleLowerCase()
|
|
34
|
+
.includes(searchQuery)) &&
|
|
35
|
+
(!search.types?.length || search.types.includes(post.type)) &&
|
|
36
|
+
(!search.statuses?.length || search.statuses.includes(post.status)),
|
|
37
|
+
);
|
|
38
|
+
return visible;
|
|
39
|
+
}
|