From 90f76983f59e5b6c0e263781fc362d4a017a8747 Mon Sep 17 00:00:00 2001 From: Kosta Date: Mon, 2 Sep 2024 14:28:31 +0200 Subject: [PATCH] Help Center: localize queries (#93791) Co-authored-by: escapemanuele --- .../use-context-based-search-mapping.tsx | 12 +--- .../src/route-to-query-mapping.json | 44 -------------- .../help-center/src/route-to-query-mapping.ts | 58 +++++++++++++++++++ 3 files changed, 60 insertions(+), 54 deletions(-) delete mode 100644 packages/help-center/src/route-to-query-mapping.json create mode 100644 packages/help-center/src/route-to-query-mapping.ts diff --git a/packages/help-center/src/hooks/use-context-based-search-mapping.tsx b/packages/help-center/src/hooks/use-context-based-search-mapping.tsx index bcc1fc3bc1bea1..e317e5c5dfc8b1 100644 --- a/packages/help-center/src/hooks/use-context-based-search-mapping.tsx +++ b/packages/help-center/src/hooks/use-context-based-search-mapping.tsx @@ -1,5 +1,5 @@ import { useSelect } from '@wordpress/data'; -import urlMapping from '../route-to-query-mapping.json'; +import { useQueryForRoute } from '../route-to-query-mapping'; interface CoreBlockEditor { getSelectedBlock: () => object; @@ -25,15 +25,7 @@ export function useContextBasedSearchMapping( currentRoute: string | undefined ) return ''; }, [] ); - // Fuzzier matches - const urlMatchKey = Object.keys( urlMapping ).find( ( key ) => currentRoute?.startsWith( key ) ); - const urlSearchQuery = urlMatchKey ? urlMapping[ urlMatchKey as keyof typeof urlMapping ] : ''; - - // Find exact URL matches - const exactMatch = urlMapping[ currentRoute as keyof typeof urlMapping ]; - if ( exactMatch ) { - return { contextSearch: exactMatch }; - } + const urlSearchQuery = useQueryForRoute( currentRoute ?? '' ); return { contextSearch: blockSearchQuery || urlSearchQuery || '', diff --git a/packages/help-center/src/route-to-query-mapping.json b/packages/help-center/src/route-to-query-mapping.json deleted file mode 100644 index e564837045be8e..00000000000000 --- a/packages/help-center/src/route-to-query-mapping.json +++ /dev/null @@ -1,44 +0,0 @@ -{ - "/add-ons/": "add-ons", - "/comments/": "comments", - "/plugins/manage": "manage plugins", - "/plugins": "plugins", - "/plans/": "upgrade plan", - "/email/": "manage emails", - "/woocommerce": "woocommerce", - "/wp-admin/admin.php?page=wc": "woocommerce", - "/subscribers": "subscribers", - "/me/privacy": "privacy", - "/me/notifications": "notification settings", - "/me/site-blocks": "blocked sites", - "/me/get-apps": "wordpress apps", - "/settings/writing/": "writing settings", - "/settings/reading/": "reading settings", - "/settings/performance/": "performance settings", - "/settings/taxonomies/category/": "site categories", - "/settings/taxonomies/post_tag/": "post tag", - "/settings/podcasting/": "podcasting", - "/hosting-config/": "hosting configuration", - "/wp-admin/options-media.php": "media settings", - "/wp-admin/edit.php?post_type=jetpack-testimonial": "testimonials", - "/wp-admin/edit.php?post_type=feedback": "feedback form", - "/wp-admin/post-new.php?post_type=jetpack-testimonial": "new testimonial", - "/wp-admin/admin.php?page=akismet-key-config": "site spam", - "/wp-admin/admin.php?page=jetpack-search": "jetpack search", - "/wp-admin/admin.php?page=polls": "crowdsignal", - "/wp-admin/admin.php?page=ratings": "ratings", - "/wp-admin/options-general.php?page=debug-bar-extender": "debug bar extender", - "/wp-admin/index.php?page=my-blogs": "my sites", - "/read/conversations": "conversations", - "/read/notifications": "notifications", - "/read/subscriptions": "manage subscriptions", - "/read/list": "reader list", - "/read/search": "search", - "/read": "reader", - "/discover": "discover blogs", - "/tags": "tags", - "/sites": "manage sites", - "/marketing/sharing-buttons/": "social share", - "/marketing/business-tools/": "business tools", - "/advertising/": "advertising" -} diff --git a/packages/help-center/src/route-to-query-mapping.ts b/packages/help-center/src/route-to-query-mapping.ts new file mode 100644 index 00000000000000..1bc33893436e6b --- /dev/null +++ b/packages/help-center/src/route-to-query-mapping.ts @@ -0,0 +1,58 @@ +import { __ } from '@wordpress/i18n'; + +export const useQueryForRoute = ( currentRoute: string ) => { + const urlMapping = { + '/add-ons/': __( 'add-ons' ), + '/advertising/': __( 'advertising' ), + '/comments/': __( 'comments' ), + '/discover': __( 'discover blogs' ), + '/email/': __( 'manage emails' ), + '/hosting-config/': __( 'hosting configuration' ), + '/marketing/business-tools/': __( 'business tools' ), + '/marketing/sharing-buttons/': __( 'social share' ), + '/me/get-apps': __( 'wordpress apps' ), + '/me/notifications': __( 'notification settings' ), + '/me/privacy': __( 'privacy' ), + '/me/site-blocks': __( 'blocked sites' ), + '/plans/': __( 'upgrade plan' ), + '/plugins': __( 'plugins' ), + '/plugins/manage': __( 'manage plugins' ), + '/read': __( 'reader' ), + '/read/conversations': __( 'conversations' ), + '/read/list': __( 'reader list' ), + '/read/notifications': __( 'notifications' ), + '/read/search': __( 'search' ), + '/read/subscriptions': __( 'manage subscriptions' ), + '/settings/performance/': __( 'performance settings' ), + '/settings/podcasting/': __( 'podcasting' ), + '/settings/reading/': __( 'reading settings' ), + '/settings/taxonomies/category/': __( 'site categories' ), + '/settings/taxonomies/post_tag/': __( 'post tag' ), + '/settings/writing/': __( 'writing settings' ), + '/sites': __( 'manage sites' ), + '/subscribers': __( 'subscribers' ), + '/tags': __( 'tags' ), + '/woocommerce': __( 'woocommerce' ), + '/wp-admin/admin.php?page=akismet-key-config': __( 'site spam' ), + '/wp-admin/admin.php?page=jetpack-search': __( 'jetpack search' ), + '/wp-admin/admin.php?page=polls': __( 'crowdsignal' ), + '/wp-admin/admin.php?page=ratings': __( 'ratings' ), + '/wp-admin/admin.php?page=wc': __( 'woocommerce' ), + '/wp-admin/edit.php?post_type=feedback': __( 'feedback form' ), + '/wp-admin/edit.php?post_type=jetpack-testimonial': __( 'testimonials' ), + '/wp-admin/index.php?page=my-blogs': __( 'my sites' ), + '/wp-admin/options-general.php?page=debug-bar-extender': __( 'debug bar extender' ), + '/wp-admin/options-media.php': __( 'media settings' ), + '/wp-admin/post-new.php?post_type=jetpack-testimonial': __( 'new testimonial' ), + }; + + // Find exact URL matches + const exactMatch = urlMapping[ currentRoute as keyof typeof urlMapping ]; + if ( exactMatch ) { + return exactMatch; + } + + // Fuzzier matches + const urlMatchKey = Object.keys( urlMapping ).find( ( key ) => currentRoute?.startsWith( key ) ); + return urlMatchKey ? urlMapping[ urlMatchKey as keyof typeof urlMapping ] : ''; +};