import type { AnySupportedStyle, SupportedFramework, SupportedStyle } from '@/types/docs'; import { Select } from '@/components/Select'; import { FRAMEWORK_STYLES, isValidFramework, isValidStyleForFramework, SUPPORTED_FRAMEWORKS } from '@/types/docs'; import { resolveFrameworkChange, resolveStyleChange } from '@/utils/docs/routing'; interface SelectorProps { currentFramework: T; currentStyle: SupportedStyle; currentSlug: string; } export function Selectors({ currentFramework, currentStyle, currentSlug, }: SelectorProps) { // TODO: use astro view transitions to preserve scroll position when switching from the same slug to the same slug const handleFrameworkChange = (newFramework: SupportedFramework | null) => { if (newFramework === null) return; if (!isValidFramework(newFramework)) return; const { url, shouldReplace } = resolveFrameworkChange({ currentFramework, currentStyle, currentSlug, newFramework, }); if (shouldReplace) { // Maintaining the current slug, navigate without pushing onto the history stack window.location.replace(url); } else { // Changing slug, use normal navigation window.location.href = url; } }; const handleStyleChange = (newStyle: AnySupportedStyle | null) => { if (newStyle === null) return; if (!isValidStyleForFramework(currentFramework, newStyle)) return; const { url, shouldReplace } = resolveStyleChange({ currentFramework, currentStyle, currentSlug, newStyle, }); if (shouldReplace) { // Maintaining the current slug, navigate without pushing onto the history stack window.location.replace(url); } else { // Changing slug, use normal navigation window.location.href = url; } }; const availableStyles = FRAMEWORK_STYLES[currentFramework]; const frameworkOptions = SUPPORTED_FRAMEWORKS.map(fw => ({ value: fw, label: fw, })); const styleOptions = availableStyles.map(st => ({ value: st, label: st, })); return (
Framework
); }