X-Git-Url: http://these/git/?a=blobdiff_plain;f=src%2Fshared%2Fcomponents%2Fcommunity%2Fcommunity.tsx;h=b0787ecd9aa40278ba695a0b9c1ba12a129a5fd7;hb=afafb777b4be09d70cd8f56f8d236d98ac63ba6e;hp=6f3c9112f782231e9af853b199414b2174541a10;hpb=4ec7c26707742a3431a38a25a611e3387c4cbd50;p=lemmy-ui.git diff --git a/src/shared/components/community/community.tsx b/src/shared/components/community/community.tsx index 6f3c911..b0787ec 100644 --- a/src/shared/components/community/community.tsx +++ b/src/shared/components/community/community.tsx @@ -1,4 +1,28 @@ -import { Component, linkEvent } from "inferno"; +import { + commentsToFlatNodes, + communityRSSUrl, + editComment, + editPost, + editWith, + enableDownvotes, + enableNsfw, + getCommentParentId, + getDataTypeString, + myAuth, + postToCommentSortType, + setIsoData, + showLocal, + updateCommunityBlock, + updatePersonBlock, +} from "@utils/app"; +import { + getPageFromString, + getQueryParams, + getQueryString, +} from "@utils/helpers"; +import type { QueryParams } from "@utils/types"; +import { RouteDataResponse } from "@utils/types"; +import { Component, RefObject, createRef, linkEvent } from "inferno"; import { RouteComponentProps } from "inferno-router/dist/Route"; import { AddAdmin, @@ -38,6 +62,7 @@ import { LockPost, MarkCommentReplyAsRead, MarkPersonMentionAsRead, + MarkPostAsRead, PostResponse, PurgeComment, PurgeCommunity, @@ -52,43 +77,16 @@ import { SortType, TransferCommunity, } from "lemmy-js-client"; -import { i18n } from "../../i18next"; +import { fetchLimit, relTags } from "../../config"; import { CommentViewType, DataType, InitialFetchRequest, } from "../../interfaces"; -import { UserService } from "../../services"; -import { FirstLoadService } from "../../services/FirstLoadService"; +import { FirstLoadService, I18NextService, UserService } from "../../services"; import { HttpService, RequestState } from "../../services/HttpService"; -import { - QueryParams, - RouteDataResponse, - commentsToFlatNodes, - communityRSSUrl, - editComment, - editPost, - editWith, - enableDownvotes, - enableNsfw, - fetchLimit, - getCommentParentId, - getDataTypeString, - getPageFromString, - getQueryParams, - getQueryString, - myAuth, - postToCommentSortType, - relTags, - restoreScrollPosition, - saveScrollPosition, - setIsoData, - setupTippy, - showLocal, - toast, - updateCommunityBlock, - updatePersonBlock, -} from "../../utils"; +import { setupTippy } from "../../tippy"; +import { toast } from "../../toast"; import { CommentNodes } from "../comment/comment-nodes"; import { BannerIconHeader } from "../common/banner-icon-header"; import { DataTypeSelect } from "../common/data-type-select"; @@ -157,7 +155,7 @@ export class Community extends Component< finished: new Map(), isIsomorphic: false, }; - + private readonly mainContentRef: RefObject; constructor(props: RouteComponentProps<{ name: string }>, context: any) { super(props, context); @@ -198,7 +196,8 @@ export class Community extends Component< this.handleSavePost = this.handleSavePost.bind(this); this.handlePurgePost = this.handlePurgePost.bind(this); this.handleFeaturePost = this.handleFeaturePost.bind(this); - + this.handleMarkPostAsRead = this.handleMarkPostAsRead.bind(this); + this.mainContentRef = createRef(); // Only fetch the data if coming from another route if (FirstLoadService.isFirstLoad) { const { communityRes, commentsRes, postsRes } = this.isoData.routeData; @@ -231,10 +230,6 @@ export class Community extends Component< setupTippy(); } - componentWillUnmount() { - saveScrollPosition(this.context); - } - static async fetchInitialData({ client, path, @@ -297,7 +292,7 @@ export class Community extends Component< get documentTitle(): string { const cRes = this.state.communityRes; - return cRes.state == "success" + return cRes.state === "success" ? `${cRes.data.community_view.community.title} - ${this.isoData.site_res.site_view.site.name}` : ""; } @@ -319,19 +314,23 @@ export class Community extends Component<
-
+
{this.communityInfo(res)}
-
+
+
+
); @@ -358,7 +357,9 @@ export class Community extends Component< } render() { - return
{this.renderCommunity()}
; + return ( +
{this.renderCommunity()}
+ ); } sidebar(res: GetCommunityResponse) { @@ -432,6 +433,7 @@ export class Community extends Component< onAddAdmin={this.handleAddAdmin} onTransferCommunity={this.handleTransferCommunity} onFeaturePost={this.handleFeaturePost} + onMarkPostAsRead={this.handleMarkPostAsRead} /> ); } @@ -449,7 +451,7 @@ export class Community extends Component< nodes={commentsToFlatNodes(this.state.commentsRes.data.comments)} viewType={CommentViewType.Flat} finished={this.state.finished} - noIndent + isTopLevel showContext enableDownvotes={enableDownvotes(site_res)} moderators={communityRes.moderators} @@ -487,7 +489,7 @@ export class Community extends Component< community && (
-
{community.title}
+

{community.title}

- + - + {communityRss && ( @@ -571,7 +573,7 @@ export class Community extends Component< }; this.props.history.push( - `/c/${this.props.match.params.name}${getQueryString(queryParams)}` + `/c/${this.props.match.params.name}${getQueryString(queryParams)}`, ); await this.fetchData(); @@ -609,7 +611,6 @@ export class Community extends Component< }); } - restoreScrollPosition(this.context); setupTippy(); } @@ -628,11 +629,11 @@ export class Community extends Component< this.updateCommunity(followCommunityRes); // Update myUserInfo - if (followCommunityRes.state == "success") { + if (followCommunityRes.state === "success") { const communityId = followCommunityRes.data.community_view.community.id; const mui = UserService.Instance.myUserInfo; if (mui) { - mui.follows = mui.follows.filter(i => i.community.id != communityId); + mui.follows = mui.follows.filter(i => i.community.id !== communityId); } } } @@ -659,10 +660,10 @@ export class Community extends Component< async handleBlockCommunity(form: BlockCommunity) { const blockCommunityRes = await HttpService.client.blockCommunity(form); - if (blockCommunityRes.state == "success") { + if (blockCommunityRes.state === "success") { updateCommunityBlock(blockCommunityRes.data); this.setState(s => { - if (s.communityRes.state == "success") { + if (s.communityRes.state === "success") { s.communityRes.data.community_view.blocked = blockCommunityRes.data.blocked; } @@ -672,7 +673,7 @@ export class Community extends Component< async handleBlockPerson(form: BlockPerson) { const blockPersonRes = await HttpService.client.blockPerson(form); - if (blockPersonRes.state == "success") { + if (blockPersonRes.state === "success") { updatePersonBlock(blockPersonRes.data); } } @@ -755,15 +756,15 @@ export class Community extends Component< async handleCommentReport(form: CreateCommentReport) { const reportRes = await HttpService.client.createCommentReport(form); - if (reportRes.state == "success") { - toast(i18n.t("report_created")); + if (reportRes.state === "success") { + toast(I18NextService.i18n.t("report_created")); } } async handlePostReport(form: CreatePostReport) { const reportRes = await HttpService.client.createPostReport(form); - if (reportRes.state == "success") { - toast(i18n.t("report_created")); + if (reportRes.state === "success") { + toast(I18NextService.i18n.t("report_created")); } } @@ -780,16 +781,16 @@ export class Community extends Component< async handleAddAdmin(form: AddAdmin) { const addAdminRes = await HttpService.client.addAdmin(form); - if (addAdminRes.state == "success") { + if (addAdminRes.state === "success") { this.setState(s => ((s.siteRes.admins = addAdminRes.data.admins), s)); } } async handleTransferCommunity(form: TransferCommunity) { const transferCommunityRes = await HttpService.client.transferCommunity( - form + form, ); - toast(i18n.t("transfer_community")); + toast(I18NextService.i18n.t("transfer_community")); this.updateCommunityFull(transferCommunityRes); } @@ -803,6 +804,11 @@ export class Community extends Component< await HttpService.client.markPersonMentionAsRead(form); } + async handleMarkPostAsRead(form: MarkPostAsRead) { + const res = await HttpService.client.markPostAsRead(form); + this.findAndUpdatePost(res); + } + async handleBanFromCommunity(form: BanFromCommunity) { const banRes = await HttpService.client.banFromCommunity(form); this.updateBanFromCommunity(banRes); @@ -815,20 +821,20 @@ export class Community extends Component< updateBanFromCommunity(banRes: RequestState) { // Maybe not necessary - if (banRes.state == "success") { + if (banRes.state === "success") { this.setState(s => { - if (s.postsRes.state == "success") { + if (s.postsRes.state === "success") { s.postsRes.data.posts - .filter(c => c.creator.id == banRes.data.person_view.person.id) + .filter(c => c.creator.id === banRes.data.person_view.person.id) .forEach( - c => (c.creator_banned_from_community = banRes.data.banned) + c => (c.creator_banned_from_community = banRes.data.banned), ); } - if (s.commentsRes.state == "success") { + if (s.commentsRes.state === "success") { s.commentsRes.data.comments - .filter(c => c.creator.id == banRes.data.person_view.person.id) + .filter(c => c.creator.id === banRes.data.person_view.person.id) .forEach( - c => (c.creator_banned_from_community = banRes.data.banned) + c => (c.creator_banned_from_community = banRes.data.banned), ); } return s; @@ -838,16 +844,16 @@ export class Community extends Component< updateBan(banRes: RequestState) { // Maybe not necessary - if (banRes.state == "success") { + if (banRes.state === "success") { this.setState(s => { - if (s.postsRes.state == "success") { + if (s.postsRes.state === "success") { s.postsRes.data.posts - .filter(c => c.creator.id == banRes.data.person_view.person.id) + .filter(c => c.creator.id === banRes.data.person_view.person.id) .forEach(c => (c.creator.banned = banRes.data.banned)); } - if (s.commentsRes.state == "success") { + if (s.commentsRes.state === "success") { s.commentsRes.data.comments - .filter(c => c.creator.id == banRes.data.person_view.person.id) + .filter(c => c.creator.id === banRes.data.person_view.person.id) .forEach(c => (c.creator.banned = banRes.data.banned)); } return s; @@ -857,7 +863,7 @@ export class Community extends Component< updateCommunity(res: RequestState) { this.setState(s => { - if (s.communityRes.state == "success" && res.state == "success") { + if (s.communityRes.state === "success" && res.state === "success") { s.communityRes.data.community_view = res.data.community_view; s.communityRes.data.discussion_languages = res.data.discussion_languages; @@ -868,7 +874,7 @@ export class Community extends Component< updateCommunityFull(res: RequestState) { this.setState(s => { - if (s.communityRes.state == "success" && res.state == "success") { + if (s.communityRes.state === "success" && res.state === "success") { s.communityRes.data.community_view = res.data.community_view; s.communityRes.data.moderators = res.data.moderators; } @@ -877,18 +883,18 @@ export class Community extends Component< } purgeItem(purgeRes: RequestState) { - if (purgeRes.state == "success") { - toast(i18n.t("purge_success")); + if (purgeRes.state === "success") { + toast(I18NextService.i18n.t("purge_success")); this.context.router.history.push(`/`); } } findAndUpdateComment(res: RequestState) { this.setState(s => { - if (s.commentsRes.state == "success" && res.state == "success") { + if (s.commentsRes.state === "success" && res.state === "success") { s.commentsRes.data.comments = editComment( res.data.comment_view, - s.commentsRes.data.comments + s.commentsRes.data.comments, ); s.finished.set(res.data.comment_view.comment.id, true); } @@ -898,13 +904,13 @@ export class Community extends Component< createAndUpdateComments(res: RequestState) { this.setState(s => { - if (s.commentsRes.state == "success" && res.state == "success") { + if (s.commentsRes.state === "success" && res.state === "success") { s.commentsRes.data.comments.unshift(res.data.comment_view); // Set finished for the parent s.finished.set( getCommentParentId(res.data.comment_view.comment) ?? 0, - true + true, ); } return s; @@ -913,10 +919,10 @@ export class Community extends Component< findAndUpdateCommentReply(res: RequestState) { this.setState(s => { - if (s.commentsRes.state == "success" && res.state == "success") { + if (s.commentsRes.state === "success" && res.state === "success") { s.commentsRes.data.comments = editWith( res.data.comment_reply_view, - s.commentsRes.data.comments + s.commentsRes.data.comments, ); } return s; @@ -925,10 +931,10 @@ export class Community extends Component< findAndUpdatePost(res: RequestState) { this.setState(s => { - if (s.postsRes.state == "success" && res.state == "success") { + if (s.postsRes.state === "success" && res.state === "success") { s.postsRes.data.posts = editPost( res.data.post_view, - s.postsRes.data.posts + s.postsRes.data.posts, ); } return s; @@ -938,7 +944,7 @@ export class Community extends Component< updateModerators(res: RequestState) { // Update the moderators this.setState(s => { - if (s.communityRes.state == "success" && res.state == "success") { + if (s.communityRes.state === "success" && res.state === "success") { s.communityRes.data.moderators = res.data.moderators; } return s;