Skip to content

Commit

Permalink
chore: cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
ttypic committed Aug 21, 2023
1 parent 302b868 commit 891d8e9
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 34 deletions.
2 changes: 1 addition & 1 deletion demo/src/components/Image.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ interface Props extends React.HTMLAttributes<HTMLImageElement> {

export const Image = ({ src, children, className, id, slide, locatable = true }: Props) => {
const { members, self } = useMembers();
const { handleSelect } = useElementSelect(id);
const { handleSelect } = useElementSelect(id, false);
const activeMember = findActiveMember(id, slide, members);
const { outlineClasses, stickyLabelClasses } = getOutlineClasses(activeMember);
const memberName = getMemberFirstName(activeMember);
Expand Down
16 changes: 5 additions & 11 deletions demo/src/hooks/useElementSelect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,12 @@ export const useElementSelect = (element?: string, lockable: boolean = true) =>
const lockId = buildLockId(self.location?.slide, element);
const lock = space.locks.get(lockId);

if (lock?.request.status === 'locked' && self.locks.get(lockId)) {
// alert('You are already locking this location');
} else if (lock?.request.status === 'locked') {
// alert('Location is locked by another user');
} else {
if (/*window.confirm('Would you like to lock this location?')*/ true) {
await releaseMyLocks(space, self);
await space.locks.acquire(lockId);
if (lock?.request.status !== 'locked') {
await releaseMyLocks(space, self);
await space.locks.acquire(lockId);

// The lock is pending but we enter the location optimistically
space.locations.set({ slide: self.location?.slide, element });
}
// The lock is pending but we enter the location optimistically
space.locations.set({ slide: self.location?.slide, element });
}
} else {
await releaseMyLocks(space, self);
Expand Down
21 changes: 0 additions & 21 deletions demo/src/hooks/useLock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@ import { SpacesContext } from '../components';
import { buildLockId } from '../utils/locking';
import { isMember, useMembers } from '../hooks';

import { getMemberFirstName } from '../utils';

import { type Member } from '../utils/types';
import members from '../../../src/Members.ts';

export const useLock = (slide: string, id: string): { status?: string; member?: Member } => {
const space = useContext(SpacesContext);
Expand Down Expand Up @@ -52,24 +49,6 @@ export const useLock = (slide: string, id: string): { status?: string; member?:
return { status, member };
};

export const useLockLabelCallback = (slide: string, id: string, selfConnectionId?: string) => {
const { member, status } = useLock(slide, id);
const [label, setLabel] = useState<string | undefined>(undefined);

useEffect(() => {
// We're locking this component
if (status === 'locked' && member?.connectionId === selfConnectionId) {
setLabel(`Locked by You`);
} else if (status === 'locked') {
setLabel(`Locked by ${getMemberFirstName(member)}`);
} else if (status === 'unlocked') {
setLabel(undefined);
}
}, [member, status, slide, id, selfConnectionId]);

return label;
};

export const useLockStatus = (slide: string, id: string, selfConnectionId?: string) => {
const { member, status } = useLock(slide, id);

Expand Down
3 changes: 2 additions & 1 deletion demo/src/utils/locking.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ export const releaseMyLocks = async (space: Space, self: Member) => {
}
};

export const buildLockId = (slide: string, element: string | undefined) => `/slide/${slide}/element/${element}`;
export const buildLockId = (slide: string | undefined, element: string | undefined) =>
`/slide/${slide}/element/${element}`;

0 comments on commit 891d8e9

Please sign in to comment.