Skip to content

Commit

Permalink
(chore) Refactor markdown modal code (#1562)
Browse files Browse the repository at this point in the history
  • Loading branch information
aeharding authored Aug 9, 2024
1 parent b92ab8c commit d73cfaf
Show file tree
Hide file tree
Showing 20 changed files with 386 additions and 274 deletions.
6 changes: 3 additions & 3 deletions src/features/auth/AccountSwitcher.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,15 @@ type AccountSwitcherProps = {
}
);

export default function AccountSwitcherContainer(props: AccountSwitcherProps) {
export default function AccountSwitcher(props: AccountSwitcherProps) {
return (
<ListEditorProvider>
<AccountSwitcher {...props} />
<AccountSwitcherContents {...props} />
</ListEditorProvider>
);
}

function AccountSwitcher({
function AccountSwitcherContents({
onDismiss,
onSelectAccount,
allowEdit = true,
Expand Down
129 changes: 74 additions & 55 deletions src/features/auth/PageContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,8 @@ import React, {
useRef,
useState,
} from "react";
import { CommentReplyItem } from "../comment/compose/reply/CommentReply";
import { useAppDispatch, useAppSelector } from "../../store";
import { changeAccount } from "../auth/authSlice";
import CommentReplyModal from "../comment/compose/reply/CommentReplyModal";
import {
Comment,
CommentView,
Expand All @@ -20,7 +18,6 @@ import {
PostView,
PrivateMessageView,
} from "lemmy-js-client";
import CommentEditModal from "../comment/compose/edit/CommentEditModal";
import { Report, ReportHandle, ReportableItem } from "../report/Report";
import PostEditorModal from "../post/new/PostEditorModal";
import SelectTextModal from "../shared/SelectTextModal";
Expand All @@ -32,6 +29,11 @@ import { jwtSelector } from "./authSelectors";
import BanUserModal from "../moderation/ban/BanUserModal";
import CreateCrosspostDialog from "../post/crosspost/create/CreateCrosspostDialog";
import LoginModal from "./login/LoginModal";
import GenericMarkdownEditorModal, {
MarkdownEditorData,
} from "../shared/markdown/editing/modal/GenericMarkdownEditorModal";
import { NewPrivateMessage } from "../shared/markdown/editing/modal/contents/PrivateMessagePage";
import { CommentReplyItem } from "../shared/markdown/editing/modal/contents/CommentReplyPage";

export interface BanUserPayload {
user: Person;
Expand All @@ -47,18 +49,24 @@ interface IPageContext {
*/
presentLoginIfNeeded: () => boolean;

/**
* @returns private message payload if submitted
*/
presentPrivateMessageCompose: (
item: NewPrivateMessage,
) => Promise<PrivateMessageView | undefined>;

/**
* @returns comment payload if replied
*/
presentCommentReply: (
item: CommentReplyItem,
) => Promise<CommentView | PrivateMessageView | undefined>;
presentCommentEdit: (item: Comment) => Promise<CommentView | undefined>;

/**
* Will mutate comment in store, which view should be linked to for updates
* That's why this does not return anything
* @returns comment payload if replied
*/
presentCommentEdit: (item: Comment) => void;
presentCommentReply: (
item: CommentReplyItem,
) => Promise<CommentView | undefined>;

presentReport: (item: ReportableItem) => void;

Expand Down Expand Up @@ -86,8 +94,9 @@ interface IPageContext {
export const PageContext = createContext<IPageContext>({
pageRef: undefined,
presentLoginIfNeeded: () => false,
presentCommentEdit: async () => undefined,
presentCommentReply: async () => undefined,
presentCommentEdit: () => false,
presentPrivateMessageCompose: async () => undefined,
presentReport: () => {},
presentPostEditor: () => {},
presentSelectText: () => {},
Expand Down Expand Up @@ -148,41 +157,58 @@ export function PageContextProvider({ value, children }: PageContextProvider) {
[presentShareAsImageModal],
);

// Comment reply start
const commentReplyItem = useRef<CommentReplyItem>();
const commentReplyCb = useRef<
| ((replied: CommentView | PrivateMessageView | undefined) => void)
| undefined
>();
const [isReplyOpen, setIsReplyOpen] = useState(false);
const presentCommentReply = useCallback((item: CommentReplyItem) => {
const promise = new Promise<CommentView | PrivateMessageView | undefined>(
(resolve) => (commentReplyCb.current = resolve),
);

commentReplyItem.current = item;
setIsReplyOpen(true);

return promise;
}, []);
// Markdown editor start
const markdownEditorData = useRef<MarkdownEditorData>();
const [isMarkdownEditorOpen, setIsMarkdownEditorOpen] = useState(false);
const presentMarkdownEditor = useCallback(
<T extends MarkdownEditorData>(data: Omit<T, "onSubmit">) =>
new Promise<Parameters<T["onSubmit"]>[0]>((resolve) => {
markdownEditorData.current = {
...data,
onSubmit: resolve,
} as T;
setIsMarkdownEditorOpen(true);
}),
[],
);

useEffect(() => {
if (isReplyOpen) return;
if (isMarkdownEditorOpen) return;

commentReplyCb.current?.(undefined);
commentReplyCb.current = undefined;
markdownEditorData.current?.onSubmit(undefined);
markdownEditorData.current = undefined;
return;
}, [isReplyOpen]);
// Comment reply end

// Edit comment start
const commentEditItem = useRef<Comment>();
const [isEditCommentOpen, setIsEditCommentOpen] = useState(false);
const presentCommentEdit = useCallback((item: Comment) => {
commentEditItem.current = item;
setIsEditCommentOpen(true);
}, []);
// Edit comment end
}, [isMarkdownEditorOpen]);

const presentPrivateMessageCompose = useCallback<
IPageContext["presentPrivateMessageCompose"]
>(
(item) =>
presentMarkdownEditor({
type: "PRIVATE_MESSAGE",
item,
}) as ReturnType<IPageContext["presentPrivateMessageCompose"]>,
[presentMarkdownEditor],
);

const presentCommentEdit = useCallback<IPageContext["presentCommentEdit"]>(
(item) =>
presentMarkdownEditor({
type: "COMMENT_EDIT",
item,
}) as ReturnType<IPageContext["presentCommentEdit"]>,
[presentMarkdownEditor],
);

const presentCommentReply = useCallback<IPageContext["presentCommentReply"]>(
(item) =>
presentMarkdownEditor({
type: "COMMENT_REPLY",
item,
}) as ReturnType<IPageContext["presentCommentReply"]>,
[presentMarkdownEditor],
);
// Markdown editor end

// Edit/new post start
const postItem = useRef<PostView | string>();
Expand Down Expand Up @@ -257,8 +283,9 @@ export function PageContextProvider({ value, children }: PageContextProvider) {
() => ({
...value,
presentLoginIfNeeded,
presentCommentReply,
presentPrivateMessageCompose,
presentCommentEdit,
presentCommentReply,
presentReport,
presentPostEditor,
presentSelectText,
Expand All @@ -268,6 +295,7 @@ export function PageContextProvider({ value, children }: PageContextProvider) {
presentCreateCrosspost,
}),
[
presentPrivateMessageCompose,
presentCommentEdit,
presentCommentReply,
presentLoginIfNeeded,
Expand All @@ -287,19 +315,10 @@ export function PageContextProvider({ value, children }: PageContextProvider) {
{children}

<LoginModal isOpen={isLoginOpen} setIsOpen={setIsLoginOpen} />
<CommentReplyModal
item={commentReplyItem.current!}
isOpen={isReplyOpen}
setIsOpen={setIsReplyOpen}
onReply={(reply) => {
commentReplyCb.current?.(reply);
commentReplyCb.current = undefined;
}}
/>
<CommentEditModal
item={commentEditItem.current!}
isOpen={isEditCommentOpen}
setIsOpen={setIsEditCommentOpen}
<GenericMarkdownEditorModal
{...markdownEditorData.current!}
isOpen={isMarkdownEditorOpen}
setIsOpen={setIsMarkdownEditorOpen}
/>
<Report ref={reportRef} />
<PostEditorModal
Expand Down
4 changes: 4 additions & 0 deletions src/features/auth/login/LoginNav.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ export const Centered = styled.div`
.ios & {
justify-content: center;
}
ion-spinner {
flex-shrink: 0;
}
`;

function blurDocument() {
Expand Down
2 changes: 2 additions & 0 deletions src/features/comment/commentSlice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,8 @@ export const editComment =
});

dispatch(mutatedComment(response.comment_view));

return response.comment_view;
};

export const modRemoveComment =
Expand Down
32 changes: 0 additions & 32 deletions src/features/comment/compose/edit/CommentEditModal.tsx

This file was deleted.

33 changes: 0 additions & 33 deletions src/features/comment/compose/reply/CommentReplyModal.tsx

This file was deleted.

3 changes: 1 addition & 2 deletions src/features/comment/useCommentActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -239,8 +239,7 @@ export default function useCommentActions({

const reply = await presentCommentReply(commentView);

if (reply && !("private_message" in reply))
prependComments([reply]);
if (reply) prependComments([reply]);
})();
},
},
Expand Down
6 changes: 3 additions & 3 deletions src/features/inbox/PrivateMessageMoreActions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export default forwardRef<
>(function PrivateMessageMoreActions({ item, markReadAction }, ref) {
const dispatch = useAppDispatch();
const [presentActionSheet] = useIonActionSheet();
const { presentReport, presentSelectText, presentCommentReply } =
const { presentReport, presentSelectText, presentPrivateMessageCompose } =
useContext(PageContext);

const { navigateToUser } = useAppNavigation();
Expand All @@ -58,7 +58,7 @@ export default forwardRef<
icon: arrowUndoOutline,
handler: () => {
(async () => {
await presentCommentReply({
await presentPrivateMessageCompose({
private_message: {
recipient:
item.private_message.creator_id ===
Expand Down Expand Up @@ -113,7 +113,7 @@ export default forwardRef<
markReadAction,
item,
isBlocked,
presentCommentReply,
presentPrivateMessageCompose,
dispatch,
presentSelectText,
navigateToUser,
Expand Down
5 changes: 2 additions & 3 deletions src/features/inbox/SendMessageBox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ export default function SendMessageBox({
const [value, setValue] = useState("");
const client = useClient();
const presentToast = useAppToast();
const { presentCommentReply } = useContext(PageContext);
const { presentPrivateMessageCompose } = useContext(PageContext);

const send = useCallback(async () => {
setLoading(true);
Expand Down Expand Up @@ -132,13 +132,12 @@ export default function SendMessageBox({
shape="round"
fill="clear"
onClick={async () => {
const message = await presentCommentReply({
const message = await presentPrivateMessageCompose({
private_message: { recipient },
value,
});

if (!message) return;
if (!("private_message" in message)) return;

scrollToBottom?.();
setValue("");
Expand Down
3 changes: 1 addition & 2 deletions src/features/post/detail/PostHeader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -294,8 +294,7 @@ function PostHeader({

const reply = await presentCommentReply(post);

if (reply && !("private_message" in reply))
onPrependComment?.(reply);
if (reply) onPrependComment?.(reply);
}}
/>
</BorderlessIonItem>
Expand Down
Loading

0 comments on commit d73cfaf

Please sign in to comment.