Merge branch 'main' into darkly-tweak

This commit is contained in:
SleeplessOne1917 2023-07-04 21:19:30 +00:00 committed by GitHub
commit 2e621c2d57
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
12 changed files with 70 additions and 35 deletions

View file

@ -2,7 +2,7 @@ import { initializeSite, setupDateFns } from "@utils/app";
import { hydrate } from "inferno-hydrate"; import { hydrate } from "inferno-hydrate";
import { Router } from "inferno-router"; import { Router } from "inferno-router";
import { App } from "../shared/components/app/app"; import { App } from "../shared/components/app/app";
import { HistoryService } from "../shared/services"; import { HistoryService, UserService } from "../shared/services";
import "bootstrap/js/dist/collapse"; import "bootstrap/js/dist/collapse";
import "bootstrap/js/dist/dropdown"; import "bootstrap/js/dist/dropdown";
@ -14,7 +14,7 @@ async function startClient() {
const wrapper = ( const wrapper = (
<Router history={HistoryService.history}> <Router history={HistoryService.history}>
<App /> <App user={UserService.Instance.myUserInfo} />
</Router> </Router>
); );

View file

@ -114,7 +114,7 @@ export default async (req: Request, res: Response) => {
const wrapper = ( const wrapper = (
<StaticRouter location={url} context={isoData}> <StaticRouter location={url} context={isoData}>
<App /> <App user={site?.my_user} />
</StaticRouter> </StaticRouter>
); );

View file

@ -3,6 +3,7 @@ import { dataBsTheme } from "@utils/browser";
import { Component, RefObject, createRef, linkEvent } from "inferno"; import { Component, RefObject, createRef, linkEvent } from "inferno";
import { Provider } from "inferno-i18next-dess"; import { Provider } from "inferno-i18next-dess";
import { Route, Switch } from "inferno-router"; import { Route, Switch } from "inferno-router";
import { MyUserInfo } from "lemmy-js-client";
import { IsoDataOptionalSite } from "../../interfaces"; import { IsoDataOptionalSite } from "../../interfaces";
import { routes } from "../../routes"; import { routes } from "../../routes";
import { FirstLoadService, I18NextService, UserService } from "../../services"; import { FirstLoadService, I18NextService, UserService } from "../../services";
@ -14,10 +15,14 @@ import { Navbar } from "./navbar";
import "./styles.scss"; import "./styles.scss";
import { Theme } from "./theme"; import { Theme } from "./theme";
export class App extends Component<any, any> { interface AppProps {
user?: MyUserInfo;
}
export class App extends Component<AppProps, any> {
private isoData: IsoDataOptionalSite = setIsoData(this.context); private isoData: IsoDataOptionalSite = setIsoData(this.context);
private readonly mainContentRef: RefObject<HTMLElement>; private readonly mainContentRef: RefObject<HTMLElement>;
constructor(props: any, context: any) { constructor(props: AppProps, context: any) {
super(props, context); super(props, context);
this.mainContentRef = createRef(); this.mainContentRef = createRef();
} }
@ -29,10 +34,6 @@ export class App extends Component<any, any> {
user = UserService.Instance.myUserInfo; user = UserService.Instance.myUserInfo;
componentDidMount() {
this.setState({ bsTheme: dataBsTheme(this.user) });
}
render() { render() {
const siteRes = this.isoData.site_res; const siteRes = this.isoData.site_res;
const siteView = siteRes?.site_view; const siteView = siteRes?.site_view;
@ -43,7 +44,7 @@ export class App extends Component<any, any> {
<div <div
id="app" id="app"
className="lemmy-site" className="lemmy-site"
data-bs-theme={this.state?.bsTheme} data-bs-theme={dataBsTheme(this.props.user)}
> >
<button <button
type="button" type="button"

View file

@ -53,7 +53,7 @@ export class UserBadges extends Component<UserBadgesProps> {
{getRoleLabelPill({ {getRoleLabelPill({
label: I18NextService.i18n.t("banned"), label: I18NextService.i18n.t("banned"),
tooltip: I18NextService.i18n.t("banned"), tooltip: I18NextService.i18n.t("banned"),
classes: "text-bg-danger", classes: "text-danger border border-danger",
shrink: false, shrink: false,
})} })}
</span> </span>
@ -63,7 +63,7 @@ export class UserBadges extends Component<UserBadgesProps> {
{getRoleLabelPill({ {getRoleLabelPill({
label: I18NextService.i18n.t("deleted"), label: I18NextService.i18n.t("deleted"),
tooltip: I18NextService.i18n.t("deleted"), tooltip: I18NextService.i18n.t("deleted"),
classes: "text-bg-danger", classes: "text-danger border border-danger",
shrink: false, shrink: false,
})} })}
</span> </span>
@ -74,7 +74,7 @@ export class UserBadges extends Component<UserBadgesProps> {
{getRoleLabelPill({ {getRoleLabelPill({
label: I18NextService.i18n.t("op").toUpperCase(), label: I18NextService.i18n.t("op").toUpperCase(),
tooltip: I18NextService.i18n.t("creator"), tooltip: I18NextService.i18n.t("creator"),
classes: "text-bg-info", classes: "text-info border border-info",
shrink: false, shrink: false,
})} })}
</span> </span>
@ -84,7 +84,7 @@ export class UserBadges extends Component<UserBadgesProps> {
{getRoleLabelPill({ {getRoleLabelPill({
label: I18NextService.i18n.t("mod"), label: I18NextService.i18n.t("mod"),
tooltip: I18NextService.i18n.t("mod"), tooltip: I18NextService.i18n.t("mod"),
classes: "text-bg-primary", classes: "text-primary border border-primary",
})} })}
</span> </span>
)} )}
@ -93,7 +93,7 @@ export class UserBadges extends Component<UserBadgesProps> {
{getRoleLabelPill({ {getRoleLabelPill({
label: I18NextService.i18n.t("admin"), label: I18NextService.i18n.t("admin"),
tooltip: I18NextService.i18n.t("admin"), tooltip: I18NextService.i18n.t("admin"),
classes: "text-bg-danger", classes: "text-danger border border-danger",
})} })}
</span> </span>
)} )}

View file

@ -114,7 +114,7 @@ interface HomeState {
} }
interface HomeProps { interface HomeProps {
listingType: ListingType; listingType?: ListingType;
dataType: DataType; dataType: DataType;
sort: SortType; sort: SortType;
page: number; page: number;
@ -163,12 +163,12 @@ function getDataTypeFromQuery(type?: string): DataType {
return type ? DataType[type] : DataType.Post; return type ? DataType[type] : DataType.Post;
} }
function getListingTypeFromQuery(type?: string): ListingType { function getListingTypeFromQuery(type?: string): ListingType | undefined {
const myListingType = const myListingType =
UserService.Instance.myUserInfo?.local_user_view?.local_user UserService.Instance.myUserInfo?.local_user_view?.local_user
?.default_listing_type; ?.default_listing_type;
return (type ? (type as ListingType) : myListingType) ?? "Local"; return type ? (type as ListingType) : myListingType;
} }
function getSortTypeFromQuery(type?: string): SortType { function getSortTypeFromQuery(type?: string): SortType {
@ -311,11 +311,12 @@ export class Home extends Component<any, HomeState> {
client, client,
auth, auth,
query: { dataType: urlDataType, listingType, page: urlPage, sort: urlSort }, query: { dataType: urlDataType, listingType, page: urlPage, sort: urlSort },
site,
}: InitialFetchRequest<QueryParams<HomeProps>>): Promise<HomeData> { }: InitialFetchRequest<QueryParams<HomeProps>>): Promise<HomeData> {
const dataType = getDataTypeFromQuery(urlDataType); const dataType = getDataTypeFromQuery(urlDataType);
const type_ =
// TODO figure out auth default_listingType, default_sort_type getListingTypeFromQuery(listingType) ??
const type_ = getListingTypeFromQuery(listingType); site.site_view.local_site.default_post_listing_type;
const sort = getSortTypeFromQuery(urlSort); const sort = getSortTypeFromQuery(urlSort);
const page = urlPage ? Number(urlPage) : 1; const page = urlPage ? Number(urlPage) : 1;
@ -761,7 +762,10 @@ export class Home extends Component<any, HomeState> {
</div> </div>
<div className="col-auto"> <div className="col-auto">
<ListingTypeSelect <ListingTypeSelect
type_={listingType} type_={
listingType ??
this.state.siteRes.site_view.local_site.default_post_listing_type
}
showLocal={showLocal(this.isoData)} showLocal={showLocal(this.isoData)}
showSubscribed showSubscribed
onChange={this.handleListingTypeChange} onChange={this.handleListingTypeChange}
@ -770,7 +774,12 @@ export class Home extends Component<any, HomeState> {
<div className="col-auto"> <div className="col-auto">
<SortSelect sort={sort} onChange={this.handleSortChange} /> <SortSelect sort={sort} onChange={this.handleSortChange} />
</div> </div>
<div className="col-auto ps-0">{getRss(listingType)}</div> <div className="col-auto ps-0">
{getRss(
listingType ??
this.state.siteRes.site_view.local_site.default_post_listing_type
)}
</div>
</div> </div>
); );
} }

View file

@ -175,7 +175,9 @@ export class Login extends Component<any, State> {
} }
case "success": { case "success": {
UserService.Instance.login(loginRes.data); UserService.Instance.login({
res: loginRes.data,
});
const site = await HttpService.client.getSite({ const site = await HttpService.client.getSite({
auth: myAuth(), auth: myAuth(),
}); });

View file

@ -206,7 +206,7 @@ export class Setup extends Component<any, State> {
if (i.state.registerRes.state == "success") { if (i.state.registerRes.state == "success") {
const data = i.state.registerRes.data; const data = i.state.registerRes.data;
UserService.Instance.login(data); UserService.Instance.login({ res: data });
i.setState({ doneRegisteringUser: true }); i.setState({ doneRegisteringUser: true });
} }
} }

View file

@ -469,7 +469,9 @@ export class Signup extends Component<any, State> {
// Only log them in if a jwt was set // Only log them in if a jwt was set
if (data.jwt) { if (data.jwt) {
UserService.Instance.login(data); UserService.Instance.login({
res: data,
});
const site = await HttpService.client.getSite({ auth: myAuth() }); const site = await HttpService.client.getSite({ auth: myAuth() });

View file

@ -135,7 +135,9 @@ export class PasswordChange extends Component<any, State> {
if (i.state.passwordChangeRes.state === "success") { if (i.state.passwordChangeRes.state === "success") {
const data = i.state.passwordChangeRes.data; const data = i.state.passwordChangeRes.data;
UserService.Instance.login(data); UserService.Instance.login({
res: data,
});
const site = await HttpService.client.getSite({ auth: myAuth() }); const site = await HttpService.client.getSite({ auth: myAuth() });
if (site.state === "success") { if (site.state === "success") {

View file

@ -1175,8 +1175,12 @@ export class Settings extends Component<any, SettingsState> {
...i.state.saveUserSettingsForm, ...i.state.saveUserSettingsForm,
auth: myAuthRequired(), auth: myAuthRequired(),
}); });
if (saveRes.state === "success") { if (saveRes.state === "success") {
UserService.Instance.login(saveRes.data); UserService.Instance.login({
res: saveRes.data,
showToast: false,
});
toast(I18NextService.i18n.t("saved")); toast(I18NextService.i18n.t("saved"));
window.scrollTo(0, 0); window.scrollTo(0, 0);
} }
@ -1198,7 +1202,10 @@ export class Settings extends Component<any, SettingsState> {
auth: myAuthRequired(), auth: myAuthRequired(),
}); });
if (changePasswordRes.state === "success") { if (changePasswordRes.state === "success") {
UserService.Instance.login(changePasswordRes.data); UserService.Instance.login({
res: changePasswordRes.data,
showToast: false,
});
window.scrollTo(0, 0); window.scrollTo(0, 0);
toast(I18NextService.i18n.t("password_changed")); toast(I18NextService.i18n.t("password_changed"));
} }

View file

@ -26,12 +26,18 @@ export class UserService {
this.#setJwtInfo(); this.#setJwtInfo();
} }
public login(res: LoginResponse) { public login({
res,
showToast = true,
}: {
res: LoginResponse;
showToast?: boolean;
}) {
const expires = new Date(); const expires = new Date();
expires.setDate(expires.getDate() + 365); expires.setDate(expires.getDate() + 365);
if (isBrowser() && res.jwt) { if (isBrowser() && res.jwt) {
toast(I18NextService.i18n.t("logged_in")); showToast && toast(I18NextService.i18n.t("logged_in"));
setAuthCookie(res.jwt); setAuthCookie(res.jwt);
this.#setJwtInfo(); this.#setJwtInfo();
} }

View file

@ -1,11 +1,17 @@
import { MyUserInfo } from "lemmy-js-client";
import isDark from "./is-dark"; import isDark from "./is-dark";
export default function dataBsTheme(user) { export default function dataBsTheme(user?: MyUserInfo) {
return (isDark() && user?.local_user_view.local_user.theme === "browser") || return (isDark() && user?.local_user_view.local_user.theme === "browser") ||
(user && (user &&
["darkly", "darkly-red", "darkly-pureblack"].includes( [
user.local_user_view.local_user.theme "darkly",
)) "darkly-red",
"darkly-pureblack",
"darkly-compact",
"i386",
"vaporwave-dark",
].includes(user.local_user_view.local_user.theme))
? "dark" ? "dark"
: "light"; : "light";
} }