File size: 4,875 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 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 |
/**
* Returns the current user ID
* @param {Object} state Global state tree
* @returns {?number} Current user ID
*/
export function getCurrentUserId( state ) {
return state.currentUser?.id;
}
/**
* Is the current user logged in?
* @param {Object} state Global state tree
* @returns {boolean} True if logged in, False if not
*/
export function isUserLoggedIn( state ) {
return getCurrentUserId( state ) !== null;
}
/**
* Returns the user object for the current user.
* @param {Object} state Global state tree
* @returns {import('calypso/lib/user/user').UserData|null} Current user
*/
export function getCurrentUser( state ) {
return state?.currentUser?.user ?? null;
}
/**
* Returns a selector that fetches a property from the current user object
* @template S,T
* @param {string} path Path to the property in the user object
* @param {?T} otherwise A default value that is returned if no user or property is found
* @returns {(state: S) => T} A selector which takes the state as a parameter
*/
export const createCurrentUserSelector =
( path, otherwise = null ) =>
( state ) => {
const user = getCurrentUser( state );
return user?.[ path ] ?? otherwise;
};
/**
* Returns the locale slug for the current user.
* @param {Object} state Global state tree
* @returns {?string} Current user locale
*/
export const getCurrentUserLocale = createCurrentUserSelector( 'localeSlug' );
/**
* Returns the country code for the current user.
* @param {Object} state Global state tree
* @returns {?string} Current user country code
*/
export const getCurrentUserCountryCode = createCurrentUserSelector( 'user_ip_country_code' );
/**
* Returns the number of sites for the current user.
* @param {Object} state Global state tree
* @returns {?number} Current user site count
*/
export function getCurrentUserSiteCount( state ) {
const user = getCurrentUser( state );
if ( ! user ) {
return null;
}
return user.site_count || 0;
}
/**
* Returns the number of visible sites for the current user.
* @param {Object} state Global state tree
* @returns {?number} Current user visible site count
*/
export function getCurrentUserVisibleSiteCount( state ) {
const user = getCurrentUser( state );
if ( ! user ) {
return null;
}
return user.visible_site_count || 0;
}
/**
* Returns the number of visible Jetpack sites for the current user.
* @param {Object} state Global state tree
* @returns {?number} Current user visible Jetpack site count
*/
export function getCurrentUserJetpackVisibleSiteCount( state ) {
const user = getCurrentUser( state );
if ( ! user ) {
return null;
}
return user.jetpack_visible_site_count || 0;
}
/**
* Returns the date (of registration) for the current user.
* @param {Object} state Global state tree
* @returns {?string} Date of registration for user
*/
export const getCurrentUserDate = createCurrentUserSelector( 'date' );
/**
* Returns the username of the current user.
* @param {Object} state Global state tree
* @returns {?string} The username of the current user.
*/
export const getCurrentUserName = createCurrentUserSelector( 'username' );
/**
* Returns the primary email of the current user.
* @param {Object} state Global state tree
* @returns {?string} The primary email of the current user.
*/
export const getCurrentUserEmail = createCurrentUserSelector( 'email' );
/**
* Returns the primary email of the current user.
* @param {Object} state Global state tree
* @returns {?string} The primary email of the current user.
*/
export const getCurrentUserDisplayName = createCurrentUserSelector( 'display_name' );
/**
* Returns true if the specified flag is enabled for the user
* @param {Object} state Global state tree
* @param {string} flagName Flag name
* @returns {boolean} Whether the flag is enabled for the user
*/
export function currentUserHasFlag( state, flagName ) {
return state.currentUser.flags.indexOf( flagName ) !== -1;
}
/**
* Returns true if the current user is email-verified.
* @param {Object} state Global state tree
* @returns {boolean} Whether the current user is email-verified.
*/
export const isCurrentUserEmailVerified = createCurrentUserSelector( 'email_verified', false );
/**
* Returns the Lasagna JWT for the current user.
* @param {Object} state Global state tree
* @returns {?string} Lasagna JWT
*/
export function getCurrentUserLasagnaJwt( state ) {
return state.currentUser.lasagnaJwt;
}
/**
* Returns true if the user was bootstrapped (i.e. user data was fetched by the server
* and hydrated using window.currentUser)
* @returns {boolean} Whether the current user is bootstrapped
*/
export const isCurrentUserBootstrapped = createCurrentUserSelector( 'bootstrapped', false );
|