Skip to content

Commit

Permalink
Merge pull request #1069 from The-Commit-Company/ability-to-delete-th…
Browse files Browse the repository at this point in the history
…read

feat: ability to delete thread for channel admin
  • Loading branch information
nikkothari22 authored Oct 1, 2024
2 parents e76878d + 2f8af2e commit 34d0a50
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import { useNavigate, useParams } from "react-router-dom"
import { DropdownMenu, Flex, Heading, IconButton } from "@radix-ui/themes"
import { BiBell, BiBellOff, BiDotsVerticalRounded, BiExit } from "react-icons/bi"
import { useFrappePostCall, useSWRConfig } from "frappe-react-sdk"
import { BiBell, BiBellOff, BiDotsVerticalRounded, BiExit, BiTrash } from "react-icons/bi"
import { useFrappeDeleteDoc, useFrappePostCall, useSWRConfig } from "frappe-react-sdk"
import { toast } from "sonner"
import { getErrorMessage } from "@/components/layout/AlertBanner/ErrorBanner"
import { AiOutlineClose } from "react-icons/ai"
import { useUserData } from "@/hooks/useUserData"
import useFetchChannelMembers, { Member } from "@/hooks/fetchers/useFetchChannelMembers"
import { useMemo } from "react"
import { useContext, useMemo } from "react"
import useIsPushNotificationEnabled from "@/hooks/fetchers/useIsPushNotificationEnabled"
import { UserContext } from "@/utils/auth/UserProvider"

export const ThreadHeader = () => {

Expand All @@ -18,6 +19,7 @@ export const ThreadHeader = () => {

const { name: user } = useUserData()
const { channelMembers } = useFetchChannelMembers(threadID ?? '')
const { currentUser } = useContext(UserContext)

const channelMember = useMemo(() => {
if (user && channelMembers) {
Expand All @@ -26,7 +28,6 @@ export const ThreadHeader = () => {
return null
}, [user, channelMembers])


return (
<header className='dark:bg-gray-2 bg-white fixed top-0 px-3 sm:w-[calc((100vw-var(--sidebar-width)-var(--space-8))/2)] w-screen' style={{ zIndex: 999 }}>
<Flex direction={'column'} gap='2' className='pt-3'>
Expand All @@ -43,6 +44,7 @@ export const ThreadHeader = () => {
<DropdownMenu.Content className='min-w-48'>
<ToggleNotificationButton channelMember={channelMember} />
<LeaveThreadButton />
{channelMembers[currentUser].is_admin === 1 && <DeleteThreadButton />}
</DropdownMenu.Content>
</DropdownMenu.Root>
}
Expand All @@ -63,6 +65,38 @@ export const ThreadHeader = () => {
}


const DeleteThreadButton = () => {

const { threadID } = useParams()
const navigate = useNavigate()

const { deleteDoc } = useFrappeDeleteDoc()

const onDeleteThread = () => {

const promise = deleteDoc('Raven Channel', threadID)
.then(() => {
navigate('../')
return Promise.resolve()
})

toast.promise(promise, {
success: 'You have deleted the thread',
error: (e) => `Could not delete thread - ${getErrorMessage(e)}`
})
}

return (
<DropdownMenu.Item color="red" onClick={onDeleteThread}>
<Flex gap='2' align='center'>
<BiTrash size={'16'} />
Delete Thread
</Flex>
</DropdownMenu.Item>
)
}


const LeaveThreadButton = () => {

const { threadID } = useParams()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,25 @@ def on_trash(self):
# Delete the pinned channels
frappe.db.delete("Raven Pinned Channels", {"channel_id": self.name})

# If the channel was a thread, (i.e. a message exists with the same name), remove the 'is_thread' flag from the message
if self.is_thread and frappe.db.exists("Raven Message", {"name": self.name}):
message_channel_id = frappe.get_cached_value("Raven Message", self.name, "channel_id")
frappe.db.set_value("Raven Message", self.name, "is_thread", 0)
# Update the message which used to be a thread
frappe.publish_realtime(
"message_edited",
{
"channel_id": message_channel_id,
"sender": frappe.session.user,
"message_id": self.name,
"message_details": {
"is_thread": 0,
},
},
doctype="Raven Channel",
docname=message_channel_id
)

def after_insert(self):
"""
After inserting a channel, we need to check if it is a direct message channel or not.
Expand Down

0 comments on commit 34d0a50

Please sign in to comment.