Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Develop Release #98

Merged
merged 5 commits into from
Nov 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "lemmy-modder",
"version": "1.3.4",
"version": "1.3.5",
"description": "Lemmy Moderation App",
"author": "tgxn",
"license": "MIT",
Expand Down
16 changes: 8 additions & 8 deletions src/components/Actions/GenButtons.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -188,21 +188,21 @@ export const BanUserSiteButton = ({ person, ...props }) => {
placeholder={`${actionText.toLowerCase()} reason`}
/>,
!person.banned ? (
<CheckboxElement
key="removeData"
inputText="Remove Data"
value={removeData}
setValue={setRemoveData}
/>
) : null,
!person ? (
<ExpiryLengthElement
key="banExpires"
inputText="Ban Expires"
value={expires}
setValue={setExpires}
/>
) : null,
!person.banned ? (
<CheckboxElement
key="removeData"
inputText="Remove Data"
value={removeData}
setValue={setRemoveData}
/>
) : null,
]}
// disabled={banReason == ""}
buttonMessage={actionText}
Expand Down
32 changes: 23 additions & 9 deletions src/components/SiteHeader.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React from "react";

import { useDispatch, useSelector } from "react-redux";

import { useQueryClient } from "@tanstack/react-query";
import { useQueryClient, useIsFetching } from "@tanstack/react-query";

import { Toaster, toast } from "sonner";

Expand Down Expand Up @@ -35,8 +35,6 @@ import GitHubIcon from "@mui/icons-material/GitHub";
import FlagIcon from "@mui/icons-material/Flag";
import HowToRegIcon from "@mui/icons-material/HowToReg";

import { logoutCurrent, selectUsers } from "../reducers/accountReducer";

import { LemmyHttp } from "lemmy-js-client";

import { useLemmyHttp, refreshAllData } from "../hooks/useLemmyHttp";
Expand All @@ -47,7 +45,13 @@ import { BasicInfoTooltip } from "./Tooltip.jsx";

import { parseActorId } from "../utils.js";

import { addUser, setAccountIsLoading, setUsers, setCurrentUser } from "../reducers/accountReducer";
import {
setAccountIsLoading,
setCurrentUser,
logoutCurrent,
selectUsers,
selectAccountIsLoading,
} from "../reducers/accountReducer";

function SiteMenu() {
// const dispatch = useDispatch();
Expand Down Expand Up @@ -252,10 +256,10 @@ function UserMenu() {
const queryClient = useQueryClient();

const users = useSelector(selectUsers);
const accountIsLoading = useSelector(selectAccountIsLoading);

const { mutate: refreshMutate } = refreshAllData();

const [isLoading, setIsLoading] = React.useState(false);
const isFetching = useIsFetching();

const { baseUrl, siteData, localPerson, userRole } = getSiteData();

Expand Down Expand Up @@ -285,12 +289,15 @@ function UserMenu() {
userIcon = <SupervisedUserCircleIcon />;
}

const anythingLoading = accountIsLoading == true || isFetching == true;

const parsedActor = parseActorId(localPerson.actor_id);

return (
<>
<BasicInfoTooltip title="Reload all data" placement="bottom" variant="soft">
<IconButton
disabled={anythingLoading}
size="sm"
variant="outlined"
color="primary"
Expand All @@ -301,7 +308,16 @@ function UserMenu() {
refreshMutate();
}}
>
<CachedIcon />
{!anythingLoading && <CachedIcon />}
{anythingLoading && (
<CircularProgress
size="sm"
color="neutral"
sx={{
"--CircularProgress-size": "16px",
}}
/>
)}
</IconButton>
</BasicInfoTooltip>

Expand Down Expand Up @@ -341,8 +357,6 @@ function UserMenu() {
queryClient.invalidateQueries({ queryKey: ["lemmyHttp", localPerson.id] });
dispatch(logoutCurrent());

setIsLoading(true);

dispatch(setAccountIsLoading(true));

try {
Expand Down
6 changes: 5 additions & 1 deletion src/hooks/useLemmyHttp.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { getSiteData } from "../hooks/getSiteData";

import { LemmyHttp } from "lemmy-js-client";

import { selectCurrentUser, updateCurrentUserData } from "../reducers/accountReducer";
import { selectCurrentUser, updateCurrentUserData, setAccountIsLoading } from "../reducers/accountReducer";

export function useLemmyHttp(callLemmyMethod, formData) {
const currentUser = useSelector(selectCurrentUser);
Expand Down Expand Up @@ -82,6 +82,8 @@ export function refreshAllData() {

const mutation = useMutation({
mutationFn: async () => {
dispatch(setAccountIsLoading(true));

const lemmyClient = new LemmyHttp(`https://${currentUser.base}`);

const getSite = await lemmyClient.getSite({
Expand All @@ -91,6 +93,8 @@ export function refreshAllData() {
dispatch(updateCurrentUserData(getSite));

queryClient.invalidateQueries({ queryKey: ["lemmyHttp"] });

dispatch(setAccountIsLoading(false));
},
});

Expand Down
83 changes: 38 additions & 45 deletions src/reducers/accountReducer.js
Original file line number Diff line number Diff line change
@@ -1,50 +1,41 @@
export function addUser(base, jwt, site) {
return {
type: "addUser",
payload: { base, jwt, site },
};
}

export function setCurrentUser(base, jwt, site) {
return {
type: "setCurrentUser",
payload: { base, jwt, site },
};
}
export function updateCurrentUserData(site) {
return {
type: "updateCurrentUserData",
payload: { site },
};
}

export function logoutCurrent() {
return {
type: "logoutCurrent",
};
}

export function setAccountIsLoading(isLoading) {
return {
type: "setAccountIsLoading",
payload: { isLoading },
};
}

export function setUsers(users) {
return {
type: "setUsers",
payload: { users },
};
}

const lsUsers = localStorage.getItem("users");
const cUser = localStorage.getItem("currentUser");
export const setAccountIsLoading = (isLoading) => ({
type: "setAccountIsLoading",
payload: { isLoading },
});

export const addUser = (base, jwt, site) => ({
type: "addUser",
payload: { base, jwt, site },
});

export const setCurrentUser = (base, jwt, site) => ({
type: "setCurrentUser",
payload: { base, jwt, site },
});

export const updateCurrentUserData = (site) => ({
type: "updateCurrentUserData",
payload: { site },
});

export const logoutCurrent = () => ({
type: "logoutCurrent",
});

export const setUsers = (users) => ({
type: "setUsers",
payload: { users },
});

// both stored arrays include the full data for the current user, since we might not always have a saved user
const storedUsers = localStorage.getItem("users");
const storedCurrentUser = localStorage.getItem("currentUser");

const initialState = {
accountIsLoading: false,
users: lsUsers ? JSON.parse(lsUsers) : [], // { base, jwt, site }
currentUser: cUser ? JSON.parse(cUser) : null, // { base, jwt, site }

users: storedUsers ? JSON.parse(storedUsers) : [], // { base, jwt, site }
currentUser: storedCurrentUser ? JSON.parse(storedCurrentUser) : null, // { base, jwt, site }
};

const accountReducer = (state = initialState, action = {}) => {
Expand Down Expand Up @@ -126,6 +117,8 @@ const accountReducer = (state = initialState, action = {}) => {

export default accountReducer;

export const selectIsLoading = (state) => state.accountReducer.isLoading;
export const selectAccountIsLoading = (state) => state.accountReducer.accountIsLoading;

export const selectCurrentUser = (state) => state.accountReducer.currentUser;
export const selectUsers = (state) => state.accountReducer.users;
export const selectAccountIsLoading = (state) => state.accountReducer.accountIsLoading;