Skip to content
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

Track selection #188

Merged
merged 16 commits into from
Oct 19, 2024
Merged

Track selection #188

merged 16 commits into from
Oct 19, 2024

Conversation

uDaiko
Copy link
Contributor

@uDaiko uDaiko commented Oct 15, 2024

closes #125

Adds a content reveal checkbox list to the delegate page. based on checked and unchecked boxes it updates the state array containing the trackIds which are further being handled.

Submission checklist:

Layout

  • Change inspected in the mobile web ui
  • Change inspected in dark mode

@wirednkod
Copy link
Member

Thank you @uDaiko for the PR. Can you please resolve the conflicts?

@uDaiko
Copy link
Contributor Author

uDaiko commented Oct 16, 2024

Hey @wirednkod , yeah yesterday I noticed that I was 1 commit behind but did not have the time to fix it anymore. Merged it now so the conflicts should be resolved now.

Copy link
Contributor

@Tbaut Tbaut left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @uDaiko, thank you so much for this I have made quite a lot of changes that would have taken forever to go through in the comments, but I couldn't push to your branch. Is there any chance you can give me push access to your forked repo?

My changes include:

  • manage state only in one component (the parent) removing all the useEffect
  • use useMemo and useCallback anywhere possible
  • show the track id and order by them
  • move consts outside of the component
  • use label with checkboxes (we should actually use shadn here and create a component)

I still need to inspect and properly test, to see how it behaves when doing the transaction.

Here is how the TrackSelection looks like

import { useCallback, useMemo } from 'react'

interface TrackSelectionProps {
  trackNamesMap: Map<number, string>
  onTrackSelectionChange: (selectedTracks: number[]) => void
  selectedTracks: number[]
}

const firstColumn = [0, 1, 2]
const secondColumn = [10, 11, 12, 13, 14, 15]
const fourthColumn = [30, 31, 32, 33, 34]
const thirdColumn = [20, 21]
const orderedTracks = [
  ...firstColumn,
  ...secondColumn,
  ...thirdColumn,
  ...fourthColumn,
]
export const TrackSelection = ({
  trackNamesMap,
  onTrackSelectionChange,
  selectedTracks,
}: TrackSelectionProps) => {
  //due to hardcoded ids to achieve a specific order we have to make sure to have a catch all to capture the possibility of uncaught ids
  const columnArrays = useMemo(() => {
    const remainingTracks = Array.from(trackNamesMap.keys()).filter(
      (trackId) => !orderedTracks.includes(trackId),
    )

    return [
      firstColumn,
      secondColumn,
      thirdColumn,
      fourthColumn,
      remainingTracks,
    ]
  }, [trackNamesMap])

  const addTrackToSelection = useCallback(
    (trackId: number) => {
      const newSelection = [...selectedTracks, trackId]
      onTrackSelectionChange(newSelection)
    },
    [onTrackSelectionChange, selectedTracks],
  )

  const removeTrackFromSelection = useCallback(
    (trackId: number) => {
      const newSelection = selectedTracks.filter((id) => id !== trackId)
      onTrackSelectionChange(newSelection)
    },
    [onTrackSelectionChange, selectedTracks],
  )

  return (
    <div className="container mx-auto p-4">
      <div className="flex flex-wrap">
        {columnArrays.map((column, columnIndex) => (
          <div key={columnIndex} className="min-w-[180px] px-2 md:w-1/4">
            <div className="flex flex-col">
              {column.map((trackId) => {
                const item = trackNamesMap.get(trackId)
                const isSelected = selectedTracks.includes(trackId)
                if (!item) return null
                return (
                  <div key={trackId} className="mb-2 min-h-[3rem] p-2 text-xs">
                    <input
                      type="checkbox"
                      id={trackId.toString()}
                      className="self-start accent-[hsl(var(--primary))]"
                      checked={isSelected}
                      onChange={() =>
                        isSelected
                          ? removeTrackFromSelection(trackId)
                          : addTrackToSelection(trackId)
                      }
                    />
                    <label
                      htmlFor={trackId.toString()}
                      className="ml-2 cursor-pointer"
                    >{`${trackId} - ${item}`}</label>
                  </div>
                )
              })}
            </div>
          </div>
        ))}
      </div>
    </div>
  )
}

@uDaiko
Copy link
Contributor Author

uDaiko commented Oct 17, 2024

@Tbaut Sure no problem. You should have an invite for collaboration.

@Tbaut
Copy link
Contributor

Tbaut commented Oct 17, 2024

Thanks, I made a PR in your repo, I let you take a look. I'll verify the proper execution shortly uDaiko#1

@Tbaut Tbaut self-requested a review October 19, 2024 20:15
Copy link
Contributor

@Tbaut Tbaut left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me, thank you so much 🙏

@Tbaut Tbaut merged commit cd3b3da into delegit-xyz:main Oct 19, 2024
2 checks passed
@Tbaut Tbaut deleted the track_selection branch October 19, 2024 20:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Allow a selection of tracks
3 participants