rusackas commented on code in PR #44101: URL: https://github.com/apache/superset/pull/44101#discussion_r4332250088
########## superset-frontend/src/core/contributions.ts: ########## @@ -0,0 +1,61 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +/** + * Global (non-SQL-Lab-scoped) view/menu locations for extension + * integration. Unlike `src/SqlLab/contributions.ts`, these locations + * aren't declared via the manifest `contributions.views`/`contributions.menus` + * field -- `ViewContributions`/`MenuContributions` in `superset-core` + * only accept a `sqllab` scope today -- so extensions register against + * them imperatively instead. + * + * This constant is internal to the Superset app; it isn't exported from + * `@apache-superset/core`, so extensions reference these locations by + * their literal string values instead of importing `GlobalLocations`. Review Comment: Added `docs/developer_docs/extensions/extension-points/settings.md` next to `sqllab.md`: the two location strings, the `primary`-only rule, the view route, and the `window` toasts (which replaced `ctx.window` since you wrote this). ########## superset-frontend/src/core/views/index.ts: ########## @@ -89,11 +89,53 @@ export const resolveView = (id: string): React.ReactElement => { if (!entry) { return React.createElement(ExtensionPlaceholder, { id }); } - return React.createElement( - ErrorBoundary, - null, - React.createElement(entry.component), + return React.createElement(ErrorBoundary, { + key: id, + // No JSX in a .ts file; `key` requires a props object, so `children` + // has to travel through it too. + // oxlint-disable-next-line no-children-prop + children: React.createElement(entry.component), + }); +}; + +/** + * Reactive counterpart to `resolveView`, for hosting a single + * extension-registered view (see `src/pages/ExtensionView`). + * + * `resolveView` reads the registry once, synchronously, at call time. + * That's a real gap for a host page reached by direct/full navigation + * (a bookmark, a page refresh, or an extension's own menu command using + * `window.location.assign` -- see `ExtensionView`'s own docstring): the + * extension providing the view loads asynchronously (its remote entry is + * fetched over the network), so on first render the registry is still + * empty and a plain `resolveView` call permanently commits to the + * "could not be loaded" placeholder, with nothing to trigger a re-render + * once the extension actually finishes loading and registers. Subscribing + * via `useSyncExternalStore` (the same registry-change events `useViews` + * already subscribes to) re-renders once that registration lands. + * + * Snapshots the registered `component` itself, not a freshly + * `React.createElement`-ed result -- `useSyncExternalStore` requires a + * `getSnapshot` that returns a stable reference when nothing changed, and + * `entry.component` is exactly that (unlike a new element object created + * fresh on every call). + */ +export const useResolveView = (id: string): React.ReactElement => { + const component = useSyncExternalStore( + subscribe, + () => viewRegistry.get(id)?.component, + () => undefined, ); + if (!component) { + return React.createElement(ExtensionPlaceholder, { id }); Review Comment: Good call. `ExtensionView` now renders `Loading` until `initializeExtensions()` settles and only then falls back to the placeholder, with a test covering both halves. ########## superset-frontend/src/pages/ExtensionView/index.tsx: ########## @@ -0,0 +1,76 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +import { useParams } from 'react-router-dom'; +import { t } from '@apache-superset/core/translation'; +import SubMenu, { SubMenuProps } from 'src/features/home/SubMenu'; +import { useResolveView } from 'src/core/views'; + +/** + * Generic full-page host for a single extension-registered view, reached at + * `/extensions/view/:viewId+`. Extensions cannot render their own views + * directly (`resolveView`/`useResolveView` are host-internal, not part of + * the public `@apache-superset/core` SDK) -- they register a view at + * `GlobalLocations.settings.panel` and a matching command at + * `GlobalLocations.settings.menu` whose callback navigates here, and the + * host resolves and renders it. + * + * Uses the reactive `useResolveView`, not the plain `resolveView`: this + * page is reachable by a direct/full navigation (a bookmark, a page + * refresh, or an extension's own menu command falling back to + * `window.location.assign` in the absence of an SDK-level SPA-navigation + * primitive), and the providing extension's own code loads asynchronously + * -- a non-reactive resolve would permanently commit to the "could not be + * loaded" placeholder from before that load finishes. + */ +const decodeViewId = (viewId: string | undefined): string => { + if (!viewId) { + return ''; + } + try { + return decodeURIComponent(viewId); + } catch { + return viewId; + } +}; + +const ExtensionView = () => { + const { viewId: rawViewId } = useParams<{ viewId: string }>(); + // The router leaves percent-encoded separators (`%2F`) intact, so decode + // to match the registry key whether a command used a literal or encoded `/`. + const viewId = decodeViewId(rawViewId); + + const menuData: SubMenuProps = { + name: t('Extension'), Review Comment: Took it. There is a `useRegisteredView` hook now (`useResolveView` is built on it), and the header shows the view's `name`, falling back to "Extension" while nothing is registered. ########## superset-frontend/src/pages/ExtensionView/index.tsx: ########## @@ -0,0 +1,62 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +import { useParams } from 'react-router-dom'; +import { t } from '@apache-superset/core/translation'; +import SubMenu, { SubMenuProps } from 'src/features/home/SubMenu'; +import { useResolveView } from 'src/core/views'; + +/** + * Generic full-page host for a single extension-registered view, reached at + * `/extensions/view/:viewId+`. Extensions cannot render their own views + * directly (`resolveView`/`useResolveView` are host-internal, not part of + * the public `@apache-superset/core` SDK) -- they register a view at + * `GlobalLocations.settings.panel` and a matching command at + * `GlobalLocations.settings.menu` whose callback navigates here, and the + * host resolves and renders it. + * + * Uses the reactive `useResolveView`, not the plain `resolveView`: this + * page is reachable by a direct/full navigation (a bookmark, a page + * refresh, or an extension's own menu command falling back to + * `window.location.assign` in the absence of an SDK-level SPA-navigation + * primitive), and the providing extension's own code loads asynchronously + * -- a non-reactive resolve would permanently commit to the "could not be + * loaded" placeholder from before that load finishes. + */ +const ExtensionView = () => { + const { viewId } = useParams<{ viewId: string }>(); + + const menuData: SubMenuProps = { + name: t('Extension'), + }; + + const resolved = useResolveView(viewId ?? ''); Review Comment: @sadpandajoe you were right, the second decode threw on `my-ext/rate%`. `ExtensionView` now only decodes the reserved-character escapes that `history`'s own `decodeURI` pass leaves behind (`%2F` and friends), so `%25` becomes `%` exactly once. Added a percent-containing id to the tests. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
