Files
pawol.nu/components/awtis/mizik-lis.js
T

96 lines
2.5 KiB
JavaScript
Raw Normal View History

2023-07-22 13:45:08 +04:00
'use client'
2020-12-11 01:48:25 +01:00
2023-07-22 13:45:08 +04:00
import React from 'react'
import PropTypes from 'prop-types'
import {useParams} from 'next/navigation'
import {GroupedVirtuoso} from 'react-virtuoso'
import List from '@mui/material/List'
import ListItem from '@mui/material/ListItem'
import ListSubheader from '@mui/material/ListSubheader'
2021-01-12 21:59:17 +01:00
2023-07-22 13:45:08 +04:00
import {groupBy} from 'lodash'
import {formatKuveti} from '../../lib/kuveti'
import MizikLyen from './mizik-lyen'
2020-12-11 01:48:25 +01:00
2023-07-22 13:45:08 +04:00
function grupPawol(pawol) {
2026-04-21 19:16:11 +04:00
const pawolTrie = pawol.sort((a, b) => a.titre.localeCompare(b.titre, 'fr', {sensitivity: 'base'}))
const grupPawol = groupBy(pawol, anPawol => anPawol.titre[0].toUpperCase())
2023-07-22 13:45:08 +04:00
const grupCounts = Object.values(grupPawol).map(anPawol => anPawol.length)
const grup = Object.keys(grupPawol)
grup.sort((a, b) => a[0].localeCompare(b[0], 'fr', {sensitivity: 'base'}))
2022-01-19 06:35:04 +04:00
2023-07-22 13:45:08 +04:00
return {pawol: pawolTrie, grupCounts, grup}
2022-01-19 06:35:04 +04:00
}
2023-07-22 14:50:26 +04:00
export default function MizikLis({niAwtis, paroles, meteEsMobilOuve}) {
2023-07-22 13:45:08 +04:00
const params = useParams()
const {pawol, grupCounts, grup} = grupPawol(paroles)
2020-12-11 01:48:25 +01:00
2023-07-22 13:45:08 +04:00
return (
<GroupedVirtuoso
groupCounts={grupCounts}
components={MUIComponents}
groupContent={index => <div>{grup[index]}</div>}
itemContent={index => {
const anPawol = pawol[index]
2026-04-21 19:16:11 +04:00
const {couverture} = anPawol
2023-07-22 13:45:08 +04:00
const kuvetiFormat = formatKuveti(couverture)
2023-07-22 13:45:08 +04:00
return (
2023-07-22 14:50:26 +04:00
<MizikLyen niAwtis={niAwtis} anPawol={anPawol} kuveti={kuvetiFormat} slug={params.slug} meteEsMobilOuve={meteEsMobilOuve} />
2023-07-22 13:45:08 +04:00
)
}}
/>
)
}
2023-07-22 13:45:08 +04:00
const MUIComponents = {
List: React.forwardRef(({style, children}, listRef) => (
<List ref={listRef} style={{padding: 0, ...style, margin: 0}} component='div'>
{children}
</List>
)),
2023-07-22 13:45:08 +04:00
Item: ({children, ...props}) => (
<ListItem component='div' {...props} style={{margin: 0, paddingInline: 0}}>
{children}
</ListItem>
),
2020-12-11 01:48:25 +01:00
2023-07-22 13:45:08 +04:00
Group: ({children, style, ...props}) => (
<ListSubheader
component='div'
{...props}
style={{
...style,
backgroundColor: 'transparent',
margin: 0,
paddingLeft: 5,
fontWeight: 'bold'
}}
>
{children}
</ListSubheader>
2020-12-11 01:48:25 +01:00
)
}
MizikLis.propTypes = {
2020-12-18 22:13:52 +01:00
niAwtis: PropTypes.bool,
2023-07-22 14:50:26 +04:00
paroles: PropTypes.array.isRequired,
meteEsMobilOuve: PropTypes.func.isRequired
2023-07-22 13:45:08 +04:00
}
MUIComponents.List.propTypes = {
style: PropTypes.object,
children: PropTypes.node.isRequired,
}
MUIComponents.Item.propTypes = {
children: PropTypes.node.isRequired,
}
2023-07-22 13:45:08 +04:00
MUIComponents.Group.propTypes = {
children: PropTypes.node.isRequired,
style: PropTypes.object,
2020-12-11 01:48:25 +01:00
}