// @flow import { observer } from "mobx-react"; import { PlusIcon } from "outline-icons"; import * as React from "react"; import { useTranslation, Trans } from "react-i18next"; import styled from "styled-components"; import Collection from "models/Collection"; import Button from "components/Button"; import Divider from "components/Divider"; import Flex from "components/Flex"; import HelpText from "components/HelpText"; import InputSelectPermission from "components/InputSelectPermission"; import Labeled from "components/Labeled"; import Modal from "components/Modal"; import PaginatedList from "components/PaginatedList"; import Switch from "components/Switch"; import AddGroupsToCollection from "./AddGroupsToCollection"; import AddPeopleToCollection from "./AddPeopleToCollection"; import CollectionGroupMemberListItem from "./components/CollectionGroupMemberListItem"; import MemberListItem from "./components/MemberListItem"; import useBoolean from "hooks/useBoolean"; import useCurrentUser from "hooks/useCurrentUser"; import useStores from "hooks/useStores"; import useToasts from "hooks/useToasts"; type Props = {| collection: Collection, |}; function CollectionPermissions({ collection }: Props) { const { t } = useTranslation(); const user = useCurrentUser(); const { memberships, collectionGroupMemberships, users, groups, auth, } = useStores(); const { showToast } = useToasts(); const [ addGroupModalOpen, handleAddGroupModalOpen, handleAddGroupModalClose, ] = useBoolean(); const [ addMemberModalOpen, handleAddMemberModalOpen, handleAddMemberModalClose, ] = useBoolean(); const handleRemoveUser = React.useCallback( async (user) => { try { await memberships.delete({ collectionId: collection.id, userId: user.id, }); showToast( t(`{{ userName }} was removed from the collection`, { userName: user.name, }), { type: "success", } ); } catch (err) { showToast(t("Could not remove user"), { type: "error" }); } }, [memberships, showToast, collection, t] ); const handleUpdateUser = React.useCallback( async (user, permission) => { try { await memberships.create({ collectionId: collection.id, userId: user.id, permission, }); showToast( t(`{{ userName }} permissions were updated`, { userName: user.name }), { type: "success", } ); } catch (err) { showToast(t("Could not update user"), { type: "error" }); } }, [memberships, showToast, collection, t] ); const handleRemoveGroup = React.useCallback( async (group) => { try { await collectionGroupMemberships.delete({ collectionId: collection.id, groupId: group.id, }); showToast( t(`The {{ groupName }} group was removed from the collection`, { groupName: group.name, }), { type: "success", } ); } catch (err) { showToast(t("Could not remove group"), { type: "error" }); } }, [collectionGroupMemberships, showToast, collection, t] ); const handleUpdateGroup = React.useCallback( async (group, permission) => { try { await collectionGroupMemberships.create({ collectionId: collection.id, groupId: group.id, permission, }); showToast( t(`{{ groupName }} permissions were updated`, { groupName: group.name, }), { type: "success", } ); } catch (err) { showToast(t("Could not update user"), { type: "error" }); } }, [collectionGroupMemberships, showToast, collection, t] ); const handleChangePermission = React.useCallback( async (ev) => { try { await collection.save({ permission: ev.target.value }); showToast(t("Default access permissions were updated"), { type: "success", }); } catch (err) { showToast(t("Could not update permissions"), { type: "error" }); } }, [collection, showToast, t] ); const fetchOptions = React.useMemo(() => ({ id: collection.id }), [ collection.id, ]); const handleSharingChange = React.useCallback( async (ev: SyntheticInputEvent<*>) => { try { await collection.save({ sharing: ev.target.checked }); showToast(t("Public document sharing permissions were updated"), { type: "success", }); } catch (err) { showToast(t("Could not update public document sharing"), { type: "error", }); } }, [collection, showToast, t] ); const collectionName = collection.name; const collectionGroups = groups.inCollection(collection.id); const collectionUsers = users.inCollection(collection.id); const isEmpty = !collectionGroups.length && !collectionUsers.length; const sharing = collection.sharing; const teamSharingEnabled = !!auth.team && auth.team.sharing; return ( {!collection.permission && ( }} /> )} {collection.permission === "read" && ( }} /> )} {collection.permission === "read_write" && ( }} /> )} {teamSharingEnabled ? ( When enabled, documents can be shared publicly on the internet. ) : ( Public sharing is currently disabled in the team security settings. )} {" "} {isEmpty && ( Add specific access for individual groups and team members )} ( handleRemoveGroup(group)} onUpdate={(permission) => handleUpdateGroup(group, permission)} /> )} /> {collectionGroups.length ? : null} ( handleRemoveUser(item)} onUpdate={(permission) => handleUpdateUser(item, permission)} /> )} /> ); } const Empty = styled(HelpText)` margin-top: 8px; `; const PermissionExplainer = styled(HelpText)` margin-top: -8px; margin-bottom: 24px; `; const Actions = styled.div` margin-bottom: 12px; `; export default observer(CollectionPermissions);