@blogmcp/community 0.4.1 → 0.5.0

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.
Files changed (150) hide show
  1. package/dist/blogmcp-community-private.global.js +4 -0
  2. package/dist/blogmcp-community.global.js +3 -3
  3. package/dist/chunks/{mount-Ts_r2fwt.js → mount-SKmc4NgD.js} +39 -9
  4. package/dist/chunks/private-mount-bhCQeScD.js +5397 -0
  5. package/dist/core/attrs.d.ts +15 -0
  6. package/dist/core/settings.d.ts +5 -0
  7. package/dist/index.js +2 -2
  8. package/dist/private/core/dock.d.ts +38 -0
  9. package/dist/private/core/push.d.ts +49 -0
  10. package/dist/private/core/realtime.d.ts +57 -0
  11. package/dist/private/core/setup.d.ts +43 -0
  12. package/dist/private/core/view.d.ts +179 -0
  13. package/dist/private/dm/composer.d.ts +34 -0
  14. package/dist/private/dm/controller.d.ts +162 -0
  15. package/dist/private/dm/index.d.ts +14 -0
  16. package/dist/private/dm/links.d.ts +33 -0
  17. package/dist/private/dm/muted.d.ts +37 -0
  18. package/dist/private/dm/search.d.ts +7 -0
  19. package/dist/private/dm/text.d.ts +27 -0
  20. package/dist/private/dm/ui/DmBlocks.vue.d.ts +8 -0
  21. package/dist/private/dm/ui/DmCases.vue.d.ts +8 -0
  22. package/dist/private/dm/ui/DmComposer.vue.d.ts +20 -0
  23. package/dist/private/dm/ui/DmInbox.vue.d.ts +12 -0
  24. package/dist/private/dm/ui/DmLinkWarning.vue.d.ts +8 -0
  25. package/dist/private/dm/ui/DmLocalAids.vue.d.ts +8 -0
  26. package/dist/private/dm/ui/DmMessageText.vue.d.ts +8 -0
  27. package/dist/private/dm/ui/DmNew.vue.d.ts +12 -0
  28. package/dist/private/dm/ui/DmReport.vue.d.ts +8 -0
  29. package/dist/private/dm/ui/DmRequests.vue.d.ts +12 -0
  30. package/dist/private/dm/ui/DmScreens.vue.d.ts +9 -0
  31. package/dist/private/dm/ui/DmThread.vue.d.ts +9 -0
  32. package/dist/private/element.d.ts +23 -0
  33. package/dist/private/global.d.ts +10 -0
  34. package/dist/private/i18n.d.ts +41 -0
  35. package/dist/private/index.d.ts +32 -0
  36. package/dist/private/index.js +57 -0
  37. package/dist/private/mount.d.ts +55 -0
  38. package/dist/private/pages/api.d.ts +44 -0
  39. package/dist/private/pages/element.d.ts +6 -0
  40. package/dist/private/pages/fragment.d.ts +43 -0
  41. package/dist/private/pages/i18n.d.ts +25 -0
  42. package/dist/private/pages/index.d.ts +1 -0
  43. package/dist/private/pages/pages.d.ts +276 -0
  44. package/dist/private/pages/proof.d.ts +36 -0
  45. package/dist/private/pages/provider-view.d.ts +33 -0
  46. package/dist/private/pages/setup.d.ts +42 -0
  47. package/dist/private/pages/ui/AccountPageRoot.vue.d.ts +9 -0
  48. package/dist/private/pages/ui/PageActivation.vue.d.ts +9 -0
  49. package/dist/private/pages/ui/PageAddress.vue.d.ts +8 -0
  50. package/dist/private/pages/ui/PageDeviceApprove.vue.d.ts +8 -0
  51. package/dist/private/pages/ui/PageDevices.vue.d.ts +8 -0
  52. package/dist/private/pages/ui/PageExport.vue.d.ts +8 -0
  53. package/dist/private/pages/ui/PageMessages.vue.d.ts +9 -0
  54. package/dist/private/react.d.ts +12 -0
  55. package/dist/private/react.js +36 -0
  56. package/dist/private/ui/ChatDockRoot.vue.d.ts +20 -0
  57. package/dist/private/ui/LegalText.vue.d.ts +13 -0
  58. package/dist/private/ui/PrivateScreens.vue.d.ts +14 -0
  59. package/dist/private/ui/PrivateViewRoot.vue.d.ts +23 -0
  60. package/dist/private/ui/PromiseBlock.vue.d.ts +9 -0
  61. package/dist/private/ui/ProviderLine.vue.d.ts +8 -0
  62. package/dist/private/ui/ScreenActivation.vue.d.ts +8 -0
  63. package/dist/private/ui/ScreenAddress.vue.d.ts +8 -0
  64. package/dist/private/ui/ScreenApproval.vue.d.ts +8 -0
  65. package/dist/private/ui/ScreenDevices.vue.d.ts +8 -0
  66. package/dist/private/ui/ScreenHome.vue.d.ts +12 -0
  67. package/dist/private/ui/ScreenNotifications.vue.d.ts +8 -0
  68. package/dist/private/ui/ScreenReads.vue.d.ts +8 -0
  69. package/dist/private/ui/ScreenSettings.vue.d.ts +8 -0
  70. package/dist/private/ui/root.d.ts +35 -0
  71. package/dist/private/ui/text.d.ts +22 -0
  72. package/dist/private/vue.d.ts +88 -0
  73. package/dist/private/vue.js +72 -0
  74. package/dist/react.js +14 -14
  75. package/dist/vue.js +14 -14
  76. package/package.json +23 -6
  77. package/src/core/attrs.ts +19 -0
  78. package/src/core/settings.ts +55 -12
  79. package/src/private/core/dock.ts +105 -0
  80. package/src/private/core/push.ts +166 -0
  81. package/src/private/core/realtime.ts +109 -0
  82. package/src/private/core/setup.ts +75 -0
  83. package/src/private/core/view.ts +920 -0
  84. package/src/private/dm/composer.ts +74 -0
  85. package/src/private/dm/controller.ts +636 -0
  86. package/src/private/dm/index.ts +14 -0
  87. package/src/private/dm/links.ts +55 -0
  88. package/src/private/dm/muted.ts +160 -0
  89. package/src/private/dm/search.ts +18 -0
  90. package/src/private/dm/text.ts +60 -0
  91. package/src/private/dm/ui/DmBlocks.vue +27 -0
  92. package/src/private/dm/ui/DmCases.vue +58 -0
  93. package/src/private/dm/ui/DmComposer.vue +68 -0
  94. package/src/private/dm/ui/DmInbox.vue +34 -0
  95. package/src/private/dm/ui/DmLinkWarning.vue +27 -0
  96. package/src/private/dm/ui/DmLocalAids.vue +43 -0
  97. package/src/private/dm/ui/DmMessageText.vue +16 -0
  98. package/src/private/dm/ui/DmNew.vue +46 -0
  99. package/src/private/dm/ui/DmReport.vue +93 -0
  100. package/src/private/dm/ui/DmRequests.vue +50 -0
  101. package/src/private/dm/ui/DmScreens.vue +66 -0
  102. package/src/private/dm/ui/DmThread.vue +235 -0
  103. package/src/private/element.ts +90 -0
  104. package/src/private/global.ts +14 -0
  105. package/src/private/i18n.ts +127 -0
  106. package/src/private/index.ts +68 -0
  107. package/src/private/messages/cs.json +311 -0
  108. package/src/private/messages/en.json +319 -0
  109. package/src/private/mount.ts +103 -0
  110. package/src/private/pages/api.ts +96 -0
  111. package/src/private/pages/build.d.mts +2 -0
  112. package/src/private/pages/build.mjs +72 -0
  113. package/src/private/pages/element.ts +49 -0
  114. package/src/private/pages/fragment.ts +63 -0
  115. package/src/private/pages/i18n.ts +82 -0
  116. package/src/private/pages/index.ts +10 -0
  117. package/src/private/pages/messages/cs.json +97 -0
  118. package/src/private/pages/messages/en.json +97 -0
  119. package/src/private/pages/pages.ts +410 -0
  120. package/src/private/pages/proof.ts +110 -0
  121. package/src/private/pages/provider-view.ts +121 -0
  122. package/src/private/pages/setup.ts +79 -0
  123. package/src/private/pages/ui/AccountPageRoot.vue +45 -0
  124. package/src/private/pages/ui/PageActivation.vue +42 -0
  125. package/src/private/pages/ui/PageAddress.vue +39 -0
  126. package/src/private/pages/ui/PageDeviceApprove.vue +62 -0
  127. package/src/private/pages/ui/PageDevices.vue +84 -0
  128. package/src/private/pages/ui/PageExport.vue +35 -0
  129. package/src/private/pages/ui/PageMessages.vue +39 -0
  130. package/src/private/pages/ui/account.css +6 -0
  131. package/src/private/react.ts +49 -0
  132. package/src/private/shims.d.ts +6 -0
  133. package/src/private/ui/ChatDockRoot.vue +119 -0
  134. package/src/private/ui/LegalText.vue +18 -0
  135. package/src/private/ui/PrivateScreens.vue +61 -0
  136. package/src/private/ui/PrivateViewRoot.vue +70 -0
  137. package/src/private/ui/PromiseBlock.vue +18 -0
  138. package/src/private/ui/ProviderLine.vue +20 -0
  139. package/src/private/ui/ScreenActivation.vue +82 -0
  140. package/src/private/ui/ScreenAddress.vue +37 -0
  141. package/src/private/ui/ScreenApproval.vue +71 -0
  142. package/src/private/ui/ScreenDevices.vue +42 -0
  143. package/src/private/ui/ScreenHome.vue +29 -0
  144. package/src/private/ui/ScreenNotifications.vue +44 -0
  145. package/src/private/ui/ScreenReads.vue +23 -0
  146. package/src/private/ui/ScreenSettings.vue +44 -0
  147. package/src/private/ui/private.css +81 -0
  148. package/src/private/ui/root.ts +89 -0
  149. package/src/private/ui/text.ts +40 -0
  150. package/src/private/vue.ts +87 -0
@@ -0,0 +1,81 @@
1
+ /* The private elements' one stylesheet (each shadow root carries it, with the CSP nonce). Colours follow the host's
2
+ `color-scheme`; a host themes them through the custom properties below. */
3
+ :host {
4
+ --bcm-p-fg: CanvasText;
5
+ --bcm-p-bg: Canvas;
6
+ --bcm-p-muted: GrayText;
7
+ --bcm-p-accent: #2f6f4f;
8
+ --bcm-p-danger: #a3261f;
9
+ --bcm-p-radius: 10px;
10
+ display: block;
11
+ font: inherit;
12
+ color: var(--bcm-p-fg);
13
+ }
14
+ /* The host hides the dock on a page without unmounting it (§6.2): its session and windows stay. */
15
+ :host([hidden-by-host]) {
16
+ display: none !important;
17
+ }
18
+ :host([hidden]) {
19
+ display: none;
20
+ }
21
+ .bcm-p-root { box-sizing: border-box; }
22
+ .bcm-p-root *, .bcm-p-root *::before, .bcm-p-root *::after { box-sizing: inherit; }
23
+ .bcm-p-screen h2 { font-size: 1.15em; margin: 0 0 0.5em; }
24
+ .bcm-p-screen h3 { font-size: 1em; margin: 0.75em 0 0.25em; }
25
+ .bcm-p-muted { color: var(--bcm-p-muted); font-size: 0.9em; }
26
+ .bcm-p-note { border-left: 3px solid var(--bcm-p-accent); padding-left: 0.6em; }
27
+ .bcm-p-warning { border-left: 3px solid var(--bcm-p-danger); padding-left: 0.6em; }
28
+ .bcm-p-error { color: var(--bcm-p-danger); }
29
+ .bcm-p-sr { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
30
+ .bcm-p-btn {
31
+ font: inherit; cursor: pointer; border: 1px solid currentColor; background: transparent; color: inherit;
32
+ border-radius: var(--bcm-p-radius); padding: 0.45em 0.9em; margin: 0.2em 0.4em 0.2em 0;
33
+ }
34
+ .bcm-p-btn:disabled { opacity: 0.55; cursor: default; }
35
+ .bcm-p-primary { background: var(--bcm-p-accent); border-color: var(--bcm-p-accent); color: #fff; }
36
+ .bcm-p-danger { border-color: var(--bcm-p-danger); color: var(--bcm-p-danger); }
37
+ .bcm-p-link { font: inherit; background: none; border: 0; padding: 0.2em 0; color: var(--bcm-p-accent); cursor: pointer; text-decoration: underline; }
38
+ .bcm-p-icon { font: inherit; background: none; border: 0; cursor: pointer; font-size: 1.3em; line-height: 1; color: inherit; }
39
+ .bcm-p-input { font: inherit; padding: 0.45em; border: 1px solid var(--bcm-p-muted); border-radius: var(--bcm-p-radius); min-width: 10ch; }
40
+ .bcm-p-row { display: flex; gap: 0.5em; align-items: center; flex-wrap: wrap; }
41
+ .bcm-p-actions { display: flex; flex-wrap: wrap; gap: 0.25em; margin: 0.5em 0; }
42
+ .bcm-p-list, .bcm-p-menu { list-style: none; padding: 0; margin: 0.5em 0; }
43
+ .bcm-p-list > li { border-top: 1px solid color-mix(in srgb, var(--bcm-p-muted) 35%, transparent); padding: 0.5em 0; }
44
+ .bcm-p-unread { font-weight: 600; }
45
+ .bcm-p-badge { display: inline-block; font-size: 0.8em; border: 1px solid var(--bcm-p-muted); border-radius: 999px; padding: 0 0.5em; margin-left: 0.4em; }
46
+ .bcm-p-code { font-family: ui-monospace, monospace; font-size: 1.4em; letter-spacing: 0.15em; }
47
+ .bcm-p-check { display: flex; gap: 0.5em; align-items: flex-start; margin: 0.4em 0; }
48
+ .bcm-p-legal { margin: 0.75em 0; }
49
+ .bcm-p-legal-body { white-space: pre-wrap; max-height: 18em; overflow: auto; border: 1px solid color-mix(in srgb, var(--bcm-p-muted) 35%, transparent); border-radius: var(--bcm-p-radius); padding: 0.6em; }
50
+ .bcm-p-provider { margin-top: 1em; font-size: 0.85em; color: var(--bcm-p-muted); }
51
+ .bcm-p-provider-links a { margin-right: 0.8em; color: inherit; }
52
+ .bcm-p-dot { display: inline-block; width: 0.6em; height: 0.6em; border-radius: 50%; background: var(--bcm-p-danger); margin-left: 0.35em; vertical-align: middle; }
53
+ /* The dock: fixed to the bottom corner, windows beside the launcher. */
54
+ .bcm-p-dock { position: fixed; right: 16px; bottom: 16px; z-index: 2147483000; display: flex; align-items: flex-end; gap: 12px; }
55
+ .bcm-p-launcher {
56
+ font: inherit; cursor: pointer; border: 0; border-radius: 999px; padding: 0.7em 1.1em; background: var(--bcm-p-accent); color: #fff;
57
+ box-shadow: 0 4px 16px rgb(0 0 0 / 0.2); text-decoration: none;
58
+ }
59
+ .bcm-p-panel, .bcm-p-window {
60
+ background: var(--bcm-p-bg); border-radius: var(--bcm-p-radius); box-shadow: 0 8px 28px rgb(0 0 0 / 0.25);
61
+ width: 320px; max-height: 70vh; overflow: auto; padding: 0.8em;
62
+ }
63
+ .bcm-p-windows { display: flex; gap: 12px; align-items: flex-end; }
64
+ .bcm-p-window-head { display: flex; justify-content: space-between; align-items: center; gap: 0.5em; }
65
+ .bcm-p-window-head h2 { font-size: 1em; margin: 0; }
66
+ /* A phone: a window takes the whole screen. */
67
+ .bcm-p-mobile .bcm-p-window { position: fixed; inset: 0; width: auto; max-height: none; border-radius: 0; }
68
+ .bcm-p-mobile .bcm-p-panel { position: fixed; left: 8px; right: 8px; bottom: 72px; width: auto; }
69
+ /* The conversations (DM8b): the member's own messages to the right, search hits marked, the composer full width. */
70
+ .bcm-p-dm-messages > li { position: relative; padding-right: 2em; }
71
+ .bcm-p-dm-messages > li > .bcm-p-icon { position: absolute; top: 0.4em; right: 0; }
72
+ .bcm-p-dm-mine { text-align: right; }
73
+ .bcm-p-dm-mine .bcm-p-actions { justify-content: flex-end; }
74
+ .bcm-p-dm-hit { background: color-mix(in srgb, var(--bcm-p-accent) 14%, transparent); }
75
+ .bcm-p-dm-body { white-space: pre-wrap; overflow-wrap: anywhere; margin: 0.2em 0; }
76
+ .bcm-p-dm-meta { margin: 0; }
77
+ .bcm-p-dm-box { width: 100%; resize: vertical; }
78
+ .bcm-p-dm-sentence p { margin: 0.15em 0; }
79
+ .bcm-p-dm-href { overflow-wrap: anywhere; }
80
+ .bcm-p-dm-tabs { margin: 0.25em 0 0.75em; }
81
+ .bcm-p-dm-tabs [aria-current='page'] { font-weight: 600; text-decoration: none; }
@@ -0,0 +1,89 @@
1
+ /**
2
+ * What both private roots share: their props (attributes, and the `transport`, `client` and `push` properties), the
3
+ * setup from them, and the private-root mark on the host element (`markPrivateRoot`, §6.1, §7.1). The mark makes the
4
+ * host pause its analytics and RUM, so it is on only while the root may show something private: from the moment the
5
+ * host's `me/get` names a private block that is live or read_only and rendered here — before the private client is
6
+ * asked anything — through the private screens, a reload of them and a refusal said in their place. It is off for a
7
+ * visitor, on a dark instance, under `provider_page` (only a link to the account host), when the setup is refused and
8
+ * after a refusal no retry cures (FINAL_REFUSALS), and it goes with the element. A dock in the root layout is therefore unmarked on every page of a signed-out visitor.
9
+ */
10
+ import { computed, onBeforeUnmount, onMounted, shallowRef, useHost, watch, type ComputedRef, type ShallowRef } from 'vue'
11
+ import { markPrivateRoot, type PrivateClient } from '@blogmcp/sdk/community/private'
12
+ import type { CommunityTransport } from '@blogmcp/sdk/community'
13
+ import { documentLang, pickLang, type Lang } from '../i18n'
14
+ import { privateSetupFor, type PrivateSetup } from '../core/setup'
15
+ import type { PrivatePushPort } from '../core/push'
16
+ import type { PrivateRealtimeInput } from '../core/realtime'
17
+ import { isFinalFailure } from '../core/view'
18
+
19
+ export interface PrivateRootProps {
20
+ kbOrigin?: string
21
+ apiBase?: string
22
+ siteBase?: string
23
+ routes?: string
24
+ lang?: string
25
+ siteName?: string
26
+ swPath?: string
27
+ accountOrigin?: string
28
+ transport?: CommunityTransport
29
+ client?: PrivateClient
30
+ push?: PrivatePushPort | null
31
+ realtime?: PrivateRealtimeInput
32
+ }
33
+
34
+ export interface PrivateRoot {
35
+ setup: ShallowRef<PrivateSetup | null>
36
+ lang: ComputedRef<Lang>
37
+ }
38
+
39
+ export function usePrivateRoot(props: PrivateRootProps): PrivateRoot {
40
+ const host = useHost()
41
+ const setup = shallowRef<PrivateSetup | null>(null)
42
+ const lang = computed(() => pickLang(props.lang, documentLang()))
43
+ let unmark: (() => void) | null = null
44
+
45
+ function start(): void {
46
+ setup.value?.controller.dispose()
47
+ setup.value = privateSetupFor({
48
+ kbOrigin: props.kbOrigin,
49
+ apiBase: props.apiBase,
50
+ siteBase: props.siteBase,
51
+ routes: props.routes,
52
+ siteName: props.siteName,
53
+ swPath: props.swPath,
54
+ accountOrigin: props.accountOrigin,
55
+ transport: props.transport,
56
+ client: props.client,
57
+ ...(props.push !== undefined ? { push: props.push } : {}),
58
+ realtime: props.realtime ?? null,
59
+ })
60
+ void setup.value?.controller.load()
61
+ }
62
+
63
+ /** Whether something private may be on screen (see the module doc). */
64
+ const privateOnPage = computed(() => {
65
+ const s = setup.value?.controller.state
66
+ if (!s) return false
67
+ const b = s.block
68
+ const admitted = b !== null && (b.state === 'live' || b.state === 'read_only') && b.rendering !== 'provider_page'
69
+ // A refusal no retry cures (FINAL_REFUSALS) ends the private part of the page: the mark goes with it.
70
+ return admitted && (s.phase === 'ready' || s.phase === 'loading' || (s.phase === 'failed' && !isFinalFailure(s)))
71
+ })
72
+
73
+ onMounted(() => start())
74
+ watch(privateOnPage, (on) => {
75
+ if (on && !unmark && host) unmark = markPrivateRoot(host)
76
+ else if (!on && unmark) {
77
+ unmark()
78
+ unmark = null
79
+ }
80
+ })
81
+ // The attributes that change who or where the session is make a new controller; the language does not.
82
+ watch(() => [props.kbOrigin, props.apiBase, props.siteBase, props.routes, props.transport, props.client, props.realtime] as const, () => start())
83
+ onBeforeUnmount(() => {
84
+ setup.value?.controller.dispose()
85
+ unmark?.()
86
+ unmark = null
87
+ })
88
+ return { setup, lang }
89
+ }
@@ -0,0 +1,40 @@
1
+ /**
2
+ * The screens' words: catalogue keys turned into text in the element's language (i18n.ts), a controller's text
3
+ * reference (an `error:<CODE>` one is its refusal's sentence), and a date.
4
+ */
5
+ import { formatWhen, errorKey, privateMessage, type Lang, type PrivateParams } from '../i18n'
6
+ import type { PrivateTextRef } from '../core/view'
7
+
8
+ export interface ScreenText {
9
+ pt(key: string, params?: PrivateParams): string
10
+ refText(ref: PrivateTextRef | null): string
11
+ when(iso: string | null | undefined, withTime?: boolean): string
12
+ }
13
+
14
+ export function screenText(lang: Lang): ScreenText {
15
+ return {
16
+ pt: (key, params) => privateMessage(lang, key, params),
17
+ refText(ref) {
18
+ if (!ref) return ''
19
+ if (ref.msg.startsWith('error:')) return privateMessage(lang, errorKey(ref.msg.slice('error:'.length)))
20
+ return privateMessage(lang, ref.msg, ref.params)
21
+ },
22
+ when: (iso, withTime = true) => formatWhen(lang, iso, withTime),
23
+ }
24
+ }
25
+
26
+ /** An https address kb-api handed over (the address link, the provider's links), or null: nothing else is a link. */
27
+ export function httpsHref(value: string | null | undefined): string | null {
28
+ return typeof value === 'string' && /^https:\/\/[^\s/?#]+(?:[/?#][^\s]*)?$/i.test(value) ? value : null
29
+ }
30
+
31
+ /**
32
+ * A served text with the session's placeholders filled from `display` (§5.11: `{{device_label}}`, `{{approval_email}}`
33
+ * — masked on this screen, B-63 —, `{{accepted_at}}`); the text stays text.
34
+ */
35
+ export function fillDisplay(body: string, display: { approvalEmail: string | null; deviceLabel: string } | null | undefined, acceptedAt: string): string {
36
+ return body
37
+ .replace(/\{\{device_label\}\}/g, display?.deviceLabel ?? '')
38
+ .replace(/\{\{approval_email\}\}/g, display?.approvalEmail ?? '')
39
+ .replace(/\{\{accepted_at\}\}/g, acceptedAt)
40
+ }
@@ -0,0 +1,87 @@
1
+ /**
2
+ * `<KbChatDock>` and `<KbPrivateView>` for Vue / Nuxt (m4-contracts.md §6.2): each mounts its element on the client,
3
+ * after hydration, and removes it on unmount; each renders one empty `<div>` as the mount point. The element's module
4
+ * (which imports the browser-only `@blogmcp/sdk/community/private`) is imported lazily, so a server render never
5
+ * loads it. `<KbChatDock>` belongs in the root layout: a client-side navigation then keeps its session and windows,
6
+ * and `hidden-by-host` hides it on a page without unmounting it.
7
+ */
8
+ import { defineComponent, h, onBeforeUnmount, onMounted, ref, watch, type PropType } from 'vue'
9
+ import type { CommunityTransport } from '@blogmcp/sdk/community'
10
+ import type { PrivateClient } from '@blogmcp/sdk/community/private'
11
+ import type { CommunityRoutes } from '../core/attrs'
12
+ import type { PrivateDockRealtime } from './core/dock'
13
+ import type { MountChatDockOptions, MountPrivateViewOptions } from './mount'
14
+
15
+ const HIDDEN_BY_HOST = 'hidden-by-host'
16
+
17
+ function defined<T extends object>(o: T): T {
18
+ const out = {} as T
19
+ for (const [k, v] of Object.entries(o)) if (v !== undefined && v !== null) (out as Record<string, unknown>)[k] = v
20
+ return out
21
+ }
22
+
23
+ const commonProps = {
24
+ kbOrigin: { type: String, required: true },
25
+ apiBase: String,
26
+ siteBase: String,
27
+ lang: String,
28
+ routes: Object as PropType<CommunityRoutes>,
29
+ siteName: String,
30
+ swPath: String,
31
+ accountOrigin: String,
32
+ nonce: String,
33
+ transport: Function as PropType<CommunityTransport>,
34
+ client: Object as PropType<PrivateClient>,
35
+ /** The live inbox: the SDK's `createPrivateRealtime`, or a source of the host's own (`{ subscribe }`). */
36
+ realtime: [Function, Object] as PropType<PrivateDockRealtime>,
37
+ } as const
38
+
39
+ function useMount<O extends object>(kind: string, options: () => O, load: () => Promise<(o: O & { container: Element }) => () => void>, after?: (el: Element | null) => void) {
40
+ const host = ref<HTMLElement | null>(null)
41
+ let unmount: (() => void) | null = null
42
+ let gone = false
43
+ let ticket = 0
44
+ async function mount(): Promise<void> {
45
+ const mine = ++ticket
46
+ const mountFn = await load()
47
+ if (gone || mine !== ticket || !host.value) return
48
+ unmount?.()
49
+ unmount = mountFn({ ...options(), container: host.value })
50
+ after?.(host.value.firstElementChild)
51
+ }
52
+ onMounted(() => void mount())
53
+ watch(
54
+ () => JSON.stringify(options(), (_k, v: unknown) => (typeof v === 'function' ? '[fn]' : v)),
55
+ () => void mount(),
56
+ )
57
+ onBeforeUnmount(() => {
58
+ gone = true
59
+ unmount?.()
60
+ unmount = null
61
+ })
62
+ return host
63
+ }
64
+
65
+ export const KbChatDock = defineComponent({
66
+ name: 'KbChatDock',
67
+ props: { ...commonProps, hiddenByHost: Boolean },
68
+ setup(props) {
69
+ // `hiddenByHost` is applied to the element in place (never by mounting it anew): that is the point of the attribute.
70
+ const opts = (): MountChatDockOptions => defined({ ...props, hiddenByHost: undefined }) as MountChatDockOptions
71
+ const host = useMount('chat-dock', opts, async () => (await import('./mount')).mountChatDock, (el) => el?.toggleAttribute(HIDDEN_BY_HOST, props.hiddenByHost))
72
+ watch(
73
+ () => props.hiddenByHost,
74
+ (hidden) => host.value?.firstElementChild?.toggleAttribute(HIDDEN_BY_HOST, hidden),
75
+ )
76
+ return () => h('div', { ref: host, 'data-blogmcp-community': 'chat-dock' })
77
+ },
78
+ })
79
+
80
+ export const KbPrivateView = defineComponent({
81
+ name: 'KbPrivateView',
82
+ props: { ...commonProps, screen: String },
83
+ setup(props) {
84
+ const host = useMount('private-view', () => defined({ ...props }) as MountPrivateViewOptions, async () => (await import('./mount')).mountPrivateView)
85
+ return () => h('div', { ref: host, 'data-blogmcp-community': 'private-view' })
86
+ },
87
+ })