@alepha/ui 0.35.0 → 0.36.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.
Files changed (264) hide show
  1. package/README.md +54 -1
  2. package/dist/account/AccountBrand.d.ts +10 -3
  3. package/dist/account/AccountBrand.d.ts.map +1 -1
  4. package/dist/account/AccountBrand.js +12 -6
  5. package/dist/account/AccountBrand.js.map +1 -1
  6. package/dist/account/AccountLayout.d.ts.map +1 -1
  7. package/dist/account/AccountLayout.js +1 -1
  8. package/dist/account/AccountLayout.js.map +1 -1
  9. package/dist/admin/AdminUserDetail.d.ts +3 -3
  10. package/dist/admin/AdminUserDetail.js +3 -3
  11. package/dist/admin/AdminUserDetail.js.map +1 -1
  12. package/dist/ai/AiChat.d.ts +36 -6
  13. package/dist/ai/AiChat.d.ts.map +1 -1
  14. package/dist/ai/AiChat.js +101 -63
  15. package/dist/ai/AiChat.js.map +1 -1
  16. package/dist/ai/AiChatComposer.d.ts +22 -0
  17. package/dist/ai/AiChatComposer.d.ts.map +1 -0
  18. package/dist/ai/AiChatComposer.js +75 -0
  19. package/dist/ai/AiChatComposer.js.map +1 -0
  20. package/dist/ai/AiChatConfirmCard.d.ts +5 -2
  21. package/dist/ai/AiChatConfirmCard.d.ts.map +1 -1
  22. package/dist/ai/AiChatConfirmCard.js +7 -6
  23. package/dist/ai/AiChatConfirmCard.js.map +1 -1
  24. package/dist/ai/AiChatConfirmFields.d.ts +9 -0
  25. package/dist/ai/AiChatConfirmFields.d.ts.map +1 -0
  26. package/dist/ai/AiChatConfirmFields.js +45 -0
  27. package/dist/ai/AiChatConfirmFields.js.map +1 -0
  28. package/dist/ai/AiChatEmpty.d.ts +26 -0
  29. package/dist/ai/AiChatEmpty.d.ts.map +1 -0
  30. package/dist/ai/AiChatEmpty.js +47 -0
  31. package/dist/ai/AiChatEmpty.js.map +1 -0
  32. package/dist/ai/AiChatHistory.d.ts +3 -2
  33. package/dist/ai/AiChatHistory.d.ts.map +1 -1
  34. package/dist/ai/AiChatHistory.js +51 -22
  35. package/dist/ai/AiChatHistory.js.map +1 -1
  36. package/dist/ai/AiChatMessage.d.ts +14 -0
  37. package/dist/ai/AiChatMessage.d.ts.map +1 -1
  38. package/dist/ai/AiChatMessage.js +24 -12
  39. package/dist/ai/AiChatMessage.js.map +1 -1
  40. package/dist/ai/index.d.ts +3 -0
  41. package/dist/ai/index.d.ts.map +1 -1
  42. package/dist/ai/index.js +4 -1
  43. package/dist/ai/useAiChat.d.ts +9 -0
  44. package/dist/ai/useAiChat.d.ts.map +1 -1
  45. package/dist/ai/useAiChat.js +21 -3
  46. package/dist/ai/useAiChat.js.map +1 -1
  47. package/dist/core/Sidebar.js +2 -2
  48. package/dist/core/Sidebar.js.map +1 -1
  49. package/dist/core/index.d.ts +1 -0
  50. package/dist/core/index.d.ts.map +1 -1
  51. package/dist/core/index.js +2 -1
  52. package/dist/core/useToast.d.ts +22 -1
  53. package/dist/core/useToast.d.ts.map +1 -1
  54. package/dist/core/useToast.js +4 -1
  55. package/dist/core/useToast.js.map +1 -1
  56. package/dist/core/useUndoable.d.ts +93 -0
  57. package/dist/core/useUndoable.d.ts.map +1 -0
  58. package/dist/core/useUndoable.js +172 -0
  59. package/dist/core/useUndoable.js.map +1 -0
  60. package/dist/i18n/fr/uiFr.d.ts.map +1 -1
  61. package/dist/i18n/fr/uiFr.js +51 -1
  62. package/dist/i18n/fr/uiFr.js.map +1 -1
  63. package/dist/markdown/MarkdownView.d.ts +15 -0
  64. package/dist/markdown/MarkdownView.d.ts.map +1 -1
  65. package/dist/markdown/MarkdownView.js +4 -1
  66. package/dist/markdown/MarkdownView.js.map +1 -1
  67. package/dist/organizations/useInviteOrganizationMember.d.ts +2 -2
  68. package/dist/organizations/useInviteOrganizationMember.js.map +1 -1
  69. package/dist/shell/AppShellSidebarNavItem.d.ts.map +1 -1
  70. package/dist/shell/AppShellSidebarNavItem.js +25 -2
  71. package/dist/shell/AppShellSidebarNavItem.js.map +1 -1
  72. package/dist/shell/PlateTabBar.d.ts.map +1 -1
  73. package/dist/shell/PlateTabBar.js +2 -2
  74. package/dist/shell/PlateTabBar.js.map +1 -1
  75. package/dist/shell/SidebarBackHeader.d.ts +35 -0
  76. package/dist/shell/SidebarBackHeader.d.ts.map +1 -0
  77. package/dist/shell/SidebarBackHeader.js +34 -0
  78. package/dist/shell/SidebarBackHeader.js.map +1 -0
  79. package/dist/shell/appShellNav.d.ts +30 -0
  80. package/dist/shell/appShellNav.d.ts.map +1 -1
  81. package/dist/shell/appShellNav.js.map +1 -1
  82. package/dist/shell/index.d.ts +2 -1
  83. package/dist/shell/index.d.ts.map +1 -1
  84. package/dist/shell/index.js +2 -1
  85. package/dist/swagger/$uiSwagger.d.ts +75 -0
  86. package/dist/swagger/$uiSwagger.d.ts.map +1 -0
  87. package/dist/swagger/$uiSwagger.js +51 -0
  88. package/dist/swagger/$uiSwagger.js.map +1 -0
  89. package/dist/swagger/OpenApiSpec.d.ts +113 -0
  90. package/dist/swagger/OpenApiSpec.d.ts.map +1 -0
  91. package/dist/swagger/OpenApiSpec.js +186 -0
  92. package/dist/swagger/OpenApiSpec.js.map +1 -0
  93. package/dist/swagger/SwaggerAccessBadge.d.ts +16 -0
  94. package/dist/swagger/SwaggerAccessBadge.d.ts.map +1 -0
  95. package/dist/swagger/SwaggerAccessBadge.js +40 -0
  96. package/dist/swagger/SwaggerAccessBadge.js.map +1 -0
  97. package/dist/swagger/SwaggerAlephaMeta.d.ts +11 -0
  98. package/dist/swagger/SwaggerAlephaMeta.d.ts.map +1 -0
  99. package/dist/swagger/SwaggerAlephaMeta.js +103 -0
  100. package/dist/swagger/SwaggerAlephaMeta.js.map +1 -0
  101. package/dist/swagger/SwaggerCredentials.d.ts +11 -0
  102. package/dist/swagger/SwaggerCredentials.d.ts.map +1 -0
  103. package/dist/swagger/SwaggerCredentials.js +35 -0
  104. package/dist/swagger/SwaggerCredentials.js.map +1 -0
  105. package/dist/swagger/SwaggerExplorer.d.ts +31 -0
  106. package/dist/swagger/SwaggerExplorer.d.ts.map +1 -0
  107. package/dist/swagger/SwaggerExplorer.js +156 -0
  108. package/dist/swagger/SwaggerExplorer.js.map +1 -0
  109. package/dist/swagger/SwaggerMethodBadge.d.ts +10 -0
  110. package/dist/swagger/SwaggerMethodBadge.d.ts.map +1 -0
  111. package/dist/swagger/SwaggerMethodBadge.js +27 -0
  112. package/dist/swagger/SwaggerMethodBadge.js.map +1 -0
  113. package/dist/swagger/SwaggerNav.d.ts +16 -0
  114. package/dist/swagger/SwaggerNav.d.ts.map +1 -0
  115. package/dist/swagger/SwaggerNav.js +63 -0
  116. package/dist/swagger/SwaggerNav.js.map +1 -0
  117. package/dist/swagger/SwaggerOperationView.d.ts +22 -0
  118. package/dist/swagger/SwaggerOperationView.d.ts.map +1 -0
  119. package/dist/swagger/SwaggerOperationView.js +89 -0
  120. package/dist/swagger/SwaggerOperationView.js.map +1 -0
  121. package/dist/swagger/SwaggerParameters.d.ts +11 -0
  122. package/dist/swagger/SwaggerParameters.d.ts.map +1 -0
  123. package/dist/swagger/SwaggerParameters.js +80 -0
  124. package/dist/swagger/SwaggerParameters.js.map +1 -0
  125. package/dist/swagger/SwaggerRequestBuilder.d.ts +83 -0
  126. package/dist/swagger/SwaggerRequestBuilder.d.ts.map +1 -0
  127. package/dist/swagger/SwaggerRequestBuilder.js +231 -0
  128. package/dist/swagger/SwaggerRequestBuilder.js.map +1 -0
  129. package/dist/swagger/SwaggerResponseView.d.ts +25 -0
  130. package/dist/swagger/SwaggerResponseView.d.ts.map +1 -0
  131. package/dist/swagger/SwaggerResponseView.js +94 -0
  132. package/dist/swagger/SwaggerResponseView.js.map +1 -0
  133. package/dist/swagger/SwaggerResponses.d.ts +12 -0
  134. package/dist/swagger/SwaggerResponses.d.ts.map +1 -0
  135. package/dist/swagger/SwaggerResponses.js +75 -0
  136. package/dist/swagger/SwaggerResponses.js.map +1 -0
  137. package/dist/swagger/SwaggerSchemaView.d.ts +19 -0
  138. package/dist/swagger/SwaggerSchemaView.d.ts.map +1 -0
  139. package/dist/swagger/SwaggerSchemaView.js +98 -0
  140. package/dist/swagger/SwaggerSchemaView.js.map +1 -0
  141. package/dist/swagger/SwaggerSection.d.ts +14 -0
  142. package/dist/swagger/SwaggerSection.d.ts.map +1 -0
  143. package/dist/swagger/SwaggerSection.js +23 -0
  144. package/dist/swagger/SwaggerSection.js.map +1 -0
  145. package/dist/swagger/SwaggerTryIt.d.ts +31 -0
  146. package/dist/swagger/SwaggerTryIt.d.ts.map +1 -0
  147. package/dist/swagger/SwaggerTryIt.js +68 -0
  148. package/dist/swagger/SwaggerTryIt.js.map +1 -0
  149. package/dist/swagger/SwaggerTryItForm.d.ts +20 -0
  150. package/dist/swagger/SwaggerTryItForm.d.ts.map +1 -0
  151. package/dist/swagger/SwaggerTryItForm.js +52 -0
  152. package/dist/swagger/SwaggerTryItForm.js.map +1 -0
  153. package/dist/swagger/index.d.ts +29 -0
  154. package/dist/swagger/index.d.ts.map +1 -0
  155. package/dist/swagger/index.js +18 -0
  156. package/dist/swagger/useSwaggerAccess.d.ts +24 -0
  157. package/dist/swagger/useSwaggerAccess.d.ts.map +1 -0
  158. package/dist/swagger/useSwaggerAccess.js +33 -0
  159. package/dist/swagger/useSwaggerAccess.js.map +1 -0
  160. package/dist/table/DataTable.d.ts.map +1 -1
  161. package/dist/table/DataTable.js +50 -13
  162. package/dist/table/DataTable.js.map +1 -1
  163. package/dist/table/DataTableBody.d.ts +18 -1
  164. package/dist/table/DataTableBody.d.ts.map +1 -1
  165. package/dist/table/DataTableBody.js +83 -58
  166. package/dist/table/DataTableBody.js.map +1 -1
  167. package/dist/table/DataTableRowActionsInline.d.ts +17 -0
  168. package/dist/table/DataTableRowActionsInline.d.ts.map +1 -0
  169. package/dist/table/DataTableRowActionsInline.js +45 -0
  170. package/dist/table/DataTableRowActionsInline.js.map +1 -0
  171. package/dist/table/PermissionMatrix.d.ts +4 -4
  172. package/dist/table/PermissionMatrix.js +2 -2
  173. package/dist/table/PermissionMatrix.js.map +1 -1
  174. package/dist/table/dataTableBaseProps.d.ts +31 -0
  175. package/dist/table/dataTableBaseProps.d.ts.map +1 -1
  176. package/dist/table/dataTableTypes.d.ts +16 -2
  177. package/dist/table/dataTableTypes.d.ts.map +1 -1
  178. package/dist/table/useDataTableData.d.ts +3 -0
  179. package/dist/table/useDataTableData.d.ts.map +1 -1
  180. package/dist/table/useDataTableData.js +9 -2
  181. package/dist/table/useDataTableData.js.map +1 -1
  182. package/dist/table/useDataTableRefresh.d.ts +1 -0
  183. package/dist/table/useDataTableRefresh.d.ts.map +1 -1
  184. package/dist/table/useDataTableRefresh.js +13 -1
  185. package/dist/table/useDataTableRefresh.js.map +1 -1
  186. package/dist/table/useOptimisticRows.d.ts +64 -0
  187. package/dist/table/useOptimisticRows.d.ts.map +1 -0
  188. package/dist/table/useOptimisticRows.js +185 -0
  189. package/dist/table/useOptimisticRows.js.map +1 -0
  190. package/dist/table/useRowMotion.d.ts +27 -0
  191. package/dist/table/useRowMotion.d.ts.map +1 -0
  192. package/dist/table/useRowMotion.js +112 -0
  193. package/dist/table/useRowMotion.js.map +1 -0
  194. package/docs/packages-alepha-ui-core.md +1 -0
  195. package/docs/packages-alepha-ui-overview.md +53 -1
  196. package/docs/packages-alepha-ui-swagger.md +35 -0
  197. package/package.json +11 -3
  198. package/src/account/AccountBrand.tsx +25 -6
  199. package/src/account/AccountLayout.tsx +5 -1
  200. package/src/admin/AdminUserDetail.tsx +3 -3
  201. package/src/admin/__tests__/AdminUserDetail.spec.ts +2 -2
  202. package/src/ai/AiChat.tsx +160 -82
  203. package/src/ai/AiChatComposer.tsx +117 -0
  204. package/src/ai/AiChatConfirmCard.tsx +7 -5
  205. package/src/ai/AiChatConfirmFields.tsx +65 -0
  206. package/src/ai/AiChatEmpty.tsx +73 -0
  207. package/src/ai/AiChatHistory.tsx +64 -28
  208. package/src/ai/AiChatMessage.tsx +47 -15
  209. package/src/ai/__tests__/AiChat.browser.spec.tsx +156 -5
  210. package/src/ai/index.ts +3 -0
  211. package/src/ai/useAiChat.ts +35 -0
  212. package/src/core/Sidebar.tsx +2 -2
  213. package/src/core/__tests__/useUndoable.browser.spec.tsx +201 -0
  214. package/src/core/index.ts +7 -0
  215. package/src/core/useToast.tsx +30 -2
  216. package/src/core/useUndoable.tsx +285 -0
  217. package/src/form/__tests__/ControlChoices.browser.spec.tsx +2 -1
  218. package/src/i18n/fr/uiFr.ts +53 -0
  219. package/src/markdown/MarkdownView.tsx +27 -9
  220. package/src/organizations/useInviteOrganizationMember.ts +2 -2
  221. package/src/shell/AppShellSidebarNavItem.tsx +51 -1
  222. package/src/shell/PlateTabBar.tsx +8 -2
  223. package/src/shell/SidebarBackHeader.tsx +58 -0
  224. package/src/shell/appShellNav.tsx +31 -0
  225. package/src/shell/index.ts +5 -1
  226. package/src/swagger/$uiSwagger.tsx +95 -0
  227. package/src/swagger/OpenApiSpec.ts +300 -0
  228. package/src/swagger/SwaggerAccessBadge.tsx +64 -0
  229. package/src/swagger/SwaggerAlephaMeta.tsx +141 -0
  230. package/src/swagger/SwaggerCredentials.tsx +44 -0
  231. package/src/swagger/SwaggerExplorer.tsx +208 -0
  232. package/src/swagger/SwaggerMethodBadge.tsx +32 -0
  233. package/src/swagger/SwaggerNav.tsx +92 -0
  234. package/src/swagger/SwaggerOperationView.tsx +102 -0
  235. package/src/swagger/SwaggerParameters.tsx +74 -0
  236. package/src/swagger/SwaggerRequestBuilder.ts +331 -0
  237. package/src/swagger/SwaggerResponseView.tsx +107 -0
  238. package/src/swagger/SwaggerResponses.tsx +83 -0
  239. package/src/swagger/SwaggerSchemaView.tsx +134 -0
  240. package/src/swagger/SwaggerSection.tsx +29 -0
  241. package/src/swagger/SwaggerTryIt.tsx +95 -0
  242. package/src/swagger/SwaggerTryItForm.tsx +87 -0
  243. package/src/swagger/__tests__/$uiSwagger.spec.ts +52 -0
  244. package/src/swagger/__tests__/OpenApiSpec.spec.ts +139 -0
  245. package/src/swagger/__tests__/SwaggerAccess.browser.spec.tsx +124 -0
  246. package/src/swagger/__tests__/SwaggerOperationView.browser.spec.tsx +194 -0
  247. package/src/swagger/__tests__/SwaggerRequestBuilder.spec.ts +250 -0
  248. package/src/swagger/index.ts +75 -0
  249. package/src/swagger/useSwaggerAccess.ts +59 -0
  250. package/src/table/DataTable.tsx +85 -34
  251. package/src/table/DataTableBody.tsx +51 -7
  252. package/src/table/DataTableRowActionsInline.tsx +80 -0
  253. package/src/table/PermissionMatrix.tsx +4 -4
  254. package/src/table/__tests__/DataTableEmptyState.browser.spec.tsx +1 -1
  255. package/src/table/__tests__/DataTableListMode.browser.spec.tsx +174 -0
  256. package/src/table/__tests__/DataTableOptimistic.browser.spec.tsx +173 -0
  257. package/src/table/__tests__/PermissionMatrix.browser.spec.tsx +2 -2
  258. package/src/table/__tests__/queryFilters.spec.ts +10 -12
  259. package/src/table/dataTableBaseProps.ts +31 -0
  260. package/src/table/dataTableTypes.ts +16 -2
  261. package/src/table/useDataTableData.ts +18 -2
  262. package/src/table/useDataTableRefresh.ts +26 -2
  263. package/src/table/useOptimisticRows.ts +265 -0
  264. package/src/table/useRowMotion.ts +152 -0
@@ -1,7 +1,9 @@
1
1
  import { useAuth } from "alepha/react/auth";
2
2
  import { useI18n } from "alepha/react/i18n";
3
+ import { useRouter } from "alepha/react/router";
3
4
 
4
5
  import { UserAvatar } from "../core/UserAvatar.tsx";
6
+ import { SidebarBackHeader } from "../shell/SidebarBackHeader.tsx";
5
7
 
6
8
  export interface AccountBrandProps {
7
9
  /**
@@ -9,12 +11,19 @@ export interface AccountBrandProps {
9
11
  * says whose area this is and what it is.
10
12
  */
11
13
  subtitle?: string;
14
+ /**
15
+ * Route name the header's back arrow leads to: the application's home,
16
+ * since the account shell hides the application's own chrome.
17
+ *
18
+ * @default "home"
19
+ */
20
+ homeRouteName?: string;
12
21
  }
13
22
 
14
23
  /**
15
- * The account sidebar's default header: the signed-in user's avatar and
16
- * display name, collapsing to the avatar alone when the sidebar folds to its
17
- * icon rail.
24
+ * The account sidebar's default header: a back arrow home, then the
25
+ * signed-in user's avatar and display name, collapsing to the arrow alone
26
+ * when the sidebar folds to its icon rail.
18
27
  *
19
28
  * The account area is the user's own, so the header says whose it is rather
20
29
  * than repeating the application's name. An application that wants its own
@@ -28,13 +37,23 @@ export interface AccountBrandProps {
28
37
  export const AccountBrand = (props: AccountBrandProps) => {
29
38
  const auth = useAuth();
30
39
  const { tr } = useI18n();
40
+ const router = useRouter<any>();
41
+ // `router.path` throws on a name the application never declared, and an
42
+ // application without a home page has nowhere for the arrow to go.
43
+ const homeName = props.homeRouteName ?? "home";
44
+ const home = router.pages.some((page) => page.name === homeName)
45
+ ? homeName
46
+ : undefined;
31
47
  const user = auth.user;
32
48
  const name = user?.name ?? user?.username ?? user?.email ?? "";
33
49
 
34
50
  return (
35
- <div className="flex min-w-0 items-center gap-2 px-2 py-1.5 group-data-[collapsible=icon]:justify-center group-data-[collapsible=icon]:gap-0 group-data-[collapsible=icon]:px-0">
51
+ <SidebarBackHeader
52
+ href={home ? router.path(home) : undefined}
53
+ label={tr("account.nav.backToSite", { default: "Back to site" })}
54
+ >
36
55
  <UserAvatar fileId={user?.picture} className="size-7" alt={name} />
37
- <div className="flex min-w-0 flex-col group-data-[collapsible=icon]:hidden">
56
+ <div className="flex min-w-0 flex-col">
38
57
  <span
39
58
  className="truncate text-sm font-semibold tracking-tight"
40
59
  data-testid="account-brand-name"
@@ -46,6 +65,6 @@ export const AccountBrand = (props: AccountBrandProps) => {
46
65
  tr("account.brand.subtitle", { default: "Account" })}
47
66
  </span>
48
67
  </div>
49
- </div>
68
+ </SidebarBackHeader>
50
69
  );
51
70
  };
@@ -53,7 +53,11 @@ export const AccountLayout = () => {
53
53
  variant="floating"
54
54
  fill
55
55
  extraNav={options.extraNav}
56
- brand={options.brand ?? <AccountBrand />}
56
+ brand={
57
+ options.brand ?? (
58
+ <AccountBrand homeRouteName={options.homeRouteName} />
59
+ )
60
+ }
57
61
  topbarActions={
58
62
  <div className="flex items-center gap-1">
59
63
  <button
@@ -74,9 +74,9 @@ type TabKey = "overview" | "security" | "sessions" | "audits";
74
74
  * `AdminRouter`'s own route declares) and falling back to `:id` (the name
75
75
  * this component read exclusively before `AdminRouter` existed).
76
76
  *
77
- * The fallback exists for `~/git/club/apps/platform`, which vendors
78
- * `@alepha/ui` and declares its own user-detail route as `/users/:id` against
79
- * this same component. Without it, that application's next upgrade of
77
+ * The fallback exists for downstream apps that vendor `@alepha/ui`
78
+ * and declare their own user-detail route as `/users/:id` against
79
+ * this same component. Without it, such an application's next upgrade of
80
80
  * `@alepha/ui` would resolve `userId` to `undefined`, fall through to the
81
81
  * empty string, and load `AdminUserDetail` with no id at all instead of
82
82
  * failing to build.
@@ -3,8 +3,8 @@ import { describe, expect, it } from "vitest";
3
3
  import { resolveUserDetailId } from "../AdminUserDetail.tsx";
4
4
 
5
5
  /**
6
- * `resolveUserDetailId` is the whole reason `~/git/club/apps/platform` keeps
7
- * working across a vendored `@alepha/ui` upgrade: it declares its user-detail
6
+ * `resolveUserDetailId` is what keeps a downstream app working across
7
+ * a vendored `@alepha/ui` upgrade when it declares its user-detail
8
8
  * route as `/users/:id` against this same component, while `AdminRouter`'s
9
9
  * own route declares `:userId`. Losing the `:id` fallback would resolve an
10
10
  * empty id for that application instead of failing loudly.
package/src/ai/AiChat.tsx CHANGED
@@ -1,22 +1,28 @@
1
- import { useForm } from "alepha/react/form";
2
1
  import { useI18n } from "alepha/react/i18n";
2
+ import { SquarePen, Sparkles, X } from "lucide-react";
3
+ import { type ComponentType, useEffect, useRef } from "react";
3
4
 
4
5
  import { Alert, AlertDescription } from "../core/Alert.tsx";
5
6
  import { Button } from "../core/Button.tsx";
6
7
  import {
7
8
  Drawer,
9
+ DrawerClose,
8
10
  DrawerContent,
9
- DrawerFooter,
10
- DrawerHeader,
11
11
  DrawerTitle,
12
12
  } from "../core/Drawer.tsx";
13
13
  import { Spinner } from "../core/Spinner.tsx";
14
- import { Control } from "../form/Control.tsx";
15
- import { MarkdownView } from "../markdown/MarkdownView.tsx";
16
- import { AiChatConfirmCard } from "./AiChatConfirmCard.tsx";
14
+ import {
15
+ type MarkdownLinkRenderer,
16
+ MarkdownView,
17
+ } from "../markdown/MarkdownView.tsx";
18
+ import { AiChatComposer } from "./AiChatComposer.tsx";
19
+ import {
20
+ AiChatConfirmCard,
21
+ type AiChatConfirmCardProps,
22
+ } from "./AiChatConfirmCard.tsx";
23
+ import { AiChatEmpty, type AiChatSuggestion } from "./AiChatEmpty.tsx";
17
24
  import { AiChatHistory } from "./AiChatHistory.tsx";
18
- import { AiChatMessage } from "./AiChatMessage.tsx";
19
- import { aiChatMessageFormSchema } from "./aiChatMessageFormSchema.ts";
25
+ import { AiChatMessage, type AiChatToolLabel } from "./AiChatMessage.tsx";
20
26
  import { type UseAiChatOptions, useAiChat } from "./useAiChat.ts";
21
27
 
22
28
  export interface AiChatProps extends UseAiChatOptions {
@@ -27,16 +33,37 @@ export interface AiChatProps extends UseAiChatOptions {
27
33
  */
28
34
  title?: string;
29
35
  /**
30
- * Show the list of past conversations. Default true.
36
+ * Offer the list of past conversations, in a header menu. Default true.
31
37
  */
32
38
  history?: boolean;
39
+ /**
40
+ * Questions a new conversation offers to start from.
41
+ */
42
+ suggestions?: AiChatSuggestion[];
43
+ /**
44
+ * Names each tool step in the user's words ("Searched clients"), running
45
+ * and done. Without it a step shows the tool's own name.
46
+ */
47
+ toolLabel?: AiChatToolLabel;
48
+ /**
49
+ * A confirmation card per tool name, in place of the generic field list:
50
+ * an app shows a booking as a booking. It gets the pending call as the
51
+ * server stored it, and the approve and deny callbacks.
52
+ */
53
+ confirmCards?: Record<string, ComponentType<AiChatConfirmCardProps>>;
54
+ /**
55
+ * How links in the assistant's replies render (`MarkdownView`'s
56
+ * `renderLink`): route the paths the app knows, leave the rest as text.
57
+ */
58
+ renderLink?: MarkdownLinkRenderer;
33
59
  }
34
60
 
35
61
  /**
36
- * The assistant in a drawer: streamed markdown replies, the conversation
37
- * history, and a confirmation card for every mutation the assistant wants to
38
- * run. It knows the page through `useAiContext()`, and runs `navigate` and
39
- * `fillForm` for the model.
62
+ * The assistant in a drawer: a quiet header (history menu, new
63
+ * conversation, close), the conversation with its tool steps and a
64
+ * confirmation card for every mutation the assistant wants to run, and a
65
+ * composer that sends on Enter and stops a running turn. It knows the page
66
+ * through `useAiContext()`, and runs `navigate` and `fillForm` for the model.
40
67
  *
41
68
  * Needs `AlephaApiAi` on the server and a `$ai` declared by the app.
42
69
  *
@@ -44,22 +71,39 @@ export interface AiChatProps extends UseAiChatOptions {
44
71
  * ```tsx
45
72
  * const [open, setOpen] = useState(false);
46
73
  * <Button onClick={() => setOpen(true)}>Ask</Button>
47
- * <AiChat open={open} onOpenChange={setOpen} />
74
+ * <AiChat
75
+ * open={open}
76
+ * onOpenChange={setOpen}
77
+ * suggestions={[{ label: "Who still owes today?" }]}
78
+ * />
48
79
  * ```
49
80
  */
50
81
  export const AiChat = (props: AiChatProps) => {
51
82
  const { tr } = useI18n();
52
83
  const chat = useAiChat(props);
53
- // No deps: `chat.send` is a new function every render, and a dep change
54
- // rebuilds an empty form, dropping what was typed before Send. `useForm`
55
- // keeps the handler fresh on every commit anyway (#Q2934).
56
- const form = useForm({
57
- schema: aiChatMessageFormSchema,
58
- handler: async (values) => {
59
- form.reset();
60
- await chat.send(values.message);
61
- },
62
- });
84
+ const thread = useRef<HTMLDivElement>(null);
85
+ const busy = chat.loading && !chat.pending;
86
+ const empty = chat.items.length === 0 && !chat.streaming && !busy;
87
+
88
+ // Keep the newest line in view as the conversation grows.
89
+ useEffect(() => {
90
+ const node = thread.current;
91
+ if (node) {
92
+ node.scrollTop = node.scrollHeight;
93
+ }
94
+ }, [chat.items.length, chat.streaming, chat.activity, chat.pending]);
95
+
96
+ const running = chat.activity
97
+ ? (props.toolLabel?.(chat.activity, "running") ??
98
+ tr("ai.chat.running", {
99
+ default: "Running $1",
100
+ args: [chat.activity],
101
+ }))
102
+ : undefined;
103
+
104
+ const Card = chat.pending
105
+ ? (props.confirmCards?.[chat.pending.name] ?? AiChatConfirmCard)
106
+ : undefined;
63
107
 
64
108
  return (
65
109
  <Drawer
@@ -69,14 +113,15 @@ export const AiChat = (props: AiChatProps) => {
69
113
  >
70
114
  <DrawerContent
71
115
  data-testid="ai-chat"
72
- className="flex h-full w-full flex-col sm:max-w-md"
116
+ // From `sm` up the panel floats: a gap from the page's edges, every
117
+ // corner rounded, a shadow, and no bleed strip past its edge.
118
+ className="flex h-full w-full flex-col gap-0 p-0 sm:h-[calc(100dvh-1.5rem)] sm:max-w-md sm:[--drawer-inset:0.75rem] sm:after:hidden sm:data-[swipe-direction=right]:rounded-2xl sm:data-[swipe-direction=right]:border sm:data-[swipe-direction=right]:shadow-2xl"
73
119
  >
74
- <DrawerHeader>
75
- <DrawerTitle>
120
+ <div className="border-border flex items-center gap-2 border-b py-2.5 pr-2.5 pl-4">
121
+ <Sparkles className="text-primary size-4 shrink-0" />
122
+ <DrawerTitle className="flex-1 truncate text-base font-medium">
76
123
  {props.title ?? tr("ai.chat.title", { default: "Assistant" })}
77
124
  </DrawerTitle>
78
- </DrawerHeader>
79
- <div className="flex min-h-0 flex-1 flex-col gap-3 overflow-y-auto px-4">
80
125
  {props.history !== false && (
81
126
  <AiChatHistory
82
127
  current={chat.conversationId}
@@ -84,39 +129,85 @@ export const AiChat = (props: AiChatProps) => {
84
129
  onOpen={(id) => chat.open(id)}
85
130
  />
86
131
  )}
87
- {chat.items.length === 0 && !chat.streaming && (
88
- <p className="text-muted-foreground text-sm">
89
- {tr("ai.chat.empty", {
90
- default: "Ask anything about this page.",
91
- })}
92
- </p>
93
- )}
94
- <div className="flex flex-col gap-2">
95
- {chat.items.map((item) => (
96
- <AiChatMessage key={item.id} item={item} />
97
- ))}
98
- {chat.streaming && (
99
- <div data-testid="ai-chat-streaming" className="mr-8 self-start">
100
- <MarkdownView content={chat.streaming} />
101
- </div>
102
- )}
103
- {chat.activity && (
104
- <div className="text-muted-foreground flex items-center gap-2 text-xs">
105
- <Spinner />
106
- {tr("ai.chat.running", {
107
- default: "Running $1",
108
- args: [chat.activity],
109
- })}
110
- </div>
111
- )}
112
- </div>
113
- {chat.pending?.kind === "confirm" && (
114
- <AiChatConfirmCard
115
- pending={chat.pending}
116
- loading={chat.loading}
117
- onApprove={() => chat.approve()}
118
- onDeny={() => chat.deny()}
132
+ <Button
133
+ size="icon-sm"
134
+ variant="minimal"
135
+ disabled={chat.loading || empty}
136
+ aria-label={tr("ai.chat.new", { default: "New conversation" })}
137
+ onClick={() => chat.open(undefined)}
138
+ >
139
+ <SquarePen className="size-4" />
140
+ </Button>
141
+ <DrawerClose
142
+ render={
143
+ <Button
144
+ size="icon-sm"
145
+ variant="minimal"
146
+ aria-label={tr("ai.chat.close", { default: "Close" })}
147
+ />
148
+ }
149
+ >
150
+ <X className="size-4" />
151
+ </DrawerClose>
152
+ </div>
153
+ <div
154
+ ref={thread}
155
+ // `shrink-0` on every line: in a scrolling column a card (overflow
156
+ // hidden, so no content minimum) would shrink to its border once
157
+ // the thread outgrows the drawer (#Q3174).
158
+ className="flex min-h-0 flex-1 flex-col gap-3 overflow-y-auto px-4 py-4 *:shrink-0"
159
+ >
160
+ {empty ? (
161
+ <AiChatEmpty
162
+ suggestions={props.suggestions}
163
+ disabled={chat.loading}
164
+ onPick={(prompt) => chat.send(prompt)}
119
165
  />
166
+ ) : (
167
+ <>
168
+ {chat.items.map((item) => (
169
+ <AiChatMessage
170
+ key={item.id}
171
+ item={item}
172
+ renderLink={props.renderLink}
173
+ toolLabel={props.toolLabel}
174
+ />
175
+ ))}
176
+ {chat.streaming && (
177
+ <div
178
+ data-testid="ai-chat-streaming"
179
+ className="mr-4 self-start text-sm leading-relaxed"
180
+ >
181
+ <MarkdownView
182
+ content={chat.streaming}
183
+ renderLink={props.renderLink}
184
+ />
185
+ </div>
186
+ )}
187
+ {running && (
188
+ <div className="text-muted-foreground flex items-center gap-1.5 px-0.5 text-xs">
189
+ <Spinner className="size-3.5" />
190
+ {running}
191
+ </div>
192
+ )}
193
+ {busy && !running && !chat.streaming && (
194
+ <div
195
+ className="text-muted-foreground flex items-center gap-1.5 px-0.5 text-xs"
196
+ aria-live="polite"
197
+ >
198
+ <Spinner className="size-3.5" />
199
+ {tr("ai.chat.thinking", { default: "Thinking…" })}
200
+ </div>
201
+ )}
202
+ {chat.pending?.kind === "confirm" && Card && (
203
+ <Card
204
+ pending={chat.pending}
205
+ loading={chat.loading}
206
+ onApprove={() => chat.approve()}
207
+ onDeny={() => chat.deny()}
208
+ />
209
+ )}
210
+ </>
120
211
  )}
121
212
  {chat.error && (
122
213
  <Alert variant="destructive" data-testid="ai-chat-error">
@@ -124,27 +215,14 @@ export const AiChat = (props: AiChatProps) => {
124
215
  </Alert>
125
216
  )}
126
217
  </div>
127
- <DrawerFooter>
128
- <form {...form.props} className="flex flex-col gap-2">
129
- <Control
130
- input={form.input.message}
131
- area
132
- rows={3}
133
- label={tr("ai.chat.message", { default: "Message" })}
134
- placeholder={tr("ai.chat.placeholder", {
135
- default: "Ask the assistant",
136
- })}
137
- />
138
- <Button
139
- type="submit"
140
- intent="primary"
141
- disabled={chat.loading || Boolean(chat.pending)}
142
- >
143
- {chat.loading && <Spinner />}
144
- {tr("ai.chat.send", { default: "Send" })}
145
- </Button>
146
- </form>
147
- </DrawerFooter>
218
+ <div className="border-border border-t px-3 pt-3 pb-2.5">
219
+ <AiChatComposer
220
+ busy={busy}
221
+ waiting={chat.pending?.kind === "confirm"}
222
+ onSend={(message) => chat.send(message)}
223
+ onStop={() => chat.stop()}
224
+ />
225
+ </div>
148
226
  </DrawerContent>
149
227
  </Drawer>
150
228
  );
@@ -0,0 +1,117 @@
1
+ import { useI18n } from "alepha/react/i18n";
2
+ import { ArrowUp, Square } from "lucide-react";
3
+ import { type KeyboardEvent, useState } from "react";
4
+
5
+ import { Button } from "../core/Button.tsx";
6
+ import { cn } from "../core/utils.ts";
7
+
8
+ export interface AiChatComposerProps {
9
+ /**
10
+ * A turn is running: the send button becomes Stop.
11
+ */
12
+ busy?: boolean;
13
+ /**
14
+ * A confirmation card waits for the user: nothing can be sent until it is
15
+ * answered, and the field says so.
16
+ */
17
+ waiting?: boolean;
18
+ onSend: (message: string) => void;
19
+ onStop?: () => void;
20
+ }
21
+
22
+ /**
23
+ * Where the user writes to the assistant: a field that grows with what is
24
+ * typed, Enter to send, Shift+Enter for a new line, and one round button
25
+ * that sends, or stops the turn while it runs.
26
+ *
27
+ * What is typed lives here, so a re-render of the drawer never drops it.
28
+ */
29
+ export const AiChatComposer = (props: AiChatComposerProps) => {
30
+ const { tr } = useI18n();
31
+ const [value, setValue] = useState("");
32
+ const blocked = Boolean(props.waiting);
33
+ const canSend = !props.busy && !blocked && value.trim().length > 0;
34
+
35
+ const send = () => {
36
+ if (!canSend) {
37
+ return;
38
+ }
39
+ const message = value.trim();
40
+ setValue("");
41
+ props.onSend(message);
42
+ };
43
+
44
+ const onKeyDown = (event: KeyboardEvent<HTMLTextAreaElement>) => {
45
+ if (
46
+ event.key === "Enter" &&
47
+ !event.shiftKey &&
48
+ !event.nativeEvent.isComposing
49
+ ) {
50
+ event.preventDefault();
51
+ send();
52
+ }
53
+ };
54
+
55
+ return (
56
+ <form
57
+ data-testid="ai-chat-composer"
58
+ className="flex flex-col gap-1.5"
59
+ onSubmit={(event) => {
60
+ event.preventDefault();
61
+ send();
62
+ }}
63
+ >
64
+ <div
65
+ className={cn(
66
+ "border-border bg-background focus-within:border-ring focus-within:ring-ring/40 flex items-end gap-2 rounded-2xl border py-2 pr-2 pl-3 transition-shadow focus-within:ring-3",
67
+ blocked && "bg-muted/50",
68
+ )}
69
+ >
70
+ <textarea
71
+ aria-label={tr("ai.chat.message", { default: "Message" })}
72
+ rows={1}
73
+ value={value}
74
+ disabled={blocked}
75
+ placeholder={
76
+ blocked
77
+ ? tr("ai.chat.waiting", {
78
+ default: "Confirm or cancel the card to go on",
79
+ })
80
+ : tr("ai.chat.placeholder", { default: "Ask the assistant" })
81
+ }
82
+ onChange={(event) => setValue(event.target.value)}
83
+ onKeyDown={onKeyDown}
84
+ className="placeholder:text-muted-foreground field-sizing-content max-h-40 min-h-7 flex-1 resize-none bg-transparent py-1 text-sm leading-relaxed outline-none disabled:cursor-not-allowed"
85
+ />
86
+ {props.busy ? (
87
+ <Button
88
+ type="button"
89
+ size="icon-sm"
90
+ variant="outlined"
91
+ className="shrink-0 rounded-full"
92
+ aria-label={tr("ai.chat.stop", { default: "Stop" })}
93
+ onClick={() => props.onStop?.()}
94
+ >
95
+ <Square className="size-3 fill-current" />
96
+ </Button>
97
+ ) : (
98
+ <Button
99
+ type="submit"
100
+ size="icon-sm"
101
+ intent="primary"
102
+ className="shrink-0 rounded-full"
103
+ disabled={!canSend}
104
+ aria-label={tr("ai.chat.send", { default: "Send" })}
105
+ >
106
+ <ArrowUp className="size-4" />
107
+ </Button>
108
+ )}
109
+ </div>
110
+ <p className="text-muted-foreground px-1 text-[11px]">
111
+ {tr("ai.chat.hint", {
112
+ default: "Enter to send, Shift+Enter for a new line",
113
+ })}
114
+ </p>
115
+ </form>
116
+ );
117
+ };
@@ -9,6 +9,7 @@ import {
9
9
  CardHeader,
10
10
  CardTitle,
11
11
  } from "../core/Card.tsx";
12
+ import { AiChatConfirmFields } from "./AiChatConfirmFields.tsx";
12
13
 
13
14
  export interface AiChatConfirmCardProps {
14
15
  /**
@@ -22,8 +23,11 @@ export interface AiChatConfirmCardProps {
22
23
  }
23
24
 
24
25
  /**
25
- * Asks the user before the assistant runs a mutation. Approving or denying
26
- * sends the call id only.
26
+ * Asks the user before the assistant runs a mutation, its input as a list of
27
+ * fields. Approving or denying sends the call id only.
28
+ *
29
+ * An app replaces it for one tool with `AiChat`'s `confirmCards`, and gets
30
+ * the same props.
27
31
  */
28
32
  export const AiChatConfirmCard = (props: AiChatConfirmCardProps) => {
29
33
  const { tr } = useI18n();
@@ -39,9 +43,7 @@ export const AiChatConfirmCard = (props: AiChatConfirmCardProps) => {
39
43
  </CardTitle>
40
44
  </CardHeader>
41
45
  <CardContent>
42
- <pre className="bg-muted max-h-48 overflow-auto rounded p-2 text-xs">
43
- {JSON.stringify(props.pending.input, null, 2)}
44
- </pre>
46
+ <AiChatConfirmFields input={props.pending.input} />
45
47
  </CardContent>
46
48
  <CardFooter className="gap-2">
47
49
  <Button
@@ -0,0 +1,65 @@
1
+ export interface AiChatConfirmFieldsProps {
2
+ input: Record<string, unknown>;
3
+ }
4
+
5
+ /**
6
+ * A call's input as label and value rows: the `params`, `query` and `body`
7
+ * parts flattened into one list, a nested object written as `a.b`.
8
+ */
9
+ export const AiChatConfirmFields = (props: AiChatConfirmFieldsProps) => {
10
+ const rows = flattenInput(props.input);
11
+ if (rows.length === 0) {
12
+ return null;
13
+ }
14
+ return (
15
+ <dl
16
+ data-testid="ai-chat-confirm-fields"
17
+ className="grid grid-cols-[auto_1fr] gap-x-3 gap-y-1 text-xs"
18
+ >
19
+ {rows.map(([label, value]) => (
20
+ <div key={label} className="contents">
21
+ <dt className="text-muted-foreground">{label}</dt>
22
+ <dd className="break-all">{value}</dd>
23
+ </div>
24
+ ))}
25
+ </dl>
26
+ );
27
+ };
28
+
29
+ const PARTS = new Set(["params", "query", "body"]);
30
+
31
+ /**
32
+ * A value as one line: a scalar as written, anything else as JSON.
33
+ */
34
+ const scalar = (value: unknown): string =>
35
+ value === null ||
36
+ typeof value === "string" ||
37
+ typeof value === "number" ||
38
+ typeof value === "boolean"
39
+ ? String(value)
40
+ : JSON.stringify(value);
41
+
42
+ const flattenInput = (
43
+ input: Record<string, unknown>,
44
+ prefix = "",
45
+ ): Array<[string, string]> => {
46
+ const rows: Array<[string, string]> = [];
47
+ for (const [key, value] of Object.entries(input ?? {})) {
48
+ // The action's input parts are plumbing, not fields the user reads.
49
+ const label = !prefix && PARTS.has(key) ? "" : `${prefix}${key}`;
50
+ if (value && typeof value === "object" && !Array.isArray(value)) {
51
+ rows.push(
52
+ ...flattenInput(
53
+ value as Record<string, unknown>,
54
+ label ? `${label}.` : "",
55
+ ),
56
+ );
57
+ } else if (value !== undefined) {
58
+ rows.push([
59
+ label || key,
60
+ Array.isArray(value) ? value.map(scalar).join(", ") : scalar(value),
61
+ ]);
62
+ }
63
+ }
64
+ return rows;
65
+ };