// Dependencies import { atomWithHash } from 'jotai-location' import { about, collection, tags, techniques, designers, developers, examples, } from '@freesewing/collection' import { mutateObject } from '@freesewing/utils' //import { urls } from '@freesewing/config' //import { measurements as measurementTranslations } from '@freesewing/i18n' //import { measurements as designMeasurements } from '@freesewing/collection' // Context //import { LoadingStatusContext } from '@freesewing/react/context/LoadingStatus' import { ModalContext } from '@freesewing/react/context/Modal' // Hooks import React, { useState, useContext } from 'react' //import { useAccount } from '@freesewing/react/hooks/useAccount' //import { useBackend } from '@freesewing/react/hooks/useBackend' import { useAtom } from 'jotai' // Components import { Link as WebLink, AnchorLink } from '@freesewing/react/components/Link' import { CircleIcon, CisFemaleIcon, FilterIcon, ResetIcon, ShowcaseIcon, } from '@freesewing/react/components/Icon' import { lineDrawingsFront as lineDrawings } from '@freesewing/react/components/LineDrawing' /* import { designs, tags, techniques } from 'shared/config/designs.mjs' import { DesignCard, DesignLink, ns as designNs } from 'shared/components/designs/design.mjs' import { useTranslation } from 'next-i18next' import { useState } from 'react' import { FilterIcon, ShowcaseIcon, CisFemaleIcon, ResetIcon } from 'shared/components/icons.mjs' import { capitalize, objUpdate } from 'shared/utils.mjs' import { Difficulty } from 'shared/components/designs/difficulty.mjs' import { collection } from 'site/hooks/use-design.mjs' export const ns = designNs */ const filterAtom = atomWithHash('filter', { example: true }) export const useFilter = () => { return useAtom(filterAtom) } /** * React component to show the FreeSewing collection and pick a design * * @param {object} props - All React props * @param {function} Link - An optional framework specific Link component for client-side routing */ export const Collection = ({ Link = false, linkTo = 'about' }) => { if (!Link) Link = WebLink // State const [filter, setFilter] = useFilter() const [showFilters, setShowFilters] = useState(false) console.log(tags) /* * Apply filter */ const filtered = {} for (const d of collection) { let skip = false if ( filter.tag && filter.tag.filter((tag) => about[d].tags.includes(tag)).length < filter.tag.length ) skip = true if ( filter.tech && filter.tech.filter((tech) => about[d].techniques.includes(tech)).length < filter.tech.length ) skip = true if (filter.difficulty && filter.difficulty !== about[d].difficulty) skip = true if (!skip) filtered[d] = d } const updateFilter = (path, val) => { // Allow clicking the same difficulty to remove it as a filter if (path === 'difficulty' && val === filter.difficulty) val = 'unset' const newFilter = mutateObject({ ...filter }, path, val) setFilter(newFilter) } const toggle = (type, val) => { const current = filter[type] || [] const newSet = new Set(current) if (newSet.has(val)) newSet.delete(val) else newSet.add(val) updateFilter(type, [...newSet]) } return ( <>
{Object.keys(filtered) .sort() .map((d) => ( {d} ))}
{showFilters ? ( <>
Filters ({Object.keys(filtered).length}/{collection.length})
Tags: {tags.map((tag) => ( ))}
Techniques {techniques.sort().map((tech) => ( ))}
Difficulty: {[1, 2, 3, 4, 5].map((score) => ( ))}
) : (
)}
{Object.keys(filtered) .sort() .map((d) => ( ))}
) } /* * A helper component to show a design technique * * @param {object} props - All React props * @param {function} props.Link - A Link component, typically specific to the framework for client-side routing * @param {string} props.technique - The technique name/id */ const Technique = ({ Link = WebLink, technique }) => ( {technique} ) /* * A helper component to show a design tag * * @param {object} props - All React props * @param {function} props.Link - A Link component, typically specific to the framework for client-side routing * @param {string} props.tag - The tag name/id */ const Tag = ({ Link = WebLink, technique }) => ( {tag} ) const DesignCard = ({ name, lineDrawing = false, linkTo, Link }) => { if (!Link) Link = WebLink const LineDrawing = lineDrawing && lineDrawings[name] ? lineDrawings[name] : ({ className }) =>
const exampleImageUrl = examples.href[name] ? examples.href[name] : noExample const bg = { aspectRatio: '1/1.4' } if (!lineDrawing) { bg.backgroundImage = `url(${exampleImageUrl}` bg.backgroundSize = 'cover' bg.backgroundPosition = 'center center' } return (
{about[name].name}
{lineDrawing ? (
) : ( )}
) } /* * A helper component to show difficulety of a design * * @param {object} props - All React props * @param {number} props.score - The difficulty score of the design (1-5) */ const Difficulty = ({ score = 0 }) => (
{[0, 1, 2, 3, 4].map((i) => ( ))}
) const linkBuilders = { new: (design) => `/-/?d=${design.toLowerCase()}`, docs: (design) => `/docs/designs/${design.toLowerCase()}/`, about: (design) => `/designs/${design.toLowerCase()}/`, } const noExample = 'https://images.pexels.com/photos/5626595/pexels-photo-5626595.jpeg?cs=srgb&fm=jpg&w=640&h=427'