import React , { useEffect } from 'react' ; import Debug from 'debug' ; import Content from '../../Content' ; import { createDocumentLink , parseDocumentLink , HypermergeUrl } from '../../../ShareLink' ; import { Doc as WorkspaceDoc } from './Workspace' ; import './Authors.css' ; import { useDocument } from '../../../Hooks' ; const log = Debug ( 'pushpin:authors' ) ; interface Props { hypermergeUrl : HypermergeUrl ; } interface DocWithAuthors { authorIds : HypermergeUrl [ ] ; hypermergeUrl : HypermergeUrl ; } export default function Authors ( props ) { const authorIds = useAuthors ( props . hypermergeUrl ) ; const authors = authorIds . filter ( ( id , i , a ) => a . indexOf ( id ) === i ) . map ( ( id ) => < Content key = { id } context = "title-bar" url = { createDocumentLink ( 'contact' , id ) } / > ) ; return < div className = "Authors" > { authors } < /div>; } export function useAuthors ( workspaceUrl ) { const [ workspace , changeWorkspace ] = useDocument < WorkspaceDoc > ( workspaceUrl ) ; const { hypermergeUrl = null } = workspace ? parseDocumentLink ( workspace . currentDocUrl ) : { } ; const [ board , changeBoard ] = useDocument < DocWithAuthors > ( hypermergeUrl ) ; const selfId = workspace && workspace . selfId ; useEffect ( ( ) => { if ( ! workspace || ! board ) { return ; } log ( 'updating workspace contacts' ) ; const { authorIds = [ ] } = board ; changeWorkspace ( ( { contactIds } ) => { const newContactIds = authorIds . filter ( ( a ) => selfId !== a && ! contactIds . includes ( a ) ) ; if ( newContactIds . length ) { contactIds . push ( ... newContactIds ) ; } } ) ; } , [ selfId , board && board . authorIds ] ) ; useEffect ( ( ) => { if ( ! workspace || ! board ) { return ; } log ( 'adding self to authors' ) ; changeBoard ( ( board ) => { if ( ! board . authorIds ) { board . authorIds = [ ] ; } if ( selfId && ! board . authorIds . includes ( selfId ) ) { board . authorIds . push ( selfId ) ; } } ) ; } , [ selfId , board ? board . authorIds : false ] ) ; return ( board && board . authorIds ) || [ ] ; }