From 2a16c85ed028c7e08f3b6612e0ffce908f96b7f2 Mon Sep 17 00:00:00 2001
From: abias <abias1122@gmail.com>
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 e8412e7..6b4eecf 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<GetPostsResponse> | undefined = undefined;
-    let commentsResponse: RequestState<GetCommentsResponse> | undefined =
-      undefined;
+    let postsResponse: RequestState<GetPostsResponse> = { state: "empty" };
+    let commentsResponse: RequestState<GetCommentsResponse> = {
+      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 11be725..91ba727 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<any, AdminSettingsState> {
 
     // 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<any, AdminSettingsState> {
     client,
   }: InitialFetchRequest): Promise<AdminSettingsData> {
     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 3daaa4f..4dc797c 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<any, HomeState> {
     // 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<any, HomeState> {
 
     const page = urlPage ? Number(urlPage) : 1;
 
-    let postsResponse: RequestState<GetPostsResponse> | undefined = undefined;
-    let commentsResponse: RequestState<GetCommentsResponse> | undefined =
-      undefined;
+    let postsRes: RequestState<GetPostsResponse> = { state: "empty" };
+    let commentsRes: RequestState<GetCommentsResponse> = {
+      state: "empty",
+    };
 
     if (dataType === DataType.Post) {
       const getPostsForm: GetPosts = {
@@ -304,7 +298,7 @@ export class Home extends Component<any, HomeState> {
         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<any, HomeState> {
         auth,
       };
 
-      commentsResponse = await client.getComments(getCommentsForm);
+      commentsRes = await client.getComments(getCommentsForm);
     }
 
     const trendingCommunitiesForm: ListCommunities = {
@@ -326,9 +320,11 @@ export class Home extends Component<any, HomeState> {
     };
 
     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 bec472c..ec2ff63 100644
--- a/src/shared/components/home/instances.tsx
+++ b/src/shared/components/home/instances.tsx
@@ -59,11 +59,11 @@ export class Instances extends Component<any, InstancesState> {
     });
   }
 
-  static async fetchInitialData(
-    req: InitialFetchRequest
-  ): Promise<InstancesData> {
+  static async fetchInitialData({
+    client,
+  }: InitialFetchRequest): Promise<InstancesData> {
     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 48be10b..b3f1fff 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<GetCommunityResponse> | undefined =
-      undefined;
+    let communityResponse: RequestState<GetCommunityResponse> = {
+      state: "empty",
+    };
 
     if (communityId) {
       const communityForm: GetCommunity = {
@@ -1014,8 +1005,9 @@ export class Modlog extends Component<
       communityResponse = await client.getCommunity(communityForm);
     }
 
-    let modUserResponse: RequestState<GetPersonDetailsResponse> | undefined =
-      undefined;
+    let modUserResponse: RequestState<GetPersonDetailsResponse> = {
+      state: "empty",
+    };
 
     if (modId) {
       const getPersonForm: GetPersonDetails = {
@@ -1026,8 +1018,9 @@ export class Modlog extends Component<
       modUserResponse = await client.getPersonDetails(getPersonForm);
     }
 
-    let userResponse: RequestState<GetPersonDetailsResponse> | undefined =
-      undefined;
+    let userResponse: RequestState<GetPersonDetailsResponse> = {
+      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 b0550f2..c65d897 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<any, InboxState> {
 
     // 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<any, InboxState> {
     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<any, InboxState> {
             auth,
           })
         : { state: "empty" },
-      privateMessagesResponse: auth
+      messagesRes: auth
         ? await client.getPrivateMessages({
             unread_only: true,
             page: 1,
@@ -719,7 +715,7 @@ export class Inbox extends Component<any, InboxState> {
             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 fb8e8b8..99a0333 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<any, ReportsState> {
 
     // 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<any, ReportsState> {
       if (amAdmin()) {
         this.state = {
           ...this.state,
-          messageReportsRes: messageReportsRes ?? { state: "empty" },
+          messageReportsRes: messageReportsRes,
         };
       }
     }
@@ -515,10 +512,9 @@ export class Reports extends Component<any, ReportsState> {
     };
 
     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<any, ReportsState> {
         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 bb39cda..a0e439b 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 9f46673..054cab0 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<any, SearchState> {
     query: { communityId, creatorId, q, type, sort, listingType, page },
   }: InitialFetchRequest<QueryParams<SearchProps>>): Promise<SearchData> {
     const community_id = getIdFromString(communityId);
-    let communityResponse: RequestState<GetCommunityResponse> | undefined =
-      undefined;
-    let listCommunitiesResponse:
-      | RequestState<ListCommunitiesResponse>
-      | undefined = undefined;
+    let communityResponse: RequestState<GetCommunityResponse> = {
+      state: "empty",
+    };
+    let listCommunitiesResponse: RequestState<ListCommunitiesResponse> = {
+      state: "empty",
+    };
     if (community_id) {
       const getCommunityForm: GetCommunity = {
         id: community_id,
@@ -391,9 +392,9 @@ export class Search extends Component<any, SearchState> {
     }
 
     const creator_id = getIdFromString(creatorId);
-    let creatorDetailsResponse:
-      | RequestState<GetPersonDetailsResponse>
-      | undefined = undefined;
+    let creatorDetailsResponse: RequestState<GetPersonDetailsResponse> = {
+      state: "empty",
+    };
     if (creator_id) {
       const getCreatorForm: GetPersonDetails = {
         person_id: creator_id,
@@ -405,9 +406,10 @@ export class Search extends Component<any, SearchState> {
 
     const query = getSearchQueryFromQuery(q);
 
-    let searchResponse: RequestState<SearchResponse> | undefined = undefined;
-    let resolveObjectResponse: RequestState<ResolveObjectResponse> | undefined =
-      undefined;
+    let searchResponse: RequestState<SearchResponse> = { state: "empty" };
+    let resolveObjectResponse: RequestState<ResolveObjectResponse> = {
+      state: "empty",
+    };
 
     if (query) {
       const form: SearchForm = {
@@ -429,9 +431,7 @@ export class Search extends Component<any, SearchState> {
             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 cf59086..e1a0c14 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<T extends Record<string, any>> = {
-  [K in keyof T]: RequestState<Exclude<T[K], undefined>>;
+  [K in keyof T]: RequestState<T[K]>;
 };
 
 function sleep(millis: number): Promise<void> {
-- 
2.44.1