Snooze
This commit is contained in:
parent
e7bd56f959
commit
ba2ffd1501
|
@ -7,12 +7,13 @@ import List, { ListItem } from 'soapbox/components/list';
|
||||||
import { Avatar, Divider, HStack, Icon, Stack, Text, Toggle } from 'soapbox/components/ui';
|
import { Avatar, Divider, HStack, Icon, Stack, Text, Toggle } from 'soapbox/components/ui';
|
||||||
import { useChatContext } from 'soapbox/contexts/chat-context';
|
import { useChatContext } from 'soapbox/contexts/chat-context';
|
||||||
import { useAppDispatch } from 'soapbox/hooks';
|
import { useAppDispatch } from 'soapbox/hooks';
|
||||||
import { useChat } from 'soapbox/queries/chats';
|
import { useChat, useChatSnoozes } from 'soapbox/queries/chats';
|
||||||
|
|
||||||
import ChatPaneHeader from './chat-pane-header';
|
import ChatPaneHeader from './chat-pane-header';
|
||||||
|
|
||||||
const ChatSettings = () => {
|
const ChatSettings = () => {
|
||||||
const dispatch = useAppDispatch();
|
const dispatch = useAppDispatch();
|
||||||
|
const { isSnoozed, handleSnooze } = useChatSnoozes();
|
||||||
|
|
||||||
const { chat, setEditing, toggleChatPane } = useChatContext();
|
const { chat, setEditing, toggleChatPane } = useChatContext();
|
||||||
const { deleteChat } = useChat(chat?.id as string);
|
const { deleteChat } = useChat(chat?.id as string);
|
||||||
|
@ -89,7 +90,7 @@ const ChatSettings = () => {
|
||||||
|
|
||||||
<List>
|
<List>
|
||||||
<ListItem label='Snooze notifications'>
|
<ListItem label='Snooze notifications'>
|
||||||
<Toggle />
|
<Toggle checked={isSnoozed} onChange={handleSnooze} />
|
||||||
</ListItem>
|
</ListItem>
|
||||||
</List>
|
</List>
|
||||||
|
|
||||||
|
|
|
@ -1,7 +1,9 @@
|
||||||
import { useInfiniteQuery, useMutation, useQuery } from '@tanstack/react-query';
|
import { useInfiniteQuery, useMutation, useQuery } from '@tanstack/react-query';
|
||||||
|
import { useEffect, useState } from 'react';
|
||||||
|
|
||||||
|
import snackbar from 'soapbox/actions/snackbar';
|
||||||
import { useChatContext } from 'soapbox/contexts/chat-context';
|
import { useChatContext } from 'soapbox/contexts/chat-context';
|
||||||
import { useApi } from 'soapbox/hooks';
|
import { useApi, useAppDispatch } from 'soapbox/hooks';
|
||||||
|
|
||||||
import { queryClient } from './client';
|
import { queryClient } from './client';
|
||||||
|
|
||||||
|
@ -123,4 +125,64 @@ const useChat = (chatId: string) => {
|
||||||
return { createChatMessage, markChatAsRead, deleteChatMessage, acceptChat, deleteChat };
|
return { createChatMessage, markChatAsRead, deleteChatMessage, acceptChat, deleteChat };
|
||||||
};
|
};
|
||||||
|
|
||||||
export { useChat, useChats, useChatMessages };
|
const useChatSnoozes = () => {
|
||||||
|
const api = useApi();
|
||||||
|
const dispatch = useAppDispatch();
|
||||||
|
|
||||||
|
const { chat } = useChatContext();
|
||||||
|
const [isSnoozed, setSnoozed] = useState<boolean>(false);
|
||||||
|
|
||||||
|
const getChatSnoozes = async() => {
|
||||||
|
const { data } = await api.get(`api/v1/pleroma/chats/snooze?account_id=${chat?.account.id}`);
|
||||||
|
return data;
|
||||||
|
};
|
||||||
|
|
||||||
|
const fetchChatSnooze = async() => {
|
||||||
|
const data = await getChatSnoozes();
|
||||||
|
if (data) {
|
||||||
|
setSnoozed(true);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const handleSnooze = () => {
|
||||||
|
if (isSnoozed) {
|
||||||
|
deleteSnooze();
|
||||||
|
} else {
|
||||||
|
createSnooze();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const createSnooze = () => {
|
||||||
|
setSnoozed(true);
|
||||||
|
|
||||||
|
api.post(`api/v1/pleroma/chats/snooze?account_id=${chat?.account.id}`)
|
||||||
|
.then(() => {
|
||||||
|
dispatch(snackbar.success('Successfully snoozed this chat.'));
|
||||||
|
})
|
||||||
|
.catch(() => {
|
||||||
|
dispatch(snackbar.error('Something went wrong trying to snooze this chat. Please try again.'));
|
||||||
|
setSnoozed(false);
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
const deleteSnooze = () => {
|
||||||
|
setSnoozed(false);
|
||||||
|
|
||||||
|
api.delete(`api/v1/pleroma/chats/snooze?account_id=${chat?.account.id}`)
|
||||||
|
.then(() => {
|
||||||
|
dispatch(snackbar.success('Successfully unsnoozed this chat.'));
|
||||||
|
})
|
||||||
|
.catch(() => {
|
||||||
|
dispatch(snackbar.error('Something went wrong trying to unsnooze this chat. Please try again.'));
|
||||||
|
setSnoozed(true);
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
fetchChatSnooze();
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
return { isSnoozed, handleSnooze };
|
||||||
|
};
|
||||||
|
|
||||||
|
export { useChat, useChats, useChatMessages, useChatSnoozes };
|
||||||
|
|
Loading…
Reference in New Issue