@shware/workflow-ui 0.0.0-stage → 0.0.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 (363) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +180 -2
  3. package/dist/assets/iphone-17-pro-DzaacWSl.png +0 -0
  4. package/dist/assets/pixel-10-pro-back-7MDj1_lz.webp +0 -0
  5. package/dist/assets/pixel-10-pro-mask-Bi1deQ9Y.webp +0 -0
  6. package/dist/cli.d.mts +1 -0
  7. package/dist/cli.mjs +673 -0
  8. package/dist/cli.mjs.map +1 -0
  9. package/dist/config.d.mts +247 -0
  10. package/dist/config.d.mts.map +1 -0
  11. package/dist/config.mjs +27 -0
  12. package/dist/config.mjs.map +1 -0
  13. package/dist/index.d.mts +451 -0
  14. package/dist/index.d.mts.map +1 -0
  15. package/dist/index.mjs +2722 -0
  16. package/dist/index.mjs.map +1 -0
  17. package/dist/postgres.d.mts +34 -0
  18. package/dist/postgres.d.mts.map +1 -0
  19. package/dist/postgres.mjs +313 -0
  20. package/dist/postgres.mjs.map +1 -0
  21. package/package.json +93 -3
  22. package/src/app/assets/devices/iphone-17-pro.png +0 -0
  23. package/src/app/assets/devices/pixel-10-pro-back.webp +0 -0
  24. package/src/app/assets/devices/pixel-10-pro-mask.webp +0 -0
  25. package/src/app/assets/flags/AC.svg +1 -0
  26. package/src/app/assets/flags/AD.svg +1 -0
  27. package/src/app/assets/flags/AE.svg +1 -0
  28. package/src/app/assets/flags/AF.svg +1 -0
  29. package/src/app/assets/flags/AG.svg +1 -0
  30. package/src/app/assets/flags/AI.svg +1 -0
  31. package/src/app/assets/flags/AL.svg +1 -0
  32. package/src/app/assets/flags/AM.svg +1 -0
  33. package/src/app/assets/flags/AO.svg +1 -0
  34. package/src/app/assets/flags/AQ.svg +1 -0
  35. package/src/app/assets/flags/AR.svg +1 -0
  36. package/src/app/assets/flags/AS.svg +1 -0
  37. package/src/app/assets/flags/AT.svg +1 -0
  38. package/src/app/assets/flags/AU.svg +1 -0
  39. package/src/app/assets/flags/AW.svg +1 -0
  40. package/src/app/assets/flags/AX.svg +1 -0
  41. package/src/app/assets/flags/AZ.svg +1 -0
  42. package/src/app/assets/flags/BA.svg +1 -0
  43. package/src/app/assets/flags/BB.svg +1 -0
  44. package/src/app/assets/flags/BD.svg +1 -0
  45. package/src/app/assets/flags/BE.svg +1 -0
  46. package/src/app/assets/flags/BF.svg +1 -0
  47. package/src/app/assets/flags/BG.svg +1 -0
  48. package/src/app/assets/flags/BH.svg +1 -0
  49. package/src/app/assets/flags/BI.svg +1 -0
  50. package/src/app/assets/flags/BJ.svg +1 -0
  51. package/src/app/assets/flags/BL.svg +1 -0
  52. package/src/app/assets/flags/BM.svg +1 -0
  53. package/src/app/assets/flags/BN.svg +1 -0
  54. package/src/app/assets/flags/BO.svg +1 -0
  55. package/src/app/assets/flags/BQ-BO.svg +1 -0
  56. package/src/app/assets/flags/BQ-SA.svg +1 -0
  57. package/src/app/assets/flags/BQ-SE.svg +1 -0
  58. package/src/app/assets/flags/BQ.svg +1 -0
  59. package/src/app/assets/flags/BR.svg +1 -0
  60. package/src/app/assets/flags/BS.svg +1 -0
  61. package/src/app/assets/flags/BT.svg +1 -0
  62. package/src/app/assets/flags/BV.svg +1 -0
  63. package/src/app/assets/flags/BW.svg +1 -0
  64. package/src/app/assets/flags/BY.svg +1 -0
  65. package/src/app/assets/flags/BZ.svg +1 -0
  66. package/src/app/assets/flags/CA.svg +1 -0
  67. package/src/app/assets/flags/CC.svg +1 -0
  68. package/src/app/assets/flags/CD.svg +1 -0
  69. package/src/app/assets/flags/CF.svg +1 -0
  70. package/src/app/assets/flags/CG.svg +1 -0
  71. package/src/app/assets/flags/CH.svg +1 -0
  72. package/src/app/assets/flags/CI.svg +1 -0
  73. package/src/app/assets/flags/CK.svg +1 -0
  74. package/src/app/assets/flags/CL.svg +1 -0
  75. package/src/app/assets/flags/CM.svg +1 -0
  76. package/src/app/assets/flags/CN.svg +1 -0
  77. package/src/app/assets/flags/CO.svg +1 -0
  78. package/src/app/assets/flags/CR.svg +1 -0
  79. package/src/app/assets/flags/CU.svg +1 -0
  80. package/src/app/assets/flags/CV.svg +1 -0
  81. package/src/app/assets/flags/CW.svg +1 -0
  82. package/src/app/assets/flags/CX.svg +1 -0
  83. package/src/app/assets/flags/CY.svg +1 -0
  84. package/src/app/assets/flags/CZ.svg +1 -0
  85. package/src/app/assets/flags/DE.svg +1 -0
  86. package/src/app/assets/flags/DJ.svg +1 -0
  87. package/src/app/assets/flags/DK.svg +1 -0
  88. package/src/app/assets/flags/DM.svg +1 -0
  89. package/src/app/assets/flags/DO.svg +1 -0
  90. package/src/app/assets/flags/DZ.svg +1 -0
  91. package/src/app/assets/flags/EC.svg +1 -0
  92. package/src/app/assets/flags/EE.svg +1 -0
  93. package/src/app/assets/flags/EG.svg +1 -0
  94. package/src/app/assets/flags/EH.svg +1 -0
  95. package/src/app/assets/flags/ER.svg +1 -0
  96. package/src/app/assets/flags/ES-CT.svg +1 -0
  97. package/src/app/assets/flags/ES.svg +1 -0
  98. package/src/app/assets/flags/ET.svg +1 -0
  99. package/src/app/assets/flags/EU.svg +1 -0
  100. package/src/app/assets/flags/FI.svg +1 -0
  101. package/src/app/assets/flags/FJ.svg +1 -0
  102. package/src/app/assets/flags/FK.svg +1 -0
  103. package/src/app/assets/flags/FM.svg +1 -0
  104. package/src/app/assets/flags/FO.svg +1 -0
  105. package/src/app/assets/flags/FR.svg +1 -0
  106. package/src/app/assets/flags/GA.svg +1 -0
  107. package/src/app/assets/flags/GB-ENG.svg +1 -0
  108. package/src/app/assets/flags/GB-NIR.svg +1 -0
  109. package/src/app/assets/flags/GB-SCT.svg +1 -0
  110. package/src/app/assets/flags/GB-WLS.svg +1 -0
  111. package/src/app/assets/flags/GB.svg +1 -0
  112. package/src/app/assets/flags/GD.svg +1 -0
  113. package/src/app/assets/flags/GE.svg +1 -0
  114. package/src/app/assets/flags/GF.svg +1 -0
  115. package/src/app/assets/flags/GG.svg +1 -0
  116. package/src/app/assets/flags/GH.svg +1 -0
  117. package/src/app/assets/flags/GI.svg +1 -0
  118. package/src/app/assets/flags/GL.svg +1 -0
  119. package/src/app/assets/flags/GM.svg +1 -0
  120. package/src/app/assets/flags/GN.svg +1 -0
  121. package/src/app/assets/flags/GP.svg +1 -0
  122. package/src/app/assets/flags/GQ.svg +1 -0
  123. package/src/app/assets/flags/GR.svg +1 -0
  124. package/src/app/assets/flags/GS.svg +1 -0
  125. package/src/app/assets/flags/GT.svg +1 -0
  126. package/src/app/assets/flags/GU.svg +1 -0
  127. package/src/app/assets/flags/GW.svg +1 -0
  128. package/src/app/assets/flags/GY.svg +1 -0
  129. package/src/app/assets/flags/HK.svg +1 -0
  130. package/src/app/assets/flags/HM.svg +1 -0
  131. package/src/app/assets/flags/HN.svg +1 -0
  132. package/src/app/assets/flags/HR.svg +1 -0
  133. package/src/app/assets/flags/HT.svg +1 -0
  134. package/src/app/assets/flags/HU.svg +1 -0
  135. package/src/app/assets/flags/IC.svg +1 -0
  136. package/src/app/assets/flags/ID.svg +1 -0
  137. package/src/app/assets/flags/IE.svg +1 -0
  138. package/src/app/assets/flags/IL.svg +1 -0
  139. package/src/app/assets/flags/IM.svg +1 -0
  140. package/src/app/assets/flags/IN.svg +1 -0
  141. package/src/app/assets/flags/IO.svg +1 -0
  142. package/src/app/assets/flags/IQ.svg +1 -0
  143. package/src/app/assets/flags/IR.svg +1 -0
  144. package/src/app/assets/flags/IS.svg +1 -0
  145. package/src/app/assets/flags/IT.svg +1 -0
  146. package/src/app/assets/flags/JE.svg +1 -0
  147. package/src/app/assets/flags/JM.svg +1 -0
  148. package/src/app/assets/flags/JO.svg +1 -0
  149. package/src/app/assets/flags/JP.svg +1 -0
  150. package/src/app/assets/flags/KE.svg +1 -0
  151. package/src/app/assets/flags/KG.svg +1 -0
  152. package/src/app/assets/flags/KH.svg +1 -0
  153. package/src/app/assets/flags/KI.svg +1 -0
  154. package/src/app/assets/flags/KM.svg +1 -0
  155. package/src/app/assets/flags/KN.svg +1 -0
  156. package/src/app/assets/flags/KP.svg +1 -0
  157. package/src/app/assets/flags/KR.svg +1 -0
  158. package/src/app/assets/flags/KW.svg +1 -0
  159. package/src/app/assets/flags/KY.svg +1 -0
  160. package/src/app/assets/flags/KZ.svg +1 -0
  161. package/src/app/assets/flags/LA.svg +1 -0
  162. package/src/app/assets/flags/LB.svg +1 -0
  163. package/src/app/assets/flags/LC.svg +1 -0
  164. package/src/app/assets/flags/LI.svg +1 -0
  165. package/src/app/assets/flags/LK.svg +1 -0
  166. package/src/app/assets/flags/LR.svg +1 -0
  167. package/src/app/assets/flags/LS.svg +1 -0
  168. package/src/app/assets/flags/LT.svg +1 -0
  169. package/src/app/assets/flags/LU.svg +1 -0
  170. package/src/app/assets/flags/LV.svg +1 -0
  171. package/src/app/assets/flags/LY.svg +1 -0
  172. package/src/app/assets/flags/MA.svg +1 -0
  173. package/src/app/assets/flags/MC.svg +1 -0
  174. package/src/app/assets/flags/MD.svg +1 -0
  175. package/src/app/assets/flags/ME.svg +1 -0
  176. package/src/app/assets/flags/MF.svg +1 -0
  177. package/src/app/assets/flags/MG.svg +1 -0
  178. package/src/app/assets/flags/MH.svg +1 -0
  179. package/src/app/assets/flags/MK.svg +1 -0
  180. package/src/app/assets/flags/ML.svg +1 -0
  181. package/src/app/assets/flags/MM.svg +1 -0
  182. package/src/app/assets/flags/MN.svg +1 -0
  183. package/src/app/assets/flags/MO.svg +1 -0
  184. package/src/app/assets/flags/MP.svg +1 -0
  185. package/src/app/assets/flags/MQ.svg +1 -0
  186. package/src/app/assets/flags/MR.svg +1 -0
  187. package/src/app/assets/flags/MS.svg +1 -0
  188. package/src/app/assets/flags/MT.svg +1 -0
  189. package/src/app/assets/flags/MU.svg +1 -0
  190. package/src/app/assets/flags/MV.svg +1 -0
  191. package/src/app/assets/flags/MW.svg +1 -0
  192. package/src/app/assets/flags/MX.svg +1 -0
  193. package/src/app/assets/flags/MY.svg +1 -0
  194. package/src/app/assets/flags/MZ.svg +1 -0
  195. package/src/app/assets/flags/NA.svg +1 -0
  196. package/src/app/assets/flags/NC.svg +1 -0
  197. package/src/app/assets/flags/NE.svg +1 -0
  198. package/src/app/assets/flags/NF.svg +1 -0
  199. package/src/app/assets/flags/NG.svg +1 -0
  200. package/src/app/assets/flags/NI.svg +1 -0
  201. package/src/app/assets/flags/NL.svg +1 -0
  202. package/src/app/assets/flags/NO.svg +1 -0
  203. package/src/app/assets/flags/NP.svg +1 -0
  204. package/src/app/assets/flags/NR.svg +1 -0
  205. package/src/app/assets/flags/NU.svg +1 -0
  206. package/src/app/assets/flags/NZ.svg +1 -0
  207. package/src/app/assets/flags/OM.svg +1 -0
  208. package/src/app/assets/flags/PA.svg +1 -0
  209. package/src/app/assets/flags/PE.svg +1 -0
  210. package/src/app/assets/flags/PF.svg +1 -0
  211. package/src/app/assets/flags/PG.svg +1 -0
  212. package/src/app/assets/flags/PH.svg +1 -0
  213. package/src/app/assets/flags/PK.svg +1 -0
  214. package/src/app/assets/flags/PL.svg +1 -0
  215. package/src/app/assets/flags/PM.svg +1 -0
  216. package/src/app/assets/flags/PN.svg +1 -0
  217. package/src/app/assets/flags/PR.svg +1 -0
  218. package/src/app/assets/flags/PS.svg +1 -0
  219. package/src/app/assets/flags/PT.svg +1 -0
  220. package/src/app/assets/flags/PW.svg +1 -0
  221. package/src/app/assets/flags/PY.svg +1 -0
  222. package/src/app/assets/flags/QA.svg +1 -0
  223. package/src/app/assets/flags/RE.svg +1 -0
  224. package/src/app/assets/flags/README.md +6 -0
  225. package/src/app/assets/flags/RO.svg +1 -0
  226. package/src/app/assets/flags/RS.svg +1 -0
  227. package/src/app/assets/flags/RU.svg +1 -0
  228. package/src/app/assets/flags/RW.svg +1 -0
  229. package/src/app/assets/flags/SA.svg +1 -0
  230. package/src/app/assets/flags/SB.svg +1 -0
  231. package/src/app/assets/flags/SC.svg +1 -0
  232. package/src/app/assets/flags/SD.svg +1 -0
  233. package/src/app/assets/flags/SE.svg +1 -0
  234. package/src/app/assets/flags/SG.svg +1 -0
  235. package/src/app/assets/flags/SH.svg +1 -0
  236. package/src/app/assets/flags/SI.svg +1 -0
  237. package/src/app/assets/flags/SJ.svg +1 -0
  238. package/src/app/assets/flags/SK.svg +1 -0
  239. package/src/app/assets/flags/SL.svg +1 -0
  240. package/src/app/assets/flags/SM.svg +1 -0
  241. package/src/app/assets/flags/SN.svg +1 -0
  242. package/src/app/assets/flags/SO.svg +1 -0
  243. package/src/app/assets/flags/SR.svg +1 -0
  244. package/src/app/assets/flags/SS.svg +1 -0
  245. package/src/app/assets/flags/ST.svg +1 -0
  246. package/src/app/assets/flags/SV.svg +1 -0
  247. package/src/app/assets/flags/SX.svg +1 -0
  248. package/src/app/assets/flags/SY.svg +1 -0
  249. package/src/app/assets/flags/SZ.svg +1 -0
  250. package/src/app/assets/flags/TA.svg +1 -0
  251. package/src/app/assets/flags/TC.svg +1 -0
  252. package/src/app/assets/flags/TD.svg +1 -0
  253. package/src/app/assets/flags/TF.svg +1 -0
  254. package/src/app/assets/flags/TG.svg +1 -0
  255. package/src/app/assets/flags/TH.svg +1 -0
  256. package/src/app/assets/flags/TJ.svg +1 -0
  257. package/src/app/assets/flags/TK.svg +1 -0
  258. package/src/app/assets/flags/TL.svg +1 -0
  259. package/src/app/assets/flags/TM.svg +1 -0
  260. package/src/app/assets/flags/TN.svg +1 -0
  261. package/src/app/assets/flags/TO.svg +1 -0
  262. package/src/app/assets/flags/TR.svg +1 -0
  263. package/src/app/assets/flags/TT.svg +1 -0
  264. package/src/app/assets/flags/TV.svg +1 -0
  265. package/src/app/assets/flags/TW.svg +1 -0
  266. package/src/app/assets/flags/TZ.svg +1 -0
  267. package/src/app/assets/flags/UA.svg +1 -0
  268. package/src/app/assets/flags/UG.svg +1 -0
  269. package/src/app/assets/flags/UM.svg +1 -0
  270. package/src/app/assets/flags/US.svg +1 -0
  271. package/src/app/assets/flags/UY.svg +1 -0
  272. package/src/app/assets/flags/UZ.svg +1 -0
  273. package/src/app/assets/flags/VA.svg +1 -0
  274. package/src/app/assets/flags/VC.svg +1 -0
  275. package/src/app/assets/flags/VE.svg +1 -0
  276. package/src/app/assets/flags/VG.svg +1 -0
  277. package/src/app/assets/flags/VI.svg +1 -0
  278. package/src/app/assets/flags/VN.svg +1 -0
  279. package/src/app/assets/flags/VU.svg +1 -0
  280. package/src/app/assets/flags/WF.svg +1 -0
  281. package/src/app/assets/flags/WS.svg +1 -0
  282. package/src/app/assets/flags/XA.svg +1 -0
  283. package/src/app/assets/flags/XC.svg +1 -0
  284. package/src/app/assets/flags/XK.svg +1 -0
  285. package/src/app/assets/flags/XO.svg +1 -0
  286. package/src/app/assets/flags/YE.svg +1 -0
  287. package/src/app/assets/flags/YT.svg +1 -0
  288. package/src/app/assets/flags/ZA.svg +1 -0
  289. package/src/app/assets/flags/ZM.svg +1 -0
  290. package/src/app/assets/flags/ZW.svg +1 -0
  291. package/src/app/email-preview.ts +46 -0
  292. package/src/app/index.css +203 -0
  293. package/src/app/index.html +12 -0
  294. package/src/app/integrations/i18n/react-i18next.d.ts +11 -0
  295. package/src/app/integrations/i18n/root-provider.tsx +62 -0
  296. package/src/app/integrations/tanstack-query/devtools.tsx +6 -0
  297. package/src/app/integrations/tanstack-query/root-provider.tsx +37 -0
  298. package/src/app/integrations/theme/root-provider.tsx +75 -0
  299. package/src/app/integrations/toast/toast-provider.tsx +46 -0
  300. package/src/app/locales/en-US/common.ts +268 -0
  301. package/src/app/locales/zh-CN/common.ts +263 -0
  302. package/src/app/main.tsx +40 -0
  303. package/src/app/page-chrome.tsx +79 -0
  304. package/src/app/router.tsx +40 -0
  305. package/src/app/routes/__root.tsx +293 -0
  306. package/src/app/routes/emails.tsx +199 -0
  307. package/src/app/routes/home.tsx +87 -0
  308. package/src/app/routes/index.ts +19 -0
  309. package/src/app/routes/metrics.tsx +541 -0
  310. package/src/app/routes/segment-overview.tsx +211 -0
  311. package/src/app/routes/segments.tsx +236 -0
  312. package/src/app/routes/settings.tsx +90 -0
  313. package/src/app/routes/workflows.tsx +348 -0
  314. package/src/app/studio.ts +16 -0
  315. package/src/app/svg.d.ts +17 -0
  316. package/src/app/virtual-config.d.ts +6 -0
  317. package/src/cli.ts +110 -0
  318. package/src/components/avatar.tsx +57 -0
  319. package/src/components/breadcrumb.tsx +49 -0
  320. package/src/components/button/index.tsx +86 -0
  321. package/src/components/channel-icon.ts +39 -0
  322. package/src/components/chat-markup.tsx +109 -0
  323. package/src/components/corner-shape.ts +13 -0
  324. package/src/components/date-range.tsx +181 -0
  325. package/src/components/discord-preview.tsx +165 -0
  326. package/src/components/dropdown/index.tsx +172 -0
  327. package/src/components/email-defaults.tsx +40 -0
  328. package/src/components/email-list.tsx +149 -0
  329. package/src/components/email-thumbnail.tsx +123 -0
  330. package/src/components/flag.tsx +40 -0
  331. package/src/components/input/index.tsx +40 -0
  332. package/src/components/input/search-input.tsx +24 -0
  333. package/src/components/layout.ts +403 -0
  334. package/src/components/menu/index.tsx +118 -0
  335. package/src/components/metric-card.tsx +321 -0
  336. package/src/components/modal/index.tsx +70 -0
  337. package/src/components/nav-drawer.tsx +84 -0
  338. package/src/components/node-inspector.tsx +242 -0
  339. package/src/components/profile-drawer.tsx +331 -0
  340. package/src/components/push-preview.tsx +470 -0
  341. package/src/components/push-thumbnail.tsx +78 -0
  342. package/src/components/segment-list.tsx +184 -0
  343. package/src/components/select/index.tsx +70 -0
  344. package/src/components/series-chart.tsx +120 -0
  345. package/src/components/slack-preview.tsx +223 -0
  346. package/src/components/sparkline.tsx +58 -0
  347. package/src/components/tabs/index.tsx +45 -0
  348. package/src/components/template-refs.ts +110 -0
  349. package/src/components/templates-page.tsx +601 -0
  350. package/src/components/textarea/index.tsx +37 -0
  351. package/src/components/thumbnail-window.tsx +65 -0
  352. package/src/components/workflow-canvas.tsx +503 -0
  353. package/src/components/workflow-list.tsx +231 -0
  354. package/src/config.ts +288 -0
  355. package/src/generate.ts +56 -0
  356. package/src/index.ts +35 -0
  357. package/src/postgres.ts +415 -0
  358. package/src/server/project.ts +290 -0
  359. package/src/server.ts +500 -0
  360. package/src/utils/cn.ts +6 -0
  361. package/src/utils/label.ts +12 -0
  362. package/src/utils/lookup.ts +11 -0
  363. package/src/utils/template-content.ts +213 -0
@@ -0,0 +1,541 @@
1
+ import { useQuery } from '@tanstack/react-query';
2
+ import { Link, createRoute } from '@tanstack/react-router';
3
+ import { ChevronDown, ChevronUp } from 'lucide-react';
4
+ import { type ReactNode, useState } from 'react';
5
+ import { useTranslation } from 'react-i18next';
6
+ import { ALL_CHANNELS_ICON, CHANNEL_ICON, channelIcon } from '../../components/channel-icon';
7
+ import { superellipse } from '../../components/corner-shape';
8
+ import {
9
+ type DateRange,
10
+ DateRangePicker,
11
+ isoDay,
12
+ lastDays,
13
+ parseIsoDay,
14
+ } from '../../components/date-range';
15
+ import { Dropdown, type DropdownOption } from '../../components/dropdown';
16
+ import { MetricCard, type MetricSeries, type Trend } from '../../components/metric-card';
17
+ import { findNode } from '../../components/template-refs';
18
+ import {
19
+ type Granularity,
20
+ METRIC_CHANNELS,
21
+ type MessageStat,
22
+ type MetricChannel,
23
+ type MetricPoint,
24
+ } from '../../config';
25
+ import { lookup } from '../../utils/lookup';
26
+ import { lookupTemplate } from '../../utils/template-content';
27
+ import { workflowDetailRoute } from './workflows';
28
+
29
+ /** Sentinel for "no channel filter" — a `<select>` value cannot be undefined. */
30
+ const ALL = 'all';
31
+
32
+ /** Top N links; the card grows with its content, so the list has to stop somewhere. */
33
+ const LINK_LIMIT = 10;
34
+
35
+ const GRANULARITIES: Granularity[] = ['day', 'week', 'month'];
36
+
37
+ const number = (value: number) => value.toLocaleString();
38
+ const percent = (value: number) => `${(value * 100).toFixed(1)}%`;
39
+ const signed = (value: number, format: (value: number) => string) =>
40
+ `${value > 0 ? '+' : ''}${format(value)}`;
41
+
42
+ /** A rate with no denominator is 0, not NaN — an empty bucket is still a bucket. */
43
+ const rate = (part: number, whole: number) => (whole > 0 ? part / whole : 0);
44
+
45
+ /** The share one funnel stage keeps of the one before it, ready to print. */
46
+ const share = (part: number, whole: number) => percent(rate(part, whole));
47
+
48
+ /**
49
+ * Sent is its own denominator, so its rate is a foregone conclusion — but it
50
+ * is printed anyway: without it the column's count sits a line higher than
51
+ * every other column's, and a row stops reading as a row.
52
+ */
53
+ const SENT_SHARE = '100%';
54
+
55
+ function trendOf(delta: number): Trend {
56
+ if (delta > 0) return 'up';
57
+ if (delta < 0) return 'down';
58
+ return 'flat';
59
+ }
60
+
61
+ const asChannel = (value: string): MetricChannel | undefined =>
62
+ METRIC_CHANNELS.find((channel) => channel === value);
63
+
64
+ /* --------------------------------- Layout --------------------------------- */
65
+
66
+ /**
67
+ * The shell every panel below the chart cards shares.
68
+ *
69
+ * A title and nothing else: the range lives in the toolbar that drives all of
70
+ * these, and repeating it per panel only invited the two to disagree.
71
+ */
72
+ function Panel({ title, children }: { title: string; children: ReactNode }) {
73
+ return (
74
+ <section className="border-border bg-card mt-6 rounded-2xl border" style={superellipse}>
75
+ <div className="border-border border-b px-5 py-3">
76
+ <h2 className="text-sm font-semibold">{title}</h2>
77
+ </div>
78
+ {children}
79
+ </section>
80
+ );
81
+ }
82
+
83
+ /**
84
+ * One funnel cell: the rate on top in grey, the absolute below it. The rate is
85
+ * what you scan a column for and the count is what you check it against, so
86
+ * neither can be the one you have to hover for.
87
+ */
88
+ function FunnelCell({ rate, count }: { rate: string; count: number }) {
89
+ return (
90
+ <td className="border-border border-b px-4 py-3 align-top tabular-nums">
91
+ <div className="text-muted text-xs">{rate}</div>
92
+ <div className="text-sm">{number(count)}</div>
93
+ </td>
94
+ );
95
+ }
96
+
97
+ /* ------------------------------- Chart cards ------------------------------- */
98
+
99
+ const CARD_KEYS = ['sent', 'opened', 'clicked', 'converted'] as const;
100
+ type CardKey = (typeof CARD_KEYS)[number];
101
+
102
+ /**
103
+ * Sent is a count; the other three are rates.
104
+ *
105
+ * Denominators follow the convention the message table uses, which is the
106
+ * industry's: delivered against sent, everything after it against delivered.
107
+ * Rates against `opened` (a click-to-open rate) read better in isolation but
108
+ * stop being comparable across cards, and comparing them is the point of
109
+ * putting the four side by side.
110
+ */
111
+ const SERIES: Record<CardKey, (point: MetricPoint) => number> = {
112
+ sent: (point) => point.sent,
113
+ opened: (point) => rate(point.opened, point.delivered),
114
+ clicked: (point) => rate(point.clicked, point.delivered),
115
+ // A conversion is a user's, not a message's: entered users are its denominator
116
+ converted: (point) => rate(point.converted, point.entered),
117
+ };
118
+
119
+ /** The card headline: one number for the whole range, the same rate a chart point uses. */
120
+ const TOTAL: Record<CardKey, (points: readonly MetricPoint[]) => number> = {
121
+ sent: (points) => sum(points, 'sent'),
122
+ opened: (points) => rate(sum(points, 'opened'), sum(points, 'delivered')),
123
+ clicked: (points) => rate(sum(points, 'clicked'), sum(points, 'delivered')),
124
+ converted: (points) => rate(sum(points, 'converted'), sum(points, 'entered')),
125
+ };
126
+
127
+ function sum(
128
+ points: readonly MetricPoint[],
129
+ key: 'sent' | 'delivered' | 'opened' | 'clicked' | 'entered' | 'converted'
130
+ ) {
131
+ return points.reduce((total, point) => total + point[key], 0);
132
+ }
133
+
134
+ /** The range just before `range`, of the same length: what the headline is compared against. */
135
+ function previousRange(range: DateRange): DateRange {
136
+ const from = parseIsoDay(range.from);
137
+ const days = Math.round((parseIsoDay(range.to).getTime() - from.getTime()) / 86_400_000) + 1;
138
+ const to = new Date(from);
139
+ to.setDate(to.getDate() - 1);
140
+ const start = new Date(to);
141
+ start.setDate(start.getDate() - (days - 1));
142
+ return { from: isoDay(start), to: isoDay(to) };
143
+ }
144
+
145
+ /** The two halves of a split metric, in the order they are plotted. */
146
+ type SplitKey = 'openedHuman' | 'openedMachine' | 'clickedHuman' | 'clickedMachine';
147
+
148
+ /**
149
+ * Which cards decompose, and into what.
150
+ *
151
+ * Opens and clicks are the two the machines touch. Apple's Mail Privacy
152
+ * Protection fetches the tracking pixel of everything it relays, Gmail's image
153
+ * proxy caches it, and corporate scanners follow the links behind it — so both
154
+ * numbers arrive inflated by traffic nobody caused, and an open rate that
155
+ * doubled overnight usually means a client changed, not an audience. Sent is
156
+ * ours to count and a conversion is a person doing something in the product,
157
+ * so neither of those splits.
158
+ *
159
+ * Both halves are rated against the same denominator as their total, so the
160
+ * three lines add up on screen exactly the way the counts do.
161
+ */
162
+ const SPLIT: Record<CardKey, readonly [SplitKey, SplitKey] | undefined> = {
163
+ sent: undefined,
164
+ opened: ['openedHuman', 'openedMachine'],
165
+ clicked: ['clickedHuman', 'clickedMachine'],
166
+ converted: undefined,
167
+ };
168
+
169
+ /* ------------------------------- Message table ----------------------------- */
170
+
171
+ const MESSAGE_COLUMNS = ['sent', 'delivered', 'opened', 'clicked', 'unsubscribed'] as const;
172
+
173
+ /** The totals row, summed here so it can never disagree with the rows above it. */
174
+ function totalsOf(rows: readonly MessageStat[]) {
175
+ return rows.reduce(
176
+ (total, row) => ({
177
+ sent: total.sent + row.sent,
178
+ delivered: total.delivered + row.delivered,
179
+ opened: total.opened + row.opened,
180
+ clicked: total.clicked + row.clicked,
181
+ unsubscribed: total.unsubscribed + row.unsubscribed,
182
+ }),
183
+ { sent: 0, delivered: 0, opened: 0, clicked: 0, unsubscribed: 0 }
184
+ );
185
+ }
186
+
187
+ /* ---------------------------------- Page ---------------------------------- */
188
+
189
+ /**
190
+ * A workflow's Overview: how the funnel is doing right now (four cards), which
191
+ * single send is dragging the rest down (message metrics), and what people
192
+ * clicked (links). One range, one granularity and one channel drive all three,
193
+ * because three panels disagreeing about which fortnight they cover is worse
194
+ * than no panels at all.
195
+ */
196
+ function MetricsTab() {
197
+ const { id } = workflowDetailRoute.useParams();
198
+ const { config } = workflowDetailRoute.useRouteContext();
199
+ const { t, i18n } = useTranslation();
200
+
201
+ const [range, setRange] = useState<DateRange>(() => lastDays(30));
202
+ const [granularity, setGranularity] = useState<Granularity>('day');
203
+ const [channel, setChannel] = useState<MetricChannel | undefined>(undefined);
204
+ const [sortAscending, setSortAscending] = useState(true);
205
+
206
+ const ir = lookup(config.workflows, id);
207
+ const workflowName = ir?.id ?? '';
208
+
209
+ const { data: points } = useQuery({
210
+ queryKey: ['metrics', workflowName, range.from, range.to, granularity, channel ?? ALL],
211
+ queryFn: async () =>
212
+ (await config.stats?.metrics?.(workflowName, { ...range, granularity, channel })) ?? [],
213
+ enabled: ir !== undefined && config.stats?.metrics !== undefined,
214
+ });
215
+
216
+ const previous = previousRange(range);
217
+ const { data: previousPoints } = useQuery({
218
+ queryKey: ['metrics', workflowName, previous.from, previous.to, granularity, channel ?? ALL],
219
+ queryFn: async () =>
220
+ (await config.stats?.metrics?.(workflowName, { ...previous, granularity, channel })) ?? [],
221
+ enabled: ir !== undefined && config.stats?.metrics !== undefined,
222
+ });
223
+
224
+ const { data: links } = useQuery({
225
+ queryKey: ['links', workflowName, range.from, range.to, channel ?? ALL],
226
+ queryFn: async () => (await config.stats?.links?.(workflowName, { ...range, channel })) ?? [],
227
+ enabled: ir !== undefined && config.stats?.links !== undefined,
228
+ });
229
+
230
+ const { data: messages } = useQuery({
231
+ queryKey: ['message-stats', workflowName, range.from, range.to, channel ?? ALL],
232
+ queryFn: async () =>
233
+ (await config.stats?.messages?.(workflowName, { ...range, channel })) ?? [],
234
+ enabled: ir !== undefined && config.stats?.messages !== undefined,
235
+ });
236
+
237
+ /* The same glyphs the message table puts in front of its rows: a channel has
238
+ to read the same in the picker as it does in the data it filters. */
239
+ const channelOptions: DropdownOption[] = [
240
+ { value: ALL, label: t('metrics.channel.all'), icon: ALL_CHANNELS_ICON },
241
+ ...METRIC_CHANNELS.map((value) => ({
242
+ value,
243
+ label: t(`metrics.channel.${value}`),
244
+ icon: CHANNEL_ICON[value],
245
+ })),
246
+ ];
247
+
248
+ /**
249
+ * What a message is called: the node's own label first (the author named
250
+ * this send), then the template module's name, then the registry key — which
251
+ * is wire identity and the last thing anyone wants to read.
252
+ */
253
+ const nameOf = (row: MessageStat): string => {
254
+ const label = ir === undefined ? undefined : findNode(ir, row.nodeId)?.label?.trim();
255
+ if (label !== undefined && label !== '') return label;
256
+ /* The row's own channel picks the registry: one key can name an email and a push. */
257
+ const found = lookupTemplate(
258
+ config,
259
+ row.channel.startsWith('push') ? 'push' : row.channel,
260
+ row.template
261
+ );
262
+ const moduleName = found?.entry.title.trim();
263
+ return moduleName !== undefined && moduleName !== '' ? moduleName : row.template;
264
+ };
265
+
266
+ const rows = (messages ?? []).map((row) => ({ row, label: nameOf(row) }));
267
+ rows.sort((a, b) => (sortAscending ? 1 : -1) * a.label.localeCompare(b.label));
268
+
269
+ if (config.stats?.metrics === undefined) {
270
+ return (
271
+ <div className="flex flex-1 items-center justify-center p-8">
272
+ <div
273
+ className="border-border bg-card max-w-md rounded-2xl border border-dashed p-8 text-center"
274
+ style={superellipse}
275
+ >
276
+ <p className="text-sm font-medium">{t('metrics.noSource')}</p>
277
+ <p className="text-muted mt-2 text-sm">{t('metrics.noSourceHint')}</p>
278
+ </div>
279
+ </div>
280
+ );
281
+ }
282
+
283
+ const buckets = points ?? [];
284
+ /* Formatted once for all four cards: the hover readout wants a date a person
285
+ can read, and the card has no locale of its own. Long form, weekday and
286
+ all — the readout is the one place there is room for it, and a weekday is
287
+ what tells you whether a dip is the weekend. A month bucket has no weekday
288
+ worth printing, so it names the month instead. */
289
+ const dates = buckets.map((point) => {
290
+ const day = parseIsoDay(point.date);
291
+ if (granularity === 'month') {
292
+ return day.toLocaleDateString(i18n.language, { year: 'numeric', month: 'long' });
293
+ }
294
+ /* A week is keyed by its Monday, so naming that Monday would read as one
295
+ day's number when the value is seven days summed. Say which week it is. */
296
+ if (granularity === 'week') {
297
+ return t('metrics.range.weekOf', {
298
+ date: day.toLocaleDateString(i18n.language, { month: 'long', day: 'numeric' }),
299
+ });
300
+ }
301
+ return day.toLocaleDateString(i18n.language, {
302
+ weekday: 'long',
303
+ month: 'long',
304
+ day: 'numeric',
305
+ });
306
+ });
307
+
308
+ /**
309
+ * The lines one card plots: its total, and — where the source measured them
310
+ * — the human and machine halves of it.
311
+ *
312
+ * Presence in the data is the switch, not a channel the UI hardcodes. The
313
+ * demo source reports the split for email alone, which is where the pixel
314
+ * prefetch problem lives, so All channels and every other filter come back
315
+ * single-line on their own; a project that learns to tell a scanner from a
316
+ * person on another transport gets three lines there without a UI change.
317
+ */
318
+ const seriesOf = (key: CardKey): MetricSeries[] => {
319
+ const head = { label: t(`metrics.${key}`), values: buckets.map(SERIES[key]) };
320
+ const split = SPLIT[key];
321
+ if (split === undefined) return [head];
322
+ /* Partial coverage is worse than none: halves that only sometimes sum to
323
+ the whole read as a broken measurement rather than a narrower filter. */
324
+ if (!buckets.every((point) => split.every((half) => point[half] !== undefined))) return [head];
325
+ return [
326
+ head,
327
+ ...split.map((half) => ({
328
+ label: t(`metrics.series.${half}`),
329
+ values: buckets.map((point) => rate(point[half] ?? 0, point.delivered)),
330
+ })),
331
+ ];
332
+ };
333
+
334
+ /*
335
+ * The headline is the whole range — a total, or one rate over all of it — and the delta
336
+ * compares it with the range of the same length just before. The last bucket alone (today, a
337
+ * day still in progress) made the headline small and jumpy, and 0 the moment sends paused.
338
+ */
339
+ const cardOf = (key: CardKey) => {
340
+ const format = key === 'sent' ? number : percent;
341
+ const current = TOTAL[key](buckets);
342
+ const before = previousPoints === undefined ? undefined : TOTAL[key](previousPoints);
343
+ const delta = before === undefined ? undefined : current - before;
344
+ return (
345
+ <MetricCard
346
+ key={key}
347
+ label={t(`metrics.${key}`)}
348
+ value={buckets.length === 0 ? '—' : format(current)}
349
+ delta={
350
+ delta === undefined
351
+ ? undefined
352
+ : {
353
+ text: t('metrics.vsPrevious', { value: signed(delta, format) }),
354
+ trend: trendOf(delta),
355
+ }
356
+ }
357
+ dates={dates}
358
+ series={seriesOf(key)}
359
+ format={format}
360
+ />
361
+ );
362
+ };
363
+
364
+ const topLinks = [...(links ?? [])].sort((a, b) => b.clicks - a.clicks).slice(0, LINK_LIMIT);
365
+ const totals = totalsOf(rows.map((entry) => entry.row));
366
+
367
+ return (
368
+ <div className="flex-1 p-6">
369
+ <div className="flex flex-wrap items-center gap-2">
370
+ <DateRangePicker value={range} onChange={setRange} />
371
+ <Dropdown
372
+ value={granularity}
373
+ options={GRANULARITIES.map((value) => ({
374
+ value,
375
+ label: t(`metrics.granularity.${value}`),
376
+ }))}
377
+ onChange={(value) => setGranularity(GRANULARITIES.find((g) => g === value) ?? 'day')}
378
+ className="w-auto"
379
+ />
380
+ <Dropdown
381
+ value={channel ?? ALL}
382
+ options={channelOptions}
383
+ onChange={(value) => setChannel(asChannel(value))}
384
+ className="w-auto"
385
+ />
386
+ </div>
387
+
388
+ {buckets.length === 0 ? (
389
+ <p className="text-muted mt-6 text-sm">{t('metrics.noData')}</p>
390
+ ) : (
391
+ <div className="mt-4 grid gap-4 sm:grid-cols-2 xl:grid-cols-4">{CARD_KEYS.map(cardOf)}</div>
392
+ )}
393
+
394
+ {config.stats.messages !== undefined && (
395
+ <Panel title={t('metrics.messages.title')}>
396
+ <table className="w-full border-separate border-spacing-0 text-sm">
397
+ <thead>
398
+ <tr className="text-left text-xs font-semibold">
399
+ <th className="border-border border-b px-4 py-2.5">
400
+ <button
401
+ type="button"
402
+ className="inline-flex items-center gap-1"
403
+ onClick={() => setSortAscending((ascending) => !ascending)}
404
+ >
405
+ {t('metrics.messages.actionName')}
406
+ {sortAscending ? (
407
+ <ChevronUp size={14} strokeWidth={2} aria-hidden className="text-muted" />
408
+ ) : (
409
+ <ChevronDown size={14} strokeWidth={2} aria-hidden className="text-muted" />
410
+ )}
411
+ </button>
412
+ </th>
413
+ {MESSAGE_COLUMNS.map((column) => (
414
+ <th key={column} className="border-border w-32 border-b px-4 py-2.5">
415
+ {t(`metrics.messages.${column}`)}
416
+ </th>
417
+ ))}
418
+ </tr>
419
+ </thead>
420
+ <tbody>
421
+ {rows.length === 0 ? (
422
+ <tr>
423
+ <td className="text-muted px-4 py-4 text-sm" colSpan={MESSAGE_COLUMNS.length + 1}>
424
+ {t('metrics.messages.empty')}
425
+ </td>
426
+ </tr>
427
+ ) : (
428
+ <>
429
+ <tr>
430
+ <td className="border-border border-b px-4 py-3 align-top font-semibold">
431
+ {t('metrics.messages.allMessages')}
432
+ </td>
433
+ <FunnelCell rate={SENT_SHARE} count={totals.sent} />
434
+ <FunnelCell
435
+ rate={share(totals.delivered, totals.sent)}
436
+ count={totals.delivered}
437
+ />
438
+ <FunnelCell
439
+ rate={share(totals.opened, totals.delivered)}
440
+ count={totals.opened}
441
+ />
442
+ <FunnelCell
443
+ rate={share(totals.clicked, totals.delivered)}
444
+ count={totals.clicked}
445
+ />
446
+ <FunnelCell
447
+ rate={share(totals.unsubscribed, totals.delivered)}
448
+ count={totals.unsubscribed}
449
+ />
450
+ </tr>
451
+ {rows.map(({ row, label }) => {
452
+ const RowGlyph = channelIcon(row.channel);
453
+ return (
454
+ /* One send site lands on several channels (a push on iOS and Android): one row each. */
455
+ <tr
456
+ key={`${row.nodeId}:${row.template}:${row.channel}`}
457
+ className="last:[&>td]:border-b-0"
458
+ >
459
+ <td className="border-border border-b px-4 py-3 align-top">
460
+ <span className="flex items-center gap-2">
461
+ <RowGlyph
462
+ size={14}
463
+ strokeWidth={2}
464
+ aria-hidden
465
+ className="text-muted shrink-0"
466
+ />
467
+ <Link
468
+ to="/templates/$key"
469
+ params={{ key: row.template }}
470
+ search={{
471
+ channel: row.channel.startsWith('push') ? 'push' : row.channel,
472
+ }}
473
+ className="text-accent truncate hover:underline"
474
+ >
475
+ {label}
476
+ </Link>
477
+ </span>
478
+ </td>
479
+ <FunnelCell rate={SENT_SHARE} count={row.sent} />
480
+ <FunnelCell rate={share(row.delivered, row.sent)} count={row.delivered} />
481
+ <FunnelCell rate={share(row.opened, row.delivered)} count={row.opened} />
482
+ <FunnelCell rate={share(row.clicked, row.delivered)} count={row.clicked} />
483
+ <FunnelCell
484
+ rate={share(row.unsubscribed, row.delivered)}
485
+ count={row.unsubscribed}
486
+ />
487
+ </tr>
488
+ );
489
+ })}
490
+ </>
491
+ )}
492
+ </tbody>
493
+ </table>
494
+ </Panel>
495
+ )}
496
+
497
+ {config.stats.links !== undefined && (
498
+ <Panel title={t('metrics.links.title')}>
499
+ <table className="w-full table-fixed border-separate border-spacing-0 text-sm">
500
+ <thead>
501
+ <tr className="text-left text-xs font-semibold">
502
+ <th className="border-border border-b px-5 py-2.5">
503
+ {t('metrics.links.topClicked')}
504
+ </th>
505
+ <th className="border-border w-40 border-b px-5 py-2.5 text-right">
506
+ {t('metrics.links.totalClicks')}
507
+ </th>
508
+ </tr>
509
+ </thead>
510
+ <tbody>
511
+ {topLinks.length === 0 ? (
512
+ <tr>
513
+ <td className="text-muted px-5 py-4 text-sm" colSpan={2}>
514
+ {t('metrics.links.empty')}
515
+ </td>
516
+ </tr>
517
+ ) : (
518
+ topLinks.map((link) => (
519
+ <tr key={link.url} className="last:[&>td]:border-b-0">
520
+ <td className="border-border truncate border-b px-5 py-2.5" title={link.url}>
521
+ {link.url}
522
+ </td>
523
+ <td className="border-border border-b px-5 py-2.5 text-right tabular-nums">
524
+ {number(link.clicks)}
525
+ </td>
526
+ </tr>
527
+ ))
528
+ )}
529
+ </tbody>
530
+ </table>
531
+ </Panel>
532
+ )}
533
+ </div>
534
+ );
535
+ }
536
+
537
+ export const workflowMetricsRoute = createRoute({
538
+ getParentRoute: () => workflowDetailRoute,
539
+ path: '/',
540
+ component: MetricsTab,
541
+ });