manifold/web/components/groups/contract-groups-list.tsx

83 lines
2.6 KiB
TypeScript
Raw Permalink Normal View History

import { Col } from 'web/components/layout/col'
import { Row } from 'web/components/layout/row'
import clsx from 'clsx'
import { GroupLinkItem } from 'web/pages/groups'
import { XIcon } from '@heroicons/react/outline'
import { Button } from 'web/components/button'
import { GroupSelector } from 'web/components/groups/group-selector'
import {
addContractToGroup,
removeContractFromGroup,
} from 'web/lib/firebase/groups'
import { User } from 'common/user'
import { Contract } from 'common/contract'
2022-07-22 19:53:19 +00:00
import { SiteLink } from 'web/components/site-link'
import { useGroupsWithContract, useMemberGroupIds } from 'web/hooks/use-group'
import { Group } from 'common/group'
export function ContractGroupsList(props: {
contract: Contract
user: User | null | undefined
}) {
const { user, contract } = props
const { groupLinks } = contract
const groups = useGroupsWithContract(contract)
const memberGroupIds = useMemberGroupIds(user)
const canModifyGroupContracts = (group: Group, userId: string) => {
return (
group.creatorId === userId ||
group.anyoneCanJoin ||
memberGroupIds?.includes(group.id)
)
}
return (
<Col className={'gap-2'}>
2022-07-22 19:53:19 +00:00
<span className={'text-xl text-indigo-700'}>
<SiteLink href={'/groups/'}>Groups</SiteLink>
</span>
{user && (
2022-07-22 19:53:19 +00:00
<Col className={'ml-2 items-center justify-between sm:flex-row'}>
<span>Add to: </span>
<GroupSelector
options={{
showSelector: true,
showLabel: false,
ignoreGroupIds: groupLinks?.map((g) => g.groupId),
}}
2022-07-22 19:53:19 +00:00
setSelectedGroup={(group) =>
group && addContractToGroup(group, contract, user.id)
2022-07-22 19:53:19 +00:00
}
selectedGroup={undefined}
creator={user}
/>
2022-07-22 19:53:19 +00:00
</Col>
)}
2022-10-05 22:07:41 +00:00
<Col className="h-96 overflow-auto">
{groups.length === 0 && (
<Col className="text-greyscale-4">No groups yet...</Col>
)}
{groups.map((group) => (
<Row
key={group.id}
className={clsx('items-center justify-between gap-2 p-2')}
>
<Row className="line-clamp-1 items-center gap-2">
<GroupLinkItem group={group} />
</Row>
{user && canModifyGroupContracts(group, user.id) && (
<Button
color={'gray-white'}
size={'xs'}
onClick={() => removeContractFromGroup(group, contract)}
>
<XIcon className="text-greyscale-4 h-4 w-4" />
</Button>
)}
</Row>
2022-10-05 22:07:41 +00:00
))}
</Col>
</Col>
)
}