Skip to content

Commit

Permalink
Link UI: build static page check into suggestions API
Browse files Browse the repository at this point in the history
  • Loading branch information
ellatrix committed Oct 25, 2024
1 parent 22a3bf5 commit 7d1469b
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,27 +51,12 @@ const handleEntitySearch = async (
val,
suggestionsQuery,
fetchSearchSuggestions,
withCreateSuggestion,
pageOnFront,
pageForPosts
withCreateSuggestion
) => {
const { isInitialSuggestions } = suggestionsQuery;

const results = await fetchSearchSuggestions( val, suggestionsQuery );

// Identify front page and update type to match.
results.map( ( result ) => {
if ( Number( result.id ) === pageOnFront ) {
result.isFrontPage = true;
return result;
} else if ( Number( result.id ) === pageForPosts ) {
result.isBlogHome = true;
return result;
}

return result;
} );

// If displaying initial suggestions just return plain results.
if ( isInitialSuggestions ) {
return results;
Expand Down Expand Up @@ -108,19 +93,10 @@ export default function useSearchHandler(
allowDirectEntry,
withCreateSuggestion
) {
const { fetchSearchSuggestions, pageOnFront, pageForPosts } = useSelect(
( select ) => {
const { getSettings } = select( blockEditorStore );

return {
pageOnFront: getSettings().pageOnFront,
pageForPosts: getSettings().pageForPosts,
fetchSearchSuggestions:
getSettings().__experimentalFetchLinkSuggestions,
};
},
[]
);
const fetchSearchSuggestions = useSelect( ( select ) => {
const { getSettings } = select( blockEditorStore );
return getSettings().__experimentalFetchLinkSuggestions;
}, [] );

const directEntryHandler = allowDirectEntry
? handleDirectEntry
Expand All @@ -134,16 +110,12 @@ export default function useSearchHandler(
val,
{ ...suggestionsQuery, isInitialSuggestions },
fetchSearchSuggestions,
withCreateSuggestion,
pageOnFront,
pageForPosts
withCreateSuggestion
);
},
[
directEntryHandler,
fetchSearchSuggestions,
pageOnFront,
pageForPosts,
suggestionsQuery,
withCreateSuggestion,
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ type MediaAPIResult = {
type: string;
};

type SettingsAPIResult = {
page_on_front: number;
posts_per_page: number;
};

export type SearchResult = {
/**
* Post or term id.
Expand All @@ -79,6 +84,8 @@ export type SearchResult = {
* Link kind of post-type or taxonomy
*/
kind?: string;
isFrontPage?: boolean;
isBlogHome?: boolean;
};

/**
Expand Down Expand Up @@ -240,13 +247,26 @@ export default async function fetchLinkSuggestions(
);
}

const responses = await Promise.all( queries );
const [ settings, ...responses ] = await Promise.all( [
apiFetch< SettingsAPIResult >( { path: '/wp/v2/settings' } ),
...queries,
] );

let results = responses.flat();
results = results.filter( ( result ) => !! result.id );
results = sortResults( results, search );
results = results.slice( 0, perPage );
return results;
return results.map( ( result ) => {
if ( Number( result.id ) === settings.page_on_front ) {
result.isFrontPage = true;
return result;
} else if ( Number( result.id ) === settings.posts_per_page ) {
result.isBlogHome = true;
return result;
}

return result;
} );
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,6 @@ function useBlockEditorSettings( settings, postType, postId, renderingMode ) {
hiddenBlockTypes,
canUseUnfilteredHTML,
userCanCreatePages,
pageOnFront,
pageForPosts,
userPatternCategories,
restBlockPatternCategories,
sectionRootClientId,
Expand All @@ -131,20 +129,13 @@ function useBlockEditorSettings( settings, postType, postId, renderingMode ) {
const {
canUser,
getRawEntityRecord,
getEntityRecord,
getUserPatternCategories,
getBlockPatternCategories,
} = select( coreStore );
const { get } = select( preferencesStore );
const { getBlockTypes } = select( blocksStore );
const { getBlocksByName, getBlockAttributes } =
select( blockEditorStore );
const siteSettings = canUser( 'read', {
kind: 'root',
name: 'site',
} )
? getEntityRecord( 'root', 'site' )
: undefined;

function getSectionRootBlock() {
if ( renderingMode === 'template-locked' ) {
Expand Down Expand Up @@ -185,8 +176,6 @@ function useBlockEditorSettings( settings, postType, postId, renderingMode ) {
kind: 'postType',
name: 'page',
} ),
pageOnFront: siteSettings?.page_on_front,
pageForPosts: siteSettings?.page_for_posts,
userPatternCategories: getUserPatternCategories(),
restBlockPatternCategories: getBlockPatternCategories(),
sectionRootClientId: getSectionRootBlock(),
Expand Down Expand Up @@ -321,8 +310,6 @@ function useBlockEditorSettings( settings, postType, postId, renderingMode ) {
// Check these two properties: they were not present in the site editor.
__experimentalCreatePageEntity: createPageEntity,
__experimentalUserCanCreatePages: userCanCreatePages,
pageOnFront,
pageForPosts,
__experimentalPreferPatternsOnRoot: postType === 'wp_template',
templateLock:
postType === 'wp_navigation' ? 'insert' : settings.templateLock,
Expand Down Expand Up @@ -352,8 +339,6 @@ function useBlockEditorSettings( settings, postType, postId, renderingMode ) {
undo,
createPageEntity,
userCanCreatePages,
pageOnFront,
pageForPosts,
postType,
setIsInserterOpened,
sectionRootClientId,
Expand Down

0 comments on commit 7d1469b

Please sign in to comment.