2021-07-17 20:42:55 +00:00
|
|
|
import { Component, createRef, linkEvent, RefObject } from "inferno";
|
2021-02-22 02:39:04 +00:00
|
|
|
import { Link } from "inferno-router";
|
2020-09-06 16:15:25 +00:00
|
|
|
import {
|
2021-07-17 20:42:55 +00:00
|
|
|
CommentResponse,
|
2021-09-28 10:38:59 +00:00
|
|
|
GetReportCount,
|
|
|
|
GetReportCountResponse,
|
2020-09-06 16:15:25 +00:00
|
|
|
GetSiteResponse,
|
2021-10-17 17:42:30 +00:00
|
|
|
GetUnreadCount,
|
|
|
|
GetUnreadCountResponse,
|
2020-09-06 16:15:25 +00:00
|
|
|
PrivateMessageResponse,
|
2021-07-17 20:42:55 +00:00
|
|
|
UserOperation,
|
2021-02-22 02:39:04 +00:00
|
|
|
} from "lemmy-js-client";
|
2021-07-17 20:42:55 +00:00
|
|
|
import { Subscription } from "rxjs";
|
|
|
|
import { i18n } from "../../i18next";
|
|
|
|
import { UserService, WebSocketService } from "../../services";
|
2020-09-06 16:15:25 +00:00
|
|
|
import {
|
2021-07-17 20:42:55 +00:00
|
|
|
authField,
|
2021-09-18 20:40:59 +00:00
|
|
|
donateLemmyUrl,
|
2020-09-06 16:15:25 +00:00
|
|
|
getLanguage,
|
2021-07-17 20:42:55 +00:00
|
|
|
isBrowser,
|
2020-09-06 16:15:25 +00:00
|
|
|
notifyComment,
|
|
|
|
notifyPrivateMessage,
|
2021-09-18 16:35:49 +00:00
|
|
|
numToSI,
|
2021-07-17 20:42:55 +00:00
|
|
|
setTheme,
|
|
|
|
showAvatars,
|
|
|
|
toast,
|
2020-12-24 22:05:57 +00:00
|
|
|
wsClient,
|
2021-07-17 20:42:55 +00:00
|
|
|
wsJsonToRes,
|
|
|
|
wsSubscribe,
|
|
|
|
wsUserOp,
|
|
|
|
} from "../../utils";
|
|
|
|
import { Icon } from "../common/icon";
|
|
|
|
import { PictrsImage } from "../common/pictrs-image";
|
2020-09-06 16:15:25 +00:00
|
|
|
|
2020-09-07 03:41:46 +00:00
|
|
|
interface NavbarProps {
|
2020-12-24 01:58:27 +00:00
|
|
|
site_res: GetSiteResponse;
|
2020-09-07 03:41:46 +00:00
|
|
|
}
|
|
|
|
|
2020-09-06 16:15:25 +00:00
|
|
|
interface NavbarState {
|
|
|
|
isLoggedIn: boolean;
|
|
|
|
expanded: boolean;
|
2021-09-28 10:38:59 +00:00
|
|
|
unreadInboxCount: number;
|
|
|
|
unreadReportCount: number;
|
2020-09-06 16:15:25 +00:00
|
|
|
searchParam: string;
|
|
|
|
toggleSearch: boolean;
|
2021-08-20 02:56:18 +00:00
|
|
|
showDropdown: boolean;
|
2020-09-06 16:15:25 +00:00
|
|
|
onSiteBanner?(url: string): any;
|
|
|
|
}
|
|
|
|
|
2020-09-07 03:41:46 +00:00
|
|
|
export class Navbar extends Component<NavbarProps, NavbarState> {
|
2020-09-06 16:15:25 +00:00
|
|
|
private wsSub: Subscription;
|
|
|
|
private userSub: Subscription;
|
2021-09-28 10:38:59 +00:00
|
|
|
private unreadInboxCountSub: Subscription;
|
|
|
|
private unreadReportCountSub: Subscription;
|
2020-09-06 16:15:25 +00:00
|
|
|
private searchTextField: RefObject<HTMLInputElement>;
|
|
|
|
emptyState: NavbarState = {
|
2020-12-24 01:58:27 +00:00
|
|
|
isLoggedIn: !!this.props.site_res.my_user,
|
2021-09-28 10:38:59 +00:00
|
|
|
unreadInboxCount: 0,
|
|
|
|
unreadReportCount: 0,
|
2020-09-06 16:15:25 +00:00
|
|
|
expanded: false,
|
2021-02-22 02:39:04 +00:00
|
|
|
searchParam: "",
|
2020-09-06 16:15:25 +00:00
|
|
|
toggleSearch: false,
|
2021-08-20 02:56:18 +00:00
|
|
|
showDropdown: false,
|
2020-09-06 16:15:25 +00:00
|
|
|
};
|
2020-09-09 20:33:40 +00:00
|
|
|
subscription: any;
|
2020-09-06 16:15:25 +00:00
|
|
|
|
|
|
|
constructor(props: any, context: any) {
|
|
|
|
super(props, context);
|
|
|
|
this.state = this.emptyState;
|
|
|
|
|
2020-09-09 20:33:40 +00:00
|
|
|
this.parseMessage = this.parseMessage.bind(this);
|
|
|
|
this.subscription = wsSubscribe(this.parseMessage);
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
componentDidMount() {
|
2020-09-07 03:41:46 +00:00
|
|
|
// Subscribe to jwt changes
|
2020-09-06 16:15:25 +00:00
|
|
|
if (isBrowser()) {
|
2021-07-17 15:38:28 +00:00
|
|
|
this.websocketEvents();
|
|
|
|
|
2020-09-09 20:33:40 +00:00
|
|
|
this.searchTextField = createRef();
|
|
|
|
console.log(`isLoggedIn = ${this.state.isLoggedIn}`);
|
|
|
|
|
|
|
|
// On the first load, check the unreads
|
|
|
|
if (this.state.isLoggedIn == false) {
|
|
|
|
// setTheme(data.my_user.theme, true);
|
|
|
|
// i18n.changeLanguage(getLanguage());
|
|
|
|
// i18n.changeLanguage('de');
|
|
|
|
} else {
|
|
|
|
this.requestNotificationPermission();
|
2020-12-24 22:05:57 +00:00
|
|
|
WebSocketService.Instance.send(
|
|
|
|
wsClient.userJoin({
|
|
|
|
auth: authField(),
|
|
|
|
})
|
|
|
|
);
|
2020-09-09 20:33:40 +00:00
|
|
|
this.fetchUnreads();
|
|
|
|
}
|
|
|
|
|
2020-09-06 16:15:25 +00:00
|
|
|
this.userSub = UserService.Instance.jwtSub.subscribe(res => {
|
|
|
|
// A login
|
|
|
|
if (res !== undefined) {
|
|
|
|
this.requestNotificationPermission();
|
2020-12-24 22:05:57 +00:00
|
|
|
WebSocketService.Instance.send(
|
|
|
|
wsClient.getSite({ auth: authField() })
|
|
|
|
);
|
2020-09-06 16:15:25 +00:00
|
|
|
} else {
|
2020-09-09 20:33:40 +00:00
|
|
|
this.setState({ isLoggedIn: false });
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// Subscribe to unread count changes
|
2021-09-28 10:38:59 +00:00
|
|
|
this.unreadInboxCountSub =
|
|
|
|
UserService.Instance.unreadInboxCountSub.subscribe(res => {
|
|
|
|
this.setState({ unreadInboxCount: res });
|
|
|
|
});
|
|
|
|
// Subscribe to unread report count changes
|
|
|
|
this.unreadReportCountSub =
|
|
|
|
UserService.Instance.unreadReportCountSub.subscribe(res => {
|
|
|
|
this.setState({ unreadReportCount: res });
|
|
|
|
});
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-20 02:56:18 +00:00
|
|
|
componentWillUnmount() {
|
|
|
|
this.wsSub.unsubscribe();
|
|
|
|
this.userSub.unsubscribe();
|
2021-09-28 10:38:59 +00:00
|
|
|
this.unreadInboxCountSub.unsubscribe();
|
|
|
|
this.unreadReportCountSub.unsubscribe();
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
updateUrl() {
|
2020-09-07 03:41:46 +00:00
|
|
|
const searchParam = this.state.searchParam;
|
2021-02-22 02:39:04 +00:00
|
|
|
this.setState({ searchParam: "" });
|
2020-09-07 03:41:46 +00:00
|
|
|
this.setState({ toggleSearch: false });
|
2021-08-20 02:56:18 +00:00
|
|
|
this.setState({ showDropdown: false, expanded: false });
|
2021-02-22 02:39:04 +00:00
|
|
|
if (searchParam === "") {
|
2020-09-07 03:41:46 +00:00
|
|
|
this.context.router.history.push(`/search/`);
|
|
|
|
} else {
|
2020-10-07 10:39:07 +00:00
|
|
|
const searchParamEncoded = encodeURIComponent(searchParam);
|
2020-09-07 03:41:46 +00:00
|
|
|
this.context.router.history.push(
|
2021-04-23 21:48:31 +00:00
|
|
|
`/search/q/${searchParamEncoded}/type/All/sort/TopAll/listing_type/All/community_id/0/creator_id/0/page/1`
|
2020-09-07 03:41:46 +00:00
|
|
|
);
|
|
|
|
}
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
return this.navbar();
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO class active corresponding to current page
|
|
|
|
navbar() {
|
2021-08-20 02:56:18 +00:00
|
|
|
let myUserInfo =
|
|
|
|
UserService.Instance.myUserInfo || this.props.site_res.my_user;
|
|
|
|
let person = myUserInfo?.local_user_view.person;
|
2020-09-06 16:15:25 +00:00
|
|
|
return (
|
|
|
|
<nav class="navbar navbar-expand-lg navbar-light shadow-sm p-0 px-3">
|
|
|
|
<div class="container">
|
2020-12-24 01:58:27 +00:00
|
|
|
{this.props.site_res.site_view && (
|
2021-08-20 02:56:18 +00:00
|
|
|
<button
|
2021-04-26 13:57:41 +00:00
|
|
|
title={
|
|
|
|
this.props.site_res.site_view.site.description ||
|
|
|
|
this.props.site_res.site_view.site.name
|
|
|
|
}
|
2021-08-20 02:56:18 +00:00
|
|
|
className="d-flex align-items-center navbar-brand mr-md-3 btn btn-link"
|
|
|
|
onClick={linkEvent(this, this.handleGotoHome)}
|
2020-09-15 16:07:18 +00:00
|
|
|
>
|
2020-12-24 01:58:27 +00:00
|
|
|
{this.props.site_res.site_view.site.icon && showAvatars() && (
|
|
|
|
<PictrsImage
|
|
|
|
src={this.props.site_res.site_view.site.icon}
|
|
|
|
icon
|
|
|
|
/>
|
2020-09-15 16:07:18 +00:00
|
|
|
)}
|
2020-12-24 01:58:27 +00:00
|
|
|
{this.props.site_res.site_view.site.name}
|
2021-08-20 02:56:18 +00:00
|
|
|
</button>
|
2020-09-15 16:07:18 +00:00
|
|
|
)}
|
2020-09-06 16:15:25 +00:00
|
|
|
{this.state.isLoggedIn && (
|
2021-09-28 10:38:59 +00:00
|
|
|
<>
|
|
|
|
<ul class="navbar-nav ml-auto">
|
|
|
|
<li className="nav-item">
|
|
|
|
<button
|
|
|
|
className="p-1 navbar-toggler nav-link border-0 btn btn-link"
|
|
|
|
onClick={linkEvent(this, this.handleGotoInbox)}
|
|
|
|
title={i18n.t("unread_messages", {
|
|
|
|
count: this.state.unreadInboxCount,
|
|
|
|
formattedCount: numToSI(this.state.unreadInboxCount),
|
|
|
|
})}
|
|
|
|
>
|
|
|
|
<Icon icon="bell" />
|
|
|
|
{this.state.unreadInboxCount > 0 && (
|
|
|
|
<span class="mx-1 badge badge-light">
|
|
|
|
{numToSI(this.state.unreadInboxCount)}
|
|
|
|
</span>
|
|
|
|
)}
|
|
|
|
</button>
|
|
|
|
</li>
|
|
|
|
</ul>
|
|
|
|
{UserService.Instance.myUserInfo?.moderates.length > 0 && (
|
|
|
|
<ul class="navbar-nav ml-1">
|
|
|
|
<li className="nav-item">
|
|
|
|
<button
|
|
|
|
className="p-1 navbar-toggler nav-link border-0 btn btn-link"
|
|
|
|
onClick={linkEvent(this, this.handleGotoReports)}
|
|
|
|
title={i18n.t("unread_reports", {
|
|
|
|
count: this.state.unreadReportCount,
|
|
|
|
formattedCount: numToSI(this.state.unreadReportCount),
|
|
|
|
})}
|
|
|
|
>
|
|
|
|
<Icon icon="shield" />
|
|
|
|
{this.state.unreadReportCount > 0 && (
|
|
|
|
<span class="mx-1 badge badge-light">
|
|
|
|
{numToSI(this.state.unreadReportCount)}
|
|
|
|
</span>
|
|
|
|
)}
|
|
|
|
</button>
|
|
|
|
</li>
|
|
|
|
</ul>
|
2020-09-06 16:15:25 +00:00
|
|
|
)}
|
2021-09-28 10:38:59 +00:00
|
|
|
</>
|
2020-09-06 16:15:25 +00:00
|
|
|
)}
|
|
|
|
<button
|
|
|
|
class="navbar-toggler border-0 p-1"
|
|
|
|
type="button"
|
|
|
|
aria-label="menu"
|
|
|
|
onClick={linkEvent(this, this.expandNavbar)}
|
2021-02-22 02:39:04 +00:00
|
|
|
data-tippy-content={i18n.t("expand_here")}
|
2020-09-06 16:15:25 +00:00
|
|
|
>
|
2021-02-11 20:35:27 +00:00
|
|
|
<Icon icon="menu" />
|
2020-09-06 16:15:25 +00:00
|
|
|
</button>
|
2020-09-07 03:41:46 +00:00
|
|
|
<div
|
2021-02-22 02:39:04 +00:00
|
|
|
className={`${!this.state.expanded && "collapse"} navbar-collapse`}
|
2020-09-07 03:41:46 +00:00
|
|
|
>
|
|
|
|
<ul class="navbar-nav my-2 mr-auto">
|
|
|
|
<li class="nav-item">
|
2021-08-20 02:56:18 +00:00
|
|
|
<button
|
|
|
|
className="nav-link btn btn-link"
|
|
|
|
onClick={linkEvent(this, this.handleGotoCommunities)}
|
2021-02-22 02:39:04 +00:00
|
|
|
title={i18n.t("communities")}
|
2020-09-07 03:41:46 +00:00
|
|
|
>
|
2021-02-22 02:39:04 +00:00
|
|
|
{i18n.t("communities")}
|
2021-08-20 02:56:18 +00:00
|
|
|
</button>
|
2020-09-07 03:41:46 +00:00
|
|
|
</li>
|
|
|
|
<li class="nav-item">
|
2021-08-20 02:56:18 +00:00
|
|
|
<button
|
|
|
|
className="nav-link btn btn-link"
|
|
|
|
onClick={linkEvent(this, this.handleGotoCreatePost)}
|
2021-02-22 02:39:04 +00:00
|
|
|
title={i18n.t("create_post")}
|
2020-09-07 03:41:46 +00:00
|
|
|
>
|
2021-02-22 02:39:04 +00:00
|
|
|
{i18n.t("create_post")}
|
2021-08-20 02:56:18 +00:00
|
|
|
</button>
|
2020-09-07 03:41:46 +00:00
|
|
|
</li>
|
2021-04-24 03:55:51 +00:00
|
|
|
{this.canCreateCommunity && (
|
|
|
|
<li class="nav-item">
|
2021-08-20 02:56:18 +00:00
|
|
|
<button
|
|
|
|
className="nav-link btn btn-link"
|
|
|
|
onClick={linkEvent(this, this.handleGotoCreateCommunity)}
|
2021-04-24 03:55:51 +00:00
|
|
|
title={i18n.t("create_community")}
|
|
|
|
>
|
|
|
|
{i18n.t("create_community")}
|
2021-08-20 02:56:18 +00:00
|
|
|
</button>
|
2021-04-24 03:55:51 +00:00
|
|
|
</li>
|
|
|
|
)}
|
2020-12-01 15:27:27 +00:00
|
|
|
<li class="nav-item">
|
|
|
|
<a
|
|
|
|
className="nav-link"
|
2021-02-22 02:39:04 +00:00
|
|
|
title={i18n.t("support_lemmy")}
|
2021-09-18 20:40:59 +00:00
|
|
|
href={donateLemmyUrl}
|
2020-12-01 15:27:27 +00:00
|
|
|
>
|
2021-03-25 15:20:54 +00:00
|
|
|
<Icon icon="heart" classes="small" />
|
2020-12-01 15:27:27 +00:00
|
|
|
</a>
|
|
|
|
</li>
|
2020-09-07 03:41:46 +00:00
|
|
|
</ul>
|
|
|
|
<ul class="navbar-nav my-2">
|
|
|
|
{this.canAdmin && (
|
2020-09-06 16:15:25 +00:00
|
|
|
<li className="nav-item">
|
2021-08-20 02:56:18 +00:00
|
|
|
<button
|
|
|
|
className="nav-link btn btn-link"
|
|
|
|
onClick={linkEvent(this, this.handleGotoAdmin)}
|
2021-02-22 02:39:04 +00:00
|
|
|
title={i18n.t("admin_settings")}
|
2020-09-06 16:15:25 +00:00
|
|
|
>
|
2021-02-11 20:35:27 +00:00
|
|
|
<Icon icon="settings" />
|
2021-08-20 02:56:18 +00:00
|
|
|
</button>
|
2020-09-06 16:15:25 +00:00
|
|
|
</li>
|
2020-09-07 03:41:46 +00:00
|
|
|
)}
|
|
|
|
</ul>
|
|
|
|
{!this.context.router.history.location.pathname.match(
|
|
|
|
/^\/search/
|
|
|
|
) && (
|
|
|
|
<form
|
2021-09-19 20:31:17 +00:00
|
|
|
class="form-inline mr-2"
|
2020-09-07 03:41:46 +00:00
|
|
|
onSubmit={linkEvent(this, this.handleSearchSubmit)}
|
|
|
|
>
|
|
|
|
<input
|
2021-02-09 16:21:24 +00:00
|
|
|
id="search-input"
|
2020-09-07 03:41:46 +00:00
|
|
|
class={`form-control mr-0 search-input ${
|
2021-02-22 02:39:04 +00:00
|
|
|
this.state.toggleSearch ? "show-input" : "hide-input"
|
2020-09-07 03:41:46 +00:00
|
|
|
}`}
|
|
|
|
onInput={linkEvent(this, this.handleSearchParam)}
|
|
|
|
value={this.state.searchParam}
|
|
|
|
ref={this.searchTextField}
|
|
|
|
type="text"
|
2021-02-22 02:39:04 +00:00
|
|
|
placeholder={i18n.t("search")}
|
2020-09-07 03:41:46 +00:00
|
|
|
onBlur={linkEvent(this, this.handleSearchBlur)}
|
|
|
|
></input>
|
2021-02-09 16:21:24 +00:00
|
|
|
<label class="sr-only" htmlFor="search-input">
|
2021-02-22 02:39:04 +00:00
|
|
|
{i18n.t("search")}
|
2021-02-09 16:21:24 +00:00
|
|
|
</label>
|
2020-09-07 03:41:46 +00:00
|
|
|
<button
|
|
|
|
name="search-btn"
|
|
|
|
onClick={linkEvent(this, this.handleSearchBtn)}
|
|
|
|
class="px-1 btn btn-link"
|
|
|
|
style="color: var(--gray)"
|
2021-02-22 02:39:04 +00:00
|
|
|
aria-label={i18n.t("search")}
|
2020-09-07 03:41:46 +00:00
|
|
|
>
|
2021-02-11 20:35:27 +00:00
|
|
|
<Icon icon="search" />
|
2020-09-07 03:41:46 +00:00
|
|
|
</button>
|
|
|
|
</form>
|
|
|
|
)}
|
|
|
|
{this.state.isLoggedIn ? (
|
|
|
|
<>
|
|
|
|
<ul class="navbar-nav my-2">
|
2020-09-06 16:15:25 +00:00
|
|
|
<li className="nav-item">
|
|
|
|
<Link
|
2020-09-07 03:41:46 +00:00
|
|
|
className="nav-link"
|
|
|
|
to="/inbox"
|
2021-09-28 10:38:59 +00:00
|
|
|
title={i18n.t("unread_messages", {
|
|
|
|
count: this.state.unreadInboxCount,
|
|
|
|
formattedCount: numToSI(this.state.unreadInboxCount),
|
|
|
|
})}
|
2020-09-06 16:15:25 +00:00
|
|
|
>
|
2021-02-11 20:35:27 +00:00
|
|
|
<Icon icon="bell" />
|
2021-09-28 10:38:59 +00:00
|
|
|
{this.state.unreadInboxCount > 0 && (
|
|
|
|
<span class="ml-1 badge badge-light">
|
|
|
|
{numToSI(this.state.unreadInboxCount)}
|
2020-09-07 03:41:46 +00:00
|
|
|
</span>
|
|
|
|
)}
|
2020-09-06 16:15:25 +00:00
|
|
|
</Link>
|
|
|
|
</li>
|
2020-09-07 03:41:46 +00:00
|
|
|
</ul>
|
2021-09-28 10:38:59 +00:00
|
|
|
{UserService.Instance.myUserInfo?.moderates.length > 0 && (
|
|
|
|
<ul class="navbar-nav my-2">
|
|
|
|
<li className="nav-item">
|
|
|
|
<Link
|
|
|
|
className="nav-link"
|
|
|
|
to="/reports"
|
|
|
|
title={i18n.t("unread_reports", {
|
|
|
|
count: this.state.unreadReportCount,
|
|
|
|
formattedCount: numToSI(this.state.unreadReportCount),
|
|
|
|
})}
|
|
|
|
>
|
|
|
|
<Icon icon="shield" />
|
|
|
|
{this.state.unreadReportCount > 0 && (
|
|
|
|
<span class="ml-1 badge badge-light">
|
|
|
|
{numToSI(this.state.unreadReportCount)}
|
|
|
|
</span>
|
|
|
|
)}
|
|
|
|
</Link>
|
|
|
|
</li>
|
|
|
|
</ul>
|
|
|
|
)}
|
2020-09-07 03:41:46 +00:00
|
|
|
<ul class="navbar-nav">
|
2021-08-20 02:56:18 +00:00
|
|
|
<li class="nav-item dropdown">
|
|
|
|
<button
|
|
|
|
class="nav-link btn btn-link dropdown-toggle"
|
|
|
|
onClick={linkEvent(this, this.handleShowDropdown)}
|
|
|
|
id="navbarDropdown"
|
|
|
|
role="button"
|
|
|
|
aria-expanded="false"
|
2020-09-06 16:15:25 +00:00
|
|
|
>
|
2020-09-07 03:41:46 +00:00
|
|
|
<span>
|
2021-08-20 02:56:18 +00:00
|
|
|
{person.avatar && showAvatars() && (
|
|
|
|
<PictrsImage src={person.avatar} icon />
|
2020-09-07 03:41:46 +00:00
|
|
|
)}
|
2021-08-20 02:56:18 +00:00
|
|
|
{person.display_name
|
|
|
|
? person.display_name
|
|
|
|
: person.name}
|
2020-09-07 03:41:46 +00:00
|
|
|
</span>
|
2021-08-20 02:56:18 +00:00
|
|
|
</button>
|
|
|
|
{this.state.showDropdown && (
|
|
|
|
<div class="dropdown-content">
|
|
|
|
<li className="nav-item">
|
|
|
|
<button
|
|
|
|
className="nav-link btn btn-link"
|
|
|
|
onClick={linkEvent(this, this.handleGotoProfile)}
|
|
|
|
title={i18n.t("profile")}
|
|
|
|
>
|
|
|
|
<Icon icon="user" classes="mr-1" />
|
|
|
|
{i18n.t("profile")}
|
|
|
|
</button>
|
|
|
|
</li>
|
|
|
|
<li className="nav-item">
|
|
|
|
<button
|
|
|
|
className="nav-link btn btn-link"
|
|
|
|
onClick={linkEvent(this, this.handleGotoSettings)}
|
|
|
|
title={i18n.t("settings")}
|
|
|
|
>
|
|
|
|
<Icon icon="settings" classes="mr-1" />
|
|
|
|
{i18n.t("settings")}
|
|
|
|
</button>
|
|
|
|
</li>
|
|
|
|
<li>
|
|
|
|
<hr class="dropdown-divider" />
|
|
|
|
</li>
|
|
|
|
<li className="nav-item">
|
|
|
|
<button
|
|
|
|
className="nav-link btn btn-link"
|
|
|
|
onClick={linkEvent(this, this.handleLogoutClick)}
|
|
|
|
title="test"
|
|
|
|
>
|
|
|
|
<Icon icon="log-out" classes="mr-1" />
|
|
|
|
{i18n.t("logout")}
|
|
|
|
</button>
|
|
|
|
</li>
|
|
|
|
</div>
|
|
|
|
)}
|
2020-09-06 16:15:25 +00:00
|
|
|
</li>
|
|
|
|
</ul>
|
2020-09-07 03:41:46 +00:00
|
|
|
</>
|
|
|
|
) : (
|
|
|
|
<ul class="navbar-nav my-2">
|
2021-09-19 20:31:17 +00:00
|
|
|
<li className="nav-item">
|
2021-08-20 02:56:18 +00:00
|
|
|
<button
|
2021-09-19 20:31:17 +00:00
|
|
|
className="nav-link btn btn-link"
|
2021-08-20 02:56:18 +00:00
|
|
|
onClick={linkEvent(this, this.handleGotoLogin)}
|
2021-09-19 20:31:17 +00:00
|
|
|
title={i18n.t("login")}
|
2020-09-07 03:41:46 +00:00
|
|
|
>
|
2021-09-19 20:31:17 +00:00
|
|
|
{i18n.t("login")}
|
|
|
|
</button>
|
|
|
|
</li>
|
|
|
|
<li className="nav-item">
|
|
|
|
<button
|
|
|
|
className="nav-link btn btn-link"
|
|
|
|
onClick={linkEvent(this, this.handleGotoSignup)}
|
|
|
|
title={i18n.t("sign_up")}
|
|
|
|
>
|
|
|
|
{i18n.t("sign_up")}
|
2021-08-20 02:56:18 +00:00
|
|
|
</button>
|
2020-09-07 03:41:46 +00:00
|
|
|
</li>
|
|
|
|
</ul>
|
|
|
|
)}
|
|
|
|
</div>
|
2020-09-06 16:15:25 +00:00
|
|
|
</div>
|
|
|
|
</nav>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
expandNavbar(i: Navbar) {
|
|
|
|
i.state.expanded = !i.state.expanded;
|
|
|
|
i.setState(i.state);
|
|
|
|
}
|
|
|
|
|
2021-08-20 02:56:18 +00:00
|
|
|
handleSearchParam(i: Navbar, event: any) {
|
|
|
|
i.state.searchParam = event.target.value;
|
|
|
|
i.setState(i.state);
|
|
|
|
}
|
|
|
|
|
|
|
|
handleSearchSubmit(i: Navbar, event: any) {
|
|
|
|
event.preventDefault();
|
|
|
|
i.updateUrl();
|
|
|
|
}
|
|
|
|
|
|
|
|
handleSearchBtn(i: Navbar, event: any) {
|
|
|
|
event.preventDefault();
|
|
|
|
i.setState({ toggleSearch: true });
|
|
|
|
|
|
|
|
i.searchTextField.current.focus();
|
|
|
|
const offsetWidth = i.searchTextField.current.offsetWidth;
|
|
|
|
if (i.state.searchParam && offsetWidth > 100) {
|
|
|
|
i.updateUrl();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
handleSearchBlur(i: Navbar, event: any) {
|
|
|
|
if (!(event.relatedTarget && event.relatedTarget.name !== "search-btn")) {
|
|
|
|
i.state.toggleSearch = false;
|
|
|
|
i.setState(i.state);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
handleLogoutClick(i: Navbar) {
|
|
|
|
i.setState({ showDropdown: false, expanded: false });
|
|
|
|
UserService.Instance.logout();
|
2021-09-18 21:23:11 +00:00
|
|
|
window.location.href = "/";
|
2021-08-20 02:56:18 +00:00
|
|
|
location.reload();
|
|
|
|
}
|
|
|
|
|
|
|
|
handleGotoSettings(i: Navbar) {
|
|
|
|
i.setState({ showDropdown: false, expanded: false });
|
|
|
|
i.context.router.history.push("/settings");
|
|
|
|
}
|
|
|
|
|
|
|
|
handleGotoProfile(i: Navbar) {
|
|
|
|
i.setState({ showDropdown: false, expanded: false });
|
|
|
|
i.context.router.history.push(
|
|
|
|
`/u/${UserService.Instance.myUserInfo.local_user_view.person.name}`
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
handleGotoCreatePost(i: Navbar) {
|
|
|
|
i.setState({ showDropdown: false, expanded: false });
|
|
|
|
i.context.router.history.push("/create_post", {
|
|
|
|
prevPath: i.currentLocation,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
handleGotoCreateCommunity(i: Navbar) {
|
|
|
|
i.setState({ showDropdown: false, expanded: false });
|
|
|
|
i.context.router.history.push(`/create_community`);
|
|
|
|
}
|
|
|
|
|
|
|
|
handleGotoCommunities(i: Navbar) {
|
|
|
|
i.setState({ showDropdown: false, expanded: false });
|
|
|
|
i.context.router.history.push(`/communities`);
|
|
|
|
}
|
|
|
|
|
|
|
|
handleGotoHome(i: Navbar) {
|
|
|
|
i.setState({ showDropdown: false, expanded: false });
|
|
|
|
i.context.router.history.push(`/`);
|
|
|
|
}
|
|
|
|
|
|
|
|
handleGotoInbox(i: Navbar) {
|
|
|
|
i.setState({ showDropdown: false, expanded: false });
|
|
|
|
i.context.router.history.push(`/inbox`);
|
|
|
|
}
|
|
|
|
|
2021-09-28 10:38:59 +00:00
|
|
|
handleGotoReports(i: Navbar) {
|
|
|
|
i.setState({ showDropdown: false, expanded: false });
|
|
|
|
i.context.router.history.push(`/reports`);
|
|
|
|
}
|
|
|
|
|
2021-08-20 02:56:18 +00:00
|
|
|
handleGotoAdmin(i: Navbar) {
|
|
|
|
i.setState({ showDropdown: false, expanded: false });
|
|
|
|
i.context.router.history.push(`/admin`);
|
|
|
|
}
|
|
|
|
|
|
|
|
handleGotoLogin(i: Navbar) {
|
|
|
|
i.setState({ showDropdown: false, expanded: false });
|
|
|
|
i.context.router.history.push(`/login`);
|
|
|
|
}
|
|
|
|
|
2021-09-19 20:31:17 +00:00
|
|
|
handleGotoSignup(i: Navbar) {
|
|
|
|
i.setState({ showDropdown: false, expanded: false });
|
|
|
|
i.context.router.history.push(`/signup`);
|
|
|
|
}
|
|
|
|
|
2021-08-20 02:56:18 +00:00
|
|
|
handleShowDropdown(i: Navbar) {
|
|
|
|
i.state.showDropdown = !i.state.showDropdown;
|
|
|
|
i.setState(i.state);
|
|
|
|
}
|
|
|
|
|
2020-12-24 01:58:27 +00:00
|
|
|
parseMessage(msg: any) {
|
|
|
|
let op = wsUserOp(msg);
|
2021-04-07 15:54:38 +00:00
|
|
|
console.log(msg);
|
2020-09-06 16:15:25 +00:00
|
|
|
if (msg.error) {
|
2021-02-22 02:39:04 +00:00
|
|
|
if (msg.error == "not_logged_in") {
|
2020-09-06 16:15:25 +00:00
|
|
|
UserService.Instance.logout();
|
|
|
|
location.reload();
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
} else if (msg.reconnect) {
|
2021-07-31 14:30:08 +00:00
|
|
|
console.log(i18n.t("websocket_reconnected"));
|
2020-12-24 22:05:57 +00:00
|
|
|
WebSocketService.Instance.send(
|
|
|
|
wsClient.userJoin({
|
|
|
|
auth: authField(),
|
|
|
|
})
|
|
|
|
);
|
2020-09-06 16:15:25 +00:00
|
|
|
this.fetchUnreads();
|
2021-10-17 17:42:30 +00:00
|
|
|
} else if (op == UserOperation.GetUnreadCount) {
|
|
|
|
let data = wsJsonToRes<GetUnreadCountResponse>(msg).data;
|
|
|
|
this.state.unreadInboxCount =
|
|
|
|
data.replies + data.mentions + data.private_messages;
|
2020-09-06 16:15:25 +00:00
|
|
|
this.setState(this.state);
|
|
|
|
this.sendUnreadCount();
|
2021-09-28 10:38:59 +00:00
|
|
|
} else if (op == UserOperation.GetReportCount) {
|
|
|
|
let data = wsJsonToRes<GetReportCountResponse>(msg).data;
|
|
|
|
this.state.unreadReportCount = data.post_reports + data.comment_reports;
|
|
|
|
this.setState(this.state);
|
|
|
|
this.sendReportUnread();
|
2020-12-24 01:58:27 +00:00
|
|
|
} else if (op == UserOperation.GetSite) {
|
2020-09-09 20:33:40 +00:00
|
|
|
// This is only called on a successful login
|
2020-12-24 01:58:27 +00:00
|
|
|
let data = wsJsonToRes<GetSiteResponse>(msg).data;
|
2020-12-24 22:05:57 +00:00
|
|
|
console.log(data.my_user);
|
2021-08-20 02:56:18 +00:00
|
|
|
UserService.Instance.myUserInfo = data.my_user;
|
|
|
|
setTheme(
|
|
|
|
UserService.Instance.myUserInfo.local_user_view.local_user.theme
|
|
|
|
);
|
2020-09-15 13:20:19 +00:00
|
|
|
i18n.changeLanguage(getLanguage());
|
2020-09-09 20:33:40 +00:00
|
|
|
this.state.isLoggedIn = true;
|
|
|
|
this.setState(this.state);
|
2020-12-24 01:58:27 +00:00
|
|
|
} else if (op == UserOperation.CreateComment) {
|
|
|
|
let data = wsJsonToRes<CommentResponse>(msg).data;
|
2020-09-06 16:15:25 +00:00
|
|
|
|
|
|
|
if (this.state.isLoggedIn) {
|
2021-03-15 18:09:31 +00:00
|
|
|
if (
|
|
|
|
data.recipient_ids.includes(
|
2021-08-20 02:56:18 +00:00
|
|
|
UserService.Instance.myUserInfo.local_user_view.local_user.id
|
2021-03-15 18:09:31 +00:00
|
|
|
)
|
|
|
|
) {
|
2021-09-28 10:38:59 +00:00
|
|
|
this.state.unreadInboxCount++;
|
2020-09-06 16:15:25 +00:00
|
|
|
this.setState(this.state);
|
|
|
|
this.sendUnreadCount();
|
2020-12-24 01:58:27 +00:00
|
|
|
notifyComment(data.comment_view, this.context.router);
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
}
|
2020-12-24 01:58:27 +00:00
|
|
|
} else if (op == UserOperation.CreatePrivateMessage) {
|
|
|
|
let data = wsJsonToRes<PrivateMessageResponse>(msg).data;
|
2020-09-06 16:15:25 +00:00
|
|
|
|
|
|
|
if (this.state.isLoggedIn) {
|
2020-12-24 01:58:27 +00:00
|
|
|
if (
|
2021-03-15 18:09:31 +00:00
|
|
|
data.private_message_view.recipient.id ==
|
2021-08-20 02:56:18 +00:00
|
|
|
UserService.Instance.myUserInfo.local_user_view.person.id
|
2020-12-24 01:58:27 +00:00
|
|
|
) {
|
2021-09-28 10:38:59 +00:00
|
|
|
this.state.unreadInboxCount++;
|
2020-09-06 16:15:25 +00:00
|
|
|
this.setState(this.state);
|
|
|
|
this.sendUnreadCount();
|
2020-12-24 01:58:27 +00:00
|
|
|
notifyPrivateMessage(data.private_message_view, this.context.router);
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
}
|
2020-09-07 03:41:46 +00:00
|
|
|
}
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fetchUnreads() {
|
2021-10-17 17:42:30 +00:00
|
|
|
console.log("Fetching inbox unreads...");
|
|
|
|
|
|
|
|
let unreadForm: GetUnreadCount = {
|
|
|
|
auth: authField(),
|
|
|
|
};
|
|
|
|
|
|
|
|
WebSocketService.Instance.send(wsClient.getUnreadCount(unreadForm));
|
2021-09-28 10:38:59 +00:00
|
|
|
|
|
|
|
console.log("Fetching reports...");
|
|
|
|
|
|
|
|
let reportCountForm: GetReportCount = {
|
|
|
|
auth: authField(),
|
|
|
|
};
|
|
|
|
|
|
|
|
WebSocketService.Instance.send(wsClient.getReportCount(reportCountForm));
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
get currentLocation() {
|
|
|
|
return this.context.router.history.location.pathname;
|
|
|
|
}
|
|
|
|
|
|
|
|
sendUnreadCount() {
|
2021-09-28 10:38:59 +00:00
|
|
|
UserService.Instance.unreadInboxCountSub.next(this.state.unreadInboxCount);
|
|
|
|
}
|
|
|
|
|
|
|
|
sendReportUnread() {
|
|
|
|
UserService.Instance.unreadReportCountSub.next(
|
|
|
|
this.state.unreadReportCount
|
|
|
|
);
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
get canAdmin(): boolean {
|
|
|
|
return (
|
2021-08-20 02:56:18 +00:00
|
|
|
UserService.Instance.myUserInfo &&
|
2020-12-24 01:58:27 +00:00
|
|
|
this.props.site_res.admins
|
2021-03-15 18:09:31 +00:00
|
|
|
.map(a => a.person.id)
|
2021-08-20 02:56:18 +00:00
|
|
|
.includes(UserService.Instance.myUserInfo.local_user_view.person.id)
|
2020-09-06 16:15:25 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-04-24 03:55:51 +00:00
|
|
|
get canCreateCommunity(): boolean {
|
2021-07-17 15:38:28 +00:00
|
|
|
let adminOnly =
|
|
|
|
this.props.site_res.site_view?.site.community_creation_admin_only;
|
2021-04-24 03:55:51 +00:00
|
|
|
return !adminOnly || this.canAdmin;
|
|
|
|
}
|
|
|
|
|
2021-07-17 15:38:28 +00:00
|
|
|
/// Listens for some websocket errors
|
|
|
|
websocketEvents() {
|
|
|
|
let msg = i18n.t("websocket_disconnected");
|
|
|
|
WebSocketService.Instance.closeEventListener(() => {
|
2021-07-31 14:30:08 +00:00
|
|
|
console.error(msg);
|
2021-07-17 15:38:28 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-09-06 16:15:25 +00:00
|
|
|
requestNotificationPermission() {
|
2021-08-20 02:56:18 +00:00
|
|
|
if (UserService.Instance.myUserInfo) {
|
2021-02-22 02:39:04 +00:00
|
|
|
document.addEventListener("DOMContentLoaded", function () {
|
2020-09-06 16:15:25 +00:00
|
|
|
if (!Notification) {
|
2021-02-22 02:39:04 +00:00
|
|
|
toast(i18n.t("notifications_error"), "danger");
|
2020-09-06 16:15:25 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-02-22 02:39:04 +00:00
|
|
|
if (Notification.permission !== "granted")
|
2020-09-06 16:15:25 +00:00
|
|
|
Notification.requestPermission();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|