File size: 3,542 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import { debounce, isEqual } from 'lodash';
import PropTypes from 'prop-types';
import { Component } from 'react';
import { connect } from 'react-redux';
import TranslatableString from 'calypso/components/translatable/proptype';
import { isGravPoweredOAuth2Client, isGravatarFlowOAuth2Client } from 'calypso/lib/oauth2-clients';
import {
	setDocumentHeadTitle as setTitle,
	setDocumentHeadLink as setLink,
	setDocumentHeadMeta as setMeta,
	setDocumentHeadUnreadCount as setUnreadCount,
} from 'calypso/state/document-head/actions';
import { getDocumentHeadFormattedTitle } from 'calypso/state/document-head/selectors/get-document-head-formatted-title';
import { getDocumentHeadTitle } from 'calypso/state/document-head/selectors/get-document-head-title';
import { gravatarClientData } from 'calypso/state/oauth2-clients/reducer';
import { getCurrentOAuth2Client } from 'calypso/state/oauth2-clients/ui/selectors';

const isServer = typeof document === 'undefined';

class DocumentHead extends Component {
	constructor( props ) {
		super( props );
		// In SSR, sync the state in constructor, in browser do it in effects.
		if ( isServer ) {
			this.syncState();
		}
	}

	componentDidMount() {
		this.syncState();
		this.setFormattedTitle();
	}

	componentDidUpdate( prevProps ) {
		this.syncState( prevProps );

		if ( this.props.formattedTitle !== prevProps.formattedTitle ) {
			this.setFormattedTitle();
		}
	}

	componentWillUnmount() {
		this.setFormattedTitle.cancel();
	}

	syncState( prevProps = null ) {
		const { title, unreadCount, link, meta } = this.props;
		// The `title` prop is commonly receiving its value as a result from a `translate` call
		// and in some cases it returns a React component instead of string.
		// A shallow comparison of two React components may result in unnecessary title updates.
		// To avoid that, we compare the string representation of the passed `title` prop value.
		if (
			title !== undefined &&
			! ( prevProps && prevProps.title?.toString?.() === title?.toString?.() )
		) {
			this.props.setTitle( title );
		}

		if ( unreadCount !== undefined && ! ( prevProps && prevProps.unreadCount === unreadCount ) ) {
			this.props.setUnreadCount( unreadCount );
		}

		if ( link !== undefined && ! ( prevProps && isEqual( prevProps.link, link ) ) ) {
			this.props.setLink( link );
		}

		if ( meta !== undefined && ! ( prevProps && isEqual( prevProps.meta, meta ) ) ) {
			this.props.setMeta( meta );
		}
	}

	setFormattedTitle = debounce( () => {
		document.title = this.props.formattedTitle;
	} );

	render() {
		return null;
	}
}

DocumentHead.propTypes = {
	title: TranslatableString,
	skipTitleFormatting: PropTypes.bool,
	unreadCount: PropTypes.number,
	link: PropTypes.array,
	meta: PropTypes.array,
	setTitle: PropTypes.func.isRequired,
	setLink: PropTypes.func.isRequired,
	setMeta: PropTypes.func.isRequired,
	setUnreadCount: PropTypes.func.isRequired,
};

export default connect(
	( state, props ) => {
		const oauth2Client = getCurrentOAuth2Client( state );

		// Use Gravatar's title for the Gravatar-related OAuth2 clients in CSR.
		if ( isGravPoweredOAuth2Client( oauth2Client ) ) {
			return {
				formattedTitle: isGravatarFlowOAuth2Client( oauth2Client )
					? gravatarClientData.title
					: oauth2Client.title,
			};
		}

		if ( props.skipTitleFormatting ) {
			return { formattedTitle: getDocumentHeadTitle( state ) };
		}

		return { formattedTitle: getDocumentHeadFormattedTitle( state ) };
	},
	{
		setTitle,
		setLink,
		setMeta,
		setUnreadCount,
	}
)( DocumentHead );