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

hackweek: use isLoading from useMutations for loading indicator in delete modal #3008

Merged
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
22 changes: 9 additions & 13 deletions ui/src/chat/ChatMessage/ChatMessageOptions.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import cn from 'classnames';
import React, {
useCallback,
useEffect,
useLayoutEffect,
useRef,
useMemo,
useState,
} from 'react';
import { useLocation, useNavigate, useParams } from 'react-router';
import { useSearchParams } from 'react-router-dom';
Expand All @@ -25,7 +25,6 @@ import CheckIcon from '@/components/icons/CheckIcon';
import EmojiPicker from '@/components/EmojiPicker';
import ConfirmationModal from '@/components/ConfirmationModal';
import ActionMenu, { Action } from '@/components/ActionMenu';
import useRequestState from '@/logic/useRequestState';
import { useIsMobile } from '@/logic/useMedia';
import useGroupPrivacy from '@/logic/useGroupPrivacy';
import { captureGroupsAnalyticsEvent } from '@/logic/analytics';
Expand All @@ -39,8 +38,7 @@ import {
import { emptyPost, Post } from '@/types/channel';
import VisibleIcon from '@/components/icons/VisibleIcon';
import HiddenIcon from '@/components/icons/HiddenIcon';
import { inlineSummary, inlineToString } from '@/logic/tiptap';
import { Inline } from '@/types/content';
import { inlineSummary } from '@/logic/tiptap';

function ChatMessageOptions(props: {
open: boolean;
Expand Down Expand Up @@ -76,11 +74,8 @@ function ChatMessageOptions(props: {
seal.id,
'delete'
);
const {
isPending: isDeletePending,
setPending: setDeletePending,
setReady,
} = useRequestState();
// TODO: replace this with isLoading from useMutation for deleting a DM later
const [isDeletePending, setIsDeletePending] = useState(false);
const { chShip, chName } = useParams();
const [, setSearchParams] = useSearchParams();
const { load: loadEmoji } = useEmoji();
Expand All @@ -94,7 +89,8 @@ function ChatMessageOptions(props: {
const canWrite = canWriteChannel(perms, vessel, group?.bloc);
const navigate = useNavigate();
const location = useLocation();
const { mutate: deleteChatMessage } = useDeletePostMutation();
const { mutate: deleteChatMessage, isLoading: isDeleteLoading } =
useDeletePostMutation();
const { mutate: addFeelToChat } = useAddPostReactMutation();
const isDMorMultiDM = useIsDmOrMultiDm(whom);
const {
Expand All @@ -118,7 +114,7 @@ function ChatMessageOptions(props: {
onOpenChange(false);
}

setDeletePending();
setIsDeletePending(true);

try {
if (isDMorMultiDM) {
Expand All @@ -132,7 +128,7 @@ function ChatMessageOptions(props: {
} catch (e) {
console.log('Failed to delete message', e);
}
setReady();
setIsDeletePending(false);
};

const onCopy = useCallback(() => {
Expand Down Expand Up @@ -461,7 +457,7 @@ function ChatMessageOptions(props: {
open={deleteOpen}
setOpen={setDeleteOpen}
confirmText="Delete"
loading={isDeletePending}
loading={isDeletePending || isDeleteLoading}
/>
</>
);
Expand Down
2 changes: 2 additions & 0 deletions ui/src/diary/DiaryNoteOptionsDropdown.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ export default function DiaryNoteOptionsDropdown({
didCopy,
onCopy,
delNote,
isDeleteLoading,
setIsOpen,
addToArrangedNotes,
removeFromArrangedNotes,
Expand Down Expand Up @@ -132,6 +133,7 @@ export default function DiaryNoteOptionsDropdown({
onConfirm={delNote}
open={deleteOpen}
setOpen={setDeleteOpen}
loading={isDeleteLoading}
confirmText="Delete"
/>
</>
Expand Down
4 changes: 3 additions & 1 deletion ui/src/diary/useDiaryActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ export default function useDiaryActions({ flag, time }: useDiaryActionsParams) {
const [isOpen, setIsOpen] = useState(false);
const arrangedNotes = useArrangedPosts(`diary/${flag}`);
const navigate = useNavigate();
const { mutate: deleteNote } = useDeletePostMutation();
const { mutate: deleteNote, isLoading: isDeleteLoading } =
useDeletePostMutation();
const { mutate: arrangedNotesMutation } = useArrangedPostsMutation();
const nest = `diary/${flag}`;
const { doCopy, didCopy } = useCopy(
Expand Down Expand Up @@ -93,6 +94,7 @@ export default function useDiaryActions({ flag, time }: useDiaryActionsParams) {
setIsOpen,
onCopy,
delNote,
isDeleteLoading,
addToArrangedNotes,
removeFromArrangedNotes,
moveUpInArrangedNotes,
Expand Down
5 changes: 2 additions & 3 deletions ui/src/heap/HeapBlock.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ function TopBar({
menuOpen,
setMenuOpen,
onDelete,
deleteStatus,
isDeleteLoading,
onEdit,
onCopy,
navigateToCurio,
Expand Down Expand Up @@ -183,8 +183,7 @@ function TopBar({
setOpen={setDeleteOpen}
onConfirm={onDelete}
closeOnClickOutside={true}
loading={deleteStatus === 'loading'}
succeeded={deleteStatus === 'success'}
loading={isDeleteLoading}
confirmText="Delete"
title="Delete Gallery Item"
message="Are you sure you want to delete this gallery item?"
Expand Down
4 changes: 2 additions & 2 deletions ui/src/heap/HeapRow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ function Actions({
menuOpen,
setMenuOpen,
onDelete,
deleteStatus,
isDeleteLoading,
onEdit,
onCopy,
navigateToCurio,
Expand Down Expand Up @@ -171,7 +171,7 @@ function Actions({
open={deleteOpen}
setOpen={setDeleteOpen}
onConfirm={onDelete}
loading={deleteStatus === 'loading'}
loading={isDeleteLoading}
confirmText="Delete"
title="Delete Gallery Item"
message="Are you sure you want to delete this gallery item?"
Expand Down
8 changes: 1 addition & 7 deletions ui/src/heap/useCurioActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,23 +31,17 @@ export default function useCurioActions({
const { isHidden, show, hide } = usePostToggler(time);

const [menuOpen, setMenuOpen] = useState(false);
const [deleteStatus, setDeleteStatus] = useState<Status>('idle');

const delMutation = useDeletePostMutation();

const onDelete = useCallback(async () => {
setMenuOpen(false);
setDeleteStatus('loading');
delMutation.mutate(
{ nest, time: decToUd(time) },
{
onSuccess: () => {
setDeleteStatus('success');
navigate(`/groups/${flag}/channels/heap/${chFlag}`);
},
onError: () => {
setDeleteStatus('error');
},
}
);
}, [chFlag, time, delMutation, flag, navigate, nest]);
Expand Down Expand Up @@ -83,7 +77,7 @@ export default function useCurioActions({
menuOpen,
setMenuOpen,
onDelete,
deleteStatus,
isDeleteLoading: delMutation.isLoading,
onEdit,
onCopy,
navigateToCurio,
Expand Down
27 changes: 14 additions & 13 deletions ui/src/replies/ReplyMessageOptions.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useCallback, useEffect, useMemo } from 'react';
import React, { useCallback, useEffect, useMemo, useState } from 'react';
import { useLocation, useNavigate, useParams } from 'react-router';
import { useSearchParams } from 'react-router-dom';
import { decToUd } from '@urbit/api';
Expand Down Expand Up @@ -65,11 +65,7 @@ export default function ReplyMessageOptions(props: {
seal.id,
'delete'
);
const {
isPending: isDeletePending,
setPending: setDeletePending,
setReady,
} = useRequestState();
// TODO: add delete mutation for parent DMs
const { chShip, chName } = useParams();
const isParent = threadParentId === seal.id;
const [, setSearchParams] = useSearchParams();
Expand All @@ -83,12 +79,20 @@ export default function ReplyMessageOptions(props: {
const canWrite = canWriteChannel(perms, vessel, group?.bloc);
const navigate = useNavigate();
const location = useLocation();
const { mutate: deleteReply } = useDeleteReplyMutation();
const { mutate: deleteChatMessage } = useDeletePostMutation();
const { mutate: deleteDMReply } = useDeleteDMReplyMutation();
const { mutate: deleteReply, isLoading: isDeleteReplyLoading } =
useDeleteReplyMutation();
const { mutate: deleteChatMessage, isLoading: isDeleteChatMessageLoading } =
useDeletePostMutation();
const { mutate: deleteDMReply, isLoading: isDeleteDMReplyLoading } =
useDeleteDMReplyMutation();
const { mutate: addFeelToChat } = useAddPostReactMutation();
const { mutate: addFeelToReply } = useAddReplyReactMutation();
const { mutate: addDMReplyFeel } = useAddDMReplyReactMutation();
const deleteLoading =
isDeleteReplyLoading ||
isDeleteChatMessageLoading ||
isDeleteDMReplyLoading;

const {
show: showPost,
hide: hidePost,
Expand All @@ -109,8 +113,6 @@ export default function ReplyMessageOptions(props: {
onOpenChange(false);
}

setDeletePending();

try {
if (isDMorMultiDM) {
deleteDMReply({
Expand All @@ -134,7 +136,6 @@ export default function ReplyMessageOptions(props: {
} catch (e) {
console.log('Failed to delete message', e);
}
setReady();
};

const onCopy = useCallback(() => {
Expand Down Expand Up @@ -402,7 +403,7 @@ export default function ReplyMessageOptions(props: {
open={deleteOpen}
setOpen={setDeleteOpen}
confirmText="Delete"
loading={isDeletePending}
loading={deleteLoading}
/>
</>
);
Expand Down
Loading