Skip to content
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
2 changes: 2 additions & 0 deletions Documentation/ComponentPicker/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ The picker recognizes a component by `data-cratis-component="<Name>"` on its roo

`data-cratis-part` identifies a part inside the component, such as `label`, `title` or `close`; `data-cratis-component` identifies the component itself. Some public components do not yet have their own marker. For an unmarked element, the picker selects its nearest marked ancestor, if one exists. The package's identity specs track marker coverage and require new public components to be accounted for.

Chat composer, conversation, message body, message bubble, topic list, emoji picker, failed reply, mention suggestions, reactions, avatar, typing indicator, and property-type glyph roots expose their own markers. `ChatSidebarForObservableQueries` and `GlyphMenu` remain unmarked: the former delegates through a fragment, and the latter combines a trigger with portal content.

## Pick a component

Render your screen under [CratisComponentsProvider](../Common/cratis-components-provider.md), then use `useComponentPicker()` to start and stop picking. Render the returned `picker` once under that provider and call `start()` when the user asks to select something:
Expand Down
251 changes: 135 additions & 116 deletions Source/Chat/ChatConversation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,126 @@ const typingLabel = (
return labels?.typingSeveral ?? 'Several people are typing';
};

const conversationStatusLabel = (
status: ChatStatus,
labels: ChatConversationLabels | undefined,
) => {
switch (status) {
case ChatStatus.Loading:
return <span key={status} role='status'>{labels?.loading ?? 'Loading messages…'}</span>;
case ChatStatus.Failed:
return <span key={status} role='alert'>{labels?.failed ?? 'Could not load messages.'}</span>;
case ChatStatus.Unauthorized:
return <span key={status} role='alert'>{labels?.unauthorized ?? 'You are not authorized to view these messages.'}</span>;
default:
return labels?.empty ?? 'No messages yet. Say hello!';
}
};

const renderConversationStatus = (
messageCount: number,
status: ChatStatus,
labels: ChatConversationLabels | undefined,
) => {
if (messageCount > 0 && status !== ChatStatus.Failed && status !== ChatStatus.Unauthorized) return null;
return (
<p className='cratis-chat-conversation__empty'>
{conversationStatusLabel(status, labels)}
</p>
);
};

interface ConversationMessageProps<TMessage extends ChatMessage> extends Pick<ChatConversationProps<TMessage>,
'renderAvatar' | 'renderAuthorName' | 'renderMessageExtra' | 'buildAvatarUrl' | 'labels'> {
message: TMessage;
author: ChatAuthor;
info: MessageRenderInfo;
availableActions: ChatMessageAction<TMessage>[];
quickReply: boolean;
onQuickReply: (name: string) => void;
}

const renderMessageAuthor = (
message: ChatMessage,
author: ChatAuthor,
{ renderAvatar, renderAuthorName, buildAvatarUrl }: Pick<ChatConversationProps, 'renderAvatar' | 'renderAuthorName' | 'buildAvatarUrl'>,
) => (
<div className='cratis-chat-message__author'>
{renderAvatar ? (
renderAvatar(message.authorId, author)
) : (
<PersonAvatarCircle
userId={chatIdentifierString(message.authorId)}
name={author.name}
hasAvatar={author.hasAvatar ?? false}
size={22}
ownerType={author.kind === ChatAuthorKind.Agent ? 'Agents' : 'Users'}
version={author.avatarVersion}
buildAvatarUrl={buildAvatarUrl}
/>
)}
<span className='cratis-chat-message__author-name'>
{renderAuthorName ? renderAuthorName(message.authorId, author) : author.name}
</span>
</div>
);

const renderMessageActions = <TMessage extends ChatMessage>({
message, author, availableActions, quickReply, labels, onQuickReply,
}: Pick<ConversationMessageProps<TMessage>, 'message' | 'author' | 'availableActions' | 'quickReply' | 'labels' | 'onQuickReply'>) => (
<div className='cratis-chat-message__actions'>
{quickReply && (
<button
type='button'
className='cratis-chat-message__action'
title={(labels?.quickReplyTo ?? 'Reply to {name}').replace('{name}', author.name)}
aria-label={(labels?.quickReplyTo ?? 'Reply to {name}').replace('{name}', author.name)}
onClick={() => onQuickReply(author.name)}
>
<ReplyIcon />
</button>
)}
{availableActions.map((action) => (
<button
key={action.id}
type='button'
className='cratis-chat-message__action'
title={action.label}
aria-label={action.label}
onClick={() => action.onInvoke(message)}
>
{typeof action.icon === 'string' ? <i className={action.icon} aria-hidden='true' /> : action.icon}
</button>
))}
</div>
);

const renderConversationMessage = <TMessage extends ChatMessage>(props: ConversationMessageProps<TMessage>) => {
const { message, author, info: { showAuthor, showTimestamp }, availableActions, quickReply, renderMessageExtra, labels } = props;
const showActionsRow = quickReply || availableActions.length > 0;

return (
<div
key={chatIdentifierString(message.id)}
className={`cratis-chat-message${showAuthor ? '' : ' cratis-chat-message--continuation'}`}
>
{showAuthor && renderMessageAuthor(message, author, props)}
<div className='cratis-chat-message__content'>
<div className={`cratis-chat-message__panel${showActionsRow ? ' cratis-chat-message__panel--with-actions' : ''}`}>
<ChatMessageBody body={message.body} mentions={message.mentions} />
{showActionsRow && renderMessageActions(props)}
</div>
{renderMessageExtra?.(message)}
{showTimestamp && (
<span className='cratis-chat-message__time'>
{relativeTimestamp(timestampOf(message), new Date(), labels?.timestamps)}
</span>
)}
</div>
</div>
);
};

/**
* One conversation — its messages and the composer to add to them. Host-agnostic by design: it
* renders whatever message array it is given (hand it a live query's data and it stays current),
Expand Down Expand Up @@ -277,128 +397,27 @@ export const ChatConversation = <TMessage extends ChatMessage = ChatMessage>({
const quickReplyTo = (name: string) => composerRef.current?.prefill(`@${name} `);

return (
<div className={`cratis-chat-conversation${className ? ` ${className}` : ''}`}>
<div className={`cratis-chat-conversation${className ? ` ${className}` : ''}`} data-cratis-component='ChatConversation'>
<div className='cratis-chat-conversation__messages' aria-busy={status === ChatStatus.Loading && messages.length > 0 || undefined}>
{(messages.length === 0 || status === ChatStatus.Failed || status === ChatStatus.Unauthorized) && (
<p className='cratis-chat-conversation__empty'>
{status === ChatStatus.Loading ? (
<span key={status} role='status'>{labels?.loading ?? 'Loading messages…'}</span>
) : status === ChatStatus.Failed ? (
<span key={status} role='alert'>{labels?.failed ?? 'Could not load messages.'}</span>
) : status === ChatStatus.Unauthorized ? (
<span key={status} role='alert'>{labels?.unauthorized ?? 'You are not authorized to view these messages.'}</span>
) : (
labels?.empty ?? 'No messages yet. Say hello!'
)}
</p>
)}
{renderConversationStatus(messages.length, status, labels)}
{status !== ChatStatus.Unauthorized && messages.map((message, index) => {
const author = authorFor(message.authorId);
const { showAuthor, showTimestamp } = renderInfo[index];
const availableActions = (actions ?? []).filter(
(action) => action.isAvailable?.(message) ?? true,
);
const showActionsRow = quickReply || availableActions.length > 0;

return (
<div
key={chatIdentifierString(message.id)}
className={`cratis-chat-message${showAuthor ? '' : ' cratis-chat-message--continuation'}`}
>
{showAuthor && (
<div className='cratis-chat-message__author'>
{renderAvatar ? (
renderAvatar(message.authorId, author)
) : (
<PersonAvatarCircle
userId={chatIdentifierString(
message.authorId,
)}
name={author.name}
hasAvatar={author.hasAvatar ?? false}
size={22}
ownerType={
author.kind === ChatAuthorKind.Agent
? 'Agents'
: 'Users'
}
version={author.avatarVersion}
buildAvatarUrl={buildAvatarUrl}
/>
)}
<span className='cratis-chat-message__author-name'>
{renderAuthorName
? renderAuthorName(message.authorId, author)
: author.name}
</span>
</div>
)}
<div className='cratis-chat-message__content'>
<div
className={`cratis-chat-message__panel${showActionsRow ? ' cratis-chat-message__panel--with-actions' : ''}`}
>
<ChatMessageBody
body={message.body}
mentions={message.mentions}
/>
{showActionsRow && (
<div className='cratis-chat-message__actions'>
{quickReply && (
<button
type='button'
className='cratis-chat-message__action'
title={(
labels?.quickReplyTo ??
'Reply to {name}'
).replace('{name}', author.name)}
aria-label={(
labels?.quickReplyTo ??
'Reply to {name}'
).replace('{name}', author.name)}
onClick={() =>
quickReplyTo(author.name)
}
>
<ReplyIcon />
</button>
)}
{availableActions.map((action) => (
<button
key={action.id}
type='button'
className='cratis-chat-message__action'
title={action.label}
aria-label={action.label}
onClick={() =>
action.onInvoke(message)
}
>
{typeof action.icon === 'string' ? (
<i
className={action.icon}
aria-hidden='true'
/>
) : (
action.icon
)}
</button>
))}
</div>
)}
</div>
{renderMessageExtra?.(message)}
{showTimestamp && (
<span className='cratis-chat-message__time'>
{relativeTimestamp(
timestampOf(message),
new Date(),
labels?.timestamps,
)}
</span>
)}
</div>
</div>
);
return renderConversationMessage({
message,
author,
info: renderInfo[index],
availableActions,
quickReply,
onQuickReply: quickReplyTo,
renderAvatar,
renderAuthorName,
renderMessageExtra,
buildAvatarUrl,
labels,
});
})}
{status !== ChatStatus.Unauthorized && (
<TypingIndicator
Expand Down
2 changes: 1 addition & 1 deletion Source/Chat/ChatMessageBody.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export interface ChatMessageBodyProps {
* apart and the host's own CSS can hang further behavior off them.
*/
export const ChatMessageBody = ({ body, mentions }: ChatMessageBodyProps) => (
<p className='cratis-chat-message__body'>
<p className='cratis-chat-message__body' data-cratis-component='ChatMessageBody'>
{mentionSegments(body, mentions ?? []).map((segment, index) =>
segment.mention ? (
<span
Expand Down
2 changes: 1 addition & 1 deletion Source/Chat/ChatTopicList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ export const ChatTopicList = <TTopic extends ChatTopic = ChatTopic>({
const showMessage = topics.length === 0 || status === ChatStatus.Failed || status === ChatStatus.Unauthorized;

return (
<div className={`cratis-chat-topics${className ? ` ${className}` : ''}`} aria-busy={status === ChatStatus.Loading && topics.length > 0 || undefined}>
<div className={`cratis-chat-topics${className ? ` ${className}` : ''}`} data-cratis-component='ChatTopicList' aria-busy={status === ChatStatus.Loading && topics.length > 0 || undefined}>
{onStart && <ChatTopicStartButton label={labels?.newTopic} disabled={status === ChatStatus.Unauthorized} onStart={onStart} />}
{showMessage && <ChatTopicEmptyMessage status={status} labels={labels} />}
<ul ref={listRef} className='cratis-chat-topics__list'>
Expand Down
1 change: 1 addition & 0 deletions Source/Chat/Kit/Avatar/PersonAvatarCircle.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ export const PersonAvatarCircle = ({

return (
<span
data-cratis-component='PersonAvatarCircle'
className={
className ? `person-avatar-circle ${className}` : 'person-avatar-circle'
}
Expand Down
2 changes: 1 addition & 1 deletion Source/Chat/Kit/ChatComposer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ export const ChatComposer = forwardRef<ChatComposerHandle, ChatComposerProps>(
};

return (
<div className='chat-composer'>
<div className='chat-composer' data-cratis-component='ChatComposer'>
{isSuggesting && (
<MentionSuggestions
candidates={suggestions}
Expand Down
Loading
Loading