|
| 1 | +import { usePathname, useRouter } from 'next/navigation'; |
| 2 | +import { Button, Icon, Text } from 'opub-ui'; |
| 3 | + |
| 4 | +import { Icons } from '@/components/icons'; |
| 5 | + |
| 6 | +interface StepNavigationProps { |
| 7 | + steps: string[]; // Array of steps (e.g., ['metadata', 'details', 'publish']) |
| 8 | +} |
| 9 | + |
| 10 | +const StepNavigation = ({ steps }: StepNavigationProps) => { |
| 11 | + const pathname = usePathname(); // Get the current URL path |
| 12 | + const router = useRouter(); |
| 13 | + console.log(pathname); |
| 14 | + |
| 15 | + // Find the current step's index based on the pathname (without query params) |
| 16 | + const currentIndex = steps.findIndex((step) => |
| 17 | + pathname.split('?')[0].endsWith(step) |
| 18 | + ); |
| 19 | + |
| 20 | + if (currentIndex === -1) { |
| 21 | + return null; // In case no valid step is found |
| 22 | + } |
| 23 | + |
| 24 | + const goToPrevious = () => { |
| 25 | + if (currentIndex > 0) { |
| 26 | + const newPath = pathname.replace( |
| 27 | + steps[currentIndex], |
| 28 | + steps[currentIndex - 1] |
| 29 | + ); |
| 30 | + router.push(newPath); // Update the URL to the previous step |
| 31 | + } |
| 32 | + }; |
| 33 | + |
| 34 | + const goToNext = () => { |
| 35 | + if (currentIndex < steps.length - 1) { |
| 36 | + const newPath = pathname.replace( |
| 37 | + steps[currentIndex], |
| 38 | + steps[currentIndex + 1] |
| 39 | + ); |
| 40 | + router.push(newPath); // Update the URL to the next step |
| 41 | + } |
| 42 | + }; |
| 43 | + |
| 44 | + return ( |
| 45 | + <div className="flex items-center justify-center gap-10"> |
| 46 | + <Button |
| 47 | + onClick={goToPrevious} |
| 48 | + disabled={currentIndex === 0} // Disable if at the first step |
| 49 | + kind="tertiary" |
| 50 | + > |
| 51 | + <div className="flex items-center gap-1"> |
| 52 | + <Icon |
| 53 | + source={Icons.back} |
| 54 | + size={24} |
| 55 | + color={currentIndex === 0 ? 'disabled' : 'warning'} |
| 56 | + /> |
| 57 | + <Text color={currentIndex === 0 ? 'disabled' : 'medium'}> |
| 58 | + Previous |
| 59 | + </Text> |
| 60 | + </div> |
| 61 | + </Button> |
| 62 | + <Text> |
| 63 | + Step <b>{currentIndex + 1}</b> of {steps.length} |
| 64 | + </Text> |
| 65 | + <Button |
| 66 | + onClick={goToNext} |
| 67 | + disabled={currentIndex === steps.length - 1} // Disable if at the last step |
| 68 | + kind="tertiary" |
| 69 | + className="flex gap-1" |
| 70 | + > |
| 71 | + <div className="flex items-center gap-1"> |
| 72 | + <Text |
| 73 | + color={currentIndex === steps.length - 1 ? 'disabled' : 'medium'} |
| 74 | + > |
| 75 | + {' '} |
| 76 | + Next |
| 77 | + </Text> |
| 78 | + <Icon |
| 79 | + source={Icons.arrowRight} |
| 80 | + size={24} |
| 81 | + color={currentIndex === steps.length - 1 ? 'disabled' : 'warning'} |
| 82 | + /> |
| 83 | + </div> |
| 84 | + </Button> |
| 85 | + </div> |
| 86 | + ); |
| 87 | +}; |
| 88 | + |
| 89 | +export default StepNavigation; |
0 commit comments