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

fix: API path for creating a DM channel via search #696

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
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export const AddChannelMembers = ({ presentingElement, isOpen, onDismiss, channe
const [searchText, setSearchText] = useState('')

const filteredUsers = useMemo(() => {
return users.users.filter(member => {
return users.enabledUsers.filter(member => {
return member.name.toLowerCase().includes(searchText.toLowerCase())
})
}, [users, searchText])
Expand Down
4 changes: 2 additions & 2 deletions mobile/src/components/features/chat-input/Tiptap.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ const ChannelMention = Mention.extend({
})
export const Tiptap = ({ onMessageSend, messageSending, defaultText = '' }: TiptapEditorProps) => {

const { users } = useContext(UserListContext)
const { enabledUsers } = useContext(UserListContext)

const { channels } = useContext(ChannelListContext) as ChannelListContextType

Expand Down Expand Up @@ -114,7 +114,7 @@ export const Tiptap = ({ onMessageSend, messageSending, defaultText = '' }: Tipt
},
suggestion: {
items: (query) => {
return users.filter((user) => user.full_name.toLowerCase().startsWith(query.query.toLowerCase()))
return enabledUsers.filter((user) => user.full_name.toLowerCase().startsWith(query.query.toLowerCase()))
.slice(0, 10);
},
// char: '@',
Expand Down
1 change: 1 addition & 0 deletions mobile/src/hooks/useGetUserRecords.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export const useGetUserRecords = () => {
full_name: user.full_name,
user_image: user.user_image ?? '',
first_name: user.first_name,
enabled: user.enabled
}
})
return usersMap
Expand Down
2 changes: 2 additions & 0 deletions mobile/src/types/Raven/RavenSettings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,6 @@ export interface RavenSettings{
idx?: number
/** Automatically add system users to Raven : Check */
auto_add_system_users?: 0 | 1
/** Show Raven on Desk : Check */
show_raven_on_desk?: 0 | 1
}
5 changes: 3 additions & 2 deletions mobile/src/types/Raven/RavenUser.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

export interface RavenUser{
export interface RavenUser {
creation: string
name: string
modified: string
Expand All @@ -17,5 +17,6 @@ export interface RavenUser{
/** First Name : Data */
first_name?: string
/** User Image : Attach Image */
user_image?: string
user_image?: string,
enabled: 0 | 1
}
22 changes: 13 additions & 9 deletions mobile/src/types/RavenMessaging/RavenMessage.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

export interface RavenMessage{
export interface RavenMessage {
creation: string
name: string
modified: string
Expand All @@ -16,8 +16,18 @@ export interface RavenMessage{
text?: string
/** JSON : JSON */
json?: any
/** Message Reactions : JSON */
message_reactions?: any
/** Is Reply : Check */
is_reply?: 0 | 1
/** Replied Message ID : Link - Raven Message */
linked_message?: string
/** Replied Message Details : JSON */
replied_message_details?: any
/** Message Type : Select */
message_type?: "Text" | "Image" | "File"
/** Content : Long Text */
content?: string
/** File : Attach */
file?: string
/** Image Width : Data */
Expand All @@ -30,16 +40,10 @@ export interface RavenMessage{
thumbnail_width?: string
/** Thumbnail Height : Data */
thumbnail_height?: string
/** Message Reactions : JSON */
message_reactions?: any
/** Is Reply : Check */
is_reply?: 0 | 1
/** Linked Message : Link - Raven Message */
linked_message?: string
/** Link Doctype : Link - DocType */
link_doctype?: string
/** Link Document : Dynamic Link */
link_document?: string
/** Content : Long Text */
content?: string
/** Is Edited : Check */
is_edited?: 0 | 1
}
22 changes: 15 additions & 7 deletions mobile/src/utils/users/UserListProvider.tsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
import { FrappeError, useFrappeDocTypeEventListener, useFrappeGetCall } from "frappe-react-sdk";
import { PropsWithChildren, createContext, useContext } from "react";
import { User } from "../../../../types/Core/User";
import { PropsWithChildren, createContext, useContext, useMemo } from "react";
import { UserContext } from "../auth/UserProvider";
import { ErrorBanner } from "@/components/layout";
import { IonButton, IonButtons, IonContent, IonFooter, IonHeader, IonPage, IonTitle, IonToolbar } from "@ionic/react";
import { FullPageLoader } from "@/components/layout/loaders";
import { RavenUser } from "@/types/Raven/RavenUser";


export const UserListContext = createContext<{ users: UserFields[] }>({
users: []
export const UserListContext = createContext<{ users: UserFields[], enabledUsers: UserFields[] }>({
users: [],
enabledUsers: []
})

export type UserFields = Pick<User, 'name' | 'full_name' | 'user_image' | 'first_name'>
export type UserFields = Pick<RavenUser, 'name' | 'full_name' | 'user_image' | 'first_name' | 'enabled'>

/** Hook to fetch a list of users */
export const useUserList = () => {
Expand All @@ -22,22 +23,29 @@ export const useUserList = () => {
export const UserListProvider = ({ children }: PropsWithChildren) => {

const { isLoggedIn } = useContext(UserContext)
const { data, error: usersError, isLoading, mutate } = useFrappeGetCall<{ message: UserFields[] }>('raven.api.raven_users.get_list', undefined, isLoggedIn ? undefined : null, {
const { data, error: usersError, isLoading, mutate } = useFrappeGetCall<{ message: UserFields[] }>('raven.api.raven_users.get_list', undefined, isLoggedIn ? 'raven.api.raven_users.get_list' : null, {
revalidateOnFocus: false,
revalidateOnReconnect: false,
errorRetryCount: 10
})

useFrappeDocTypeEventListener('User', () => mutate())

const { users, enabledUsers } = useMemo(() => {
return {
users: data?.message ?? [],
enabledUsers: data?.message?.filter(user => user.enabled === 1) ?? []
}
}, [data])

if (isLoading) {
return <FullPageLoader />
}
if (usersError) {
return <ErrorPage error={usersError} mutate={mutate} />
}

return <UserListContext.Provider value={{ users: data?.message ?? [] }}>
return <UserListContext.Provider value={{ users, enabledUsers }}>
{children}
</UserListContext.Provider>

Expand Down
4 changes: 2 additions & 2 deletions raven-app/src/components/feature/chat/ChatInput/Tiptap.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ export const ChannelMention = Mention.extend({
})
const Tiptap = ({ slotBefore, fileProps, onMessageSend, clearReplyMessage, placeholder = 'Type a message...', messageSending, sessionStorageKey = 'tiptap-editor', disableSessionStorage = false, defaultText = '' }: TiptapEditorProps) => {

const { users } = useContext(UserListContext)
const { enabledUsers } = useContext(UserListContext)

const { channels } = useContext(ChannelListContext) as ChannelListContextType

Expand Down Expand Up @@ -264,7 +264,7 @@ const Tiptap = ({ slotBefore, fileProps, onMessageSend, clearReplyMessage, place
},
suggestion: {
items: (query) => {
return users.filter((user) => user.full_name.toLowerCase().startsWith(query.query.toLowerCase()))
return enabledUsers.filter((user) => user.full_name.toLowerCase().startsWith(query.query.toLowerCase()))
.slice(0, 10);
},
// char: '@',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export const CommandPalette = ({ isOpen, onClose }: CommandPaletteProps) => {
const debouncedText = useDebounce(inputValue, 200)
const { currentUser } = useContext(UserContext)
const activeUsers = useContext(ActiveUsersContext)
const { call, reset } = useFrappePostCall<{ message: string }>("raven.raven_channel_management.doctype.raven_channel.raven_channel.create_direct_message_channel")
const { call, reset } = useFrappePostCall<{ message: string }>("raven.api.raven_channel.create_direct_message_channel")
let navigate = useNavigate()

const gotoDMChannel = async (user: string) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,16 @@ import { ErrorBanner } from "@/components/layout/AlertBanner"
import { TableLoader } from "@/components/layout/Loaders/TableLoader"
import { UsersTable } from "./UsersTable"
import { UserListContext } from "@/utils/users/UserListProvider"
import { Button, Dialog, Flex, Text, TextField } from "@radix-ui/themes"
import { Button, Dialog, Em, Flex, Strong, Text, TextField } from "@radix-ui/themes"
import { Loader } from "@/components/common/Loader"
import { BiSearch } from "react-icons/bi"
import { useToast } from "@/hooks/useToast"
import { ErrorCallout } from "@/components/layout/AlertBanner/ErrorBanner"

interface AddUsersResponse {
failed_users: User[],
success_users: User[]
}
const AddRavenUsersContent = ({ onClose }: { onClose: VoidFunction }) => {

const { mutate } = useSWRConfig()
Expand All @@ -25,7 +30,7 @@ const AddRavenUsersContent = ({ onClose }: { onClose: VoidFunction }) => {
setSearchText(event.target.value)
}

const filters: Filter[] = [['enabled', '=', 1], ['name', 'not in', ['Guest', 'Administrator']], ['user_type', '!=', 'Website User'], ['full_name', 'like', `%${debouncedText}%`]]
const filters: Filter[] = [['enabled', '=', 1], ['name', 'not in', ['Guest', 'Administrator']], ['full_name', 'like', `%${debouncedText}%`]]

const { start, count, selectedPageLength, setPageLength, nextPage, previousPage } = usePaginationWithDoctype("User", 10, filters)
const [sortOrder, setSortOder] = useState<"asc" | "desc">("desc")
Expand All @@ -42,25 +47,38 @@ const AddRavenUsersContent = ({ onClose }: { onClose: VoidFunction }) => {
})

const users = useContext(UserListContext)
const ravenUsersArray = users.users.map(user => user.name)
const ravenUsersArray = users.enabledUsers.map(user => user.name)

const [selected, setSelected] = useState<string[]>([])
const { loading, call, error: postError } = useFrappePostCall('raven.api.raven_users.add_users_to_raven')
const { loading, call, error: postError } = useFrappePostCall<{ message: AddUsersResponse }>('raven.api.raven_users.add_users_to_raven')
const { toast } = useToast()

const [failedUsers, setFailedUsers] = useState<User[]>([])

const handleAddUsers = async () => {
setFailedUsers([])
if (selected.length > 0) {

call({
users: JSON.stringify(selected)
}).then(() => {
toast({
title: `You have added ${selected.length} users to Raven`,
variant: 'success',
duration: 1000
})
onClose()
}).then((res) => {
if (res.message.success_users.length !== 0) {
toast({
title: `You have added ${res.message.success_users.length} users to Raven`,
variant: 'success',
duration: 1000
})
}

mutate('raven.api.raven_users.get_list')

if (res.message.failed_users.length === 0) {
onClose()
setSelected([])
} else {
setFailedUsers(res.message.failed_users)
setSelected(s => s.filter(user => res.message.failed_users.map(u => u.name).includes(user)))
}
})
}
}
Expand Down Expand Up @@ -102,6 +120,15 @@ const AddRavenUsersContent = ({ onClose }: { onClose: VoidFunction }) => {

<ErrorBanner error={error} />
<ErrorBanner error={postError} />
{failedUsers.length > 0 && <ErrorCallout>
Could not add the following users to Raven since they have a <Strong>Role Profile</Strong> attached.<br />
Please remove the role profile and try again.<br /><br />

<ol className="pl-4">
{failedUsers.map((user, i) => <li key={i}><Text as='span'><Strong>{user.full_name}</Strong> - {user.email}</Text></li>)}
</ol>

</ErrorCallout>}
{!data && !error && <TableLoader columns={3} />}

{data && data.length === 0 && debouncedText.length >= 2 &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const AddMembersDropdown = ({ channelMembers, label = 'Select users', selectedUs
const users = useContext(UserListContext)

//Options for dropdown
const nonChannelMembers = users.users?.filter((m: UserFields) => !channelMembers?.[m.name]) ?? []
const nonChannelMembers = users.enabledUsers?.filter((m: UserFields) => !channelMembers?.[m.name]) ?? []

/** Function to filter users */
function getFilteredUsers(selectedUsers: UserFields[], inputValue: string) {
Expand Down Expand Up @@ -170,7 +170,7 @@ const AddMembersDropdown = ({ channelMembers, label = 'Select users', selectedUs
</TextField.Root>
</div >
<ul
className={`absolute w-inherit bg-background rounded-b-md mt-1 shadow-md max-h-36 overflow-scroll p-0 z-50 ${!(isOpen && items.length) && 'hidden'
className={`absolute w-96 bg-background rounded-b-md mt-1 shadow-md max-h-96 overflow-scroll p-0 z-50 ${!(isOpen && items.length) && 'hidden'
}`}
{...getMenuProps()}
>
Expand Down
1 change: 1 addition & 0 deletions raven-app/src/hooks/useGetUserRecords.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export const useGetUserRecords = () => {
full_name: user.full_name,
user_image: user.user_image ?? '',
first_name: user.first_name,
enabled: user.enabled
}
})
return usersMap
Expand Down
2 changes: 1 addition & 1 deletion raven-app/src/pages/AddRavenUsersPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import { Loader } from '@/components/common/Loader'
const AddRavenUsersPage = () => {

const canAddRavenUsers = isSystemManager()
console.log('canAddRavenUsers', canAddRavenUsers)

return (
<Container>
<Flex align='center' justify='center' className='h-screen'>
Expand Down
2 changes: 2 additions & 0 deletions raven-app/src/types/Raven/RavenSettings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,6 @@ export interface RavenSettings{
idx?: number
/** Automatically add system users to Raven : Check */
auto_add_system_users?: 0 | 1
/** Show Raven on Desk : Check */
show_raven_on_desk?: 0 | 1
}
5 changes: 3 additions & 2 deletions raven-app/src/types/Raven/RavenUser.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

export interface RavenUser{
export interface RavenUser {
creation: string
name: string
modified: string
Expand All @@ -17,5 +17,6 @@ export interface RavenUser{
/** First Name : Data */
first_name?: string
/** User Image : Attach Image */
user_image?: string
user_image?: string,
enabled: 0 | 1
}
22 changes: 13 additions & 9 deletions raven-app/src/types/RavenMessaging/RavenMessage.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

export interface RavenMessage{
export interface RavenMessage {
creation: string
name: string
modified: string
Expand All @@ -16,8 +16,18 @@ export interface RavenMessage{
text?: string
/** JSON : JSON */
json?: any
/** Message Reactions : JSON */
message_reactions?: any
/** Is Reply : Check */
is_reply?: 0 | 1
/** Replied Message ID : Link - Raven Message */
linked_message?: string
/** Replied Message Details : JSON */
replied_message_details?: any
/** Message Type : Select */
message_type?: "Text" | "Image" | "File"
/** Content : Long Text */
content?: string
/** File : Attach */
file?: string
/** Image Width : Data */
Expand All @@ -30,16 +40,10 @@ export interface RavenMessage{
thumbnail_width?: string
/** Thumbnail Height : Data */
thumbnail_height?: string
/** Message Reactions : JSON */
message_reactions?: any
/** Is Reply : Check */
is_reply?: 0 | 1
/** Linked Message : Link - Raven Message */
linked_message?: string
/** Link Doctype : Link - DocType */
link_doctype?: string
/** Link Document : Dynamic Link */
link_document?: string
/** Content : Long Text */
content?: string
/** Is Edited : Check */
is_edited?: 0 | 1
}
2 changes: 1 addition & 1 deletion raven-app/src/utils/channel/ChannelListProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export const useFetchChannelList = (): ChannelListContextType => {
const { toast } = useToast()
const { data, mutate, ...rest } = useFrappeGetCall<{ message: ChannelList }>("raven.api.raven_channel.get_all_channels", {
hide_archived: false
}, undefined, {
}, `channel_list`, {
revalidateOnFocus: false,
revalidateIfStale: false,
onError: (error) => {
Expand Down
Loading
Loading