collavre 0.1.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 (410) hide show
  1. checksums.yaml +7 -0
  2. data/README.md +221 -0
  3. data/Rakefile +8 -0
  4. data/app/assets/stylesheets/collavre/actiontext.css +577 -0
  5. data/app/assets/stylesheets/collavre/activity_logs.css +99 -0
  6. data/app/assets/stylesheets/collavre/comments_popup.css +692 -0
  7. data/app/assets/stylesheets/collavre/creatives.css +559 -0
  8. data/app/assets/stylesheets/collavre/dark_mode.css +118 -0
  9. data/app/assets/stylesheets/collavre/mention_menu.css +43 -0
  10. data/app/assets/stylesheets/collavre/popup.css +160 -0
  11. data/app/assets/stylesheets/collavre/print.css +37 -0
  12. data/app/assets/stylesheets/collavre/slide_view.css +79 -0
  13. data/app/assets/stylesheets/collavre/user_menu.css +34 -0
  14. data/app/channels/collavre/comments_presence_channel.rb +54 -0
  15. data/app/channels/collavre/slide_view_channel.rb +11 -0
  16. data/app/channels/collavre/topics_channel.rb +12 -0
  17. data/app/components/collavre/avatar_component.html.erb +15 -0
  18. data/app/components/collavre/avatar_component.rb +59 -0
  19. data/app/components/collavre/inbox/badge_component.html.erb +6 -0
  20. data/app/components/collavre/inbox/badge_component.rb +18 -0
  21. data/app/components/collavre/plans_timeline_component.html.erb +14 -0
  22. data/app/components/collavre/plans_timeline_component.rb +56 -0
  23. data/app/components/collavre/popup_menu_component.html.erb +6 -0
  24. data/app/components/collavre/popup_menu_component.rb +30 -0
  25. data/app/components/collavre/progress_filter_component.html.erb +5 -0
  26. data/app/components/collavre/progress_filter_component.rb +10 -0
  27. data/app/components/collavre/user_mention_menu_component.html.erb +3 -0
  28. data/app/components/collavre/user_mention_menu_component.rb +8 -0
  29. data/app/controllers/collavre/application_controller.rb +15 -0
  30. data/app/controllers/collavre/attachments_controller.rb +44 -0
  31. data/app/controllers/collavre/calendar_events_controller.rb +15 -0
  32. data/app/controllers/collavre/comment_read_pointers_controller.rb +80 -0
  33. data/app/controllers/collavre/comments/activity_logs_controller.rb +26 -0
  34. data/app/controllers/collavre/comments/reactions_controller.rb +82 -0
  35. data/app/controllers/collavre/comments_controller.rb +464 -0
  36. data/app/controllers/collavre/contacts_controller.rb +10 -0
  37. data/app/controllers/collavre/creative_expanded_states_controller.rb +27 -0
  38. data/app/controllers/collavre/creative_imports_controller.rb +24 -0
  39. data/app/controllers/collavre/creative_plans_controller.rb +69 -0
  40. data/app/controllers/collavre/creative_shares_controller.rb +79 -0
  41. data/app/controllers/collavre/creatives_controller.rb +535 -0
  42. data/app/controllers/collavre/devices_controller.rb +19 -0
  43. data/app/controllers/collavre/email_verifications_controller.rb +16 -0
  44. data/app/controllers/collavre/emails_controller.rb +11 -0
  45. data/app/controllers/collavre/github_auth_controller.rb +25 -0
  46. data/app/controllers/collavre/google_auth_controller.rb +43 -0
  47. data/app/controllers/collavre/inbox_items_controller.rb +64 -0
  48. data/app/controllers/collavre/invites_controller.rb +27 -0
  49. data/app/controllers/collavre/notion_auth_controller.rb +25 -0
  50. data/app/controllers/collavre/passwords_controller.rb +37 -0
  51. data/app/controllers/collavre/plans_controller.rb +110 -0
  52. data/app/controllers/collavre/sessions_controller.rb +57 -0
  53. data/app/controllers/collavre/topics_controller.rb +58 -0
  54. data/app/controllers/collavre/user_themes_controller.rb +58 -0
  55. data/app/controllers/collavre/users_controller.rb +390 -0
  56. data/app/helpers/collavre/application_helper.rb +4 -0
  57. data/app/helpers/collavre/comments_helper.rb +9 -0
  58. data/app/helpers/collavre/creatives_helper.rb +343 -0
  59. data/app/helpers/collavre/navigation_helper.rb +163 -0
  60. data/app/helpers/collavre/user_themes_helper.rb +4 -0
  61. data/app/javascript/collavre.js +26 -0
  62. data/app/javascript/components/InlineLexicalEditor.jsx +889 -0
  63. data/app/javascript/components/LinkPopup.jsx +112 -0
  64. data/app/javascript/components/creative_tree_row.js +503 -0
  65. data/app/javascript/components/plugins/attachment_cleanup_plugin.jsx +95 -0
  66. data/app/javascript/components/plugins/image_upload_plugin.jsx +162 -0
  67. data/app/javascript/controllers/click_target_controller.js +13 -0
  68. data/app/javascript/controllers/comment_controller.js +162 -0
  69. data/app/javascript/controllers/comments/__tests__/popup_controller.test.js +68 -0
  70. data/app/javascript/controllers/comments/form_controller.js +530 -0
  71. data/app/javascript/controllers/comments/list_controller.js +715 -0
  72. data/app/javascript/controllers/comments/mention_menu_controller.js +41 -0
  73. data/app/javascript/controllers/comments/popup_controller.js +385 -0
  74. data/app/javascript/controllers/comments/presence_controller.js +311 -0
  75. data/app/javascript/controllers/comments/topics_controller.js +338 -0
  76. data/app/javascript/controllers/common_popup_controller.js +55 -0
  77. data/app/javascript/controllers/creatives/drag_drop_controller.js +45 -0
  78. data/app/javascript/controllers/creatives/expansion_controller.js +222 -0
  79. data/app/javascript/controllers/creatives/import_controller.js +116 -0
  80. data/app/javascript/controllers/creatives/row_editor_controller.js +8 -0
  81. data/app/javascript/controllers/creatives/select_mode_controller.js +231 -0
  82. data/app/javascript/controllers/creatives/set_plan_modal_controller.js +107 -0
  83. data/app/javascript/controllers/creatives/tree_controller.js +218 -0
  84. data/app/javascript/controllers/index.js +79 -0
  85. data/app/javascript/controllers/link_creative_controller.js +91 -0
  86. data/app/javascript/controllers/popup_menu_controller.js +82 -0
  87. data/app/javascript/controllers/progress_filter_controller.js +35 -0
  88. data/app/javascript/controllers/reaction_picker_controller.js +107 -0
  89. data/app/javascript/controllers/share_invite_controller.js +15 -0
  90. data/app/javascript/controllers/share_user_search_controller.js +121 -0
  91. data/app/javascript/controllers/tabs_controller.js +43 -0
  92. data/app/javascript/creatives/drag_drop/dom.js +170 -0
  93. data/app/javascript/creatives/drag_drop/event_handlers.js +846 -0
  94. data/app/javascript/creatives/drag_drop/indicator.js +35 -0
  95. data/app/javascript/creatives/drag_drop/operations.js +116 -0
  96. data/app/javascript/creatives/drag_drop/state.js +31 -0
  97. data/app/javascript/creatives/tree_renderer.js +248 -0
  98. data/app/javascript/lib/api/__tests__/queue_manager.test.js +153 -0
  99. data/app/javascript/lib/api/creatives.js +79 -0
  100. data/app/javascript/lib/api/csrf_fetch.js +22 -0
  101. data/app/javascript/lib/api/drag_drop.js +31 -0
  102. data/app/javascript/lib/api/queue_manager.js +423 -0
  103. data/app/javascript/lib/apply_lexical_styles.js +15 -0
  104. data/app/javascript/lib/common_popup.js +195 -0
  105. data/app/javascript/lib/lexical/__tests__/action_text_attachment_node.test.jsx +91 -0
  106. data/app/javascript/lib/lexical/__tests__/attachment_payload.test.js +194 -0
  107. data/app/javascript/lib/lexical/action_text_attachment_node.js +459 -0
  108. data/app/javascript/lib/lexical/attachment_node.jsx +170 -0
  109. data/app/javascript/lib/lexical/attachment_payload.js +293 -0
  110. data/app/javascript/lib/lexical/dom_attachment_utils.js +66 -0
  111. data/app/javascript/lib/lexical/image_node.jsx +159 -0
  112. data/app/javascript/lib/lexical/style_attributes.js +40 -0
  113. data/app/javascript/lib/responsive_images.js +54 -0
  114. data/app/javascript/lib/turbo_stream_actions.js +33 -0
  115. data/app/javascript/lib/utils/markdown.js +23 -0
  116. data/app/javascript/modules/creative_guide.js +53 -0
  117. data/app/javascript/modules/creative_row_editor.js +1841 -0
  118. data/app/javascript/modules/creative_row_swipe.js +43 -0
  119. data/app/javascript/modules/creatives.js +15 -0
  120. data/app/javascript/modules/export_to_markdown.js +34 -0
  121. data/app/javascript/modules/inbox_panel.js +226 -0
  122. data/app/javascript/modules/lexical_inline_editor.jsx +133 -0
  123. data/app/javascript/modules/mention_menu.js +77 -0
  124. data/app/javascript/modules/plans_menu.js +39 -0
  125. data/app/javascript/modules/plans_timeline.js +397 -0
  126. data/app/javascript/modules/share_modal.js +73 -0
  127. data/app/javascript/modules/share_user_popup.js +77 -0
  128. data/app/javascript/modules/slide_view.js +163 -0
  129. data/app/javascript/services/cable.js +32 -0
  130. data/app/javascript/slide_view.js +2 -0
  131. data/app/javascript/utils/caret_position.js +42 -0
  132. data/app/javascript/utils/clipboard.js +40 -0
  133. data/app/jobs/collavre/ai_agent_job.rb +27 -0
  134. data/app/jobs/collavre/inbox_summary_job.rb +24 -0
  135. data/app/jobs/collavre/notion_export_job.rb +30 -0
  136. data/app/jobs/collavre/notion_sync_job.rb +48 -0
  137. data/app/jobs/collavre/permission_cache_cleanup_job.rb +36 -0
  138. data/app/jobs/collavre/permission_cache_job.rb +71 -0
  139. data/app/jobs/collavre/push_notification_job.rb +86 -0
  140. data/app/mailers/collavre/application_mailer.rb +17 -0
  141. data/app/mailers/collavre/creative_mailer.rb +9 -0
  142. data/app/mailers/collavre/email_verification_mailer.rb +20 -0
  143. data/app/mailers/collavre/inbox_mailer.rb +19 -0
  144. data/app/mailers/collavre/invitation_mailer.rb +16 -0
  145. data/app/mailers/collavre/passwords_mailer.rb +10 -0
  146. data/app/models/collavre/activity_log.rb +13 -0
  147. data/app/models/collavre/application_record.rb +5 -0
  148. data/app/models/collavre/calendar_event.rb +20 -0
  149. data/app/models/collavre/comment.rb +307 -0
  150. data/app/models/collavre/comment_presence_store.rb +30 -0
  151. data/app/models/collavre/comment_reaction.rb +11 -0
  152. data/app/models/collavre/comment_read_pointer.rb +26 -0
  153. data/app/models/collavre/contact.rb +23 -0
  154. data/app/models/collavre/creative.rb +413 -0
  155. data/app/models/collavre/creative_expanded_state.rb +11 -0
  156. data/app/models/collavre/creative_share.rb +122 -0
  157. data/app/models/collavre/creative_shares_cache.rb +18 -0
  158. data/app/models/collavre/current.rb +14 -0
  159. data/app/models/collavre/device.rb +13 -0
  160. data/app/models/collavre/email.rb +14 -0
  161. data/app/models/collavre/github_account.rb +10 -0
  162. data/app/models/collavre/github_repository_link.rb +19 -0
  163. data/app/models/collavre/inbox_item.rb +95 -0
  164. data/app/models/collavre/invitation.rb +22 -0
  165. data/app/models/collavre/label.rb +47 -0
  166. data/app/models/collavre/mcp_tool.rb +30 -0
  167. data/app/models/collavre/notion_account.rb +17 -0
  168. data/app/models/collavre/notion_block_link.rb +10 -0
  169. data/app/models/collavre/notion_page_link.rb +19 -0
  170. data/app/models/collavre/plan.rb +20 -0
  171. data/app/models/collavre/session.rb +24 -0
  172. data/app/models/collavre/system_setting.rb +144 -0
  173. data/app/models/collavre/tag.rb +10 -0
  174. data/app/models/collavre/task.rb +10 -0
  175. data/app/models/collavre/task_action.rb +10 -0
  176. data/app/models/collavre/topic.rb +12 -0
  177. data/app/models/collavre/user.rb +174 -0
  178. data/app/models/collavre/user_theme.rb +10 -0
  179. data/app/models/collavre/variation.rb +5 -0
  180. data/app/models/collavre/webauthn_credential.rb +11 -0
  181. data/app/services/collavre/ai_agent_service.rb +193 -0
  182. data/app/services/collavre/ai_client.rb +183 -0
  183. data/app/services/collavre/ai_system_prompt_renderer.rb +38 -0
  184. data/app/services/collavre/auto_theme_generator.rb +198 -0
  185. data/app/services/collavre/comment_link_formatter.rb +60 -0
  186. data/app/services/collavre/comments/action_executor.rb +262 -0
  187. data/app/services/collavre/comments/action_validator.rb +58 -0
  188. data/app/services/collavre/comments/calendar_command.rb +97 -0
  189. data/app/services/collavre/comments/command_processor.rb +37 -0
  190. data/app/services/collavre/comments/mcp_command.rb +109 -0
  191. data/app/services/collavre/comments/mcp_command_builder.rb +32 -0
  192. data/app/services/collavre/creatives/filter_pipeline.rb +196 -0
  193. data/app/services/collavre/creatives/filters/assignee_filter.rb +30 -0
  194. data/app/services/collavre/creatives/filters/base_filter.rb +24 -0
  195. data/app/services/collavre/creatives/filters/comment_filter.rb +21 -0
  196. data/app/services/collavre/creatives/filters/date_filter.rb +58 -0
  197. data/app/services/collavre/creatives/filters/progress_filter.rb +25 -0
  198. data/app/services/collavre/creatives/filters/search_filter.rb +28 -0
  199. data/app/services/collavre/creatives/filters/tag_filter.rb +16 -0
  200. data/app/services/collavre/creatives/importer.rb +47 -0
  201. data/app/services/collavre/creatives/index_query.rb +191 -0
  202. data/app/services/collavre/creatives/path_exporter.rb +131 -0
  203. data/app/services/collavre/creatives/permission_cache_builder.rb +194 -0
  204. data/app/services/collavre/creatives/permission_checker.rb +42 -0
  205. data/app/services/collavre/creatives/plan_tagger.rb +53 -0
  206. data/app/services/collavre/creatives/progress_service.rb +89 -0
  207. data/app/services/collavre/creatives/reorderer.rb +187 -0
  208. data/app/services/collavre/creatives/tree_builder.rb +231 -0
  209. data/app/services/collavre/creatives/tree_formatter.rb +36 -0
  210. data/app/services/collavre/gemini_parent_recommender.rb +77 -0
  211. data/app/services/collavre/github/client.rb +112 -0
  212. data/app/services/collavre/github/pull_request_analyzer.rb +280 -0
  213. data/app/services/collavre/github/pull_request_processor.rb +181 -0
  214. data/app/services/collavre/github/webhook_provisioner.rb +130 -0
  215. data/app/services/collavre/google_calendar_service.rb +149 -0
  216. data/app/services/collavre/link_preview_fetcher.rb +230 -0
  217. data/app/services/collavre/markdown_importer.rb +202 -0
  218. data/app/services/collavre/mcp_service.rb +217 -0
  219. data/app/services/collavre/notion_client.rb +231 -0
  220. data/app/services/collavre/notion_creative_exporter.rb +296 -0
  221. data/app/services/collavre/notion_service.rb +249 -0
  222. data/app/services/collavre/ppt_importer.rb +76 -0
  223. data/app/services/collavre/ruby_llm_interaction_logger.rb +34 -0
  224. data/app/services/collavre/system_events/context_builder.rb +41 -0
  225. data/app/services/collavre/system_events/dispatcher.rb +19 -0
  226. data/app/services/collavre/system_events/router.rb +72 -0
  227. data/app/services/collavre/tools/creative_retrieval_service.rb +138 -0
  228. data/app/views/admin/shared/_tabs.html.erb +4 -0
  229. data/app/views/collavre/comments/_activity_log_details.html.erb +23 -0
  230. data/app/views/collavre/comments/_comment.html.erb +147 -0
  231. data/app/views/collavre/comments/_comments_popup.html.erb +46 -0
  232. data/app/views/collavre/comments/_list.html.erb +10 -0
  233. data/app/views/collavre/comments/_presence_avatars.html.erb +8 -0
  234. data/app/views/collavre/comments/_reaction_picker.html.erb +15 -0
  235. data/app/views/collavre/comments/_read_receipts.html.erb +19 -0
  236. data/app/views/collavre/creatives/_add_button.html.erb +20 -0
  237. data/app/views/collavre/creatives/_delete_button.html.erb +12 -0
  238. data/app/views/collavre/creatives/_github_integration_modal.html.erb +77 -0
  239. data/app/views/collavre/creatives/_import_upload_zone.html.erb +10 -0
  240. data/app/views/collavre/creatives/_inline_edit_form.html.erb +89 -0
  241. data/app/views/collavre/creatives/_mobile_actions_menu.html.erb +24 -0
  242. data/app/views/collavre/creatives/_notion_integration_modal.html.erb +90 -0
  243. data/app/views/collavre/creatives/_set_plan_modal.html.erb +25 -0
  244. data/app/views/collavre/creatives/_share_button.html.erb +55 -0
  245. data/app/views/collavre/creatives/edit.html.erb +4 -0
  246. data/app/views/collavre/creatives/index.html.erb +192 -0
  247. data/app/views/collavre/creatives/new.html.erb +4 -0
  248. data/app/views/collavre/creatives/show.html.erb +29 -0
  249. data/app/views/collavre/creatives/slide_view.html.erb +20 -0
  250. data/app/views/collavre/email_verification_mailer/verify.html.erb +4 -0
  251. data/app/views/collavre/email_verification_mailer/verify.text.erb +2 -0
  252. data/app/views/collavre/emails/index.html.erb +19 -0
  253. data/app/views/collavre/emails/show.html.erb +5 -0
  254. data/app/views/collavre/inbox_items/_item.html.erb +18 -0
  255. data/app/views/collavre/inbox_items/_items.html.erb +3 -0
  256. data/app/views/collavre/inbox_items/_list.html.erb +7 -0
  257. data/app/views/collavre/inbox_items/index.html.erb +1 -0
  258. data/app/views/collavre/inbox_mailer/daily_summary.html.erb +11 -0
  259. data/app/views/collavre/inbox_mailer/daily_summary.text.erb +8 -0
  260. data/app/views/collavre/invitation_mailer/invite.html.erb +5 -0
  261. data/app/views/collavre/invitation_mailer/invite.text.erb +3 -0
  262. data/app/views/collavre/invites/show.html.erb +8 -0
  263. data/app/views/collavre/passwords/edit.html.erb +12 -0
  264. data/app/views/collavre/passwords/new.html.erb +17 -0
  265. data/app/views/collavre/passwords_mailer/reset.html.erb +4 -0
  266. data/app/views/collavre/passwords_mailer/reset.text.erb +2 -0
  267. data/app/views/collavre/sessions/new.html.erb +27 -0
  268. data/app/views/collavre/sessions/providers/_google.html.erb +7 -0
  269. data/app/views/collavre/sessions/providers/_passkey.html.erb +3 -0
  270. data/app/views/collavre/shared/_link_creative_modal.html.erb +6 -0
  271. data/app/views/collavre/shared/_navigation.html.erb +37 -0
  272. data/app/views/collavre/shared/navigation/_help_button.html.erb +1 -0
  273. data/app/views/collavre/shared/navigation/_inbox_button.html.erb +5 -0
  274. data/app/views/collavre/shared/navigation/_mobile_inbox_button.html.erb +3 -0
  275. data/app/views/collavre/shared/navigation/_mobile_plans_button.html.erb +1 -0
  276. data/app/views/collavre/shared/navigation/_panels.html.erb +18 -0
  277. data/app/views/collavre/shared/navigation/_plans_button.html.erb +3 -0
  278. data/app/views/collavre/shared/navigation/_search_form.html.erb +6 -0
  279. data/app/views/collavre/user_themes/index.html.erb +75 -0
  280. data/app/views/collavre/users/_app_header.html.erb +5 -0
  281. data/app/views/collavre/users/_contact_management.html.erb +77 -0
  282. data/app/views/collavre/users/_id_pwd_fields.html.erb +27 -0
  283. data/app/views/collavre/users/edit_ai.html.erb +79 -0
  284. data/app/views/collavre/users/edit_password.html.erb +27 -0
  285. data/app/views/collavre/users/index.html.erb +80 -0
  286. data/app/views/collavre/users/new.html.erb +33 -0
  287. data/app/views/collavre/users/new_ai.html.erb +87 -0
  288. data/app/views/collavre/users/passkeys.html.erb +43 -0
  289. data/app/views/collavre/users/show.html.erb +143 -0
  290. data/app/views/inbox/badge_component/_count.html.erb +2 -0
  291. data/app/views/layouts/collavre/slide.html.erb +29 -0
  292. data/config/locales/comments.en.yml +114 -0
  293. data/config/locales/comments.ko.yml +110 -0
  294. data/config/locales/contacts.en.yml +16 -0
  295. data/config/locales/contacts.ko.yml +16 -0
  296. data/config/locales/creatives.en.yml +183 -0
  297. data/config/locales/creatives.ko.yml +164 -0
  298. data/config/locales/invites.en.yml +19 -0
  299. data/config/locales/invites.ko.yml +17 -0
  300. data/config/locales/notifications.en.yml +8 -0
  301. data/config/locales/notifications.ko.yml +8 -0
  302. data/config/locales/plans.en.yml +12 -0
  303. data/config/locales/plans.ko.yml +19 -0
  304. data/config/locales/themes.en.yml +29 -0
  305. data/config/locales/themes.ko.yml +27 -0
  306. data/config/locales/users.en.yml +151 -0
  307. data/config/locales/users.ko.yml +146 -0
  308. data/config/routes.rb +92 -0
  309. data/db/migrate/20241201000000_create_notion_integrations.rb +29 -0
  310. data/db/migrate/20250128110017_create_creatives.rb +11 -0
  311. data/db/migrate/20250128120122_create_users.rb +11 -0
  312. data/db/migrate/20250128120123_create_sessions.rb +11 -0
  313. data/db/migrate/20250128123633_create_subscribers.rb +10 -0
  314. data/db/migrate/20250312000000_create_notion_block_links.rb +16 -0
  315. data/db/migrate/20250312010000_allow_multiple_notion_blocks_per_creative.rb +5 -0
  316. data/db/migrate/20250522115048_remove_name_from_creatives.rb +5 -0
  317. data/db/migrate/20250522190651_add_parent_id_to_creatives.rb +5 -0
  318. data/db/migrate/20250523133100_rename_inventory_count_to_progress.rb +13 -0
  319. data/db/migrate/20250523133101_add_sequence_to_creatives.rb +5 -0
  320. data/db/migrate/20250525205100_add_user_id_to_creatives.rb +10 -0
  321. data/db/migrate/20250527014217_create_creative_shares.rb +12 -0
  322. data/db/migrate/20250528142349_add_origin_id_to_creatives.rb +5 -0
  323. data/db/migrate/20250530060200_create_tags.rb +9 -0
  324. data/db/migrate/20250531105150_add_value_to_tags.rb +5 -0
  325. data/db/migrate/20250531140142_create_labels.rb +12 -0
  326. data/db/migrate/20250531140145_change_tag_to_label_reference.rb +6 -0
  327. data/db/migrate/20250601000000_create_comments.rb +10 -0
  328. data/db/migrate/20250601061830_drop_plans_and_variations.rb +6 -0
  329. data/db/migrate/20250604122600_add_owner_to_labels.rb +5 -0
  330. data/db/migrate/20250606000000_rename_email_address_to_email_in_users.rb +7 -0
  331. data/db/migrate/20250606150329_create_creative_hierarchies.rb +16 -0
  332. data/db/migrate/20250610142000_create_creative_expanded_states.rb +11 -0
  333. data/db/migrate/20250611030138_create_invitations.rb +15 -0
  334. data/db/migrate/20250611105524_create_inbox_items.rb +14 -0
  335. data/db/migrate/20250612150000_update_permissions.rb +43 -0
  336. data/db/migrate/20250612232913_add_email_verified_at_to_users.rb +5 -0
  337. data/db/migrate/20250616065905_add_avatar_to_users.rb +5 -0
  338. data/db/migrate/20250617092111_add_display_level_to_users.rb +5 -0
  339. data/db/migrate/20250620004558_create_emails.rb +13 -0
  340. data/db/migrate/20250621000000_create_comment_read_pointers.rb +11 -0
  341. data/db/migrate/20250622000000_add_completion_mark_to_users.rb +5 -0
  342. data/db/migrate/20250623000000_add_theme_to_users.rb +5 -0
  343. data/db/migrate/20250624000000_add_name_to_users.rb +18 -0
  344. data/db/migrate/20250714190000_create_devices.rb +17 -0
  345. data/db/migrate/20250715120000_add_notifications_enabled_to_users.rb +5 -0
  346. data/db/migrate/20250823000000_add_no_access_permission.rb +25 -0
  347. data/db/migrate/20250826000000_add_calendar_id_to_users.rb +5 -0
  348. data/db/migrate/20250827000000_add_google_oauth_tokens_to_users.rb +8 -0
  349. data/db/migrate/20250827061238_add_timezone_to_users.rb +5 -0
  350. data/db/migrate/20250828000000_create_calendar_events.rb +16 -0
  351. data/db/migrate/20250828060000_add_creative_to_calendar_events.rb +5 -0
  352. data/db/migrate/20250830141052_add_locale_to_users.rb +5 -0
  353. data/db/migrate/20250830141101_add_message_key_to_inbox_items.rb +6 -0
  354. data/db/migrate/20250902025423_remove_description_and_featured_image_from_creatives.rb +6 -0
  355. data/db/migrate/20250910000000_add_private_to_comments.rb +5 -0
  356. data/db/migrate/20250910105640_migrate_write_permissions_to_admin.rb +17 -0
  357. data/db/migrate/20250911084338_backfill_creative_in_comment_inbox_items.rb +27 -0
  358. data/db/migrate/20250923002959_deduplicate_device_fcm_tokens.rb +25 -0
  359. data/db/migrate/20250924000000_add_system_admin_to_users.rb +6 -0
  360. data/db/migrate/20250925000000_create_github_integrations.rb +26 -0
  361. data/db/migrate/20250927000000_add_webhook_secret_to_github_repository_links.rb +29 -0
  362. data/db/migrate/20250928000000_add_action_and_approver_to_comments.rb +6 -0
  363. data/db/migrate/20250928010000_add_action_execution_tracking_to_comments.rb +6 -0
  364. data/db/migrate/20250928105957_add_github_gemini_prompt_to_creatives.rb +5 -0
  365. data/db/migrate/20250929000000_add_comment_and_creative_refs_to_inbox_items.rb +6 -0
  366. data/db/migrate/20251001000001_create_contacts.rb +71 -0
  367. data/db/migrate/20251002000000_add_shared_by_to_creative_shares.rb +14 -0
  368. data/db/migrate/20251124120902_add_ai_fields_to_users.rb +7 -0
  369. data/db/migrate/20251124122218_add_created_by_id_to_users.rb +5 -0
  370. data/db/migrate/20251124124521_add_llm_api_key_to_users.rb +5 -0
  371. data/db/migrate/20251124130000_add_searchable_to_users.rb +6 -0
  372. data/db/migrate/20251125072705_migrate_linked_creative_children_to_origin.rb +28 -0
  373. data/db/migrate/20251126040752_add_description_to_creatives.rb +75 -0
  374. data/db/migrate/20251127000000_move_comments_from_linked_creatives_to_origins.rb +18 -0
  375. data/db/migrate/20251201073823_add_tools_to_users.rb +5 -0
  376. data/db/migrate/20251202062715_create_mcp_tools.rb +16 -0
  377. data/db/migrate/20251204125754_create_tasks_and_task_actions.rb +23 -0
  378. data/db/migrate/20251204125756_add_routing_expression_to_users.rb +5 -0
  379. data/db/migrate/20251204161133_set_default_routing_expression_for_ai_agents.rb +13 -0
  380. data/db/migrate/20251211033025_nullify_self_referencing_origins.rb +9 -0
  381. data/db/migrate/20251211080040_create_topics_and_add_topic_to_comments.rb +15 -0
  382. data/db/migrate/20251215143500_create_activity_logs.rb +16 -0
  383. data/db/migrate/20251222125727_create_webauthn_credentials.rb +14 -0
  384. data/db/migrate/20251222125839_add_webauthn_id_to_users.rb +5 -0
  385. data/db/migrate/20251223022315_create_comment_reactions.rb +13 -0
  386. data/db/migrate/20251223072625_create_user_themes.rb +11 -0
  387. data/db/migrate/20251230074456_add_creative_id_to_labels.rb +5 -0
  388. data/db/migrate/20251230113607_refactor_labels.rb +38 -0
  389. data/db/migrate/20251231010012_backfill_tags_for_labels.rb +15 -0
  390. data/db/migrate/20251231013234_drop_subscribers.rb +9 -0
  391. data/db/migrate/20260106090544_allow_null_user_id_in_creative_shares.rb +6 -0
  392. data/db/migrate/20260106160643_create_system_settings.rb +11 -0
  393. data/db/migrate/20260116000000_create_creative_shares_cache.rb +15 -0
  394. data/db/migrate/20260116000001_populate_creative_shares_cache.rb +23 -0
  395. data/db/migrate/20260119022933_make_source_share_id_nullable_in_creative_shares_caches.rb +7 -0
  396. data/db/migrate/20260119023446_populate_owner_cache_entries.rb +25 -0
  397. data/db/migrate/20260119100000_add_account_lockout_to_users.rb +6 -0
  398. data/db/migrate/20260119110000_add_last_active_at_to_sessions.rb +6 -0
  399. data/db/migrate/20260120045354_encrypt_oauth_tokens.rb +60 -0
  400. data/db/migrate/20260120162259_remove_fk_from_creative_shares_caches.rb +7 -0
  401. data/db/migrate/20260120163856_remove_timestamps_from_creative_shares_caches.rb +6 -0
  402. data/lib/collavre/configuration.rb +14 -0
  403. data/lib/collavre/engine.rb +77 -0
  404. data/lib/collavre/user_extensions.rb +29 -0
  405. data/lib/collavre/version.rb +3 -0
  406. data/lib/collavre.rb +26 -0
  407. data/lib/generators/collavre/install/install_generator.rb +105 -0
  408. data/lib/generators/collavre/install/templates/build.cjs.tt +100 -0
  409. data/lib/tasks/collavre_assets.rake +15 -0
  410. metadata +591 -0
@@ -0,0 +1,311 @@
1
+ import { Controller } from '@hotwired/stimulus'
2
+ import { createSubscription } from '../../services/cable'
3
+
4
+ const TYPING_TIMEOUT = 3000
5
+
6
+ export default class extends Controller {
7
+ static targets = ['participants', 'typingIndicator', 'textarea', 'privateCheckbox']
8
+
9
+ connect() {
10
+ this.creativeId = null
11
+ this.participantsData = null
12
+ this.currentPresentIds = []
13
+ this.typingUsers = {}
14
+ this.typingTimers = {}
15
+ this.manualTypingMessage = null
16
+ this.presenceSubscription = null
17
+ this.typingTimeoutHandle = null
18
+ this.hasPresenceConnected = false
19
+
20
+ this.handleInput = this.handleInput.bind(this)
21
+ this.handleFocus = this.handleFocus.bind(this)
22
+ this.handleBlur = this.handleBlur.bind(this)
23
+
24
+ this.textareaTarget.addEventListener('input', this.handleInput)
25
+ this.textareaTarget.addEventListener('focus', this.handleFocus)
26
+ this.textareaTarget.addEventListener('blur', this.handleBlur)
27
+ this.privateCheckboxTarget?.addEventListener('change', () => this.stoppedTyping())
28
+ }
29
+
30
+ disconnect() {
31
+ this.unsubscribe()
32
+ this.textareaTarget.removeEventListener('input', this.handleInput)
33
+ this.textareaTarget.removeEventListener('focus', this.handleFocus)
34
+ this.textareaTarget.removeEventListener('blur', this.handleBlur)
35
+ }
36
+
37
+ get listController() {
38
+ return this.application.getControllerForElementAndIdentifier(this.element, 'comments--list')
39
+ }
40
+
41
+ onPopupOpened({ creativeId }) {
42
+ this.creativeId = creativeId
43
+ this.loadParticipants()
44
+ this.subscribe()
45
+ this.renderParticipants([])
46
+ this.renderTypingIndicator()
47
+ }
48
+
49
+ onPopupClosed() {
50
+ this.unsubscribe()
51
+ this.participantsData = null
52
+ this.currentPresentIds = []
53
+ this.typingUsers = {}
54
+ this.clearTypingTimers()
55
+ this.clearManualTypingMessage()
56
+ this.renderParticipants([])
57
+ this.renderTypingIndicator()
58
+ this.element.style.bottom = ''
59
+ }
60
+
61
+ setManualTypingMessage(message) {
62
+ this.manualTypingMessage = message || null
63
+ this.renderTypingIndicator()
64
+ }
65
+
66
+ clearManualTypingMessage() {
67
+ if (this.manualTypingMessage !== null) {
68
+ this.manualTypingMessage = null
69
+ this.renderTypingIndicator()
70
+ }
71
+ }
72
+
73
+ typing() {
74
+ if (!this.presenceSubscription || this.privateCheckboxTarget?.checked) return
75
+ this.presenceSubscription.perform('typing')
76
+ this.resetTypingTimeout()
77
+ }
78
+
79
+ stoppedTyping() {
80
+ if (this.presenceSubscription) {
81
+ this.presenceSubscription.perform('stopped_typing')
82
+ }
83
+ if (this.typingTimeoutHandle) {
84
+ clearTimeout(this.typingTimeoutHandle)
85
+ this.typingTimeoutHandle = null
86
+ }
87
+ }
88
+
89
+ loadParticipants() {
90
+ if (!this.creativeId) return
91
+ fetch(`/creatives/${this.creativeId}/comments/participants`)
92
+ .then((response) => response.json())
93
+ .then((data) => {
94
+ this.participantsData = data
95
+ this.renderParticipants(this.currentPresentIds)
96
+ this.renderTypingIndicator()
97
+ })
98
+ }
99
+
100
+ subscribe() {
101
+ if (!this.creativeId) return
102
+ this.unsubscribe()
103
+ this.hasPresenceConnected = false
104
+ this.presenceSubscription = createSubscription(
105
+ { channel: 'CommentsPresenceChannel', creative_id: this.creativeId },
106
+ {
107
+ connected: () => {
108
+ if (this.hasPresenceConnected) {
109
+ this.listController?.loadInitialComments()
110
+ }
111
+ this.hasPresenceConnected = true
112
+ },
113
+ received: (data) => this.handlePresenceMessage(data),
114
+ },
115
+ )
116
+ }
117
+
118
+ unsubscribe() {
119
+ if (this.presenceSubscription) {
120
+ this.presenceSubscription.unsubscribe()
121
+ this.presenceSubscription = null
122
+ }
123
+ this.stoppedTyping()
124
+ }
125
+
126
+ handlePresenceMessage(data) {
127
+ if (data.ids) {
128
+ this.currentPresentIds = data.ids.map((id) => parseInt(id, 10))
129
+ this.renderParticipants(this.currentPresentIds)
130
+ this.updateReadReceiptPresence(this.currentPresentIds)
131
+ }
132
+ if (data.typing) {
133
+ const { id, name } = data.typing
134
+ this.typingUsers[id] = name
135
+ this.renderTypingIndicator()
136
+ clearTimeout(this.typingTimers[id])
137
+ this.typingTimers[id] = setTimeout(() => {
138
+ delete this.typingUsers[id]
139
+ delete this.typingTimers[id]
140
+ this.renderTypingIndicator()
141
+ }, TYPING_TIMEOUT)
142
+ }
143
+ if (data.stop_typing) {
144
+ const { id } = data.stop_typing
145
+ delete this.typingUsers[id]
146
+ if (this.typingTimers[id]) {
147
+ clearTimeout(this.typingTimers[id])
148
+ delete this.typingTimers[id]
149
+ }
150
+ this.renderTypingIndicator()
151
+ }
152
+ }
153
+
154
+ renderParticipants(presentIds) {
155
+ if (!this.hasParticipantsTarget || !this.participantsData) {
156
+ if (this.hasParticipantsTarget) this.participantsTarget.innerHTML = ''
157
+ return
158
+ }
159
+ this.participantsTarget.innerHTML = ''
160
+ this.participantsData.forEach((user) => {
161
+ const wrapper = document.createElement('div')
162
+ wrapper.className = 'avatar-wrapper'
163
+ wrapper.style.width = '20px'
164
+ wrapper.style.height = '20px'
165
+
166
+ const img = document.createElement('img')
167
+ img.src = user.avatar_url
168
+ img.alt = ''
169
+ img.width = 20
170
+ img.height = 20
171
+ img.className = 'avatar comment-presence-avatar'
172
+ if (presentIds.indexOf(user.id) === -1) {
173
+ img.classList.add('inactive')
174
+ }
175
+ img.title = user.name
176
+ img.style.borderRadius = '50%'
177
+ if (user.email) img.dataset.email = user.email
178
+ img.dataset.userId = user.id
179
+ img.dataset.userName = user.name
180
+ wrapper.appendChild(img)
181
+
182
+ if (user.default_avatar) {
183
+ const span = document.createElement('span')
184
+ span.className = 'avatar-initial'
185
+ span.textContent = user.initial
186
+ span.style.fontSize = `${Math.round(20 / 2)}px`
187
+ wrapper.appendChild(span)
188
+ }
189
+
190
+ this.participantsTarget.appendChild(wrapper)
191
+ })
192
+
193
+ this.updateReadReceiptPresence(presentIds)
194
+ }
195
+
196
+ renderTypingIndicator() {
197
+ if (!this.hasTypingIndicatorTarget) return
198
+ this.typingIndicatorTarget.innerHTML = ''
199
+
200
+ if (this.manualTypingMessage) {
201
+ const message = document.createElement('span')
202
+ message.textContent = this.manualTypingMessage
203
+ this.typingIndicatorTarget.style.visibility = 'visible'
204
+ this.typingIndicatorTarget.appendChild(message)
205
+ return
206
+ }
207
+
208
+ const ids = Object.keys(this.typingUsers)
209
+ if (ids.length === 0) {
210
+ this.typingIndicatorTarget.style.visibility = 'hidden'
211
+ return
212
+ }
213
+
214
+ this.typingIndicatorTarget.style.visibility = 'visible'
215
+ if (this.participantsData) {
216
+ ids.forEach((id) => {
217
+ const user = this.participantsData.find((participant) => participant.id === parseInt(id, 10))
218
+ if (!user) return
219
+ const wrapper = document.createElement('span')
220
+ wrapper.className = 'avatar-wrapper'
221
+ const img = document.createElement('img')
222
+ img.src = user.avatar_url
223
+ img.alt = ''
224
+ img.width = 20
225
+ img.height = 20
226
+ img.className = 'avatar comment-presence-avatar'
227
+ img.style.borderRadius = '50%'
228
+ wrapper.appendChild(img)
229
+ if (user.default_avatar) {
230
+ const span = document.createElement('span')
231
+ span.className = 'avatar-initial'
232
+ span.textContent = user.initial
233
+ span.style.fontSize = `${Math.round(20 / 2)}px`
234
+ wrapper.appendChild(span)
235
+ }
236
+ this.typingIndicatorTarget.appendChild(wrapper)
237
+ })
238
+ }
239
+ const names = ids.map((id) => this.typingUsers[id])
240
+ const text = document.createElement('span')
241
+ text.textContent = `${names.join(', ')} ...`
242
+ this.typingIndicatorTarget.appendChild(text)
243
+ }
244
+
245
+ clearTypingTimers() {
246
+ Object.values(this.typingTimers).forEach((timer) => clearTimeout(timer))
247
+ this.typingTimers = {}
248
+ }
249
+
250
+ handleInput() {
251
+ this.clearManualTypingMessage()
252
+ this.typing()
253
+ }
254
+
255
+ handleFocus() {
256
+ if (!this.isMobile()) return
257
+ this.adjustForKeyboard()
258
+ if (window.visualViewport) {
259
+ this.visualViewportHandler = () => this.adjustForKeyboard()
260
+ window.visualViewport.addEventListener('resize', this.visualViewportHandler)
261
+ }
262
+ }
263
+
264
+ handleBlur() {
265
+ this.stoppedTyping()
266
+ if (this.typingTimeoutHandle) {
267
+ clearTimeout(this.typingTimeoutHandle)
268
+ this.typingTimeoutHandle = null
269
+ }
270
+ this.element.style.bottom = ''
271
+ if (window.visualViewport && this.visualViewportHandler) {
272
+ window.visualViewport.removeEventListener('resize', this.visualViewportHandler)
273
+ this.visualViewportHandler = null
274
+ }
275
+ }
276
+
277
+ resetTypingTimeout() {
278
+ if (this.typingTimeoutHandle) clearTimeout(this.typingTimeoutHandle)
279
+ this.typingTimeoutHandle = setTimeout(() => this.stoppedTyping(), TYPING_TIMEOUT)
280
+ }
281
+
282
+ adjustForKeyboard() {
283
+ if (!this.isMobile()) return
284
+ let inset = 0
285
+ if (window.visualViewport) {
286
+ const vv = window.visualViewport
287
+ inset = window.innerHeight - (vv.height + vv.offsetTop)
288
+ if (inset < 0) inset = 0
289
+ }
290
+ this.element.style.bottom = `${inset}px`
291
+ }
292
+
293
+ isMobile() {
294
+ return window.innerWidth <= 600
295
+ }
296
+
297
+ updateReadReceiptPresence(presentIds = []) {
298
+ const avatars = this.element.querySelectorAll('.read-receipt-avatars .comment-presence-avatar')
299
+ const presentLookup = new Set(presentIds)
300
+
301
+ avatars.forEach((avatar) => {
302
+ const userId = parseInt(avatar.dataset.userId, 10)
303
+ if (Number.isNaN(userId)) return
304
+ if (presentLookup.has(userId)) {
305
+ avatar.classList.remove('inactive')
306
+ } else {
307
+ avatar.classList.add('inactive')
308
+ }
309
+ })
310
+ }
311
+ }
@@ -0,0 +1,338 @@
1
+ import { Controller } from "@hotwired/stimulus"
2
+ import { createSubscription } from "../../services/cable"
3
+
4
+ export default class extends Controller {
5
+ static targets = ["list"]
6
+
7
+ connect() {
8
+ this.topics = []
9
+ this.canManageTopics = false
10
+ this.subscribedCreativeId = null
11
+ this.topicsSubscription = null
12
+ // Initial load if creativeId is available (e.g. from dataset if set server-side)
13
+ if (this.creativeId) {
14
+ this.loadTopics()
15
+ this.subscribe()
16
+ }
17
+ this.handleNewMessage = this.handleNewMessage.bind(this)
18
+ window.addEventListener('comments--topics:new-message', this.handleNewMessage)
19
+ }
20
+
21
+ disconnect() {
22
+ window.removeEventListener('comments--topics:new-message', this.handleNewMessage)
23
+ this.unsubscribe()
24
+ }
25
+
26
+ onPopupOpened({ creativeId }) {
27
+ this.creativeIdValue = creativeId
28
+ this.subscribe()
29
+ return this.loadTopics()
30
+ }
31
+
32
+ onPopupClosed() {
33
+ this.unsubscribe()
34
+ this.creativeIdValue = null
35
+ }
36
+
37
+ get creativeId() {
38
+ if (this.creativeIdValue) return this.creativeIdValue
39
+
40
+ // Fallback: Check dataset (updated by popup controller)
41
+ if (this.element.dataset.creativeId) return this.element.dataset.creativeId
42
+
43
+ // Fallback: URL/DOM checks
44
+ const treeUrl = document.getElementById("creatives")?.dataset?.creativesTreeUrlValue
45
+ if (treeUrl) {
46
+ const urlParams = new URLSearchParams(treeUrl.split('?')[1]);
47
+ return urlParams.get('parent_id') || urlParams.get('id');
48
+ }
49
+ const match = window.location.pathname.match(/\/creatives\/(\d+)/)
50
+ return match ? match[1] : null
51
+ }
52
+
53
+ async loadTopics() {
54
+ if (!this.creativeId) return
55
+
56
+ try {
57
+ const response = await fetch(`/creatives/${this.creativeId}/topics`)
58
+ if (response.ok) {
59
+ const data = await response.json()
60
+ const topics = Array.isArray(data) ? data : data.topics
61
+ const canManage = Array.isArray(data) ? false : data.can_manage
62
+ this.topics = topics
63
+ this.canManageTopics = canManage
64
+ this.renderTopics(this.topics, this.canManageTopics)
65
+ this.restoreSelection()
66
+ }
67
+ } catch (e) {
68
+ console.error("Failed to load topics", e)
69
+ }
70
+ }
71
+
72
+ restoreSelection() {
73
+ const lastTopicId = this.currentTopicId
74
+ if (lastTopicId) {
75
+ // Validate it exists in list
76
+ const exists = this.listTarget.querySelector(`[data-id="${lastTopicId}"]`)
77
+ if (exists) {
78
+ this.selectTopic(lastTopicId)
79
+ return
80
+ }
81
+ }
82
+
83
+ if (lastTopicId) {
84
+ this.selectTopic("")
85
+ }
86
+ }
87
+
88
+ renderTopics(topics, canManage = false) {
89
+ let html = `<span class="topic-tag ${this.currentTopicId ? '' : 'active'}" data-action="click->comments--topics#select" data-id="">#Main</span>`
90
+
91
+ topics.forEach(topic => {
92
+ // Ensure comparison handles string/number difference
93
+ const isActive = String(this.currentTopicId) === String(topic.id) ? 'active' : ''
94
+ html += `<span class="topic-tag ${isActive}" data-action="click->comments--topics#select" data-id="${topic.id}">
95
+ #${topic.name}`
96
+
97
+ if (canManage) {
98
+ html += `<button class="delete-topic-btn" data-action="click->comments--topics#deleteTopic" data-id="${topic.id}">&times;</button>`
99
+ }
100
+
101
+ html += `</span>`
102
+ })
103
+
104
+ // Add create button container
105
+ if (canManage) {
106
+ html += `<span class="topic-creation-container" data-comments--topics-target="creationContainer">
107
+ <button class="add-topic-btn" data-action="click->comments--topics#showInput">+</button>
108
+ </span>`
109
+ }
110
+
111
+ this.listTarget.innerHTML = html
112
+ }
113
+
114
+ async deleteTopic(event) {
115
+ event.stopPropagation()
116
+ const confirmText = this.listTarget.dataset.confirmDeleteText || "This will delete all messages in this topic. Are you sure?"
117
+ if (!confirm(confirmText)) return
118
+
119
+ const topicId = event.target.dataset.id
120
+ if (!topicId) return
121
+
122
+ try {
123
+ const response = await fetch(`/creatives/${this.creativeId}/topics/${topicId}`, {
124
+ method: 'DELETE',
125
+ headers: {
126
+ 'X-CSRF-Token': document.querySelector('meta[name="csrf-token"]').content
127
+ }
128
+ })
129
+
130
+ if (response.ok) {
131
+ if (String(this.currentTopicId) === String(topicId)) {
132
+ this.currentTopicId = "" // Switch to Main
133
+ this.dispatch("change", { detail: { topicId: "" } })
134
+ }
135
+ this.loadTopics()
136
+ } else {
137
+ alert("Failed to delete topic")
138
+ }
139
+ } catch (e) {
140
+ console.error("Error deleting topic", e)
141
+ }
142
+ }
143
+
144
+ showInput(event) {
145
+ event.preventDefault()
146
+ const container = this.element.querySelector('[data-comments--topics-target="creationContainer"]')
147
+ if (!container) return
148
+
149
+ const placeholder = this.listTarget.dataset.newTopicPlaceholder || "New Topic"
150
+ container.innerHTML = `<input type="text" class="topic-input" placeholder="${placeholder}"
151
+ data-action="keydown->comments--topics#handleInputKey blur->comments--topics#resetInput"
152
+ data-comments--topics-target="input">`
153
+
154
+ const input = container.querySelector('input')
155
+ requestAnimationFrame(() => input.focus())
156
+ }
157
+
158
+ resetInput() {
159
+ // Small delay to allow enter key to process first if that was the cause
160
+ setTimeout(() => {
161
+ const container = this.element.querySelector('[data-comments--topics-target="creationContainer"]')
162
+ if (container && !this.creating) {
163
+ container.innerHTML = `<button class="add-topic-btn" data-action="click->comments--topics#showInput">+</button>`
164
+ }
165
+ }, 200)
166
+ }
167
+
168
+ handleInputKey(event) {
169
+ if (event.key === 'Enter') {
170
+ event.preventDefault()
171
+ const name = event.target.value.trim()
172
+ if (name) {
173
+ this.createTopic(name)
174
+ } else {
175
+ this.resetInput()
176
+ }
177
+ } else if (event.key === 'Escape') {
178
+ this.resetInput()
179
+ }
180
+ }
181
+
182
+ select(event) {
183
+ // Ignore if clicking on delete button (though stopPropagation should handle it)
184
+ if (event.target.closest('.delete-topic-btn')) return
185
+
186
+ const id = event.currentTarget.dataset.id
187
+ this.selectTopic(id)
188
+ }
189
+
190
+ selectTopic(id) {
191
+ this.updateSelectionUI(id)
192
+ if (id) {
193
+ this.clearNewMessageBadge(id)
194
+ }
195
+ // Dispatch event
196
+ this.dispatch("change", { detail: { topicId: id } })
197
+ }
198
+
199
+ updateSelectionUI(id) {
200
+ this.currentTopicId = id
201
+ // Update UI
202
+ this.listTarget.querySelectorAll('.topic-tag').forEach(el => {
203
+ el.classList.toggle('active', String(el.dataset.id) === String(id))
204
+ if (String(el.dataset.id) === String(id)) {
205
+ el.classList.remove('has-new-messages')
206
+ }
207
+ })
208
+ }
209
+
210
+ handleNewMessage(event) {
211
+ const topicId = event.detail.topicId
212
+ if (!topicId) return
213
+
214
+ // Don't show badge if we are currently in this topic (shouldn't happen due to list_controller logic, but safety check)
215
+ if (String(this.currentTopicId) === String(topicId)) return
216
+
217
+ const topicEl = this.listTarget.querySelector(`.topic-tag[data-id="${topicId}"]`)
218
+ if (topicEl) {
219
+ topicEl.classList.add('has-new-messages')
220
+ }
221
+ }
222
+
223
+ clearNewMessageBadge(topicId) {
224
+ const topicEl = this.listTarget.querySelector(`.topic-tag[data-id="${topicId}"]`)
225
+ if (topicEl) {
226
+ topicEl.classList.remove('has-new-messages')
227
+ }
228
+ }
229
+
230
+ async createTopic(name) {
231
+ if (!this.creativeId) return
232
+
233
+ this.creating = true // Prevent blur from resetting immediately
234
+
235
+ try {
236
+ const response = await fetch(`/creatives/${this.creativeId}/topics`, {
237
+ method: 'POST',
238
+ headers: {
239
+ 'Content-Type': 'application/json',
240
+ 'X-CSRF-Token': document.querySelector('meta[name="csrf-token"]').content
241
+ },
242
+ body: JSON.stringify({ topic: { name } })
243
+ })
244
+
245
+ if (response.ok) {
246
+ const topic = await response.json()
247
+ this.currentTopicId = topic.id
248
+ await this.loadTopics()
249
+ // Dispatch change event manually since we skipped the click handler
250
+ this.dispatch("change", { detail: { topicId: topic.id } })
251
+ } else {
252
+ alert("Failed to create topic")
253
+ }
254
+ } catch (e) {
255
+ console.error("Error creating topic", e)
256
+ } finally {
257
+ this.creating = false
258
+ }
259
+ }
260
+
261
+ get currentTopicId() {
262
+ const urlParams = new URLSearchParams(window.location.search)
263
+ const urlTopicId = urlParams.get('topic_id')
264
+ if (urlTopicId) return urlTopicId
265
+
266
+ return localStorage.getItem(`collavre_creative_${this.creativeId}_last_topic`) || ""
267
+ }
268
+
269
+ set currentTopicId(id) {
270
+ if (id) {
271
+ localStorage.setItem(`collavre_creative_${this.creativeId}_last_topic`, id)
272
+ } else {
273
+ localStorage.removeItem(`collavre_creative_${this.creativeId}_last_topic`)
274
+ }
275
+ }
276
+
277
+ subscribe() {
278
+ const creativeId = this.creativeId
279
+ if (!creativeId) return
280
+
281
+ if (this.topicsSubscription && this.subscribedCreativeId === String(creativeId)) return
282
+
283
+ this.unsubscribe()
284
+
285
+ this.subscribedCreativeId = String(creativeId)
286
+ this.topicsSubscription = createSubscription(
287
+ { channel: 'TopicsChannel', creative_id: this.creativeId },
288
+ {
289
+ received: (data) => this.handleTopicMessage(data)
290
+ }
291
+ )
292
+ }
293
+
294
+ unsubscribe() {
295
+ if (this.topicsSubscription) {
296
+ this.topicsSubscription.unsubscribe()
297
+ this.topicsSubscription = null
298
+ }
299
+ this.subscribedCreativeId = null
300
+ }
301
+
302
+ handleTopicMessage(data) {
303
+ if (!data) return
304
+
305
+ const action = data.action || "created"
306
+ if (action === "deleted") {
307
+ this.removeTopic(data.topic_id)
308
+ return
309
+ }
310
+
311
+ if (!data.topic) return
312
+
313
+ const topics = this.topics || []
314
+ const exists = topics.some((topic) => String(topic.id) === String(data.topic.id))
315
+ if (exists) return
316
+
317
+ this.topics = [...topics, data.topic]
318
+ this.renderTopics(this.topics, this.canManageTopics)
319
+ this.restoreSelection()
320
+ }
321
+
322
+ removeTopic(topicId) {
323
+ if (!topicId) return
324
+
325
+ const topics = this.topics || []
326
+ const nextTopics = topics.filter((topic) => String(topic.id) !== String(topicId))
327
+ if (nextTopics.length === topics.length) return
328
+
329
+ this.topics = nextTopics
330
+ if (String(this.currentTopicId) === String(topicId)) {
331
+ this.currentTopicId = ""
332
+ this.dispatch("change", { detail: { topicId: "" } })
333
+ }
334
+
335
+ this.renderTopics(this.topics, this.canManageTopics)
336
+ this.restoreSelection()
337
+ }
338
+ }
@@ -0,0 +1,55 @@
1
+ import { Controller } from '@hotwired/stimulus'
2
+ import CommonPopup from '../lib/common_popup'
3
+
4
+ export default class extends Controller {
5
+ static targets = ['list']
6
+ static values = {
7
+ closeOnOutsideClick: { type: Boolean, default: true }
8
+ }
9
+
10
+ connect() {
11
+ this.popup = new CommonPopup(this.element, {
12
+ listElement: this.listTarget,
13
+ onSelect: this.select.bind(this),
14
+ onClose: this.dispatchClose.bind(this),
15
+ closeOnOutsideClick: this.closeOnOutsideClickValue,
16
+ renderItem: this.renderItem.bind(this)
17
+ })
18
+ }
19
+
20
+ disconnect() {
21
+ this.popup?.hide()
22
+ this.popup = null
23
+ }
24
+
25
+ open(anchorRect) {
26
+ this.popup.showAt(anchorRect)
27
+ }
28
+
29
+ close() {
30
+ this.popup.hide()
31
+ }
32
+
33
+ setItems(items) {
34
+ this.popup.setItems(items)
35
+ }
36
+
37
+ handleKey(event) {
38
+ return this.popup.handleKey(event)
39
+ }
40
+
41
+ // To be overridden or configured via callbacks if needed,
42
+ // but for now we dispatch an event that the parent controller can listen to.
43
+ select(item) {
44
+ this.dispatch('select', { detail: { item } })
45
+ }
46
+
47
+ dispatchClose(reason) {
48
+ this.dispatch('close', { detail: { reason } })
49
+ }
50
+
51
+ // Default renderer, can be overridden by extending class or passing a specific renderer
52
+ renderItem(item) {
53
+ return item.label || item.value || JSON.stringify(item)
54
+ }
55
+ }