@rebasepro/studio 0.0.1-canary.000dc36

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 +114 -0
  2. package/README.md +159 -0
  3. package/dist/ApiExplorer-DHVmWYfK.js +1053 -0
  4. package/dist/ApiExplorer-DHVmWYfK.js.map +1 -0
  5. package/dist/AuthSimulationSelector-CM488Eei.js +106 -0
  6. package/dist/AuthSimulationSelector-CM488Eei.js.map +1 -0
  7. package/dist/BranchesView-DcHZtvXo.js +292 -0
  8. package/dist/BranchesView-DcHZtvXo.js.map +1 -0
  9. package/dist/CronJobsView-CijCToeK.js +456 -0
  10. package/dist/CronJobsView-CijCToeK.js.map +1 -0
  11. package/dist/JSEditor-CSHA0t_O.js +1308 -0
  12. package/dist/JSEditor-CSHA0t_O.js.map +1 -0
  13. package/dist/MonacoEditor-CMYEjiRf.js +161 -0
  14. package/dist/MonacoEditor-CMYEjiRf.js.map +1 -0
  15. package/dist/RLSEditor-BzDjqo6w.js +1872 -0
  16. package/dist/RLSEditor-BzDjqo6w.js.map +1 -0
  17. package/dist/SQLEditor-Cr9Kg_Qg.js +1780 -0
  18. package/dist/SQLEditor-Cr9Kg_Qg.js.map +1 -0
  19. package/dist/SchemaVisualizer-BGpmzyXT.js +1069 -0
  20. package/dist/SchemaVisualizer-BGpmzyXT.js.map +1 -0
  21. package/dist/StorageView-DG9tJZG1.js +811 -0
  22. package/dist/StorageView-DG9tJZG1.js.map +1 -0
  23. package/dist/common/src/collections/CollectionRegistry.d.ts +56 -0
  24. package/dist/common/src/collections/index.d.ts +1 -0
  25. package/dist/common/src/data/buildRebaseData.d.ts +14 -0
  26. package/dist/common/src/index.d.ts +3 -0
  27. package/dist/common/src/util/builders.d.ts +57 -0
  28. package/dist/common/src/util/callbacks.d.ts +6 -0
  29. package/dist/common/src/util/collections.d.ts +11 -0
  30. package/dist/common/src/util/common.d.ts +2 -0
  31. package/dist/common/src/util/conditions.d.ts +26 -0
  32. package/dist/common/src/util/entities.d.ts +58 -0
  33. package/dist/common/src/util/enums.d.ts +3 -0
  34. package/dist/common/src/util/index.d.ts +16 -0
  35. package/dist/common/src/util/navigation_from_path.d.ts +34 -0
  36. package/dist/common/src/util/navigation_utils.d.ts +20 -0
  37. package/dist/common/src/util/parent_references_from_path.d.ts +6 -0
  38. package/dist/common/src/util/paths.d.ts +14 -0
  39. package/dist/common/src/util/permissions.d.ts +5 -0
  40. package/dist/common/src/util/references.d.ts +2 -0
  41. package/dist/common/src/util/relations.d.ts +22 -0
  42. package/dist/common/src/util/resolutions.d.ts +72 -0
  43. package/dist/common/src/util/storage.d.ts +24 -0
  44. package/dist/core/src/components/AIIcon.d.ts +16 -0
  45. package/dist/core/src/components/ConfirmationDialog.d.ts +9 -0
  46. package/dist/core/src/components/Debug/UIReferenceView.d.ts +1 -0
  47. package/dist/core/src/components/Debug/UIStyleGuide.d.ts +1 -0
  48. package/dist/core/src/components/ErrorTooltip.d.ts +2 -0
  49. package/dist/core/src/components/ErrorView.d.ts +21 -0
  50. package/dist/core/src/components/LanguageToggle.d.ts +1 -0
  51. package/dist/core/src/components/LoginView/LoginView.d.ts +68 -0
  52. package/dist/core/src/components/LoginView/index.d.ts +2 -0
  53. package/dist/core/src/components/NotFoundPage.d.ts +1 -0
  54. package/dist/core/src/components/RebaseAuth.d.ts +10 -0
  55. package/dist/core/src/components/RebaseLogo.d.ts +7 -0
  56. package/dist/core/src/components/UnsavedChangesDialog.d.ts +9 -0
  57. package/dist/core/src/components/UserDisplay.d.ts +7 -0
  58. package/dist/core/src/components/UserSelectPopover.d.ts +62 -0
  59. package/dist/core/src/components/UserSettingsView.d.ts +1 -0
  60. package/dist/core/src/components/common/index.d.ts +6 -0
  61. package/dist/core/src/components/common/table_height.d.ts +5 -0
  62. package/dist/core/src/components/common/types.d.ts +63 -0
  63. package/dist/core/src/components/common/useColumnsIds.d.ts +9 -0
  64. package/dist/core/src/components/common/useDataTableController.d.ts +45 -0
  65. package/dist/core/src/components/common/useDebouncedData.d.ts +9 -0
  66. package/dist/core/src/components/common/useScrollRestoration.d.ts +14 -0
  67. package/dist/core/src/components/index.d.ts +16 -0
  68. package/dist/core/src/contexts/AdminModeController.d.ts +4 -0
  69. package/dist/core/src/contexts/AnalyticsContext.d.ts +3 -0
  70. package/dist/core/src/contexts/AuthControllerContext.d.ts +3 -0
  71. package/dist/core/src/contexts/CustomizationControllerContext.d.ts +3 -0
  72. package/dist/core/src/contexts/DataDriverContext.d.ts +3 -0
  73. package/dist/core/src/contexts/DatabaseAdminContext.d.ts +3 -0
  74. package/dist/core/src/contexts/DialogsProvider.d.ts +4 -0
  75. package/dist/core/src/contexts/EffectiveRoleController.d.ts +4 -0
  76. package/dist/core/src/contexts/InternalUserManagementContext.d.ts +3 -0
  77. package/dist/core/src/contexts/ModeController.d.ts +4 -0
  78. package/dist/core/src/contexts/RebaseClientInstanceContext.d.ts +6 -0
  79. package/dist/core/src/contexts/RebaseDataContext.d.ts +3 -0
  80. package/dist/core/src/contexts/SnackbarProvider.d.ts +2 -0
  81. package/dist/core/src/contexts/StorageSourceContext.d.ts +3 -0
  82. package/dist/core/src/contexts/UserConfigurationPersistenceContext.d.ts +3 -0
  83. package/dist/core/src/contexts/index.d.ts +13 -0
  84. package/dist/core/src/core/PluginLifecycleManager.d.ts +17 -0
  85. package/dist/core/src/core/PluginProviderStack.d.ts +21 -0
  86. package/dist/core/src/core/Rebase.d.ts +14 -0
  87. package/dist/core/src/core/RebaseProps.d.ts +136 -0
  88. package/dist/core/src/core/RebaseRouter.d.ts +4 -0
  89. package/dist/core/src/core/RebaseRoutes.d.ts +17 -0
  90. package/dist/core/src/core/index.d.ts +4 -0
  91. package/dist/core/src/hooks/ApiConfigContext.d.ts +24 -0
  92. package/dist/core/src/hooks/data/delete.d.ts +31 -0
  93. package/dist/core/src/hooks/data/save.d.ts +34 -0
  94. package/dist/core/src/hooks/data/useCollectionFetch.d.ts +51 -0
  95. package/dist/core/src/hooks/data/useData.d.ts +13 -0
  96. package/dist/core/src/hooks/data/useDataOrder.d.ts +12 -0
  97. package/dist/core/src/hooks/data/useEntityFetch.d.ts +38 -0
  98. package/dist/core/src/hooks/data/useRelationSelector.d.ts +52 -0
  99. package/dist/core/src/hooks/data/useUserSelector.d.ts +31 -0
  100. package/dist/core/src/hooks/index.d.ts +37 -0
  101. package/dist/core/src/hooks/useAdminModeController.d.ts +19 -0
  102. package/dist/core/src/hooks/useAnalyticsController.d.ts +5 -0
  103. package/dist/core/src/hooks/useAuthController.d.ts +11 -0
  104. package/dist/core/src/hooks/useAuthSubscription.d.ts +2 -0
  105. package/dist/core/src/hooks/useBackendStorageSource.d.ts +30 -0
  106. package/dist/core/src/hooks/useBridgeRegistration.d.ts +18 -0
  107. package/dist/core/src/hooks/useBrowserTitleAndIcon.d.ts +6 -0
  108. package/dist/core/src/hooks/useBuildAdminModeController.d.ts +6 -0
  109. package/dist/core/src/hooks/useBuildEffectiveRoleController.d.ts +8 -0
  110. package/dist/core/src/hooks/useBuildLocalConfigurationPersistence.d.ts +2 -0
  111. package/dist/core/src/hooks/useBuildModeController.d.ts +6 -0
  112. package/dist/core/src/hooks/useClipboard.d.ts +57 -0
  113. package/dist/core/src/hooks/useCollapsedGroups.d.ts +12 -0
  114. package/dist/core/src/hooks/useCustomizationController.d.ts +11 -0
  115. package/dist/core/src/hooks/useDialogsController.d.ts +11 -0
  116. package/dist/core/src/hooks/useEffectiveRoleController.d.ts +7 -0
  117. package/dist/core/src/hooks/useInternalUserManagementController.d.ts +12 -0
  118. package/dist/core/src/hooks/useLargeLayout.d.ts +1 -0
  119. package/dist/core/src/hooks/useModeController.d.ts +19 -0
  120. package/dist/core/src/hooks/usePermissions.d.ts +12 -0
  121. package/dist/core/src/hooks/useRebaseClient.d.ts +5 -0
  122. package/dist/core/src/hooks/useRebaseContext.d.ts +11 -0
  123. package/dist/core/src/hooks/useRebaseRegistry.d.ts +34 -0
  124. package/dist/core/src/hooks/useSlot.d.ts +18 -0
  125. package/dist/core/src/hooks/useSnackbarController.d.ts +20 -0
  126. package/dist/core/src/hooks/useStorageSource.d.ts +7 -0
  127. package/dist/core/src/hooks/useStudioBridge.d.ts +91 -0
  128. package/dist/core/src/hooks/useTranslation.d.ts +17 -0
  129. package/dist/core/src/hooks/useUnsavedChangesDialog.d.ts +12 -0
  130. package/dist/core/src/hooks/useUserConfigurationPersistence.d.ts +8 -0
  131. package/dist/core/src/hooks/useValidateAuthenticator.d.ts +21 -0
  132. package/dist/core/src/i18n/RebaseI18nProvider.d.ts +33 -0
  133. package/dist/core/src/index.d.ts +15 -0
  134. package/dist/core/src/internal/common.d.ts +3 -0
  135. package/dist/core/src/internal/useRestoreScroll.d.ts +6 -0
  136. package/dist/core/src/locales/de.d.ts +2 -0
  137. package/dist/core/src/locales/en.d.ts +10 -0
  138. package/dist/core/src/locales/es.d.ts +10 -0
  139. package/dist/core/src/locales/fr.d.ts +2 -0
  140. package/dist/core/src/locales/hi.d.ts +2 -0
  141. package/dist/core/src/locales/it.d.ts +2 -0
  142. package/dist/core/src/locales/pt.d.ts +7 -0
  143. package/dist/core/src/util/constants.d.ts +1 -0
  144. package/dist/core/src/util/createFormexStub.d.ts +2 -0
  145. package/dist/core/src/util/entity_cache.d.ts +27 -0
  146. package/dist/core/src/util/enums.d.ts +5 -0
  147. package/dist/core/src/util/icon_list.d.ts +5 -0
  148. package/dist/core/src/util/icon_synonyms.d.ts +1 -0
  149. package/dist/core/src/util/icons.d.ts +20 -0
  150. package/dist/core/src/util/index.d.ts +10 -0
  151. package/dist/core/src/util/previews.d.ts +4 -0
  152. package/dist/core/src/util/useStorageUploadController.d.ts +38 -0
  153. package/dist/core/src/util/useTraceUpdate.d.ts +2 -0
  154. package/dist/formex/src/Field.d.ts +52 -0
  155. package/dist/formex/src/Formex.d.ts +7 -0
  156. package/dist/formex/src/index.d.ts +5 -0
  157. package/dist/formex/src/types.d.ts +40 -0
  158. package/dist/formex/src/useCreateFormex.d.ts +14 -0
  159. package/dist/formex/src/utils.d.ts +16 -0
  160. package/dist/index.es.js +726 -0
  161. package/dist/index.es.js.map +1 -0
  162. package/dist/index.umd.js +9567 -0
  163. package/dist/index.umd.js.map +1 -0
  164. package/dist/studio/src/components/ApiExplorer/ApiExplorer.d.ts +9 -0
  165. package/dist/studio/src/components/ApiExplorer/EndpointDetail.d.ts +9 -0
  166. package/dist/studio/src/components/ApiExplorer/TryItPanel.d.ts +15 -0
  167. package/dist/studio/src/components/ApiExplorer/parseSpec.d.ts +16 -0
  168. package/dist/studio/src/components/ApiExplorer/types.d.ts +90 -0
  169. package/dist/studio/src/components/AuthSimulationSelector.d.ts +11 -0
  170. package/dist/studio/src/components/Branches/BranchesView.d.ts +1 -0
  171. package/dist/studio/src/components/CronJobs/CronJobsView.d.ts +1 -0
  172. package/dist/studio/src/components/JSEditor/JSEditor.d.ts +1 -0
  173. package/dist/studio/src/components/JSEditor/JSEditorSidebar.d.ts +21 -0
  174. package/dist/studio/src/components/JSEditor/JSMonacoEditor.d.ts +18 -0
  175. package/dist/studio/src/components/RLSEditor/PolicyEditor.d.ts +9 -0
  176. package/dist/studio/src/components/RLSEditor/RLSEditor.d.ts +19 -0
  177. package/dist/studio/src/components/RLSEditor/index.d.ts +1 -0
  178. package/dist/studio/src/components/RebaseStudio.d.ts +2 -0
  179. package/dist/studio/src/components/SQLEditor/ExplainVisualizer.d.ts +24 -0
  180. package/dist/studio/src/components/SQLEditor/MonacoEditor.d.ts +17 -0
  181. package/dist/studio/src/components/SQLEditor/SQLEditor.d.ts +11 -0
  182. package/dist/studio/src/components/SQLEditor/SQLEditorSidebar.d.ts +21 -0
  183. package/dist/studio/src/components/SQLEditor/SchemaBrowser.d.ts +8 -0
  184. package/dist/studio/src/components/SchemaVisualizer/RelationEdge.d.ts +3 -0
  185. package/dist/studio/src/components/SchemaVisualizer/SchemaVisualizer.d.ts +2 -0
  186. package/dist/studio/src/components/SchemaVisualizer/TableNode.d.ts +3 -0
  187. package/dist/studio/src/components/SchemaVisualizer/index.d.ts +5 -0
  188. package/dist/studio/src/components/SchemaVisualizer/schema-visualizer.utils.d.ts +42 -0
  189. package/dist/studio/src/components/SchemaVisualizer/useSchemaGraph.d.ts +37 -0
  190. package/dist/studio/src/components/StorageView/StorageView.d.ts +1 -0
  191. package/dist/studio/src/components/StudioHomePage.d.ts +9 -0
  192. package/dist/studio/src/index.d.ts +4 -0
  193. package/dist/studio/src/utils/entities.d.ts +0 -0
  194. package/dist/studio/src/utils/pgColumnToProperty.d.ts +6 -0
  195. package/dist/studio/src/utils/sql_utils.d.ts +52 -0
  196. package/dist/types/src/controllers/analytics_controller.d.ts +7 -0
  197. package/dist/types/src/controllers/auth.d.ts +119 -0
  198. package/dist/types/src/controllers/client.d.ts +170 -0
  199. package/dist/types/src/controllers/collection_registry.d.ts +46 -0
  200. package/dist/types/src/controllers/customization_controller.d.ts +60 -0
  201. package/dist/types/src/controllers/data.d.ts +168 -0
  202. package/dist/types/src/controllers/data_driver.d.ts +195 -0
  203. package/dist/types/src/controllers/database_admin.d.ts +11 -0
  204. package/dist/types/src/controllers/dialogs_controller.d.ts +36 -0
  205. package/dist/types/src/controllers/effective_role.d.ts +4 -0
  206. package/dist/types/src/controllers/email.d.ts +34 -0
  207. package/dist/types/src/controllers/index.d.ts +18 -0
  208. package/dist/types/src/controllers/local_config_persistence.d.ts +20 -0
  209. package/dist/types/src/controllers/navigation.d.ts +213 -0
  210. package/dist/types/src/controllers/registry.d.ts +54 -0
  211. package/dist/types/src/controllers/side_dialogs_controller.d.ts +67 -0
  212. package/dist/types/src/controllers/side_entity_controller.d.ts +90 -0
  213. package/dist/types/src/controllers/snackbar.d.ts +24 -0
  214. package/dist/types/src/controllers/storage.d.ts +171 -0
  215. package/dist/types/src/index.d.ts +4 -0
  216. package/dist/types/src/rebase_context.d.ts +105 -0
  217. package/dist/types/src/types/backend.d.ts +536 -0
  218. package/dist/types/src/types/backend_hooks.d.ts +187 -0
  219. package/dist/types/src/types/builders.d.ts +15 -0
  220. package/dist/types/src/types/chips.d.ts +5 -0
  221. package/dist/types/src/types/collections.d.ts +857 -0
  222. package/dist/types/src/types/cron.d.ts +102 -0
  223. package/dist/types/src/types/data_source.d.ts +64 -0
  224. package/dist/types/src/types/entities.d.ts +145 -0
  225. package/dist/types/src/types/entity_actions.d.ts +98 -0
  226. package/dist/types/src/types/entity_callbacks.d.ts +173 -0
  227. package/dist/types/src/types/entity_link_builder.d.ts +7 -0
  228. package/dist/types/src/types/entity_overrides.d.ts +10 -0
  229. package/dist/types/src/types/entity_views.d.ts +59 -0
  230. package/dist/types/src/types/export_import.d.ts +21 -0
  231. package/dist/types/src/types/formex.d.ts +40 -0
  232. package/dist/types/src/types/index.d.ts +25 -0
  233. package/dist/types/src/types/locales.d.ts +4 -0
  234. package/dist/types/src/types/modify_collections.d.ts +5 -0
  235. package/dist/types/src/types/plugins.d.ts +282 -0
  236. package/dist/types/src/types/properties.d.ts +1148 -0
  237. package/dist/types/src/types/property_config.d.ts +70 -0
  238. package/dist/types/src/types/relations.d.ts +336 -0
  239. package/dist/types/src/types/slots.d.ts +262 -0
  240. package/dist/types/src/types/translations.d.ts +874 -0
  241. package/dist/types/src/types/user_management_delegate.d.ts +121 -0
  242. package/dist/types/src/types/websockets.d.ts +78 -0
  243. package/dist/types/src/users/index.d.ts +2 -0
  244. package/dist/types/src/users/roles.d.ts +22 -0
  245. package/dist/types/src/users/user.d.ts +46 -0
  246. package/dist/ui/src/components/Alert.d.ts +12 -0
  247. package/dist/ui/src/components/Autocomplete.d.ts +21 -0
  248. package/dist/ui/src/components/Avatar.d.ts +11 -0
  249. package/dist/ui/src/components/Badge.d.ts +8 -0
  250. package/dist/ui/src/components/BooleanSwitch.d.ts +14 -0
  251. package/dist/ui/src/components/BooleanSwitchWithLabel.d.ts +17 -0
  252. package/dist/ui/src/components/Button.d.ts +14 -0
  253. package/dist/ui/src/components/Card.d.ts +9 -0
  254. package/dist/ui/src/components/CenteredView.d.ts +9 -0
  255. package/dist/ui/src/components/Checkbox.d.ts +13 -0
  256. package/dist/ui/src/components/Chip.d.ts +26 -0
  257. package/dist/ui/src/components/CircularProgress.d.ts +5 -0
  258. package/dist/ui/src/components/CircularProgressCenter.d.ts +11 -0
  259. package/dist/ui/src/components/Collapse.d.ts +9 -0
  260. package/dist/ui/src/components/ColorPicker.d.ts +30 -0
  261. package/dist/ui/src/components/Container.d.ts +8 -0
  262. package/dist/ui/src/components/DateTimeField.d.ts +24 -0
  263. package/dist/ui/src/components/DebouncedTextField.d.ts +2 -0
  264. package/dist/ui/src/components/Dialog.d.ts +39 -0
  265. package/dist/ui/src/components/DialogActions.d.ts +7 -0
  266. package/dist/ui/src/components/DialogContent.d.ts +7 -0
  267. package/dist/ui/src/components/DialogTitle.d.ts +10 -0
  268. package/dist/ui/src/components/ErrorBoundary.d.ts +11 -0
  269. package/dist/ui/src/components/ExpandablePanel.d.ts +12 -0
  270. package/dist/ui/src/components/FileUpload.d.ts +23 -0
  271. package/dist/ui/src/components/IconButton.d.ts +12 -0
  272. package/dist/ui/src/components/InfoLabel.d.ts +5 -0
  273. package/dist/ui/src/components/InputLabel.d.ts +11 -0
  274. package/dist/ui/src/components/Label.d.ts +7 -0
  275. package/dist/ui/src/components/LoadingButton.d.ts +7 -0
  276. package/dist/ui/src/components/Markdown.d.ts +10 -0
  277. package/dist/ui/src/components/Menu.d.ts +23 -0
  278. package/dist/ui/src/components/Menubar.d.ts +80 -0
  279. package/dist/ui/src/components/MultiSelect.d.ts +48 -0
  280. package/dist/ui/src/components/Paper.d.ts +6 -0
  281. package/dist/ui/src/components/Popover.d.ts +24 -0
  282. package/dist/ui/src/components/RadioGroup.d.ts +28 -0
  283. package/dist/ui/src/components/ResizablePanels.d.ts +18 -0
  284. package/dist/ui/src/components/SearchBar.d.ts +22 -0
  285. package/dist/ui/src/components/Select.d.ts +43 -0
  286. package/dist/ui/src/components/Separator.d.ts +5 -0
  287. package/dist/ui/src/components/Sheet.d.ts +22 -0
  288. package/dist/ui/src/components/Skeleton.d.ts +6 -0
  289. package/dist/ui/src/components/Slider.d.ts +21 -0
  290. package/dist/ui/src/components/Table.d.ts +34 -0
  291. package/dist/ui/src/components/Tabs.d.ts +19 -0
  292. package/dist/ui/src/components/TextField.d.ts +58 -0
  293. package/dist/ui/src/components/TextareaAutosize.d.ts +43 -0
  294. package/dist/ui/src/components/ToggleButtonGroup.d.ts +30 -0
  295. package/dist/ui/src/components/Tooltip.d.ts +19 -0
  296. package/dist/ui/src/components/Typography.d.ts +36 -0
  297. package/dist/ui/src/components/VirtualTable/VirtualTable.d.ts +11 -0
  298. package/dist/ui/src/components/VirtualTable/VirtualTableCell.d.ts +21 -0
  299. package/dist/ui/src/components/VirtualTable/VirtualTableHeader.d.ts +29 -0
  300. package/dist/ui/src/components/VirtualTable/VirtualTableHeaderRow.d.ts +2 -0
  301. package/dist/ui/src/components/VirtualTable/VirtualTableProps.d.ts +243 -0
  302. package/dist/ui/src/components/VirtualTable/VirtualTableRow.d.ts +3 -0
  303. package/dist/ui/src/components/VirtualTable/index.d.ts +3 -0
  304. package/dist/ui/src/components/VirtualTable/types.d.ts +38 -0
  305. package/dist/ui/src/components/common/SelectInputLabel.d.ts +5 -0
  306. package/dist/ui/src/components/index.d.ts +53 -0
  307. package/dist/ui/src/hooks/PortalContainerContext.d.ts +31 -0
  308. package/dist/ui/src/hooks/index.d.ts +6 -0
  309. package/dist/ui/src/hooks/useDebounceCallback.d.ts +1 -0
  310. package/dist/ui/src/hooks/useDebounceValue.d.ts +1 -0
  311. package/dist/ui/src/hooks/useDebouncedCallback.d.ts +1 -0
  312. package/dist/ui/src/hooks/useInjectStyles.d.ts +7 -0
  313. package/dist/ui/src/hooks/useOutsideAlerter.d.ts +5 -0
  314. package/dist/ui/src/icons/GitHubIcon.d.ts +2 -0
  315. package/dist/ui/src/icons/HandleIcon.d.ts +1 -0
  316. package/dist/ui/src/icons/Icon.d.ts +20 -0
  317. package/dist/ui/src/icons/cool_icon_keys.d.ts +1 -0
  318. package/dist/ui/src/icons/icon_keys.d.ts +1 -0
  319. package/dist/ui/src/icons/index.d.ts +6 -0
  320. package/dist/ui/src/index.d.ts +5 -0
  321. package/dist/ui/src/styles.d.ts +12 -0
  322. package/dist/ui/src/util/chip_colors.d.ts +4 -0
  323. package/dist/ui/src/util/cls.d.ts +2 -0
  324. package/dist/ui/src/util/debounce.d.ts +10 -0
  325. package/dist/ui/src/util/hash.d.ts +1 -0
  326. package/dist/ui/src/util/index.d.ts +4 -0
  327. package/dist/ui/src/util/key_to_icon_component.d.ts +1 -0
  328. package/package.json +84 -0
  329. package/src/components/ApiExplorer/ApiExplorer.tsx +292 -0
  330. package/src/components/ApiExplorer/EndpointDetail.tsx +271 -0
  331. package/src/components/ApiExplorer/TryItPanel.tsx +486 -0
  332. package/src/components/ApiExplorer/parseSpec.ts +104 -0
  333. package/src/components/ApiExplorer/types.ts +84 -0
  334. package/src/components/AuthSimulationSelector.tsx +73 -0
  335. package/src/components/Branches/BranchesView.tsx +370 -0
  336. package/src/components/CronJobs/CronJobsView.tsx +346 -0
  337. package/src/components/JSEditor/JSEditor.tsx +1033 -0
  338. package/src/components/JSEditor/JSEditorSidebar.tsx +340 -0
  339. package/src/components/JSEditor/JSMonacoEditor.tsx +390 -0
  340. package/src/components/RLSEditor/PolicyEditor.tsx +444 -0
  341. package/src/components/RLSEditor/RLSEditor.tsx +771 -0
  342. package/src/components/RLSEditor/index.ts +1 -0
  343. package/src/components/RebaseStudio.tsx +121 -0
  344. package/src/components/SQLEditor/ExplainVisualizer.tsx +128 -0
  345. package/src/components/SQLEditor/MonacoEditor.tsx +203 -0
  346. package/src/components/SQLEditor/SQLEditor.tsx +1417 -0
  347. package/src/components/SQLEditor/SQLEditorSidebar.tsx +174 -0
  348. package/src/components/SQLEditor/SchemaBrowser.tsx +156 -0
  349. package/src/components/SchemaVisualizer/RelationEdge.tsx +102 -0
  350. package/src/components/SchemaVisualizer/SchemaVisualizer.tsx +663 -0
  351. package/src/components/SchemaVisualizer/TableNode.tsx +257 -0
  352. package/src/components/SchemaVisualizer/index.ts +5 -0
  353. package/src/components/SchemaVisualizer/schema-visualizer.utils.ts +140 -0
  354. package/src/components/SchemaVisualizer/useSchemaGraph.ts +397 -0
  355. package/src/components/StorageView/StorageView.tsx +938 -0
  356. package/src/components/StudioHomePage.tsx +357 -0
  357. package/src/index.ts +31 -0
  358. package/src/utils/entities.ts +2 -0
  359. package/src/utils/pgColumnToProperty.test.ts +401 -0
  360. package/src/utils/pgColumnToProperty.ts +275 -0
  361. package/src/utils/sql_utils.test.ts +265 -0
  362. package/src/utils/sql_utils.ts +291 -0
  363. package/src/vite-env.d.ts +1 -0
@@ -0,0 +1,1417 @@
1
+
2
+ import { IconForView } from "@rebasepro/core";
3
+ import { useStudioCollectionRegistry, useStudioSideEntityController } from "@rebasepro/core";
4
+ import React, { useState, useEffect, useCallback, useRef, useMemo } from "react";
5
+ import { createPortal } from "react-dom";
6
+ import { Button, Paper, Typography, CircularProgress, cls, IconButton, InputLabel, Dialog, DialogContent, DialogActions, DialogTitle, TextField, Tooltip, Alert, Tabs, Tab, defaultBorderMixin, Select, SelectItem, Menu, MenuItem, ResizablePanels, Chip, VirtualTable, VirtualTableColumn , iconSize, Checkbox, TextareaAutosize } from "@rebasepro/ui";
7
+ import { DatabaseIcon, TerminalIcon, XIcon, PlusIcon, PencilIcon, MoreVerticalIcon, MenuIcon, PlayIcon } from "lucide-react";
8
+ // VirtualTableInput is conditionally loaded from CMS when available
9
+ let VirtualTableInput: React.ComponentType<any> | null = null;
10
+ try {
11
+ // @ts-ignore — optional peer dependency
12
+ // eslint-disable-next-line @typescript-eslint/no-require-imports
13
+ const cms = require("@rebasepro/admin");
14
+ VirtualTableInput = cms.VirtualTableInput;
15
+ } catch { /* CMS not available */ }
16
+ import { useRebaseContext, useSnackbarController, ConfirmationDialog, ErrorView, useTranslation } from "@rebasepro/core";
17
+ import { MonacoEditor } from "./MonacoEditor";
18
+ import { SQLEditorSidebar, Snippet } from "./SQLEditorSidebar";
19
+ import { parseFirst } from "pgsql-ast-parser";
20
+ import { determineTableAndPK, resolveQueryCollections, ResolvedQueryCollection } from "../../utils/sql_utils";
21
+ import { ExplainVisualizer } from "./ExplainVisualizer";
22
+
23
+ export interface SQLEditorColumnInfo {
24
+ name: string;
25
+ dataType: string;
26
+ isPrimaryKey: boolean;
27
+ }
28
+
29
+ export interface TableInfo {
30
+ schemaName: string;
31
+ tableName: string;
32
+ columns: SQLEditorColumnInfo[];
33
+ }
34
+
35
+ const QueryLoadingView = () => {
36
+ const [elapsed, setElapsed] = useState(0);
37
+
38
+ useEffect(() => {
39
+ const start = Date.now();
40
+ const interval = setInterval(() => {
41
+ setElapsed(Date.now() - start);
42
+ }, 100);
43
+ return () => clearInterval(interval);
44
+ }, []);
45
+
46
+ return (
47
+ <div className="flex-grow flex items-center justify-center">
48
+ <div className="text-center">
49
+ <CircularProgress size="medium"/>
50
+ <Typography variant="body2" className="mt-4 text-text-secondary dark:text-text-secondary-dark font-mono tracking-tight animate-pulse">
51
+ EXECUTING QUERY...
52
+ </Typography>
53
+ <div className="mt-2 text-xs font-mono text-text-disabled dark:text-text-disabled-dark">
54
+ {(elapsed / 1000).toFixed(1)}s elapsed
55
+ </div>
56
+ </div>
57
+ </div>
58
+ );
59
+ };
60
+
61
+ const STORAGE_KEY_TABS = "rebase_sql_tabs";
62
+ const STORAGE_KEY_ACTIVE_TAB = "rebase_sql_active_tab";
63
+
64
+ const FixedEditorOverlay = ({
65
+ displayValue,
66
+ onSave,
67
+ onCancel
68
+ }: {
69
+ displayValue: string,
70
+ onSave: (val: string | null) => void,
71
+ onCancel: () => void
72
+ }) => {
73
+ const [rect, setRect] = useState<DOMRect | null>(null);
74
+ const [windowSize, setWindowSize] = useState({ width: 1000, height: 1000 });
75
+ const anchorRef = useRef<HTMLDivElement>(null);
76
+
77
+ useEffect(() => {
78
+ if (anchorRef.current && anchorRef.current.parentElement) {
79
+ setRect(anchorRef.current.parentElement.getBoundingClientRect());
80
+ }
81
+ if (typeof window !== "undefined") {
82
+ setWindowSize({ width: window.innerWidth, height: window.innerHeight });
83
+ const handleResize = () => setWindowSize({ width: window.innerWidth, height: window.innerHeight });
84
+ window.addEventListener('resize', handleResize);
85
+ return () => window.removeEventListener('resize', handleResize);
86
+ }
87
+ return undefined;
88
+ }, []);
89
+
90
+ if (!rect) {
91
+ return <div ref={anchorRef} className="w-full h-full min-h-[20px]" />;
92
+ }
93
+
94
+ let top = rect.top - 2;
95
+ let left = rect.left - 2;
96
+ const minWidth = Math.max(rect.width + 4, 250);
97
+ const minHeight = rect.height + 4;
98
+
99
+ if (left + minWidth > windowSize.width) {
100
+ left = Math.max(10, windowSize.width - minWidth - 10);
101
+ }
102
+
103
+ // Calculate a max height that doesn't overflow the bottom
104
+ const maxAvailableHeight = Math.max(50, windowSize.height - top - 10);
105
+ const resolvedMaxHeight = Math.min(300, maxAvailableHeight);
106
+
107
+ // If even the min height overflows, adjust top
108
+ if (top + minHeight > windowSize.height) {
109
+ top = Math.max(10, windowSize.height - minHeight - 10);
110
+ }
111
+
112
+ return (
113
+ <div ref={anchorRef} className="w-full h-full min-h-[20px]">
114
+ {createPortal(
115
+ <div
116
+ className="fixed z-[9999] bg-surface-50 dark:bg-surface-900 border-2 border-primary dark:border-primary-dark shadow-xl flex flex-col"
117
+ style={{
118
+ top,
119
+ left,
120
+ minWidth,
121
+ minHeight,
122
+ maxWidth: Math.min(400, windowSize.width - left - 10)
123
+ }}
124
+ >
125
+ <TextareaAutosize
126
+ className="w-full h-full bg-transparent outline-none border-none ring-0 font-mono text-[13px] text-text-primary dark:text-text-primary-dark px-4 py-1.5 resize-none overflow-y-auto"
127
+ defaultValue={displayValue}
128
+ autoFocus
129
+ style={{ minHeight: '32px', maxHeight: resolvedMaxHeight }}
130
+ onFocus={(e) => {
131
+ const val = e.target.value;
132
+ e.target.value = "";
133
+ e.target.value = val;
134
+ }}
135
+ onBlur={(e) => {
136
+ onSave(e.target.value || null);
137
+ onCancel();
138
+ }}
139
+ onKeyDown={(e) => {
140
+ if (e.key === "Enter" && !e.shiftKey) {
141
+ e.preventDefault();
142
+ onSave((e.currentTarget as HTMLTextAreaElement).value || null);
143
+ onCancel();
144
+ }
145
+ if (e.key === "Escape") onCancel();
146
+ }}
147
+ />
148
+ </div>,
149
+ document.body
150
+ )}
151
+ </div>
152
+ );
153
+ };
154
+
155
+ export const SQLEditor = () => {
156
+ const { databaseAdmin } = useRebaseContext();
157
+ const sideEntityController = useStudioSideEntityController();
158
+ const snackbarController = useSnackbarController();
159
+ const collectionRegistry = useStudioCollectionRegistry();
160
+
161
+ const { t } = useTranslation();
162
+
163
+ // Schema state
164
+ const [schemas, setSchemas] = useState<Record<string, TableInfo[]>>({});
165
+ const [isSchemaLoading, setIsSchemaLoading] = useState(true);
166
+ const schemaFetchedRef = useRef(false);
167
+ const [schemaError, setSchemaError] = useState<string | null>(null);
168
+
169
+ // Connection state
170
+ const [selectedDatabase, setSelectedDatabase] = useState<string | undefined>(() => {
171
+ return localStorage.getItem("rebase_sql_selected_db") || undefined;
172
+ });
173
+ const [selectedRole, setSelectedRole] = useState<string | undefined>(() => {
174
+ return localStorage.getItem("rebase_sql_selected_role") || undefined;
175
+ });
176
+
177
+ const [availableDatabases, setAvailableDatabases] = useState<string[]>([]);
178
+ const [availableRoles, setAvailableRoles] = useState<string[]>([]);
179
+ const [isLoadingConfig, setIsLoadingConfig] = useState(true);
180
+ const [connectionConfigError, setConnectionConfigError] = useState<string | null>(null);
181
+
182
+ // Tabbed interface state
183
+ const [tabs, setTabs] = useState<Array<{
184
+ id: string,
185
+ name: string,
186
+ sql: string,
187
+ database?: string,
188
+ role?: string,
189
+ results: Record<string, unknown>[] | null,
190
+ loading: boolean,
191
+ error: string | null,
192
+ execTime: number | null,
193
+ lastExecutedSql: string | null
194
+ }>>(() => {
195
+ const saved = localStorage.getItem(STORAGE_KEY_TABS);
196
+ if (saved) {
197
+ const parsed = JSON.parse(saved);
198
+ return parsed.map((t: Record<string, unknown>) => ({
199
+ ...t,
200
+ results: null,
201
+ loading: false,
202
+ error: null,
203
+ execTime: null,
204
+ lastExecutedSql: null
205
+ }));
206
+ }
207
+ return [{
208
+ id: "1",
209
+ name: "Query 1",
210
+ sql: "SELECT * FROM ",
211
+ database: localStorage.getItem("rebase_sql_selected_db") || undefined,
212
+ role: localStorage.getItem("rebase_sql_selected_role") || undefined,
213
+ results: null,
214
+ loading: false,
215
+ error: null,
216
+ execTime: null,
217
+ lastExecutedSql: null
218
+ }];
219
+ });
220
+ const [activeTabId, setActiveTabId] = useState<string>(() => {
221
+ return localStorage.getItem(STORAGE_KEY_ACTIVE_TAB) || "1";
222
+ });
223
+
224
+ const activeTab = tabs.find(t => t.id === activeTabId) || tabs[0];
225
+
226
+ // Helper to update active tab state
227
+ const updateActiveTab = useCallback((update: Partial<typeof activeTab>) => {
228
+ setTabs(prev => prev.map(t => t.id === activeTabId ? { ...t,
229
+ ...update } : t));
230
+ }, [activeTabId]);
231
+
232
+ const sql = activeTab.sql;
233
+ const results = activeTab.results;
234
+ const loading = activeTab.loading;
235
+ const error = activeTab.error;
236
+ const execTime = activeTab.execTime;
237
+
238
+ const setSql = (newSql: string) => updateActiveTab({ sql: newSql });
239
+ const setResults = (newResults: Record<string, unknown>[] | null) => updateActiveTab({ results: newResults });
240
+ const setLoading = (newLoading: boolean) => updateActiveTab({ loading: newLoading });
241
+ const setError = (newError: string | null) => updateActiveTab({ error: newError });
242
+
243
+ useEffect(() => {
244
+ let mounted = true;
245
+ const fetchConnectionConfig = async () => {
246
+ if (!databaseAdmin?.fetchAvailableDatabases || !databaseAdmin?.fetchAvailableRoles) {
247
+ setConnectionConfigError(t("studio_sql_sql_not_supported"));
248
+ setIsLoadingConfig(false);
249
+ return;
250
+ }
251
+
252
+ try {
253
+ const [dbs, roles, currentDbFromApi] = await Promise.all([
254
+ databaseAdmin.fetchAvailableDatabases(),
255
+ databaseAdmin.fetchAvailableRoles(),
256
+ typeof databaseAdmin?.fetchCurrentDatabase === "function" ? databaseAdmin.fetchCurrentDatabase() : Promise.resolve(undefined)
257
+ ]);
258
+
259
+ if (mounted) {
260
+ setAvailableDatabases(dbs);
261
+ setAvailableRoles(roles);
262
+
263
+ const loadedDb = localStorage.getItem("rebase_sql_selected_db") || undefined;
264
+ const loadedRole = localStorage.getItem("rebase_sql_selected_role") || undefined;
265
+
266
+ const initialActiveTabId = localStorage.getItem(STORAGE_KEY_ACTIVE_TAB) || "1";
267
+ let initialTabs: any[] = [];
268
+ try {
269
+ const savedTabs = localStorage.getItem(STORAGE_KEY_TABS);
270
+ if (savedTabs) initialTabs = JSON.parse(savedTabs);
271
+ } catch (e) { /* ignore */ }
272
+ const currentActiveTab = initialTabs.find(t => t.id === initialActiveTabId);
273
+
274
+ let actualDb = currentActiveTab?.database || loadedDb;
275
+ if (actualDb && !dbs.includes(actualDb)) actualDb = undefined;
276
+ if (!actualDb && dbs.length > 0) {
277
+ actualDb = currentDbFromApi && dbs.includes(currentDbFromApi) ? currentDbFromApi : dbs[0];
278
+ }
279
+
280
+ if (actualDb) {
281
+ setSelectedDatabase(actualDb);
282
+ localStorage.setItem("rebase_sql_selected_db", actualDb);
283
+ setTabs(prev => prev.map(t => t.id === initialActiveTabId && !t.database ? { ...t,
284
+ database: actualDb } : t));
285
+ }
286
+
287
+ let actualRole = currentActiveTab?.role || loadedRole;
288
+ if (actualRole && !roles.includes(actualRole)) actualRole = undefined;
289
+ if (!actualRole && roles.length > 0) {
290
+ actualRole = roles.includes("postgres") ? "postgres" : roles[0];
291
+ }
292
+
293
+ if (actualRole) {
294
+ setSelectedRole(actualRole);
295
+ localStorage.setItem("rebase_sql_selected_role", actualRole);
296
+ setTabs(prev => prev.map(t => t.id === initialActiveTabId && !t.role ? { ...t,
297
+ role: actualRole } : t));
298
+ }
299
+ }
300
+ } catch (err: unknown) {
301
+ console.error("Failed to fetch databases or roles:", err);
302
+ if (mounted) {
303
+ const message = err instanceof Error ? err.message : String(err);
304
+ setConnectionConfigError(t("studio_sql_fetch_error", { message }));
305
+ }
306
+ } finally {
307
+ if (mounted) {
308
+ setIsLoadingConfig(false);
309
+ }
310
+ }
311
+ };
312
+
313
+ fetchConnectionConfig();
314
+
315
+ return () => { mounted = false; };
316
+ }, [databaseAdmin]);
317
+
318
+ const handleDatabaseChange = (db: string, tabId?: string) => {
319
+ setSelectedDatabase(db);
320
+ localStorage.setItem("rebase_sql_selected_db", db);
321
+ setTabs(prev => prev.map(t => t.id === (tabId || activeTabId) ? { ...t,
322
+ database: db } : t));
323
+ // Reset so the schema will be re-fetched for the new database
324
+ schemaFetchedRef.current = false;
325
+ };
326
+
327
+ const handleRoleChange = (role: string, tabId?: string) => {
328
+ setSelectedRole(role);
329
+ localStorage.setItem("rebase_sql_selected_role", role);
330
+ setTabs(prev => prev.map(t => t.id === (tabId || activeTabId) ? { ...t,
331
+ role } : t));
332
+ };
333
+
334
+ const handleTabChange = useCallback((newTabId: string) => {
335
+ setActiveTabId(newTabId);
336
+ const newTab = tabs.find(t => t.id === newTabId);
337
+ if (newTab) {
338
+ if (newTab.database && newTab.database !== selectedDatabase) {
339
+ setSelectedDatabase(newTab.database);
340
+ localStorage.setItem("rebase_sql_selected_db", newTab.database);
341
+ schemaFetchedRef.current = false;
342
+ } else if (!newTab.database && selectedDatabase) {
343
+ setTabs(prev => prev.map(t => t.id === newTabId ? { ...t,
344
+ database: selectedDatabase } : t));
345
+ }
346
+
347
+ if (newTab.role && newTab.role !== selectedRole) {
348
+ setSelectedRole(newTab.role);
349
+ localStorage.setItem("rebase_sql_selected_role", newTab.role);
350
+ } else if (!newTab.role && selectedRole) {
351
+ setTabs(prev => prev.map(t => t.id === newTabId ? { ...t,
352
+ role: selectedRole } : t));
353
+ }
354
+ }
355
+ }, [tabs, selectedDatabase, selectedRole]);
356
+
357
+ const fetchSchema = useCallback(async () => {
358
+ if (!databaseAdmin?.executeSql) {
359
+ setSchemaError(t("studio_sql_sql_not_supported"));
360
+ setIsSchemaLoading(false);
361
+ return;
362
+ }
363
+
364
+ setIsSchemaLoading(true);
365
+ setSchemaError(null);
366
+ try {
367
+ const sql = `
368
+ SELECT
369
+ c.table_schema as schema,
370
+ c.table_name as "table",
371
+ c.column_name as "column",
372
+ c.data_type as "data_type",
373
+ CASE WHEN kcu.column_name IS NOT NULL THEN true ELSE false END as "is_pk"
374
+ FROM
375
+ information_schema.columns c
376
+ LEFT JOIN information_schema.table_constraints tc
377
+ ON tc.table_schema = c.table_schema
378
+ AND tc.table_name = c.table_name
379
+ AND tc.constraint_type = 'PRIMARY KEY'
380
+ LEFT JOIN information_schema.key_column_usage kcu
381
+ ON kcu.constraint_name = tc.constraint_name
382
+ AND kcu.table_schema = tc.table_schema
383
+ AND kcu.table_name = tc.table_name
384
+ AND kcu.column_name = c.column_name
385
+ WHERE
386
+ c.table_schema NOT IN ('information_schema', 'pg_catalog')
387
+ ORDER BY
388
+ c.table_schema, c.table_name, c.ordinal_position;
389
+ `;
390
+ // Pass the selected database so schema introspection targets the right DB.
391
+ const result = await databaseAdmin!.executeSql!(sql, { database: selectedDatabase });
392
+
393
+ const processGrouped = (data: Record<string, unknown>[]) => {
394
+ const grouped = data.reduce((acc: Record<string, TableInfo[]>, curr: Record<string, unknown>) => {
395
+ const schema = (curr.schema || curr.SCHEMA || curr.table_schema || "public") as string;
396
+ const table = (curr.table || curr.TABLE || curr.table_name) as string;
397
+ const column = (curr.column || curr.COLUMN || curr.column_name) as string;
398
+ const dataType = (curr.data_type || curr.DATA_TYPE || "") as string;
399
+ const isPrimaryKey = curr.is_pk === true || curr.is_pk === "true";
400
+
401
+ if (!acc[schema]) acc[schema] = [];
402
+ let tableInfo = acc[schema].find(t => t.tableName === table);
403
+ if (!tableInfo) {
404
+ tableInfo = { schemaName: schema,
405
+ tableName: table,
406
+ columns: [] };
407
+ acc[schema].push(tableInfo);
408
+ }
409
+ tableInfo.columns.push({ name: column,
410
+ dataType,
411
+ isPrimaryKey });
412
+ return acc;
413
+ }, {});
414
+ setSchemas(grouped);
415
+ };
416
+
417
+ if (!result || !Array.isArray(result)) {
418
+ if (result && typeof result === "object" && "rows" in result && Array.isArray((result as { rows: Record<string, unknown>[] }).rows)) {
419
+ processGrouped((result as { rows: Record<string, unknown>[] }).rows);
420
+ } else {
421
+ setSchemaError(t("studio_sql_unexpected_format", { type: typeof result }));
422
+ setSchemas({});
423
+ }
424
+ } else if (result.length === 0) {
425
+ setSchemas({});
426
+ setSchemaError(t("studio_sql_no_tables"));
427
+ } else {
428
+ processGrouped(result);
429
+ }
430
+
431
+ schemaFetchedRef.current = true;
432
+ } catch (e: unknown) {
433
+ console.error("Schema fetch error:", e);
434
+ const message = e instanceof Error ? e.message : String(e);
435
+ setSchemaError(t("studio_sql_schema_fetch_error", { message }));
436
+ } finally {
437
+ setIsSchemaLoading(false);
438
+ }
439
+ }, [databaseAdmin, selectedDatabase]);
440
+
441
+ useEffect(() => {
442
+ // Fetch schema after config finishes loading, and re-fetch when the selected database changes.
443
+ if (!isLoadingConfig && !schemaFetchedRef.current) {
444
+ fetchSchema();
445
+ }
446
+ }, [fetchSchema, isLoadingConfig, selectedDatabase]);
447
+
448
+ const [autoLimit, setAutoLimit] = useState(true);
449
+ const [isConfirmDialogOpen, setIsConfirmDialogOpen] = useState(false);
450
+ const [pendingAction, setPendingAction] = useState<(() => void) | null>(null);
451
+
452
+ // Inline editing state
453
+ const [editingCell, setEditingCell] = useState<{ rowIndex: number, columnKey: string, initialValue: unknown } | null>(null);
454
+
455
+ const handleDoubleClick = useCallback((rowIndex: number, columnKey: string, initialValue: unknown, rowData: Record<string, unknown>) => {
456
+ if (!activeTab.lastExecutedSql) {
457
+ snackbarController.open({
458
+ type: "error",
459
+ message: t("studio_sql_cannot_edit_missing_query")
460
+ });
461
+ return;
462
+ }
463
+
464
+ const resolution = determineTableAndPK(activeTab.lastExecutedSql, columnKey, schemas);
465
+
466
+ if (resolution.error || !resolution.primaryKeys || resolution.primaryKeys.length === 0) {
467
+ snackbarController.open({
468
+ type: "error",
469
+ message: resolution.error || t("studio_sql_cannot_resolve_table")
470
+ });
471
+ return;
472
+ }
473
+
474
+ // Check all PK values are present in the row
475
+ const missingPKs = resolution.primaryKeys.filter(
476
+ pk => rowData[pk.resultColumn] === undefined || rowData[pk.resultColumn] === null
477
+ );
478
+ if (missingPKs.length > 0) {
479
+ snackbarController.open({
480
+ type: "error",
481
+ message: t("studio_sql_missing_pk", { columns: missingPKs.map(pk => `"${pk.resultColumn}"`).join(", ") })
482
+ });
483
+ return;
484
+ }
485
+
486
+ setEditingCell({ rowIndex,
487
+ columnKey,
488
+ initialValue });
489
+ }, [activeTab.lastExecutedSql, schemas, snackbarController]);
490
+
491
+ const handleCellSave = useCallback(async (newValue: string | null, rowData: Record<string, unknown>, columnKey: string, rowIndex: number) => {
492
+ if (!editingCell || !activeTab.lastExecutedSql) return;
493
+
494
+ setEditingCell(null); // Optimistically close
495
+
496
+ if (newValue === editingCell.initialValue) return;
497
+
498
+ const resolution = determineTableAndPK(activeTab.lastExecutedSql, columnKey, schemas);
499
+ if (resolution.error || !resolution.tableName || !resolution.primaryKeys || resolution.primaryKeys.length === 0) {
500
+ snackbarController.open({ type: "error",
501
+ message: resolution.error || "Resolution failed." });
502
+ return;
503
+ }
504
+
505
+ const tableName = resolution.tableName;
506
+
507
+ const formatValue = (val: unknown) => {
508
+ if (val === null || val === undefined) return "NULL";
509
+ if (typeof val === "number") return val;
510
+ if (typeof val === "boolean") return val ? "TRUE" : "FALSE";
511
+ return `'${String(val).replace(/'/g, "''")}'`;
512
+ };
513
+
514
+ // Resolve the actual DB column name for the edited column (may differ from the result alias)
515
+ // e.g. if the query has `a.name AS author_name`, columnKey = "author_name" but DB column = "name"
516
+ const resolveDbColumnName = (resultColKey: string): string => {
517
+ try {
518
+ const ast = parseFirst(activeTab.lastExecutedSql!);
519
+ if (ast.type === "select" && ast.columns) {
520
+ for (const col of ast.columns) {
521
+ if (col.expr?.type === "ref") {
522
+ const alias = col.alias?.name;
523
+ const colName = col.expr.name;
524
+ if (alias === resultColKey || (!alias && colName === resultColKey)) {
525
+ return colName;
526
+ }
527
+ }
528
+ }
529
+ }
530
+ } catch { /* fall back to columnKey */ }
531
+ return resultColKey;
532
+ };
533
+
534
+ const dbColumnName = resolveDbColumnName(columnKey);
535
+
536
+ // Build composite WHERE clause
537
+ const whereConditions = resolution.primaryKeys.map(
538
+ pk => `"${pk.dbColumn}" = ${formatValue(rowData[pk.resultColumn])}`
539
+ ).join(" AND ");
540
+
541
+ const updateSql = `UPDATE "${tableName}" SET "${dbColumnName}" = ${formatValue(newValue)} WHERE ${whereConditions};`;
542
+
543
+ try {
544
+ if (databaseAdmin?.executeSql) {
545
+ await databaseAdmin.executeSql(updateSql, { database: selectedDatabase,
546
+ role: selectedRole });
547
+
548
+ const newResults = [...(activeTab.results || [])];
549
+ if (newResults[rowIndex]) {
550
+ newResults[rowIndex] = { ...newResults[rowIndex],
551
+ [columnKey]: newValue };
552
+ }
553
+ updateActiveTab({ results: newResults });
554
+
555
+ snackbarController.open({
556
+ type: "success",
557
+ message: t("studio_sql_row_updated")
558
+ });
559
+ }
560
+ } catch (e: unknown) {
561
+ snackbarController.open({
562
+ type: "error",
563
+ message: t("studio_sql_update_failed", { message: e instanceof Error ? e.message : String(e) })
564
+ });
565
+ }
566
+ }, [editingCell, schemas, activeTab.lastExecutedSql, activeTab.results, databaseAdmin, updateActiveTab, snackbarController, selectedDatabase, selectedRole]);
567
+
568
+ const [columnWidths, setColumnWidths] = useState<Record<string, Record<string, number>>>(() => {
569
+ const saved = localStorage.getItem("rebase_sql_column_widths");
570
+ return saved ? JSON.parse(saved) : {};
571
+ });
572
+ const [snippets, setSnippets] = useState<Snippet[]>([]);
573
+ const [history, setHistory] = useState<string[]>([]);
574
+ const [isSaveDialogOpen, setIsSaveDialogOpen] = useState(false);
575
+ const [newSnippetName, setNewSnippetName] = useState("");
576
+
577
+ // Load from local storage
578
+ useEffect(() => {
579
+ const savedSnippets = localStorage.getItem("rebase_sql_snippets");
580
+ if (savedSnippets) setSnippets(JSON.parse(savedSnippets));
581
+
582
+ const savedHistory = localStorage.getItem("rebase_sql_history");
583
+ if (savedHistory) setHistory(JSON.parse(savedHistory));
584
+ }, []);
585
+
586
+ // Save tabs and active tab to local storage
587
+ useEffect(() => {
588
+ const sanitizedTabs = tabs.map(t => ({
589
+ id: t.id,
590
+ name: t.name,
591
+ sql: t.sql,
592
+ database: t.database,
593
+ role: t.role
594
+ }));
595
+ localStorage.setItem(STORAGE_KEY_TABS, JSON.stringify(sanitizedTabs));
596
+ }, [tabs]);
597
+
598
+ useEffect(() => {
599
+ localStorage.setItem(STORAGE_KEY_ACTIVE_TAB, activeTabId);
600
+ }, [activeTabId]);
601
+
602
+ const saveSnippets = (newSnippets: Snippet[]) => {
603
+ setSnippets(newSnippets);
604
+ localStorage.setItem("rebase_sql_snippets", JSON.stringify(newSnippets));
605
+ };
606
+
607
+ const saveHistory = (newHistory: string[]) => {
608
+ setHistory(newHistory);
609
+ localStorage.setItem("rebase_sql_history", JSON.stringify(newHistory.slice(-50)));
610
+ };
611
+
612
+ const handleDeleteSnippet = (id: string) => {
613
+ saveSnippets(snippets.filter(s => s.id !== id));
614
+ };
615
+
616
+ const handleAddTab = () => {
617
+ const newId = Math.random().toString(36).substring(2, 9);
618
+
619
+ // Find the next available query number
620
+ let maxNumber = 0;
621
+ tabs.forEach(tab => {
622
+ const match = tab.name.match(/^Query (\d+)$/);
623
+ if (match) {
624
+ const num = parseInt(match[1], 10);
625
+ if (num > maxNumber) maxNumber = num;
626
+ }
627
+ });
628
+ const name = `Query ${maxNumber + 1}`;
629
+ setTabs(prev => [...prev, {
630
+ id: newId,
631
+ name,
632
+ sql: "SELECT * FROM ",
633
+ database: selectedDatabase,
634
+ role: selectedRole,
635
+ results: null,
636
+ loading: false,
637
+ error: null,
638
+ execTime: null,
639
+ lastExecutedSql: null
640
+ }]);
641
+ setActiveTabId(newId);
642
+ };
643
+
644
+ const handleCloseTab = (id: string, e: React.MouseEvent) => {
645
+ e.stopPropagation();
646
+ if (tabs.length === 1) return;
647
+
648
+ const tabIndex = tabs.findIndex(t => t.id === id);
649
+ const newTabs = tabs.filter(t => t.id !== id);
650
+ setTabs(newTabs);
651
+
652
+ if (activeTabId === id) {
653
+ // Find a new active tab: the one at the same index, or the last one if we closed the last
654
+ const nextIndex = Math.min(tabIndex, newTabs.length - 1);
655
+ if (newTabs[nextIndex]) {
656
+ setActiveTabId(newTabs[nextIndex].id);
657
+ }
658
+ }
659
+ };
660
+
661
+ const handleColumnResize = useCallback(({ key, width }: { key: string, width: number }) => {
662
+ setColumnWidths(prev => {
663
+ const newWidths = {
664
+ ...prev,
665
+ [activeTab.sql]: {
666
+ ...(prev[activeTab.sql] || {}),
667
+ [key]: width
668
+ }
669
+ };
670
+ localStorage.setItem("rebase_sql_column_widths", JSON.stringify(newWidths));
671
+ return newWidths;
672
+ });
673
+ }, [activeTab.sql]);
674
+
675
+ const handlePrettify = () => {
676
+ // Simple formatting for now
677
+ const formatted = activeTab.sql
678
+ .replace(/\s+/g, " ")
679
+ .replace(/\s?,\s?/g, ", ")
680
+ .replace(/\s?=\s?/g, " = ")
681
+ .trim();
682
+ setSql(formatted);
683
+ };
684
+
685
+ const handleExplain = async () => {
686
+ const explainSql = `EXPLAIN (FORMAT JSON, ANALYZE) ${activeTab.sql}`;
687
+ updateActiveTab({ loading: true,
688
+ error: null,
689
+ results: null });
690
+ const start = performance.now();
691
+ try {
692
+ if (databaseAdmin?.executeSql) {
693
+ const result = await databaseAdmin.executeSql(explainSql, { database: selectedDatabase,
694
+ role: selectedRole });
695
+ updateActiveTab({ results: result,
696
+ execTime: Math.round(performance.now() - start) });
697
+ }
698
+ } catch (e: unknown) {
699
+ const message = e instanceof Error ? e.message : String(e);
700
+ updateActiveTab({ error: message || t("studio_sql_error_explaining") });
701
+ } finally {
702
+ updateActiveTab({ loading: false });
703
+ }
704
+ };
705
+
706
+ const executeRun = useCallback(async (sqlOverride?: string) => {
707
+ let sqlToRun = sqlOverride || activeTab.sql;
708
+ const upperSql = sqlToRun.toUpperCase();
709
+
710
+ const isAggregate = /\b(COUNT|SUM|AVG|MIN|MAX)\s*\(/i.test(sqlToRun);
711
+ const isExplain = /\bEXPLAIN\b/i.test(sqlToRun);
712
+
713
+ if (autoLimit && upperSql.includes("SELECT") && !upperSql.includes("LIMIT") && !isAggregate && !isExplain) {
714
+ // Remove trailing semicolon if present to safely append LIMIT
715
+ sqlToRun = sqlToRun.trim().replace(/;$/, "");
716
+ sqlToRun = `${sqlToRun} LIMIT 1000;`;
717
+ }
718
+
719
+ updateActiveTab({ loading: true,
720
+ error: null,
721
+ results: null });
722
+ const start = performance.now();
723
+
724
+ try {
725
+ if (databaseAdmin?.executeSql) {
726
+ const result = await databaseAdmin.executeSql(sqlToRun, { database: selectedDatabase,
727
+ role: selectedRole });
728
+ updateActiveTab({
729
+ results: result,
730
+ execTime: Math.round(performance.now() - start),
731
+ lastExecutedSql: sqlToRun
732
+ });
733
+
734
+ if (history[history.length - 1] !== activeTab.sql) {
735
+ saveHistory([...history, activeTab.sql]);
736
+ }
737
+ } else {
738
+ updateActiveTab({ error: t("studio_sql_execution_not_supported") });
739
+ }
740
+ } catch (e: unknown) {
741
+ const message = e instanceof Error ? e.message : String(e);
742
+ updateActiveTab({ error: message || t("studio_sql_error_executing") });
743
+ } finally {
744
+ updateActiveTab({ loading: false });
745
+ }
746
+ }, [activeTab.sql, autoLimit, databaseAdmin, history, updateActiveTab]);
747
+
748
+ const handleRun = useCallback(async (selectedText?: string) => {
749
+ const sqlTarget = selectedText || activeTab.sql;
750
+ if (!sqlTarget.trim()) return;
751
+
752
+ // Destructive operation check
753
+ const destructiveKeywords = ["DELETE", "DROP", "TRUNCATE", "UPDATE"];
754
+ const hasDestructive = destructiveKeywords.some(kw => sqlTarget.toUpperCase().includes(kw));
755
+ const hasWhere = sqlTarget.toUpperCase().includes("WHERE");
756
+
757
+ if (hasDestructive && (!hasWhere || sqlTarget.toUpperCase().includes("DROP") || sqlTarget.toUpperCase().includes("TRUNCATE"))) {
758
+ setPendingAction(() => () => executeRun(selectedText));
759
+ setIsConfirmDialogOpen(true);
760
+ return;
761
+ }
762
+
763
+ executeRun(selectedText);
764
+ }, [activeTab.sql, executeRun]);
765
+
766
+ // Global keybindings
767
+ useEffect(() => {
768
+ const handleKeyDown = (e: KeyboardEvent) => {
769
+ if ((e.metaKey || e.ctrlKey) && e.key === "Enter") {
770
+ // If we are in an input or textarea (except the code editor which handles its own), we might not want to run
771
+ const activeElement = document.activeElement;
772
+ const isInput = activeElement?.tagName === "INPUT" || activeElement?.tagName === "TEXTAREA";
773
+ // If it's the monaco editor textarea, it's fine, let's trigger handleRun
774
+ // Actually the monaco editor already has its own action, so we don't need a global one IF focused in monaco.
775
+ // But wait, if we have both, it might run twice.
776
+ // Let's check if we're focused in monaco.
777
+ const isMonaco = activeElement?.className?.includes("monaco-mouse-cursor-text");
778
+
779
+ if (!isMonaco && !isInput) {
780
+ e.preventDefault();
781
+ handleRun();
782
+ }
783
+ }
784
+ };
785
+ window.addEventListener("keydown", handleKeyDown);
786
+ return () => window.removeEventListener("keydown", handleKeyDown);
787
+ }, [handleRun]);
788
+
789
+ const handleSaveSnippet = () => {
790
+ if (!newSnippetName.trim() || !sql.trim()) return;
791
+
792
+ const newSnippet: Snippet = {
793
+ id: Math.random().toString(36).substring(2, 9),
794
+ name: newSnippetName,
795
+ sql: sql,
796
+ createdAt: Date.now()
797
+ };
798
+
799
+ saveSnippets([...snippets, newSnippet]);
800
+ setNewSnippetName("");
801
+ setIsSaveDialogOpen(false);
802
+ snackbarController.open({
803
+ type: "success",
804
+ message: t("studio_sql_snippet_saved", { name: newSnippetName })
805
+ });
806
+ };
807
+
808
+ const handleExportCSV = () => {
809
+ if (!results || results.length === 0) return;
810
+
811
+ const headers = Object.keys(results[0]).join(",");
812
+ const rows = results.map(row =>
813
+ Object.values(row).map(val => {
814
+ const str = String(val);
815
+ return str.includes(",") ? `"${str}"` : str;
816
+ }).join(",")
817
+ );
818
+ const csv = [headers, ...rows].join("\n");
819
+ const blob = new Blob([csv], { type: "text/csv" });
820
+ const url = window.URL.createObjectURL(blob);
821
+ const a = document.createElement("a");
822
+ a.href = url;
823
+ a.download = `query_results_${new Date().toISOString().slice(0, 19)}.csv`;
824
+ a.click();
825
+ window.URL.revokeObjectURL(url);
826
+ };
827
+
828
+ const handleExportJSON = () => {
829
+ if (!results || results.length === 0) return;
830
+
831
+ const json = JSON.stringify(results, null, 2);
832
+ const blob = new Blob([json], { type: "application/json" });
833
+ const url = window.URL.createObjectURL(blob);
834
+ const a = document.createElement("a");
835
+ a.href = url;
836
+ a.download = `query_results_${new Date().toISOString().slice(0, 19)}.json`;
837
+ a.click();
838
+ window.URL.revokeObjectURL(url);
839
+ };
840
+
841
+ const handleExportMarkdown = () => {
842
+ if (!results || results.length === 0) return;
843
+
844
+ const headers = Object.keys(results[0]);
845
+ const headerRow = `| ${headers.join(" | ")} |`;
846
+ const dividerRow = `| ${headers.map(() => "---").join(" | ")} |`;
847
+ const dataRows = results.map(row =>
848
+ `| ${headers.map(header => {
849
+ const val = row[header];
850
+ if (val === null) return "null";
851
+ if (val === undefined) return "";
852
+ // Replace pipes and newlines to avoid breaking the markdown table
853
+ return String(val).replace(/\|/g, "\\|").replace(/\n/g, " ");
854
+ }).join(" | ")} |`
855
+ );
856
+
857
+ const markdown = [headerRow, dividerRow, ...dataRows].join("\n");
858
+ navigator.clipboard.writeText(markdown).then(() => {
859
+ snackbarController.open({
860
+ type: "success",
861
+ message: t("studio_sql_markdown_copied")
862
+ });
863
+ }).catch(() => {
864
+ snackbarController.open({
865
+ type: "error",
866
+ message: t("studio_sql_markdown_copy_failed")
867
+ });
868
+ });
869
+ };
870
+
871
+ const renderResults = () => {
872
+ if (loading) {
873
+ return (
874
+ <div className="flex-grow flex items-center justify-center">
875
+ <div className="text-center">
876
+ <CircularProgress size="medium"/>
877
+ <Typography variant="body2" className="mt-4 text-text-secondary dark:text-text-secondary-dark font-mono tracking-tight animate-pulse">{t("studio_sql_executing_query")}</Typography>
878
+ </div>
879
+ </div>
880
+ );
881
+ }
882
+
883
+ if (error) {
884
+ return (
885
+ <div className="flex-grow flex items-center justify-center p-6 overflow-auto">
886
+ <ErrorView title={t("studio_sql_query_error")} error={error}/>
887
+ </div>
888
+ );
889
+ }
890
+
891
+ if (!results) {
892
+ return (
893
+ <div className="flex-grow flex items-center justify-center text-text-disabled dark:text-text-disabled-dark">
894
+ <div className="text-center">
895
+ <svg className="w-12 h-12 mx-auto mb-4 opacity-50" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path strokeLinecap="round" strokeLinejoin="round" strokeWidth={1} d="M4 7v10c0 2.21 3.582 4 8 4s8-1.79 8-4V7M4 7c0 2.21 3.582 4 8 4s8-1.79 8-4M4 7c0-2.21 3.582-4 8-4s8 1.79 8 4m0 5c0 2.21-3.582 4-8 4s-8-1.79-8-4"/></svg>
896
+ <Typography variant="body2">{t("studio_sql_run_query_placeholder")}</Typography>
897
+ </div>
898
+ </div>
899
+ );
900
+ }
901
+
902
+ // Check for EXPLAIN (FORMAT JSON) response
903
+ if (results.length === 1 && results[0]["QUERY PLAN"] && Array.isArray(results[0]["QUERY PLAN"])) {
904
+ try {
905
+ const plan = results[0]["QUERY PLAN"][0].Plan;
906
+ if (plan) {
907
+ return (
908
+ <div className="flex-grow overflow-auto p-4 bg-surface-50 dark:bg-surface-900 flex flex-col items-start">
909
+ <Typography variant="caption" className="font-bold text-text-secondary mb-4 tracking-wider uppercase">{t("studio_sql_visual_execution_plan")}</Typography>
910
+ <div className="pb-12">
911
+ <ExplainVisualizer plan={plan}/>
912
+ </div>
913
+ </div>
914
+ );
915
+ }
916
+ } catch (e) {
917
+ console.warn("Failed to parse EXPLAIN JSON output:", e);
918
+ }
919
+ }
920
+
921
+ if (results.length === 0) {
922
+ return (
923
+ <div className="flex-grow p-6 flex flex-col items-center justify-center">
924
+ <Typography variant="body2" className="text-text-secondary dark:text-text-secondary-dark font-mono border-b border-surface-200 dark:border-surface-950 pb-2 mb-2">{t("studio_sql_success")}</Typography>
925
+ <Typography variant="caption" className="text-text-disabled dark:text-text-disabled-dark">{t("studio_sql_no_results")}</Typography>
926
+ </div>
927
+ );
928
+ }
929
+
930
+ const savedWidths = columnWidths[activeTab.sql] || {};
931
+ const resultColumnKeys = Object.keys(results[0]);
932
+
933
+ // Compute matched collections for this query, including PK column detection
934
+ const matchedCollections: ResolvedQueryCollection[] = (() => {
935
+ if (!activeTab.lastExecutedSql || !collectionRegistry.collections) return [];
936
+ try {
937
+ return resolveQueryCollections(activeTab.lastExecutedSql, schemas, collectionRegistry.collections, resultColumnKeys);
938
+ } catch {
939
+ return [];
940
+ }
941
+ })();
942
+
943
+ // Only collections that have a PK column in the result set can be opened
944
+ const actionableCollections = matchedCollections.filter(mc => mc.pkColumn && resultColumnKeys.includes(mc.pkColumn));
945
+
946
+ // For each row, determine which entities can be opened
947
+ const getRowEntityActions = (rowData: Record<string, unknown>): { collection: ResolvedQueryCollection, entityId: string | number }[] => {
948
+ if (!rowData) return [];
949
+ return actionableCollections
950
+ .filter(mc => rowData[mc.pkColumn!] != null)
951
+ .map(mc => ({
952
+ collection: mc,
953
+ entityId: rowData[mc.pkColumn!] as string | number
954
+ }));
955
+ };
956
+
957
+ // Build the columns array. If we have actionable collections, prepend a dedicated action column.
958
+ const dataColumns: VirtualTableColumn[] = resultColumnKeys.map(key => ({
959
+ key,
960
+ title: key,
961
+ width: savedWidths[key] ?? 150,
962
+ sortable: false,
963
+ resizable: true
964
+ }));
965
+
966
+ const columns: VirtualTableColumn[] = actionableCollections.length > 0
967
+ ? [{ key: "__cms_action__",
968
+ title: "",
969
+ width: 36,
970
+ sortable: false,
971
+ resizable: false }, ...dataColumns]
972
+ : dataColumns;
973
+
974
+ return (
975
+ <div className="flex-grow flex flex-col overflow-hidden min-h-0">
976
+ {/* Collection Badges Bar */}
977
+ {actionableCollections.length > 0 && (
978
+ <div className={cls("px-4 py-1.5 border-b flex items-center gap-2 shrink-0 bg-surface-50 dark:bg-surface-900", defaultBorderMixin)}>
979
+ <Tooltip title={t("studio_sql_cms_collections_tooltip")}>
980
+ <Typography variant="caption" className="text-[10px] font-bold uppercase tracking-widest text-text-disabled dark:text-text-disabled-dark mr-1 shrink-0 cursor-help">{t("studio_sql_cms")}</Typography>
981
+ </Tooltip>
982
+ <div className="flex items-center gap-1.5 overflow-x-auto no-scrollbar">
983
+ {actionableCollections.map(mc => (
984
+ <Tooltip key={mc.tableName} title={`Table "${mc.tableName}" → ${mc.collection.name} (PK: ${mc.pkColumn})`}>
985
+ <span className="inline-flex items-center gap-1 px-2 py-0.5 rounded-full text-[11px] font-medium bg-primary/10 dark:bg-primary-dark/15 text-primary dark:text-primary-dark whitespace-nowrap border border-primary/20 dark:border-primary-dark/20">
986
+ {typeof mc.collection.icon === "string" && (
987
+ <IconForView collectionOrView={mc.collection} className="text-[12px]"/>
988
+ )}
989
+ {mc.collection.name}
990
+ </span>
991
+ </Tooltip>
992
+ ))}
993
+ </div>
994
+ </div>
995
+ )}
996
+ <div className="flex-grow relative h-full min-h-0 min-w-0">
997
+ <VirtualTable
998
+ data={results}
999
+ columns={columns}
1000
+ rowHeight={32}
1001
+ headerHeight={32}
1002
+ extraData={editingCell}
1003
+ onColumnResizeEnd={handleColumnResize}
1004
+ cellRenderer={({ rowData, column, rowIndex }) => {
1005
+ // Dedicated collection action column
1006
+ if (column.key === "__cms_action__") {
1007
+ const rowActions = getRowEntityActions(rowData);
1008
+ if (rowActions.length === 0) {
1009
+ return <div className="h-full w-full"/>;
1010
+ }
1011
+ if (rowActions.length === 1) {
1012
+ const ra = rowActions[0];
1013
+ return (
1014
+ <div className="h-full flex items-center justify-center">
1015
+ <Tooltip title={t("studio_sql_edit_entity", { name: ra.collection.collection.name,
1016
+ id: String(ra.entityId) })}>
1017
+ <IconButton
1018
+ size="small"
1019
+ className="text-surface-400 dark:text-surface-500 hover:text-surface-600 dark:hover:text-surface-300 transition-colors"
1020
+ onClick={(e) => {
1021
+ e.stopPropagation();
1022
+ sideEntityController?.open({
1023
+ path: ra.collection.collection.slug,
1024
+ entityId: ra.entityId,
1025
+ collection: ra.collection.collection,
1026
+ updateUrl: false
1027
+ });
1028
+ }}
1029
+ >
1030
+ <PencilIcon size={iconSize.smallest}/>
1031
+ </IconButton>
1032
+ </Tooltip>
1033
+ </div>
1034
+ );
1035
+ }
1036
+ // Multiple matched collections (JOIN) — show a dropdown
1037
+ return (
1038
+ <div className="h-full flex items-center justify-center">
1039
+ <Menu
1040
+ trigger={
1041
+ <IconButton
1042
+ size="small"
1043
+ className="text-surface-400 dark:text-surface-500 hover:text-surface-600 dark:hover:text-surface-300 transition-colors"
1044
+ onClick={(e) => e.stopPropagation()}
1045
+ >
1046
+ <MoreVerticalIcon size={iconSize.smallest}/>
1047
+ </IconButton>
1048
+ }
1049
+ >
1050
+ {rowActions.map(ra => (
1051
+ <MenuItem
1052
+ key={ra.collection.tableName}
1053
+ dense
1054
+ onClick={() => {
1055
+ sideEntityController?.open({
1056
+ path: ra.collection.collection.slug,
1057
+ entityId: ra.entityId,
1058
+ collection: ra.collection.collection,
1059
+ updateUrl: false
1060
+ });
1061
+ }}
1062
+ >
1063
+ {t("studio_sql_edit_entity", { name: ra.collection.collection.name,
1064
+ id: String(ra.entityId) })}
1065
+ </MenuItem>
1066
+ ))}
1067
+ </Menu>
1068
+ </div>
1069
+ );
1070
+ }
1071
+
1072
+ // Regular data cell
1073
+ const isEditing = editingCell?.rowIndex === rowIndex && editingCell?.columnKey === column.key;
1074
+ const value = rowData ? rowData[column.key] : null;
1075
+ const displayValue = typeof value === "object" && value !== null ? JSON.stringify(value) : String(value ?? "");
1076
+
1077
+ if (isEditing) {
1078
+ return (
1079
+ <FixedEditorOverlay
1080
+ displayValue={displayValue}
1081
+ onSave={(val) => handleCellSave(val, rowData, column.key, rowIndex)}
1082
+ onCancel={() => setEditingCell(null)}
1083
+ />
1084
+ );
1085
+ }
1086
+
1087
+ return (
1088
+ <div
1089
+ className="px-4 py-1.5 h-full flex items-center whitespace-nowrap text-[13px] text-text-primary dark:text-text-primary-dark font-mono cursor-text group/cell"
1090
+ onDoubleClick={() => handleDoubleClick(rowIndex, column.key, displayValue, rowData)}
1091
+ >
1092
+ <div className="truncate flex-grow" title={displayValue}>
1093
+ {displayValue === "" ? <span className="text-text-disabled dark:text-text-disabled-dark italic text-[11px]">NULL</span> : displayValue}
1094
+ </div>
1095
+ </div>
1096
+ );
1097
+ }}
1098
+ />
1099
+ </div>
1100
+
1101
+ <div className={cls("p-2 px-4 border-t bg-surface-50 dark:bg-surface-900 flex justify-between items-center shrink-0", defaultBorderMixin)}>
1102
+ <div className="flex space-x-4">
1103
+ <div className="flex items-center text-[11px]">
1104
+ <span className="font-bold text-text-disabled dark:text-text-disabled-dark mr-2 uppercase tracking-tighter">{t("studio_sql_rows")}</span>
1105
+ <span className="font-mono text-text-secondary dark:text-text-secondary-dark">{results.length}</span>
1106
+ </div>
1107
+ <div className="flex items-center text-[11px]">
1108
+ <span className="font-bold text-text-disabled dark:text-text-disabled-dark mr-2 uppercase tracking-tighter">{t("studio_sql_time")}</span>
1109
+ <span className="font-mono text-text-secondary dark:text-text-secondary-dark">{execTime}ms</span>
1110
+ </div>
1111
+ </div>
1112
+ <div className="flex gap-2 overflow-x-auto no-scrollbar items-center px-2">
1113
+ <Button
1114
+ size="small"
1115
+ variant="text"
1116
+ className="text-[10px] uppercase font-bold text-text-secondary dark:text-text-secondary-dark whitespace-nowrap"
1117
+ onClick={handleExportMarkdown}
1118
+ >
1119
+ {t("studio_sql_copy_markdown")}
1120
+ </Button>
1121
+ <Button
1122
+ size="small"
1123
+ variant="text"
1124
+ className="text-[10px] uppercase font-bold text-text-secondary dark:text-text-secondary-dark whitespace-nowrap"
1125
+ onClick={handleExportJSON}
1126
+ >
1127
+ {t("studio_sql_export_json")}
1128
+ </Button>
1129
+ <Button
1130
+ size="small"
1131
+ variant="text"
1132
+ className="text-[10px] uppercase font-bold text-text-secondary dark:text-text-secondary-dark whitespace-nowrap"
1133
+ onClick={handleExportCSV}
1134
+ >
1135
+ {t("studio_sql_export_csv")}
1136
+ </Button>
1137
+ </div>
1138
+ </div>
1139
+ </div>
1140
+ );
1141
+ };
1142
+
1143
+ const [sidebarSize, setSidebarSize] = useState(() => {
1144
+ try {
1145
+ const saved = localStorage.getItem("rebase_sql_editor_sidebar_size");
1146
+ return saved !== null ? parseFloat(saved) : 20;
1147
+ } catch (e) {
1148
+ return 20;
1149
+ }
1150
+ });
1151
+ const [editorHeight, setEditorHeight] = useState(() => {
1152
+ try {
1153
+ const saved = localStorage.getItem("rebase_sql_editor_height");
1154
+ return saved !== null ? parseFloat(saved) : 50;
1155
+ } catch (e) {
1156
+ return 50;
1157
+ }
1158
+ });
1159
+
1160
+ useEffect(() => {
1161
+ try {
1162
+ localStorage.setItem("rebase_sql_editor_sidebar_size", sidebarSize.toString());
1163
+ } catch (e) { /* ignore */ }
1164
+ }, [sidebarSize]);
1165
+
1166
+ useEffect(() => {
1167
+ try {
1168
+ localStorage.setItem("rebase_sql_editor_height", editorHeight.toString());
1169
+ } catch (e) { /* ignore */ }
1170
+ }, [editorHeight]);
1171
+
1172
+ const activeSnippet = snippets.find(s => s.sql === activeTab.sql);
1173
+ const isFavorite = activeSnippet?.isFavorite || false;
1174
+
1175
+ return (
1176
+ <div className="flex h-full w-full bg-white dark:bg-surface-950 overflow-hidden text-text-primary dark:text-text-primary-dark">
1177
+ <ResizablePanels
1178
+ orientation="horizontal"
1179
+ panelSizePercent={sidebarSize}
1180
+ onPanelSizeChange={setSidebarSize}
1181
+ minPanelSizePx={220}
1182
+ firstPanel={
1183
+ <SQLEditorSidebar
1184
+ snippets={snippets}
1185
+ history={history}
1186
+ onSelectSnippet={setSql}
1187
+ onTableClick={setSql}
1188
+ onDeleteSnippet={handleDeleteSnippet}
1189
+ schemas={schemas}
1190
+ isSchemaLoading={isSchemaLoading}
1191
+ schemaError={schemaError}
1192
+ onRetrySchema={fetchSchema}
1193
+ />
1194
+ }
1195
+ secondPanel={
1196
+ <div className="flex-grow flex flex-col min-w-0 h-full w-full">
1197
+ {/* Toolbar */}
1198
+ <div className={cls("flex items-center justify-between pr-2 border-b bg-white dark:bg-surface-950", defaultBorderMixin)}>
1199
+ <div className="flex items-center flex-grow overflow-hidden mr-4">
1200
+ <div className="flex items-center no-scrollbar overflow-x-auto min-w-0">
1201
+ <Tabs value={activeTabId} onValueChange={handleTabChange} variant="boxy" className="w-[unset] flex-shrink-0" innerClassName="bg-white dark:bg-surface-950">
1202
+ {tabs.map(tab => (
1203
+ <Tab key={tab.id} value={tab.id} className="flex items-center justify-between group max-w-[200px]">
1204
+ <TerminalIcon size={iconSize.smallest} className="text-blue-500 mr-1.5 flex-shrink-0"/>
1205
+ <span className="truncate">{tab.name}</span>
1206
+ {tabs.length > 1 && (
1207
+ <IconButton
1208
+ size="smallest"
1209
+ onClick={(e) => handleCloseTab(tab.id, e)}
1210
+ className="ml-1 !p-0.5 opacity-0 group-hover:opacity-100 hover:text-red-500 transition-opacity"
1211
+ >
1212
+ <XIcon size={iconSize.smallest}/>
1213
+ </IconButton>
1214
+ )}
1215
+ </Tab>
1216
+ ))}
1217
+ </Tabs>
1218
+ <IconButton
1219
+ size="small"
1220
+ onClick={handleAddTab}
1221
+ className="ml-2 flex-shrink-0"
1222
+ >
1223
+ <PlusIcon size={iconSize.smallest}/>
1224
+ </IconButton>
1225
+ </div>
1226
+ </div>
1227
+ <div className="flex shrink-0 items-center justify-end pr-2 gap-1.5">
1228
+ <Tooltip title={t("studio_sql_format_sql")}>
1229
+ <IconButton size="small" onClick={handlePrettify}>
1230
+ <MenuIcon size={iconSize.smallest}/>
1231
+ </IconButton>
1232
+ </Tooltip>
1233
+
1234
+ <Button
1235
+ variant="text"
1236
+ size="small"
1237
+ onClick={handleExplain}
1238
+ disabled={loading}
1239
+ >
1240
+ {t("studio_sql_explain")}
1241
+ </Button>
1242
+
1243
+ <div className="h-4 w-px bg-surface-200 dark:bg-surface-950 mx-1"></div>
1244
+
1245
+ <div className="flex items-center space-x-2 px-2" onClick={(e) => {
1246
+ setAutoLimit(!autoLimit);
1247
+ e.stopPropagation();
1248
+ }}>
1249
+ <Typography variant="caption" className="text-[11px] text-text-secondary cursor-pointer select-none">{t("studio_sql_limit_1000")}</Typography>
1250
+ <div onClick={(e) => e.stopPropagation()}>
1251
+ <Checkbox
1252
+ checked={autoLimit}
1253
+ onCheckedChange={setAutoLimit}
1254
+ size="smallest"
1255
+ padding={false}
1256
+ />
1257
+ </div>
1258
+ </div>
1259
+
1260
+ <div className="h-4 w-px bg-surface-200 dark:bg-surface-950 mx-1"></div>
1261
+
1262
+ <Tooltip title={isFavorite ? t("studio_sql_remove_from_favorites") : t("studio_sql_add_to_favorites")}>
1263
+ <IconButton
1264
+ size="small"
1265
+ onClick={() => {
1266
+ if (!activeSnippet) {
1267
+ snackbarController.open({
1268
+ type: "info",
1269
+ message: t("studio_sql_save_first_to_favorite")
1270
+ });
1271
+ return;
1272
+ }
1273
+ saveSnippets(snippets.map(s => s.id === activeSnippet.id ? { ...s,
1274
+ isFavorite: !s.isFavorite } : s));
1275
+ }}
1276
+ >
1277
+ <svg className={`w-4 h-4 ${isFavorite ? "text-red-500 fill-current" : "text-text-disabled dark:text-text-disabled-dark hover:text-text-primary"}`} fill={isFavorite ? "currentColor" : "none"} stroke="currentColor" strokeWidth={2} viewBox="0 0 24 24"><path strokeLinecap="round" strokeLinejoin="round" d="M4.318 6.318a4.5 4.5 0 000 6.364L12 20.364l7.682-7.682a4.5 4.5 0 00-6.364-6.364L12 7.636l-1.318-1.318a4.5 4.5 0 00-6.364 0z"/></svg>
1278
+ </IconButton>
1279
+ </Tooltip>
1280
+
1281
+ <Button
1282
+ variant="text"
1283
+ size="small"
1284
+ onClick={() => setIsSaveDialogOpen(true)}
1285
+ >
1286
+ {t("studio_sql_save")}
1287
+ </Button>
1288
+
1289
+ <div className="h-4 w-px bg-surface-200 dark:bg-surface-950 mx-1"></div>
1290
+
1291
+ <Menu
1292
+ trigger={
1293
+ <Button
1294
+ size="small"
1295
+ variant="outlined"
1296
+ className="text-text-secondary dark:text-text-secondary-dark font-medium mr-2"
1297
+ >
1298
+ <DatabaseIcon size={iconSize.small} className="mr-1.5 text-text-disabled dark:text-text-disabled-dark"/>
1299
+ <span className="max-w-[80px] truncate">{isLoadingConfig ? "..." : (selectedDatabase || t("studio_sql_select_db"))}</span>
1300
+ </Button>
1301
+ }
1302
+ >
1303
+ <div className="max-h-64 overflow-y-auto">
1304
+ <div className="px-3 py-1.5 border-b border-surface-200 dark:border-surface-950 mb-1">
1305
+ <Typography variant="caption" className="font-bold uppercase tracking-wider text-[9px] text-text-disabled dark:text-text-disabled-dark">{t("studio_sql_database")}</Typography>
1306
+ </div>
1307
+ {isLoadingConfig ? (
1308
+ <div className="flex items-center justify-center p-4">
1309
+ <CircularProgress size="small"/>
1310
+ </div>
1311
+ ) : connectionConfigError ? (
1312
+ <div className="px-3 py-2 text-xs text-red-500 dark:text-red-400 max-w-[200px] break-words">
1313
+ {connectionConfigError}
1314
+ </div>
1315
+ ) : (
1316
+ <>
1317
+ {availableDatabases.map(db => (
1318
+ <MenuItem key={db} dense onClick={() => handleDatabaseChange(db)} className={cls("text-xs", selectedDatabase === db && "text-primary dark:text-primary-dark")}>
1319
+ {db}
1320
+ </MenuItem>
1321
+ ))}
1322
+
1323
+ <div className="px-3 py-1.5 border-y border-surface-200 dark:border-surface-950 mb-1 mt-1">
1324
+ <Typography variant="caption" className="font-bold uppercase tracking-wider text-[9px] text-text-disabled dark:text-text-disabled-dark">{t("studio_sql_role")}</Typography>
1325
+ </div>
1326
+ {availableRoles.map(role => (
1327
+ <MenuItem key={role} dense onClick={() => handleRoleChange(role)} className={cls("text-xs", selectedRole === role && "text-primary dark:text-primary-dark")}>
1328
+ {role}{role === "postgres" ? " " + t("studio_sql_admin") : ""}
1329
+ </MenuItem>
1330
+ ))}
1331
+ </>
1332
+ )}
1333
+ </div>
1334
+ </Menu>
1335
+
1336
+ <Button
1337
+ onClick={() => handleRun()}
1338
+ disabled={loading}
1339
+ size="small"
1340
+ color="primary"
1341
+ >
1342
+ {loading ? <CircularProgress size="smallest" className="mr-2"/> : <PlayIcon size={iconSize.smallest} className="mr-2"/>}
1343
+ {t("studio_sql_run")}
1344
+ </Button>
1345
+ </div>
1346
+ </div>
1347
+
1348
+ <ResizablePanels
1349
+ orientation="vertical"
1350
+ panelSizePercent={editorHeight}
1351
+ onPanelSizeChange={setEditorHeight}
1352
+ minPanelSizePx={100}
1353
+ firstPanel={
1354
+ <div className="h-full w-full relative flex flex-col min-h-0">
1355
+ <MonacoEditor
1356
+ value={sql}
1357
+ onChange={(v) => setSql(v || "")}
1358
+ onRun={handleRun}
1359
+ schemas={schemas}
1360
+ />
1361
+ </div>
1362
+ }
1363
+ secondPanel={
1364
+ <div className="h-full w-full flex flex-col bg-surface-50 dark:bg-surface-950 overflow-hidden min-h-0">
1365
+ <div className={cls("p-2 px-4 bg-surface-100 dark:bg-surface-900 border-b shrink-0 flex items-center", defaultBorderMixin)}>
1366
+ <Typography variant="caption" className="font-bold text-text-disabled dark:text-text-disabled-dark uppercase tracking-widest text-[10px]">{t("studio_sql_query_results")}</Typography>
1367
+ </div>
1368
+ <div className="flex-grow flex flex-col min-h-0 overflow-hidden">
1369
+ {renderResults()}
1370
+ </div>
1371
+ </div>
1372
+ }
1373
+ />
1374
+
1375
+ </div>
1376
+ }
1377
+ />
1378
+
1379
+ <Dialog open={isSaveDialogOpen} onOpenChange={setIsSaveDialogOpen}>
1380
+ <DialogTitle>{t("studio_sql_save_snippet")}</DialogTitle>
1381
+ <DialogContent>
1382
+ <div className="py-4 flex flex-col gap-4">
1383
+ <TextField
1384
+ label={t("studio_sql_snippet_name")}
1385
+ autoFocus
1386
+ placeholder={t("studio_sql_snippet_name_placeholder")}
1387
+ value={newSnippetName}
1388
+ onChange={(e) => setNewSnippetName(e.target.value)}
1389
+ onKeyDown={(e) => {
1390
+ if (e.key === "Enter") {
1391
+ e.preventDefault();
1392
+ handleSaveSnippet();
1393
+ }
1394
+ }}
1395
+ />
1396
+ <Typography variant="caption" className="text-text-disabled dark:text-text-disabled-dark block">{t("studio_sql_snippet_saved_local")}</Typography>
1397
+ </div>
1398
+ </DialogContent>
1399
+ <DialogActions>
1400
+ <Button variant="text" onClick={() => setIsSaveDialogOpen(false)}>{t("studio_sql_cancel")}</Button>
1401
+ <Button onClick={handleSaveSnippet} color="primary" disabled={!newSnippetName.trim()}>{t("studio_sql_save")}</Button>
1402
+ </DialogActions>
1403
+ </Dialog>
1404
+ {/* Confirmation Dialog */}
1405
+ <ConfirmationDialog
1406
+ open={isConfirmDialogOpen}
1407
+ onCancel={() => setIsConfirmDialogOpen(false)}
1408
+ title={t("studio_sql_dangerous_operation")}
1409
+ body={t("studio_sql_dangerous_operation_body")}
1410
+ onAccept={() => {
1411
+ if (pendingAction) pendingAction();
1412
+ setIsConfirmDialogOpen(false);
1413
+ }}
1414
+ />
1415
+ </div>
1416
+ );
1417
+ };