-
Notifications
You must be signed in to change notification settings - Fork 3
Jp/permute array #664
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Jp/permute array #664
Changes from 5 commits
0b811a2
da924b5
907e4ac
57cc7fd
2e6dd6d
4efb7ed
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,61 @@ | ||||||||||||||||||||||
| import React, {useRef, useState, useEffect} from 'react' | ||||||||||||||||||||||
| import { useGlobalStore } from '@/GlobalStates/GlobalStore' | ||||||||||||||||||||||
| import { | ||||||||||||||||||||||
| Select, | ||||||||||||||||||||||
| SelectContent, | ||||||||||||||||||||||
| SelectItem, | ||||||||||||||||||||||
| SelectTrigger, | ||||||||||||||||||||||
| SelectValue, | ||||||||||||||||||||||
| } from "@/components/ui/select"; | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| export const DimensionOrder = ({dimNames, setDuplicateDims}: {dimNames: string[], setDuplicateDims: React.Dispatch<React.SetStateAction<boolean>>}) => { | ||||||||||||||||||||||
| const {permute} = useGlobalStore.getState() // Just need the value on mount to set initial <select> values | ||||||||||||||||||||||
| const slotCount = Math.min(3, dimNames.length); | ||||||||||||||||||||||
| const permuteRef = useRef<number[]>( | ||||||||||||||||||||||
| Array.from({ length: slotCount }, (_, i) => i) | ||||||||||||||||||||||
| ) | ||||||||||||||||||||||
| const [warnDuplicates, setWarnDuplicates] = useState(false); | ||||||||||||||||||||||
| const handleChange = (slotIdx: number, dimIdx: number) => { | ||||||||||||||||||||||
| permuteRef.current[slotIdx] = dimIdx; | ||||||||||||||||||||||
| useGlobalStore.setState({ permute: [...permuteRef.current] }); | ||||||||||||||||||||||
| const permuteArray = permuteRef.current; | ||||||||||||||||||||||
| const hasDupes = permuteArray.length != new Set(permuteArray).size | ||||||||||||||||||||||
| setWarnDuplicates(hasDupes) | ||||||||||||||||||||||
| setDuplicateDims(hasDupes) | ||||||||||||||||||||||
| }; | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| useEffect(()=>{ //The first render didn't have dimNames and used default value. UseEffect updates after fetch. Also updates when switching to variable with diff Dims | ||||||||||||||||||||||
| // At the moment it resets each time because of the double click issue with menus that got introduced in 0.3.0. | ||||||||||||||||||||||
| permuteRef.current = Array.from({ length: slotCount }, (_, i) => i) | ||||||||||||||||||||||
| useGlobalStore.setState({ permute: [...permuteRef.current] }); | ||||||||||||||||||||||
| },[slotCount]) | ||||||||||||||||||||||
|
Comment on lines
+27
to
+31
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The
Suggested change
|
||||||||||||||||||||||
| return ( | ||||||||||||||||||||||
| <> | ||||||||||||||||||||||
| <div className="flex gap-2"> | ||||||||||||||||||||||
| {Array.from({ length: slotCount }).map((_, slotIdx) => ( | ||||||||||||||||||||||
| <Select | ||||||||||||||||||||||
| key={slotIdx} | ||||||||||||||||||||||
| value={String(permute?.[slotIdx])} | ||||||||||||||||||||||
| onValueChange={(v) => handleChange(slotIdx, Number(v))} | ||||||||||||||||||||||
| > | ||||||||||||||||||||||
| <SelectTrigger className='flex-1 min-w-0 truncate'> | ||||||||||||||||||||||
| <SelectValue /> | ||||||||||||||||||||||
| </SelectTrigger> | ||||||||||||||||||||||
| <SelectContent> | ||||||||||||||||||||||
| {dimNames.map((dim, dimIdx) => ( | ||||||||||||||||||||||
| <SelectItem key={dimIdx} value={String(dimIdx)}> | ||||||||||||||||||||||
| {dim} | ||||||||||||||||||||||
| </SelectItem> | ||||||||||||||||||||||
| ))} | ||||||||||||||||||||||
| </SelectContent> | ||||||||||||||||||||||
| </Select> | ||||||||||||||||||||||
| ))} | ||||||||||||||||||||||
| </div> | ||||||||||||||||||||||
| {warnDuplicates && <p className='text-red-500 font-bold'> | ||||||||||||||||||||||
| DUPLICATE DIMENSION USED | ||||||||||||||||||||||
| </p>} | ||||||||||||||||||||||
| </> | ||||||||||||||||||||||
| ); | ||||||||||||||||||||||
| }; | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
|
|
||||||||||||||||||||||
Uh oh!
There was an error while loading. Please reload this page.