Skip to content

armgit / fider

Public Imported from GitHub
0

Open platform to collect and prioritize feedback

A UI to allow you to select a person to mention

Matt Roberts636ec4057f9d
diff --git a/public/components/common/form/TextArea.tsx b/public/components/common/form/TextArea.tsx
index a4675c26a..4c6df2525 100644
--- a/public/components/common/form/TextArea.tsx
+++ b/public/components/common/form/TextArea.tsx
@@ -14,7 +14,8 @@ interface TextAreaProps {
minRows?: number
placeholder?: string
afterLabel?: JSX.Element
- onChange?: (value: string) => void
+ onChange?: (value: string, selectionStart?: number) => void
+ onKeyDown?: (e: React.KeyboardEvent<HTMLTextAreaElement>) => void
inputRef?: React.MutableRefObject<any>
onFocus?: React.FocusEventHandler<HTMLTextAreaElement>
className?: string
@@ -23,7 +24,13 @@ interface TextAreaProps {
export const TextArea: React.FunctionComponent<TextAreaProps> = (props) => {
const onChange = (e: React.FormEvent<HTMLTextAreaElement>) => {
if (props.onChange) {
- props.onChange(e.currentTarget.value)
+ props.onChange(e.currentTarget.value, e.currentTarget.selectionStart)
+ }
+ }
+
+ const onKeyDown = (e: React.KeyboardEvent<HTMLTextAreaElement>) => {
+ if (props.onKeyDown) {
+ props.onKeyDown(e)
}
}
@@ -47,6 +54,7 @@ export const TextArea: React.FunctionComponent<TextAreaProps> = (props) => {
id={`input-${props.field}`}
disabled={props.disabled}
onChange={onChange}
+ onKeyDown={onKeyDown}
value={props.value}
minRows={props.minRows || 3}
placeholder={props.placeholder}
diff --git a/public/pages/ShowPost/components/CommentInput.tsx b/public/pages/ShowPost/components/CommentInput.tsx
index e3ffb3e19..bc3911d69 100644
--- a/public/pages/ShowPost/components/CommentInput.tsx
+++ b/public/pages/ShowPost/components/CommentInput.tsx
@@ -3,11 +3,13 @@ import React, { useState, useRef } from "react"
import { Post, ImageUpload } from "@fider/models"
import { Avatar, UserName, Button, TextArea, Form, MultiImageUploader } from "@fider/components"
import { SignInModal } from "@fider/components"
+import { User, UserRole, UserStatus } from "@fider/models/identity"
import { cache, actions, Failure, Fider } from "@fider/services"
import { useFider } from "@fider/hooks"
import { HStack } from "@fider/components/layout"
import { t, Trans } from "@lingui/macro"
+import MentionSelector from "./MentionSelector"
interface CommentInputProps {
post: Post
@@ -15,6 +17,12 @@ interface CommentInputProps {
const CACHE_TITLE_KEY = "CommentInput-Comment-"
+const users: User[] = [
+ { id: 1, name: "Matt", role: UserRole.Administrator, avatarURL: "", status: UserStatus.Active },
+ { id: 2, name: "Matt", role: UserRole.Administrator, avatarURL: "", status: UserStatus.Active },
+ { id: 3, name: "Matt", role: UserRole.Administrator, avatarURL: "", status: UserStatus.Active },
+]
+
export const CommentInput = (props: CommentInputProps) => {
const getCacheKey = () => `${CACHE_TITLE_KEY}${props.post.id}`
@@ -25,11 +33,42 @@ export const CommentInput = (props: CommentInputProps) => {
const [attachments, setAttachments] = useState<ImageUpload[]>([])
const [error, setError] = useState<Failure | undefined>(undefined)
- const commentChanged = (newContent: string) => {
+ const [dropdownVisible, setDropdownVisible] = useState<boolean>(false)
+ const [filteredUsers, setFilteredUsers] = useState<User[]>([])
+ const [mentionIndex, setMentionIndex] = useState<number | null>(null)
+ const [cursorPosition, setCursorPosition] = useState<{ top: number; left: number } | null>(null)
+
+ const commentChanged = (newContent: string, selectionStart?: number) => {
cache.session.set(getCacheKey(), newContent)
setContent(newContent)
+
+ // Get the cursor position
+ const textBeforeCursor = newContent.slice(0, selectionStart)
+
+ // Check for "@" mention
+ const lastAtIndex = textBeforeCursor.lastIndexOf("@")
+ if (lastAtIndex >= 0) {
+ const mentionQuery = textBeforeCursor.slice(lastAtIndex + 1)
+ const matchedUsers = users.filter((user) => user.name.toLowerCase().startsWith(mentionQuery.toLowerCase()))
+
+ setFilteredUsers(matchedUsers)
+ setDropdownVisible(matchedUsers.length > 0)
+ setMentionIndex(lastAtIndex)
+
+ // Calculate position of dropdown
+ if (inputRef.current) {
+ const textareaRect = inputRef.current.getBoundingClientRect()
+ const top = textareaRect.top + window.scrollY + inputRef.current.scrollTop - 160
+ const left = textareaRect.left + window.scrollX - 50 // Adjust as needed
+ setCursorPosition({ top, left })
+ }
+ } else {
+ setDropdownVisible(false)
+ }
}
+ console.log(dropdownVisible, filteredUsers, mentionIndex, cursorPosition)
+
const hideModal = () => setIsSignInModalOpen(false)
const clearError = () => setError(undefined)
@@ -72,8 +111,16 @@ export const CommentInput = (props: CommentInputProps) => {
minRows={1}
onChange={commentChanged}
onFocus={handleOnFocus}
+ onKeyDown={(e) => {
+ if (e.key === "Escape") {
+ setDropdownVisible(false)
+ }
+ }}
inputRef={inputRef}
/>
+
+ {dropdownVisible && cursorPosition && <MentionSelector cursorPosition={cursorPosition} names={filteredUsers.map((user) => user.name)} />}
+
{content && (
<>
<MultiImageUploader field="attachments" maxUploads={2} onChange={setAttachments} />
diff --git a/public/pages/ShowPost/components/MentionSelector.scss b/public/pages/ShowPost/components/MentionSelector.scss
new file mode 100644
index 000000000..efc4c741b
--- /dev/null
+++ b/public/pages/ShowPost/components/MentionSelector.scss
@@ -0,0 +1,11 @@
+@import "~@fider/assets/styles/variables.scss";
+
+.c-mention-selector {
+ position: absolute;
+ z-index: 1000;
+ width: 200px;
+ border-radius: get("border.radius.small");
+ background-color: get("colors.white");
+ border: 1px solid get("colors.gray.200");
+ padding: spacing(2);
+}
diff --git a/public/pages/ShowPost/components/MentionSelector.tsx b/public/pages/ShowPost/components/MentionSelector.tsx
new file mode 100644
index 000000000..c7c8a5b2f
--- /dev/null
+++ b/public/pages/ShowPost/components/MentionSelector.tsx
@@ -0,0 +1,22 @@
+import React from "react"
+
+import "./MentionSelector.scss"
+
+const MentionSelector: React.FC<{ names: string[]; cursorPosition: { top: number; left: number } }> = ({ names, cursorPosition }) => {
+ return (
+ <div
+ className="c-mention-selector"
+ style={{
+ top: cursorPosition.top,
+ left: cursorPosition.left,
+ }}
+ >
+ {names.map((name, index) => (
+ <div key={index} className="c-mention-selector--item clickable hover p-2">
+ {name}
+ </div>
+ ))}
+ </div>
+ )
+}
+export default MentionSelector