Skip to content

Commit

Permalink
Revert "Lookup user id if username is in route"
Browse files Browse the repository at this point in the history
This reverts commit ac9f207.
  • Loading branch information
mehmoodak committed Jan 14, 2025
1 parent 65a6645 commit 2f46423
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 71 deletions.
103 changes: 41 additions & 62 deletions client/reader/user-stream/controller.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { ReactElement } from 'react';
import AsyncLoad from 'calypso/components/async-load';
import wpcom from 'calypso/lib/wp';
import { trackPageLoad, trackScrollPage } from 'calypso/reader/controller-helper';
import type { AppState } from 'calypso/types';

Expand All @@ -16,69 +15,49 @@ interface Context {

const analyticsPageTitle = 'Reader';

const isNumeric = ( value: string ): boolean => /^\d+$/.test( value );

async function resolveUserId( userIdentifier: string ): Promise< string > {
if ( isNumeric( userIdentifier ) ) {
return userIdentifier;
}

try {
const userData = await wpcom.req.get( `/users/${ encodeURIComponent( userIdentifier ) }/` );
return userData.ID.toString();
} catch ( error ) {
return userIdentifier; // Fallback to original identifier if lookup fails
}
}

export function userPosts( context: Context, next: () => void ): void {
const userIdentifier = context.params.user_id;

resolveUserId( userIdentifier ).then( ( userId ) => {
const basePath = '/read/users/:user_id';
const fullAnalyticsPageTitle = analyticsPageTitle + ' > User > ' + userId + ' > Posts';
const mcKey = 'user_posts';
const streamKey = 'user:' + userId;

trackPageLoad( basePath, fullAnalyticsPageTitle, mcKey );

context.primary = (
<AsyncLoad
require="calypso/reader/user-stream"
key={ 'user-posts-' + userId }
streamKey={ streamKey }
userId={ userId }
trackScrollPage={ trackScrollPage.bind(
null,
basePath,
fullAnalyticsPageTitle,
analyticsPageTitle,
mcKey
) }
placeholder={ null }
/>
);
next();
} );
const userId = context.params.user_id;
const basePath = '/read/users/:user_id';
const fullAnalyticsPageTitle = analyticsPageTitle + ' > User > ' + userId + ' > Posts';
const mcKey = 'user_posts';
const streamKey = 'user:' + userId;

trackPageLoad( basePath, fullAnalyticsPageTitle, mcKey );

context.primary = (
<AsyncLoad
require="calypso/reader/user-stream"
key={ 'user-posts-' + userId }
streamKey={ streamKey }
userId={ userId }
trackScrollPage={ trackScrollPage.bind(
null,
basePath,
fullAnalyticsPageTitle,
analyticsPageTitle,
mcKey
) }
placeholder={ null }
/>
);
next();
}

export function userLists( context: Context, next: () => void ): void {
const userIdentifier = context.params.user_id;

resolveUserId( userIdentifier ).then( ( userId ) => {
const basePath = '/read/users/:user_id/lists';
const fullAnalyticsPageTitle = analyticsPageTitle + ' > User > ' + userId + ' > Lists';
const mcKey = 'user_lists';

trackPageLoad( basePath, fullAnalyticsPageTitle, mcKey );

context.primary = (
<AsyncLoad
require="calypso/reader/user-stream"
key={ 'user-lists-' + userId }
userId={ userId }
/>
);
next();
} );
const userId = context.params.user_id;
const basePath = '/read/users/:user_id/lists';
const fullAnalyticsPageTitle = analyticsPageTitle + ' > User > ' + userId + ' > Lists';
const mcKey = 'user_lists';

trackPageLoad( basePath, fullAnalyticsPageTitle, mcKey );

context.primary = (
<AsyncLoad
require="calypso/reader/user-stream"
key={ 'user-lists-' + userId }
userId={ userId }
/>
);

next();
}
14 changes: 5 additions & 9 deletions client/reader/user-stream/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -66,17 +66,13 @@ export function UserStream( { userId, requestUser, user, streamKey, isLoading }:

const renderContent = (): React.ReactNode => {
const basePath = currentPath.split( '?' )[ 0 ];
const basePathParts = basePath.split( '/' );
const isListsPath = basePathParts[ basePathParts.length - 1 ] === 'lists';

if ( isListsPath ) {
return <UserLists />;
switch ( basePath ) {
case `/read/users/${ userId }`:
return <UserPosts streamKey={ streamKey as string } />;
case `/read/users/${ userId }/lists`:
return <UserLists />;
}

if ( ! streamKey ) {
return null;
}
return <UserPosts streamKey={ streamKey } />;
};

return (
Expand Down

0 comments on commit 2f46423

Please sign in to comment.