({ children, className })
| 118 | } |
| 119 | |
| 120 | const TabsLayout = ({ children, className }) => { |
| 121 | const { category, subcategory } = useParams(); |
| 122 | const { hasChanges, resetProps, props: currentProps, defaultProps, demoOnlyProps, computedProps } = useComponentPropsContext(); |
| 123 | |
| 124 | const { favoriteKey, componentName } = useMemo(() => { |
| 125 | if (!category || !subcategory) return null; |
| 126 | |
| 127 | const toPascal = str => |
| 128 | str |
| 129 | .split('-') |
| 130 | .map(word => word.charAt(0).toUpperCase() + word.slice(1)) |
| 131 | .join(''); |
| 132 | |
| 133 | const categoryName = toPascal(category); |
| 134 | const componentName = toPascal(subcategory); |
| 135 | return { favoriteKey: `${categoryName}/${componentName}`, componentName }; |
| 136 | }, [category, subcategory]) || { favoriteKey: null, componentName: null }; |
| 137 | |
| 138 | const [isSaved, setIsSaved] = useState(false); |
| 139 | |
| 140 | useEffect(() => { |
| 141 | if (!favoriteKey) return; |
| 142 | setIsSaved(isComponentSaved(favoriteKey)); |
| 143 | }, [favoriteKey]); |
| 144 | |
| 145 | const toggleFavorite = () => { |
| 146 | if (!favoriteKey) return; |
| 147 | const { saved } = toggleSavedComponent(favoriteKey); |
| 148 | setIsSaved(saved); |
| 149 | |
| 150 | const nameEl = ( |
| 151 | <span> |
| 152 | {' '} |
| 153 | <span style={{ color: colors.accent, fontWeight: 700 }}><{componentName} /></span> |
| 154 | </span> |
| 155 | ); |
| 156 | |
| 157 | if (saved) toast.success?.(<>Added {nameEl} to favorites</>) ?? toast(<>Added {nameEl} to favorites</>); |
| 158 | else toast.error?.(<>Removed {nameEl} from favorites</>) ?? toast(<>Removed {nameEl} from favorites</>); |
| 159 | }; |
| 160 | const contentMap = { |
| 161 | PreviewTab: null, |
| 162 | CodeTab: null |
| 163 | }; |
| 164 | |
| 165 | React.Children.forEach(children, child => { |
| 166 | if (!child) return; |
| 167 | if (child.type === PreviewTab) contentMap.PreviewTab = child; |
| 168 | if (child.type === CodeTab) contentMap.CodeTab = child; |
| 169 | }); |
| 170 | |
| 171 | // Extract codeObject/componentName from CodeExample child and propData from PropTable child |
| 172 | const codeExampleProps = contentMap.CodeTab |
| 173 | ? findChildProps(contentMap.CodeTab.props.children, CodeExample) |
| 174 | : null; |
| 175 | const propTableProps = contentMap.PreviewTab |
| 176 | ? findChildProps(contentMap.PreviewTab.props.children, PropTable) |
| 177 | : null; |
nothing calls this directly
no test coverage detected