// Dependencies import orderBy from 'lodash.orderby' import { measurements } from 'site/prebuild/design-measurements.mjs' import { siteConfig } from 'site/site.config.mjs' import { capitalize } from 'shared/utils.mjs' // Hooks import { useState, useEffect } from 'react' import { useTranslation } from 'next-i18next' import { useAccount } from 'shared/hooks/use-account.mjs' import { useBackend } from 'shared/hooks/use-backend.mjs' // Components import { SetCandidate, ns as setNs } from 'shared/components/sets/set-candidate.mjs' import { CuratedSetCandidate } from 'shared/components/sets/curated-set-candidate.mjs' import { PopoutWrapper } from 'shared/components/wrappers/popout.mjs' import { Tag } from 'shared/components/tag.mjs' import { FilterIcon } from 'shared/components/icons.mjs' export const ns = setNs export const CuratedSetPicker = ({ design, language }) => { // Hooks const { account, token } = useAccount() const backend = useBackend(token) const { t } = useTranslation('sets') // State const [curatedSets, setCuratedSets] = useState([]) const [filter, setFilter] = useState([]) const [tags, setTags] = useState([]) const [reload, setReload] = useState(0) // Force a refresh const refresh = () => setReload(reload + 1) // Effects useEffect(() => { const getCuratedSets = async () => { const result = await backend.getCuratedSets() if (result.success) { const all = [] const allTags = new Set() for (const set of result.data.curatedSets) { all.push(set) for (const tag of set[`tags${capitalize(language)}`]) allTags.add(tag) } setCuratedSets(all) setTags([...allTags]) } } getCuratedSets() }, [reload]) const addFilter = (tag) => { const newFilter = [...filter, tag] setFilter(newFilter) } const removeFilter = (tag) => { const newFilter = filter.filter((t) => t !== tag) setFilter(newFilter) } const applyFilter = () => { const newList = new Set() for (const set of curatedSets) { const setTags = [] for (const lang of siteConfig.languages) { const key = `tags${capitalize(lang)}` setTags.push(...set[key]) } let match = 0 for (const tag of filter) { if (setTags.includes(tag)) match++ } if (match === filter.length) newList.add(set) } return [...newList] } const list = applyFilter() // Need to sort designs by their translated title const translated = {} for (const d of list) translated[t(`${d}.t`)] = d return ( <>

{t('curatedSets')}

{tags.map((tag) => ( addFilter(tag)}>{tag} ))}
{list.length} / {curatedSets.length}
{filter.map((tag) => ( removeFilter(tag)} color="success" hoverColor="error"> {tag} ))}
{orderBy(list, ['name'], ['asc']).map((set) => (
))}
) } export const UserSetPicker = ({ design, t, language }) => { // Hooks const { account, token } = useAccount() const backend = useBackend(token) // State const [sets, setSets] = useState({}) const [list, setList] = useState([]) // Effects useEffect(() => { const getSets = async () => { const result = await backend.getSets() if (result.success) { const all = {} for (const set of result.data.sets) all[set.id] = set setSets(all) } } getSets() }, []) // Need to sort designs by their translated title const translated = {} for (const d of list) translated[t(`${d}.t`)] = d return Object.keys(sets).length < 1 ? (
{t('patternForWhichSet')}

{t('fsmtm')}

) : ( <>

{t('yourSets')}

{Object.keys(sets).length > 0 ? ( <>
{orderBy(sets, ['name'], ['asc']).map((set) => (
))}
) : ( Implement UI for when there are no sets )} ) } export const BookmarkedSetPicker = ({ design, t }) => ( <>

{t('bookmarkedSets')}

Implement bookmarked set picker (also implement bookmarks) ) export const SetPicker = ({ design }) => { const { t, i18n } = useTranslation('sets') const { language } = i18n const pickerProps = { design, t, language } return ( <>

{t('chooseSet')}

) }