From 2a16c85ed028c7e08f3b6612e0ffce908f96b7f2 Mon Sep 17 00:00:00 2001 From: abias Date: Thu, 15 Jun 2023 22:39:04 -0400 Subject: [PATCH] Cleanup --- src/shared/components/community/community.tsx | 49 +++++------------ src/shared/components/home/admin-settings.tsx | 13 ++--- src/shared/components/home/home.tsx | 54 +++++++++---------- src/shared/components/home/instances.tsx | 8 +-- src/shared/components/modlog.tsx | 47 +++++++--------- src/shared/components/person/inbox.tsx | 18 +++---- src/shared/components/person/reports.tsx | 27 +++++----- src/shared/components/post/create-post.tsx | 14 ++--- src/shared/components/search.tsx | 38 ++++++------- src/shared/utils.ts | 2 +- 10 files changed, 114 insertions(+), 156 deletions(-) diff --git a/src/shared/components/community/community.tsx b/src/shared/components/community/community.tsx index e8412e76..6b4eecff 100644 --- a/src/shared/components/community/community.tsx +++ b/src/shared/components/community/community.tsx @@ -102,9 +102,9 @@ import { PostListings } from "../post/post-listings"; import { CommunityLink } from "./community-link"; type CommunityData = RouteDataResponse<{ - communityResponse: GetCommunityResponse; - postsResponse?: GetPostsResponse; - commentsResponse?: GetCommentsResponse; + communityRes: GetCommunityResponse; + postsRes: GetPostsResponse; + commentsRes: GetCommentsResponse; }>; interface State { @@ -201,37 +201,15 @@ export class Community extends Component< // Only fetch the data if coming from another route if (FirstLoadService.isFirstLoad) { - const { - communityResponse: communityRes, - commentsResponse: commentsRes, - postsResponse: postsRes, - } = this.isoData.routeData; + const { communityRes, commentsRes, postsRes } = this.isoData.routeData; this.state = { ...this.state, isIsomorphic: true, + commentsRes, + communityRes, + postsRes, }; - - if (communityRes.state === "success") { - this.state = { - ...this.state, - communityRes, - }; - } - - if (postsRes?.state === "success") { - this.state = { - ...this.state, - postsRes, - }; - } - - if (commentsRes?.state === "success") { - this.state = { - ...this.state, - commentsRes, - }; - } } } @@ -279,9 +257,10 @@ export class Community extends Component< const page = getPageFromString(urlPage); - let postsResponse: RequestState | undefined = undefined; - let commentsResponse: RequestState | undefined = - undefined; + let postsResponse: RequestState = { state: "empty" }; + let commentsResponse: RequestState = { + state: "empty", + }; if (dataType === DataType.Post) { const getPostsForm: GetPosts = { @@ -310,9 +289,9 @@ export class Community extends Component< } return { - communityResponse: await client.getCommunity(communityForm), - commentsResponse, - postsResponse, + communityRes: await client.getCommunity(communityForm), + commentsRes: commentsResponse, + postsRes: postsResponse, }; } diff --git a/src/shared/components/home/admin-settings.tsx b/src/shared/components/home/admin-settings.tsx index 11be7257..91ba727f 100644 --- a/src/shared/components/home/admin-settings.tsx +++ b/src/shared/components/home/admin-settings.tsx @@ -34,8 +34,8 @@ import { SiteForm } from "./site-form"; import { TaglineForm } from "./tagline-form"; type AdminSettingsData = RouteDataResponse<{ - bannedPersonsResponse: BannedPersonsResponse; - federatedInstancesResponse: GetFederatedInstancesResponse; + bannedRes: BannedPersonsResponse; + instancesRes: GetFederatedInstancesResponse; }>; interface AdminSettingsState { @@ -72,10 +72,7 @@ export class AdminSettings extends Component { // Only fetch the data if coming from another route if (FirstLoadService.isFirstLoad) { - const { - bannedPersonsResponse: bannedRes, - federatedInstancesResponse: instancesRes, - } = this.isoData.routeData; + const { bannedRes, instancesRes } = this.isoData.routeData; this.state = { ...this.state, @@ -91,10 +88,10 @@ export class AdminSettings extends Component { client, }: InitialFetchRequest): Promise { return { - bannedPersonsResponse: await client.getBannedPersons({ + bannedRes: await client.getBannedPersons({ auth: auth as string, }), - federatedInstancesResponse: await client.getFederatedInstances({ + instancesRes: await client.getFederatedInstances({ auth: auth as string, }), }; diff --git a/src/shared/components/home/home.tsx b/src/shared/components/home/home.tsx index 3daaa4fd..4dc797ce 100644 --- a/src/shared/components/home/home.tsx +++ b/src/shared/components/home/home.tsx @@ -119,9 +119,9 @@ interface HomeProps { } type HomeData = RouteDataResponse<{ - postsResponse?: GetPostsResponse; - commentsResponse?: GetCommentsResponse; - trendingResponse: ListCommunitiesResponse; + postsRes: GetPostsResponse; + commentsRes: GetCommentsResponse; + trendingCommunitiesRes: ListCommunitiesResponse; }>; function getDataTypeFromQuery(type?: string): DataType { @@ -236,37 +236,30 @@ export class Home extends Component { // Only fetch the data if coming from another route if (FirstLoadService.isFirstLoad) { const { - trendingResponse: trendingCommunitiesRes, - commentsResponse: commentsRes, - postsResponse: postsRes, + trendingCommunitiesRes: trendingCommunitiesRes, + commentsRes: commentsRes, + postsRes: postsRes, } = this.isoData.routeData; this.state = { ...this.state, trendingCommunitiesRes, + commentsRes, + postsRes, tagline: getRandomFromList(this.state?.siteRes?.taglines ?? []) ?.content, isIsomorphic: true, }; - - if (commentsRes?.state === "success") { - this.state = { - ...this.state, - commentsRes, - }; - } - - if (postsRes?.state === "success") { - this.state = { - ...this.state, - postsRes, - }; - } } } async componentDidMount() { - if (!this.state.isIsomorphic || !this.isoData.routeData.length) { + if ( + !this.state.isIsomorphic || + !Object.values(this.isoData.routeData).some( + res => res.state === "success" || res.state === "failed" + ) + ) { await Promise.all([this.fetchTrendingCommunities(), this.fetchData()]); } @@ -290,9 +283,10 @@ export class Home extends Component { const page = urlPage ? Number(urlPage) : 1; - let postsResponse: RequestState | undefined = undefined; - let commentsResponse: RequestState | undefined = - undefined; + let postsRes: RequestState = { state: "empty" }; + let commentsRes: RequestState = { + state: "empty", + }; if (dataType === DataType.Post) { const getPostsForm: GetPosts = { @@ -304,7 +298,7 @@ export class Home extends Component { auth, }; - postsResponse = await client.getPosts(getPostsForm); + postsRes = await client.getPosts(getPostsForm); } else { const getCommentsForm: GetComments = { page, @@ -315,7 +309,7 @@ export class Home extends Component { auth, }; - commentsResponse = await client.getComments(getCommentsForm); + commentsRes = await client.getComments(getCommentsForm); } const trendingCommunitiesForm: ListCommunities = { @@ -326,9 +320,11 @@ export class Home extends Component { }; return { - trendingResponse: await client.listCommunities(trendingCommunitiesForm), - commentsResponse, - postsResponse, + trendingCommunitiesRes: await client.listCommunities( + trendingCommunitiesForm + ), + commentsRes, + postsRes, }; } diff --git a/src/shared/components/home/instances.tsx b/src/shared/components/home/instances.tsx index bec472cf..ec2ff635 100644 --- a/src/shared/components/home/instances.tsx +++ b/src/shared/components/home/instances.tsx @@ -59,11 +59,11 @@ export class Instances extends Component { }); } - static async fetchInitialData( - req: InitialFetchRequest - ): Promise { + static async fetchInitialData({ + client, + }: InitialFetchRequest): Promise { return { - federatedInstancesResponse: await req.client.getFederatedInstances({}), + federatedInstancesResponse: await client.getFederatedInstances({}), }; } diff --git a/src/shared/components/modlog.tsx b/src/shared/components/modlog.tsx index 48be10b6..b3f1fff1 100644 --- a/src/shared/components/modlog.tsx +++ b/src/shared/components/modlog.tsx @@ -77,10 +77,10 @@ type View = | AdminPurgeCommentView; type ModlogData = RouteDataResponse<{ - modlogResponse: GetModlogResponse; - communityResponse?: GetCommunityResponse; - modUserResponse?: GetPersonDetailsResponse; - userResponse?: GetPersonDetailsResponse; + res: GetModlogResponse; + communityRes: GetCommunityResponse; + modUserResponse: GetPersonDetailsResponse; + userResponse: GetPersonDetailsResponse; }>; interface ModlogType { @@ -662,33 +662,23 @@ export class Modlog extends Component< // Only fetch the data if coming from another route if (FirstLoadService.isFirstLoad) { - const { - modlogResponse: res, - communityResponse: communityRes, - modUserResponse, - userResponse, - } = this.isoData.routeData; + const { res, communityRes, modUserResponse, userResponse } = + this.isoData.routeData; this.state = { ...this.state, res, + communityRes, }; - if (communityRes?.state === "success") { - this.state = { - ...this.state, - communityRes, - }; - } - - if (modUserResponse?.state === "success") { + if (modUserResponse.state === "success") { this.state = { ...this.state, modSearchOptions: [personToChoice(modUserResponse.data.person_view)], }; } - if (userResponse?.state === "success") { + if (userResponse.state === "success") { this.state = { ...this.state, userSearchOptions: [personToChoice(userResponse.data.person_view)], @@ -1002,8 +992,9 @@ export class Modlog extends Component< auth, }; - let communityResponse: RequestState | undefined = - undefined; + let communityResponse: RequestState = { + state: "empty", + }; if (communityId) { const communityForm: GetCommunity = { @@ -1014,8 +1005,9 @@ export class Modlog extends Component< communityResponse = await client.getCommunity(communityForm); } - let modUserResponse: RequestState | undefined = - undefined; + let modUserResponse: RequestState = { + state: "empty", + }; if (modId) { const getPersonForm: GetPersonDetails = { @@ -1026,8 +1018,9 @@ export class Modlog extends Component< modUserResponse = await client.getPersonDetails(getPersonForm); } - let userResponse: RequestState | undefined = - undefined; + let userResponse: RequestState = { + state: "empty", + }; if (userId) { const getPersonForm: GetPersonDetails = { @@ -1039,8 +1032,8 @@ export class Modlog extends Component< } return { - modlogResponse: await client.getModlog(modlogForm), - communityResponse, + res: await client.getModlog(modlogForm), + communityRes: communityResponse, modUserResponse, userResponse, }; diff --git a/src/shared/components/person/inbox.tsx b/src/shared/components/person/inbox.tsx index b0550f22..c65d897b 100644 --- a/src/shared/components/person/inbox.tsx +++ b/src/shared/components/person/inbox.tsx @@ -92,9 +92,9 @@ enum ReplyEnum { } type InboxData = RouteDataResponse<{ - repliesResponse: GetRepliesResponse; - personMentionsResponse: GetPersonMentionsResponse; - privateMessagesResponse: PrivateMessagesResponse; + repliesRes: GetRepliesResponse; + mentionsRes: GetPersonMentionsResponse; + messagesRes: PrivateMessagesResponse; }>; type ReplyType = { @@ -167,11 +167,7 @@ export class Inbox extends Component { // Only fetch the data if coming from another route if (FirstLoadService.isFirstLoad) { - const { - personMentionsResponse: mentionsRes, - privateMessagesResponse: messagesRes, - repliesResponse: repliesRes, - } = this.isoData.routeData; + const { mentionsRes, messagesRes, repliesRes } = this.isoData.routeData; this.state = { ...this.state, @@ -702,7 +698,7 @@ export class Inbox extends Component { const sort: CommentSortType = "New"; return { - personMentionsResponse: auth + mentionsRes: auth ? await client.getPersonMentions({ sort, unread_only: true, @@ -711,7 +707,7 @@ export class Inbox extends Component { auth, }) : { state: "empty" }, - privateMessagesResponse: auth + messagesRes: auth ? await client.getPrivateMessages({ unread_only: true, page: 1, @@ -719,7 +715,7 @@ export class Inbox extends Component { auth, }) : { state: "empty" }, - repliesResponse: auth + repliesRes: auth ? await client.getReplies({ sort, unread_only: true, diff --git a/src/shared/components/person/reports.tsx b/src/shared/components/person/reports.tsx index fb8e8b83..99a03336 100644 --- a/src/shared/components/person/reports.tsx +++ b/src/shared/components/person/reports.tsx @@ -58,9 +58,9 @@ enum MessageEnum { } type ReportsData = RouteDataResponse<{ - commentReportsResponse: ListCommentReportsResponse; - postReportsResponse: ListPostReportsResponse; - privateMessageReportsResponse?: ListPrivateMessageReportsResponse; + commentReportsRes: ListCommentReportsResponse; + postReportsRes: ListPostReportsResponse; + messageReportsRes: ListPrivateMessageReportsResponse; }>; type ItemType = { @@ -106,11 +106,8 @@ export class Reports extends Component { // Only fetch the data if coming from another route if (FirstLoadService.isFirstLoad) { - const { - commentReportsResponse: commentReportsRes, - postReportsResponse: postReportsRes, - privateMessageReportsResponse: messageReportsRes, - } = this.isoData.routeData; + const { commentReportsRes, postReportsRes, messageReportsRes } = + this.isoData.routeData; this.state = { ...this.state, @@ -122,7 +119,7 @@ export class Reports extends Component { if (amAdmin()) { this.state = { ...this.state, - messageReportsRes: messageReportsRes ?? { state: "empty" }, + messageReportsRes: messageReportsRes, }; } } @@ -515,10 +512,9 @@ export class Reports extends Component { }; const data: ReportsData = { - commentReportsResponse: await client.listCommentReports( - commentReportsForm - ), - postReportsResponse: await client.listPostReports(postReportsForm), + commentReportsRes: await client.listCommentReports(commentReportsForm), + postReportsRes: await client.listPostReports(postReportsForm), + messageReportsRes: { state: "empty" }, }; if (amAdmin()) { @@ -529,8 +525,9 @@ export class Reports extends Component { auth: auth as string, }; - data.privateMessageReportsResponse = - await client.listPrivateMessageReports(privateMessageReportsForm); + data.messageReportsRes = await client.listPrivateMessageReports( + privateMessageReportsForm + ); } return data; diff --git a/src/shared/components/post/create-post.tsx b/src/shared/components/post/create-post.tsx index bb39cdad..a0e439bb 100644 --- a/src/shared/components/post/create-post.tsx +++ b/src/shared/components/post/create-post.tsx @@ -81,6 +81,13 @@ export class CreatePost extends Component< const { communityResponse: communityRes, initialCommunitiesRes } = this.isoData.routeData; + this.state = { + ...this.state, + loading: false, + initialCommunitiesRes, + isIsomorphic: true, + }; + if (communityRes?.state === "success") { const communityChoice: Choice = { label: communityRes.data.community_view.community.title, @@ -92,13 +99,6 @@ export class CreatePost extends Component< selectedCommunityChoice: communityChoice, }; } - - this.state = { - ...this.state, - loading: false, - initialCommunitiesRes, - isIsomorphic: true, - }; } } diff --git a/src/shared/components/search.tsx b/src/shared/components/search.tsx index 9f466730..054cab01 100644 --- a/src/shared/components/search.tsx +++ b/src/shared/components/search.tsx @@ -72,11 +72,11 @@ interface SearchProps { } type SearchData = RouteDataResponse<{ - communityResponse?: GetCommunityResponse; - listCommunitiesResponse?: ListCommunitiesResponse; - creatorDetailsResponse?: GetPersonDetailsResponse; - searchResponse?: SearchResponse; - resolveObjectResponse?: ResolveObjectResponse; + communityResponse: GetCommunityResponse; + listCommunitiesResponse: ListCommunitiesResponse; + creatorDetailsResponse: GetPersonDetailsResponse; + searchResponse: SearchResponse; + resolveObjectResponse: ResolveObjectResponse; }>; type FilterType = "creator" | "community"; @@ -365,11 +365,12 @@ export class Search extends Component { query: { communityId, creatorId, q, type, sort, listingType, page }, }: InitialFetchRequest>): Promise { const community_id = getIdFromString(communityId); - let communityResponse: RequestState | undefined = - undefined; - let listCommunitiesResponse: - | RequestState - | undefined = undefined; + let communityResponse: RequestState = { + state: "empty", + }; + let listCommunitiesResponse: RequestState = { + state: "empty", + }; if (community_id) { const getCommunityForm: GetCommunity = { id: community_id, @@ -391,9 +392,9 @@ export class Search extends Component { } const creator_id = getIdFromString(creatorId); - let creatorDetailsResponse: - | RequestState - | undefined = undefined; + let creatorDetailsResponse: RequestState = { + state: "empty", + }; if (creator_id) { const getCreatorForm: GetPersonDetails = { person_id: creator_id, @@ -405,9 +406,10 @@ export class Search extends Component { const query = getSearchQueryFromQuery(q); - let searchResponse: RequestState | undefined = undefined; - let resolveObjectResponse: RequestState | undefined = - undefined; + let searchResponse: RequestState = { state: "empty" }; + let resolveObjectResponse: RequestState = { + state: "empty", + }; if (query) { const form: SearchForm = { @@ -429,9 +431,7 @@ export class Search extends Component { q: query, auth, }; - resolveObjectResponse = await client - .resolveObject(resolveObjectForm) - .catch(() => undefined); + resolveObjectResponse = await client.resolveObject(resolveObjectForm); } } } diff --git a/src/shared/utils.ts b/src/shared/utils.ts index cf59086b..e1a0c14e 100644 --- a/src/shared/utils.ts +++ b/src/shared/utils.ts @@ -1499,7 +1499,7 @@ export function newVote(voteType: VoteType, myVote?: number): number { } export type RouteDataResponse> = { - [K in keyof T]: RequestState>; + [K in keyof T]: RequestState; }; function sleep(millis: number): Promise {