UNPKG

@mui/internal-docs-infra

Version:

MUI Infra - internal documentation creation tools.

131 lines (122 loc) 5.32 kB
import _extends from "@babel/runtime/helpers/esm/extends"; import _objectWithoutPropertiesLoose from "@babel/runtime/helpers/esm/objectWithoutPropertiesLoose"; var _excluded = ["name", "slug", "code", "components", "url"]; import * as React from 'react'; import { useCodeHighlighterContextOptional } from "../CodeHighlighter/CodeHighlighterContext.js"; import { extractNameAndSlugFromUrl } from "../pipeline/loaderUtils/index.js"; import { useVariantSelection } from "./useVariantSelection.js"; import { useTransformManagement } from "./useTransformManagement.js"; import { useFileNavigation } from "./useFileNavigation.js"; import { useUIState } from "./useUIState.js"; import { useCopyFunctionality } from "./useCopyFunctionality.js"; import { useSourceEditing } from "./useSourceEditing.js"; export function useCode(contentProps, opts) { var _ref = opts || {}, copyOpts = _ref.copy, _ref$defaultOpen = _ref.defaultOpen, defaultOpen = _ref$defaultOpen === void 0 ? false : _ref$defaultOpen, initialVariant = _ref.initialVariant, initialTransform = _ref.initialTransform, preClassName = _ref.preClassName, preRef = _ref.preRef; // Safely try to get context values - will be undefined if not in context var context = useCodeHighlighterContextOptional(); // Get the effective code - context overrides contentProps if available var effectiveCode = React.useMemo(function () { return (context == null ? void 0 : context.code) || contentProps.code || {}; }, [context == null ? void 0 : context.code, contentProps.code]); var shouldHighlight = !(context != null && context.deferHighlight); // Memoize userProps with auto-generated name and slug if missing var userProps = React.useMemo(function () { // Extract only the user-defined properties (T) from contentProps var contentName = contentProps.name, contentSlug = contentProps.slug, code = contentProps.code, components = contentProps.components, contentUrl = contentProps.url, userDefinedProps = _objectWithoutPropertiesLoose(contentProps, _excluded); // Get URL from context first, then fall back to contentProps var effectiveUrl = (context == null ? void 0 : context.url) || contentUrl; var name = contentName; var slug = contentSlug; // Generate name and slug from URL if they're missing and we have a URL if ((!name || !slug) && effectiveUrl) { try { var generated = extractNameAndSlugFromUrl(effectiveUrl); name = name || generated.name; slug = slug || generated.slug; } catch (_unused) { // If URL parsing fails, keep the original values (which might be undefined) } } return _extends(_extends({}, userDefinedProps), {}, { name: name, slug: slug }); }, [contentProps, context == null ? void 0 : context.url]); // Sub-hook: UI State Management var uiState = useUIState({ defaultOpen: defaultOpen }); // Sub-hook: Variant Selection var variantSelection = useVariantSelection({ effectiveCode: effectiveCode, initialVariant: initialVariant, variantType: contentProps.variantType }); // Sub-hook: Transform Management var transformManagement = useTransformManagement({ context: context, effectiveCode: effectiveCode, selectedVariantKey: variantSelection.selectedVariantKey, selectedVariant: variantSelection.selectedVariant, initialTransform: initialTransform, shouldHighlight: shouldHighlight }); // Sub-hook: File Navigation var fileNavigation = useFileNavigation({ selectedVariant: variantSelection.selectedVariant, transformedFiles: transformManagement.transformedFiles, mainSlug: userProps.slug, selectedVariantKey: variantSelection.selectedVariantKey, selectVariant: variantSelection.selectVariantProgrammatic, variantKeys: variantSelection.variantKeys, initialVariant: initialVariant, shouldHighlight: shouldHighlight, preClassName: preClassName, preRef: preRef, effectiveCode: effectiveCode }); // Sub-hook: Copy Functionality var copyFunctionality = useCopyFunctionality({ selectedFile: fileNavigation.selectedFile, copyOpts: copyOpts }); // Sub-hook: Source Editing var sourceEditing = useSourceEditing({ context: context, selectedVariantKey: variantSelection.selectedVariantKey, effectiveCode: effectiveCode, selectedVariant: variantSelection.selectedVariant }); return { variants: variantSelection.variantKeys, selectedVariant: variantSelection.selectedVariantKey, selectVariant: variantSelection.selectVariant, files: fileNavigation.files, selectedFile: fileNavigation.selectedFileComponent, selectedFileLines: fileNavigation.selectedFileLines, selectedFileName: fileNavigation.selectedFileName, selectFileName: fileNavigation.selectFileName, allFilesSlugs: fileNavigation.allFilesSlugs, expanded: uiState.expanded, expand: uiState.expand, setExpanded: uiState.setExpanded, copy: copyFunctionality.copy, availableTransforms: transformManagement.availableTransforms, selectedTransform: transformManagement.selectedTransform, selectTransform: transformManagement.selectTransform, setSource: sourceEditing.setSource, userProps: userProps }; }