-
Notifications
You must be signed in to change notification settings - Fork 1k
feat: allow tagging in incident comments #4669
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
Closed
Closed
Changes from 2 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
1700c16
allow tagging in incident comments
rohitdash08 4f6adc4
added migrations and made changes to the model
rohitdash08 1e1aa00
added e2e test for the allow tagging in the incident comment
rohitdash08 a0f8912
made changes to incident tagging used react quill instead of doing it…
rohitdash08 3f9d587
comment resolved changed MentionInput to CommentInput
rohitdash08 b5e6f17
Merge branch 'main' into rohitdash08
skynetigor 651edc1
Revert "refactor booleans"
skynetigor File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
153 changes: 153 additions & 0 deletions
153
keep-ui/app/(keep)/incidents/[id]/activity/ui/MentionsInput.tsx
This file contains hidden or 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,153 @@ | ||
import { User } from "@/app/(keep)/settings/models"; | ||
import { useEffect, useRef, useState } from "react"; | ||
import { TextInput } from "@tremor/react"; | ||
|
||
interface MentionsInputProps { | ||
value: string; | ||
onValueChange: (value: string) => void; | ||
users: User[]; | ||
placeholder?: string; | ||
} | ||
|
||
interface SuggestionState { | ||
isOpen: boolean; | ||
query: string; | ||
index: number; | ||
startPosition: number; | ||
} | ||
|
||
export function MentionsInput({ | ||
value, | ||
onValueChange, | ||
users, | ||
placeholder, | ||
}: MentionsInputProps) { | ||
const [suggestion, setSuggestion] = useState<SuggestionState>({ | ||
isOpen: false, | ||
query: "", | ||
index: 0, | ||
startPosition: 0, | ||
}); | ||
|
||
const inputRef = useRef<HTMLInputElement>(null); | ||
|
||
const filteredUsers = users.filter((user) => | ||
(user.name?.toLowerCase() || user.email.toLowerCase()).includes( | ||
suggestion.query.toLowerCase() | ||
) | ||
); | ||
|
||
const insertMention = (user: User) => { | ||
const before = value.slice(0, suggestion.startPosition); | ||
const after = value.slice(inputRef.current?.selectionStart || 0); | ||
const mention = `@${user.email} `; | ||
onValueChange(before + mention + after); | ||
setSuggestion({ isOpen: false, query: "", index: 0, startPosition: 0 }); | ||
}; | ||
|
||
const handleKeyDown = (event: React.KeyboardEvent) => { | ||
if (!suggestion.isOpen) return; | ||
|
||
switch (event.key) { | ||
case "ArrowDown": | ||
event.preventDefault(); | ||
setSuggestion((prev) => ({ | ||
...prev, | ||
index: (prev.index + 1) % filteredUsers.length, | ||
})); | ||
break; | ||
case "ArrowUp": | ||
event.preventDefault(); | ||
setSuggestion((prev) => ({ | ||
...prev, | ||
index: | ||
prev.index - 1 < 0 ? filteredUsers.length - 1 : prev.index - 1, | ||
})); | ||
break; | ||
case "Enter": | ||
event.preventDefault(); | ||
if (filteredUsers[suggestion.index]) { | ||
insertMention(filteredUsers[suggestion.index]); | ||
} | ||
break; | ||
case "Escape": | ||
setSuggestion({ isOpen: false, query: "", index: 0, startPosition: 0 }); | ||
break; | ||
} | ||
}; | ||
|
||
const handleInput = (newValue: string) => { | ||
onValueChange(newValue); | ||
|
||
const cursorPosition = inputRef.current?.selectionStart || 0; | ||
const textBeforeCursor = newValue.slice(0, cursorPosition); | ||
const matches = textBeforeCursor.match(/@([\w\s]*)$/); | ||
|
||
if (matches) { | ||
setSuggestion({ | ||
isOpen: true, | ||
query: matches[1], | ||
index: 0, | ||
startPosition: cursorPosition - matches[1].length - 1, | ||
}); | ||
} else { | ||
setSuggestion({ isOpen: false, query: "", index: 0, startPosition: 0 }); | ||
} | ||
}; | ||
|
||
useEffect(() => { | ||
const handleClickOutside = (event: MouseEvent) => { | ||
if ( | ||
suggestion.isOpen && | ||
inputRef.current && | ||
!inputRef.current.contains(event.target as Node) | ||
) { | ||
setSuggestion({ isOpen: false, query: "", index: 0, startPosition: 0 }); | ||
} | ||
}; | ||
|
||
document.addEventListener("click", handleClickOutside); | ||
return () => document.removeEventListener("click", handleClickOutside); | ||
}, [suggestion.isOpen]); | ||
|
||
return ( | ||
<div className="relative w-full"> | ||
<TextInput | ||
ref={inputRef} | ||
value={value} | ||
onValueChange={handleInput} | ||
placeholder={placeholder} | ||
onKeyDown={handleKeyDown} | ||
/> | ||
{suggestion.isOpen && filteredUsers.length > 0 && ( | ||
<div className="absolute z-10 w-64 mt-1 bg-white rounded-md shadow-lg border border-gray-200"> | ||
<ul className="py-1"> | ||
{filteredUsers.map((user, index) => ( | ||
<li | ||
key={user.email} | ||
className={`px-3 py-2 cursor-pointer hover:bg-gray-100 ${suggestion.index === index ? "bg-gray-100" : ""}`} | ||
onClick={() => insertMention(user)} | ||
> | ||
<div className="flex items-center"> | ||
{user.picture && ( | ||
<img | ||
src={user.picture} | ||
alt={user.name} | ||
className="w-6 h-6 rounded-full mr-2" | ||
/> | ||
)} | ||
<div> | ||
<div className="font-medium">{user.name || user.email}</div> | ||
{user.name && ( | ||
<div className="text-sm text-gray-500">{user.email}</div> | ||
)} | ||
</div> | ||
</div> | ||
</li> | ||
))} | ||
</ul> | ||
</div> | ||
)} | ||
</div> | ||
); | ||
} |
This file contains hidden or 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,8 @@ | ||
from typing import List | ||
from pydantic import BaseModel | ||
from keep.api.models.db.incident import IncidentStatus | ||
|
||
class IncidentCommentDto(BaseModel): | ||
status: IncidentStatus | ||
comment: str | ||
mentioned_users: List[str] = [] |
This file contains hidden or 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,29 @@ | ||
from datetime import datetime | ||
from typing import List | ||
from uuid import UUID, uuid4 | ||
|
||
from sqlalchemy import Column, ForeignKey, Text | ||
from sqlalchemy_utils import UUIDType | ||
from sqlmodel import Field, JSON, SQLModel | ||
|
||
from keep.api.models.db.incident import Incident, IncidentStatus | ||
|
||
|
||
class IncidentComment(SQLModel, table=True): | ||
"""Model for storing incident comments with mentioned users.""" | ||
id: UUID = Field(default_factory=uuid4, primary_key=True) | ||
incident_id: UUID = Field( | ||
sa_column=Column( | ||
UUIDType(binary=False), | ||
ForeignKey("incident.id", ondelete="CASCADE"), | ||
index=True, | ||
) | ||
) | ||
status: str = Field(sa_column=Column(Text, nullable=False)) | ||
comment: str = Field(sa_column=Column(Text, nullable=False)) | ||
mentioned_users: List[str] = Field(default=[], sa_column=Column(JSON)) | ||
created_at: datetime = Field( | ||
default_factory=datetime.utcnow, | ||
nullable=False, | ||
) | ||
created_by: str | None = Field(default=None) |
61 changes: 61 additions & 0 deletions
61
keep/api/models/db/migrations/versions/2024-07-29-18-11_add_incident_comments.py
This file contains hidden or 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,61 @@ | ||
"""Add incident comments table | ||
|
||
Revision ID: add_incident_comments | ||
Revises: 92f4f93f2140 | ||
Create Date: 2024-07-29 18:11:00.000000 | ||
|
||
""" | ||
from typing import List | ||
|
||
import sqlalchemy as sa | ||
import sqlmodel | ||
from alembic import op | ||
from sqlalchemy.dialects.postgresql import ARRAY | ||
from sqlalchemy_utils import UUIDType | ||
|
||
# revision identifiers, used by Alembic. | ||
revision = "add_incident_comments" | ||
down_revision = "92f4f93f2140" | ||
branch_labels = None | ||
depends_on = None | ||
|
||
|
||
def upgrade() -> None: | ||
# ### commands auto generated by Alembic - please adjust! ### | ||
op.create_table( | ||
"incident_comment", | ||
sa.Column("id", UUIDType(binary=False), primary_key=True), | ||
sa.Column("incident_id", UUIDType(binary=False), nullable=False), | ||
sa.Column("status", sqlmodel.sql.sqltypes.AutoString(), nullable=False), | ||
sa.Column("comment", sa.Text(), nullable=False), | ||
sa.Column( | ||
"mentioned_users", | ||
sa.JSON(), | ||
nullable=False, | ||
server_default=sa.text("'[]'"), | ||
), | ||
sa.Column( | ||
"created_at", | ||
sa.DateTime(timezone=True), | ||
server_default=sa.text("CURRENT_TIMESTAMP"), | ||
nullable=False, | ||
), | ||
sa.Column("created_by", sqlmodel.sql.sqltypes.AutoString(), nullable=True), | ||
sa.ForeignKeyConstraint( | ||
["incident_id"], | ||
["incident.id"], | ||
ondelete="CASCADE", | ||
), | ||
) | ||
op.create_index( | ||
op.f("ix_incident_comment_incident_id"), | ||
"incident_comment", | ||
["incident_id"], | ||
unique=False, | ||
) | ||
|
||
|
||
def downgrade() -> None: | ||
# ### commands auto generated by Alembic - please adjust! ### | ||
op.drop_index(op.f("ix_incident_comment_incident_id"), table_name="incident_comment") | ||
op.drop_table("incident_comment") |
This file contains hidden or 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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.