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: locking demo bugs #146

Merged
merged 2 commits into from
Aug 30, 2023
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
7 changes: 5 additions & 2 deletions demo/src/components/Avatar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,11 @@ export const Avatar = ({
<div
className={cn(
'rounded-full flex items-center justify-center h-[32px] w-[32px] md:h-[40px] md:w-[40px] bg-gradient-to-tr',
profileData.color.gradientStart.tw,
profileData.color.gradientEnd.tw,
{
[profileData.color.gradientStart.tw]: isConnected,
[profileData.color.gradientEnd.tw]: isConnected,
'bg-[#D9D9DA]': !isConnected,
},
)}
data-id="avatar-inner-wrapper"
>
Expand Down
23 changes: 16 additions & 7 deletions demo/src/components/Image.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import cn from 'classnames';
import { useElementSelect, useMembers } from '../hooks';
import { findActiveMember, getMemberFirstName, getOutlineClasses } from '../utils';
import { useClickOutside, useElementSelect, useMembers } from '../hooks';
import { findActiveMembers, getMemberFirstName, getOutlineClasses } from '../utils';
import { useRef } from 'react';
import { usePreview } from './PreviewContext.tsx';

interface Props extends React.HTMLAttributes<HTMLImageElement> {
src: string;
Expand All @@ -12,19 +14,26 @@ interface Props extends React.HTMLAttributes<HTMLImageElement> {
}

export const Image = ({ src, children, className, id, slide, locatable = true }: Props) => {
const containerRef = useRef<HTMLDivElement | null>(null);
const preview = usePreview();
const { members, self } = useMembers();
const { handleSelect } = useElementSelect(id, false);
const activeMember = findActiveMember(id, slide, members);
const { outlineClasses, stickyLabelClasses } = getOutlineClasses(activeMember);
const memberName = getMemberFirstName(activeMember);
const label = self?.connectionId === activeMember?.connectionId ? 'You' : memberName;
const activeMembers = findActiveMembers(id, slide, members);
const occupiedByMe = activeMembers.some((member) => member.connectionId === self?.connectionId);
const [firstMember] = activeMembers;
const { outlineClasses, stickyLabelClasses } = getOutlineClasses(firstMember);
const memberName = getMemberFirstName(firstMember);
const name = occupiedByMe ? 'You' : memberName;
const label = activeMembers.length > 1 ? `${name} +${activeMembers.length - 1}` : name;

useClickOutside(containerRef, self, occupiedByMe && !preview);

return (
<div
data-before={label}
className={cn('relative xs:my-4 md:my-0', className, {
[`outline-2 outline before:content-[attr(data-before)] before:absolute before:-top-[22px] before:-left-[2px] before:px-[10px] before:text-sm before:text-white before:rounded-t-lg before:normal-case ${outlineClasses} before:${stickyLabelClasses}`]:
activeMember,
firstMember,
})}
>
<img
Expand Down
5 changes: 4 additions & 1 deletion demo/src/hooks/useTextComponentLock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@ import { useMembers } from './useMembers.ts';
import { useClearOnFailedLock, useClickOutside, useElementSelect } from './useElementSelect.ts';
import { useLockStatus } from './useLock.ts';
import { useSlideElementContent } from './useSlideElementContent.ts';
import sanitize from 'sanitize-html';

interface UseTextComponentLockArgs {
id: string;
slide: string;
defaultText: string;
containerRef: MutableRefObject<HTMLElement | null>;
}

export const useTextComponentLock = ({ id, slide, defaultText, containerRef }: UseTextComponentLockArgs) => {
const spaceName = getSpaceNameFromUrl();
const { members, self } = useMembers();
Expand All @@ -32,7 +34,8 @@ export const useTextComponentLock = ({ id, slide, defaultText, containerRef }: U

const { channel } = useChannel(channelName, (message) => {
if (message.connectionId === self?.connectionId) return;
updateContent(message.data);
const sanitizedValue = sanitize(message.data, { allowedTags: [] });
updateContent(sanitizedValue);
});

const optimisticallyLocked = !!activeMember;
Expand Down
4 changes: 4 additions & 0 deletions demo/src/utils/active-member.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ export const findActiveMember = (id: string, slide: string, members?: Member[])
return members.find((member) => member.location?.element === id && member.location?.slide === slide);
};

export const findActiveMembers = (id: string, slide: string, members?: Member[]) => {
return (members ?? []).filter((member) => member.location?.element === id && member.location?.slide === slide);
};

export const getMemberFirstName = (member?: Member) => {
if (!member) return '';
return member.profileData.name.split(' ')[0];
Expand Down
Loading