-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b56b648
commit 2173291
Showing
9 changed files
with
417 additions
and
110 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
import { useState, useEffect, useContext } from "react"; | ||
import { fetchUserDmChats, fetchUserchannelsbyid } from "../../database"; | ||
import totalsearchCSS from "./totalsearch.module.css"; | ||
import { Allconvers } from "../../context api/context"; | ||
import { Channelcontext } from "../../context api/channelcontext.jsx"; | ||
import { Chatcontext } from "../../context api/chatcontext"; | ||
|
||
const Totalsearch = () => { | ||
const { currentUser, setDm, setChannelchat, setConformdm, setchat } = | ||
useContext(Allconvers); | ||
const { dispatch } = useContext(Chatcontext); | ||
const { dispatchchannel } = useContext(Channelcontext); | ||
const [dmcontacts, setdmcontacts] = useState([]); | ||
const [channels, setchannels] = useState([]); | ||
const [search, setSearch] = useState(""); | ||
const [searchResults, setSearchResults] = useState([]); | ||
|
||
useEffect(() => { | ||
const fetchdata = async () => { | ||
const fetcheddm = await fetchUserDmChats(currentUser[0]); | ||
const filteredDm = fetcheddm.filter((contact) => contact.showstatus); | ||
setdmcontacts(filteredDm); | ||
const fetchedchannel = await fetchUserchannelsbyid(currentUser[0].id); | ||
setchannels(fetchedchannel); | ||
}; | ||
fetchdata(); | ||
}, [currentUser]); | ||
|
||
useEffect(() => { | ||
if (search.trim() === "") { | ||
setSearchResults([]); | ||
return; | ||
} | ||
|
||
const filteredDmContacts = dmcontacts.filter((contact) => | ||
contact.userinfo.uusername.toLowerCase().includes(search.toLowerCase()) | ||
); | ||
|
||
const filteredChannels = channels.filter((channel) => | ||
channel.channelname.toLowerCase().includes(search.toLowerCase()) | ||
); | ||
|
||
setSearchResults([...filteredDmContacts, ...filteredChannels]); | ||
}, [dmcontacts, channels, search]); | ||
|
||
const handleInput = (e) => { | ||
setSearch(e.target.value); | ||
}; | ||
|
||
const handleChannelSelect = (channel) => { | ||
dispatchchannel({ type: "Change_channel", payload: channel }); | ||
resetSearch(); | ||
setDm(false); | ||
setChannelchat(true); | ||
setConformdm(false); | ||
setchat(false); | ||
}; | ||
|
||
const handleDMSelect = (userinfo) => { | ||
dispatch({ type: "Change_user", payload: userinfo }); | ||
resetSearch(); | ||
setDm(false); | ||
setChannelchat(false); | ||
setConformdm(false); | ||
setchat(true); | ||
}; | ||
|
||
const resetSearch = () => { | ||
setSearch(""); | ||
setSearchResults([]); | ||
}; | ||
|
||
const handleBlur = (e) => { | ||
// Check if the relatedTarget (where focus is going) is not inside results | ||
if ( | ||
e.relatedTarget && | ||
!e.currentTarget.contains(e.relatedTarget) && | ||
e.relatedTarget.className !== totalsearchCSS.resultItem | ||
) { | ||
setShowResults(false); | ||
} | ||
}; | ||
|
||
return ( | ||
<div className={totalsearchCSS.container}> | ||
<input | ||
type="text" | ||
value={search} | ||
onChange={handleInput} | ||
onClick={() => setSearchResults([])} | ||
onBlur={handleBlur} | ||
placeholder="Search a Channel or Contact...." | ||
className={totalsearchCSS.input} | ||
/> | ||
{search.trim() !== "" && ( | ||
<div className={totalsearchCSS.results}> | ||
{searchResults.map((result, index) => ( | ||
<div | ||
key={index} | ||
className={totalsearchCSS.resultItem} | ||
onClick={() => { | ||
if (result.userinfo) { | ||
handleDMSelect(result.userinfo); | ||
} else { | ||
handleChannelSelect(result); | ||
} | ||
}} | ||
> | ||
{result.userinfo ? ( | ||
<div | ||
className={`${totalsearchCSS.dmContact} ${totalsearchCSS.item}`} | ||
> | ||
<p>DM Contact: {result.userinfo.uusername}</p> | ||
<p>Email: {result.userinfo.uemail}</p> | ||
</div> | ||
) : ( | ||
<div | ||
className={`${totalsearchCSS.channel} ${totalsearchCSS.item}`} | ||
> | ||
<p>Channel: {result.channelname}</p> | ||
<p>Created by: {result.channelinfo.createdby}</p> | ||
</div> | ||
)} | ||
</div> | ||
))} | ||
</div> | ||
)} | ||
</div> | ||
); | ||
}; | ||
|
||
export default Totalsearch; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
* { | ||
box-sizing: border-box; | ||
} | ||
|
||
.container { | ||
width: 100%; | ||
max-width: 600px; | ||
height: 100%; | ||
display: flex; | ||
align-items: center; | ||
justify-content: center; | ||
position: relative; /* Ensure relative positioning for absolute children */ | ||
} | ||
|
||
.input { | ||
width: 100%; | ||
padding: 10px; | ||
font-size: 15px; | ||
height: 85%; | ||
border-radius: 5px; | ||
} | ||
|
||
.results { | ||
width: 100%; | ||
display: grid; | ||
gap: 1px; | ||
position: absolute; | ||
z-index: 2; | ||
top: calc(100% + 10px); /* Adjust top position to create a gap below the input */ | ||
background-color: #fff; /* Ensure a white background for results */ | ||
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1); /* Add shadow for a card-like appearance */ | ||
border-radius: 4px; /* Rounded corners for the results box */ | ||
max-height: 300px; /* Limit max height if needed */ | ||
overflow-y: auto; /* Enable scrolling if results exceed max height */ | ||
} | ||
|
||
.resultItem { | ||
padding: 10px; | ||
border: 1px solid #ccc; | ||
background-color: #f9f9f9; | ||
cursor: pointer; /* Add pointer cursor for clickable items */ | ||
} | ||
|
||
.resultItem:hover { | ||
background-color: #ebebeb; /* Lighten background on hover */ | ||
} | ||
|
||
.item { | ||
padding: 10px; | ||
} | ||
|
||
.dmContact { | ||
border-left: 5px solid #1a73e8; /* Blue color for DM Contacts */ | ||
} | ||
|
||
.channel { | ||
border-left: 5px solid #34a853; /* Green color for Channels */ | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.