Skip to content
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

Identify the site frontpage in link UI search results #36493

Merged
merged 8 commits into from
Jan 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export const LinkControlSearchItem = ( {
icon={ globe }
/>
) }

<span className="block-editor-link-control__search-item-header">
<span className="block-editor-link-control__search-item-title">
<TextHighlight
Expand All @@ -57,12 +58,20 @@ export const LinkControlSearchItem = ( {
</span>
{ shouldShowType && suggestion.type && (
<span className="block-editor-link-control__search-item-type">
{ /* Rename 'post_tag' to 'tag'. Ideally, the API would return the localised CPT or taxonomy label. */ }
{ suggestion.type === 'post_tag' ? 'tag' : suggestion.type }
{ getVisualTypeName( suggestion ) }
</span>
) }
</Button>
);
};

function getVisualTypeName( suggestion ) {
if ( suggestion.isFrontPage ) {
return 'front page';
}

// Rename 'post_tag' to 'tag'. Ideally, the API would return the localised CPT or taxonomy label.
return suggestion.type === 'post_tag' ? 'tag' : suggestion.type;
}

export default LinkControlSearchItem;
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ export default function LinkControlSearchResults( {
) }
searchTerm={ currentInputValue }
shouldShowType={ shouldShowSuggestionsTypes }
isFrontPage={ suggestion?.isFrontPage }
/>
);
} ) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,7 @@ $preview-image-height: 140px;
font-size: 0.9em;
background-color: $gray-100;
border-radius: 2px;
white-space: nowrap; // tags shouldn't go over two lines.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@javierarce I updated it

}

.block-editor-link-control__search-item-description {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,21 +52,39 @@ const handleEntitySearch = async (
fetchSearchSuggestions,
directEntryHandler,
withCreateSuggestion,
withURLSuggestion
withURLSuggestion,
pageOnFront
) => {
const { isInitialSuggestions } = suggestionsQuery;
let resultsIncludeFrontPage = false;

let results = await Promise.all( [
fetchSearchSuggestions( val, suggestionsQuery ),
directEntryHandler( val ),
] );

// Identify front page and update type to match.
results[ 0 ] = results[ 0 ].map( ( result ) => {
if ( Number( result.id ) === pageOnFront ) {
resultsIncludeFrontPage = true;
result.isFrontPage = true;
return result;
}

return result;
} );

const couldBeURL = ! val.includes( ' ' );

// If it's potentially a URL search then concat on a URL search suggestion
// just for good measure. That way once the actual results run out we always
// have a URL option to fallback on.
if ( couldBeURL && withURLSuggestion && ! isInitialSuggestions ) {
if (
! resultsIncludeFrontPage &&
couldBeURL &&
withURLSuggestion &&
! isInitialSuggestions
) {
results = results[ 0 ].concat( results[ 1 ] );
} else {
results = results[ 0 ];
Expand Down Expand Up @@ -109,9 +127,11 @@ export default function useSearchHandler(
withCreateSuggestion,
withURLSuggestion
) {
const { fetchSearchSuggestions } = useSelect( ( select ) => {
const { fetchSearchSuggestions, pageOnFront } = useSelect( ( select ) => {
const { getSettings } = select( blockEditorStore );

return {
pageOnFront: getSettings().pageOnFront,
fetchSearchSuggestions: getSettings()
.__experimentalFetchLinkSuggestions,
};
Expand All @@ -131,7 +151,8 @@ export default function useSearchHandler(
fetchSearchSuggestions,
directEntryHandler,
withCreateSuggestion,
withURLSuggestion
withURLSuggestion,
pageOnFront
);
},
[ directEntryHandler, fetchSearchSuggestions, withCreateSuggestion ]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,18 @@ function useBlockEditorSettings( settings, hasTemplate ) {
hasUploadPermissions,
canUseUnfilteredHTML,
userCanCreatePages,
pageOnFront,
} = useSelect( ( select ) => {
const { canUserUseUnfilteredHTML } = select( editorStore );
const isWeb = Platform.OS === 'web';
const { canUser, getUnstableBase, hasFinishedResolution } = select(
coreStore
);
const {
canUser,
getUnstableBase,
hasFinishedResolution,
getEntityRecord,
} = select( coreStore );

const siteSettings = getEntityRecord( 'root', 'site' );

const siteData = getUnstableBase();

Expand All @@ -64,6 +70,7 @@ function useBlockEditorSettings( settings, hasTemplate ) {
hasResolvedLocalSiteData: hasFinishedResolvingSiteData,
baseUrl: siteData?.url || '',
userCanCreatePages: canUser( 'create', 'pages' ),
pageOnFront: siteSettings?.page_on_front,
};
}, [] );

Expand Down Expand Up @@ -139,6 +146,7 @@ function useBlockEditorSettings( settings, hasTemplate ) {
outlineMode: hasTemplate,
__experimentalCreatePageEntity: createPageEntity,
__experimentalUserCanCreatePages: userCanCreatePages,
pageOnFront,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@adamziel Would you mind confidence checking me here? A previous PR I created introduced a typing performance degradation because the useMemo was recomputed too frequently. Do you see any possibility of this change causing that?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On the first sight it seems safe – I don't suppose pageOnFront will change that often. What's your previous PR? I'd like to compare the two.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one (owch!)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I commented with my assessment there. pageOnFront seems to be be safe as it is a number, not a function (As long as it's not a NaN). Full disclosure, I didn't test it before writing this comment.

} ),
[
settings,
Expand All @@ -148,6 +156,7 @@ function useBlockEditorSettings( settings, hasTemplate ) {
undo,
hasTemplate,
userCanCreatePages,
pageOnFront,
]
);
}
Expand Down