-
Notifications
You must be signed in to change notification settings - Fork 55
Introduce the ability to switch users from the Command Palette #277
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
9798f2c
Introduce the ability to switch users from the Command Palette.
johnbillion 7afc60b
Use the old user avatar.
johnbillion 72932ad
It's called PHPCS, not JSCS.
johnbillion e74655c
Improve the search handling.
johnbillion 2e3179d
Docs.
johnbillion 0f09511
Switch to the admin-users dashicon.
johnbillion 1175bcb
More tweaks to search handling.
johnbillion df6f6d9
Avoid using `filemtime()`.
johnbillion 2b38556
Shorten this name.
johnbillion 0eb4a0f
Docs.
johnbillion File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,142 @@ | ||
| /** | ||
| * Command Palette integration for User Switching. | ||
| */ | ||
|
|
||
| ( function () { | ||
| const commandsStore = wp.commands.store; | ||
| const coreStore = wp.coreData.store; | ||
| const dispatch = wp.data.dispatch; | ||
| const useSelect = wp.data.useSelect; | ||
| const useState = wp.element.useState; | ||
| const useEffect = wp.element.useEffect; | ||
| const useMemo = wp.element.useMemo; | ||
| const useDebounce = wp.compose.useDebounce; | ||
| const __ = wp.i18n.__; | ||
| const sprintf = wp.i18n.sprintf; | ||
| const el = wp.element.createElement; | ||
| const Dashicon = wp.components.Dashicon; | ||
|
|
||
| function useDebouncedValue( value ) { | ||
| const [ debouncedValue, setDebouncedValue ] = useState( '' ); | ||
| const debounced = useDebounce( setDebouncedValue, 250 ); | ||
| useEffect( function () { | ||
| debounced( value ); | ||
| return function () { | ||
| debounced.cancel(); | ||
| }; | ||
| }, [ debounced, value ] ); | ||
| return debouncedValue; | ||
| } | ||
|
|
||
| const settings = window.userSwitchingCommands; | ||
|
|
||
| if ( ! settings ) { | ||
| return; | ||
| } | ||
|
|
||
| const switchIcon = el( Dashicon, { icon: 'admin-users' } ); | ||
|
|
||
| // Static command: Switch Back. | ||
| if ( settings.switchBackUrl ) { | ||
| dispatch( commandsStore ).registerCommand( { | ||
| name: 'user-switching/switch-back', | ||
| label: settings.switchBackLabel, | ||
| searchLabel: `Switch back ${ settings.switchBackLabel }`, | ||
| icon: settings.switchBackAvatar ? el( 'img', { | ||
| src: settings.switchBackAvatar, | ||
| alt: '', | ||
| } ) : switchIcon, | ||
| callback: function ( args ) { | ||
| document.location.href = settings.switchBackUrl; | ||
| args.close(); | ||
| }, | ||
| } ); | ||
| } | ||
|
|
||
| // Static command: Switch Off. | ||
| if ( settings.switchOffUrl ) { | ||
| dispatch( commandsStore ).registerCommand( { | ||
| name: 'user-switching/switch-off', | ||
| label: settings.switchOffLabel, | ||
| // This facilitates searching either in English or the localized language | ||
| searchLabel: `Switch off ${ settings.switchOffLabel }`, | ||
| icon: switchIcon, | ||
| callback: function ( args ) { | ||
| document.location.href = settings.switchOffUrl; | ||
| args.close(); | ||
| }, | ||
| } ); | ||
| } | ||
|
|
||
| // Dynamic command loader for searching users to switch to. | ||
| if ( settings.canSwitchUsers ) { | ||
| dispatch( commandsStore ).registerCommandLoader( { | ||
| name: 'user-switching/switch-to-user', | ||
| hook: function useSwitchToUserLoader( options ) { | ||
| // This facilitates searching either in English or the localized language | ||
| // and trims the search query to just the name. | ||
| const trimmedSearch = options.search | ||
| .replace( new RegExp( settings.switchToLabel, 'i' ), '' ) | ||
| .replace( /Switch to/i, '' ) | ||
| .trim(); | ||
| const search = useDebouncedValue( trimmedSearch ); | ||
|
|
||
| const { users, isLoading } = useSelect( function ( select ) { | ||
| if ( ! search ) { | ||
| return { users: [], isLoading: false }; | ||
| } | ||
|
|
||
| const query = { | ||
| search: search, | ||
| per_page: 10, | ||
| context: 'view', | ||
| }; | ||
| const core = select( coreStore ); | ||
|
|
||
| return { | ||
| users: core.getEntityRecords( 'root', 'user', query ) || [], | ||
| isLoading: ! core.hasFinishedResolution( 'getEntityRecords', [ 'root', 'user', query ] ), | ||
| }; | ||
| }, [ search ] ); | ||
|
|
||
| const commands = useMemo( function () { | ||
| return users | ||
| .filter( function ( user ) { | ||
| return user.user_switching_url; | ||
| } ) | ||
| .map( function ( user ) { | ||
| const avatarUrl = user.avatar_urls && ( user.avatar_urls[ '48' ] || user.avatar_urls[ '24' ] ); | ||
|
|
||
| return { | ||
| name: `user-switching/switch-to-${ user.id }`, | ||
| label: sprintf( | ||
| /* translators: %s: User's display name. */ | ||
| __( 'Switch to %s', 'user-switching' ), | ||
| user.name, | ||
| ), | ||
| // This facilitates searching either in English or the localized language | ||
| searchLabel: sprintf( | ||
| /* translators: %s: User's display name. */ | ||
| __( 'Switch to %s', 'user-switching' ), | ||
| `${ user.name } ${ user.slug } Switch to ${ user.name } ${ user.slug }`, | ||
| ), | ||
| icon: avatarUrl ? el( 'img', { | ||
| src: avatarUrl, | ||
| alt: '', | ||
| } ) : switchIcon, | ||
| callback: function ( args ) { | ||
| document.location.href = user.user_switching_url; | ||
| args.close(); | ||
| }, | ||
| }; | ||
| } ); | ||
| }, [ users ] ); | ||
|
|
||
| return { | ||
| commands: commands, | ||
| isLoading: isLoading, | ||
| }; | ||
| }, | ||
| } ); | ||
| } | ||
| } )(); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.