Skip to content

Commit

Permalink
proper user identification between communities
Browse files Browse the repository at this point in the history
  • Loading branch information
sandrodesouza committed Oct 21, 2022
1 parent 806e0a9 commit b72f89b
Show file tree
Hide file tree
Showing 25 changed files with 86 additions and 135 deletions.
5 changes: 3 additions & 2 deletions nextjs/components/Channel/Channel.tsx
Expand Up @@ -36,15 +36,13 @@ export function Channel({
pinnedThreads: initialPinnedThreads,
currentChannel,
currentCommunity,
currentUser,
settings,
channelName,
isSubDomainRouting,
nextCursor,
pathCursor,
isBot,
permissions,
token,
}: ChannelViewProps) {
const [isLoading, setIsLoading] = useState(false);
const [threads, setThreads] = useState<SerializedThread[]>(initialThreads);
Expand All @@ -64,6 +62,9 @@ export function Channel({
const [showThread, setShowThread] = useState(false);
const [currentThreadId, setCurrentThreadId] = useState<string>();

const currentUser = permissions.user || null;
const token = permissions.token || null;

function handleLeftScroll() {
if (
isScrollAtBottom(scrollableRootRef.current as HTMLElement) ||
Expand Down
12 changes: 4 additions & 8 deletions nextjs/components/NavBar/Desktop/index.tsx
Expand Up @@ -6,7 +6,6 @@ import { Permissions } from 'types/shared';
import Link from 'components/Link/InternalLink';
import { NewChannelModal } from 'components/Channel';
import useWebsockets from 'hooks/websockets';
import { SerializedUser } from 'serializers/user';
import { toast } from 'components/Toast';
import Badge from 'components/Badge';
import styles from './index.module.scss';
Expand All @@ -16,23 +15,20 @@ import usePath from 'hooks/path';

interface Props {
channels: ChannelSerialized[];
currentUser?: SerializedUser | null;
channelName: string;
permissions: Permissions;
token: string | null;
}

export default function DesktopNavBar({
channelName,
currentUser,
channels,
permissions,
token,
}: Props) {
const [highlights, setHighlights] = useState<string[]>([]);
const router = useRouter();

const userId = currentUser?.authsId;
const userId = permissions.auth?.id || null;
const token = permissions.token || null;

useWebsockets({
room: userId && `user:${userId}`,
Expand Down Expand Up @@ -79,8 +75,8 @@ export default function DesktopNavBar({
)}
<NavLabel>
<div className="grow">Channels</div>
{permissions.channel_create && !!permissions.user?.accountId && (
<NewChannelModal communityId={permissions.user.accountId} />
{permissions.channel_create && !!permissions.accountId && (
<NewChannelModal communityId={permissions.accountId} />
)}
</NavLabel>
<div>
Expand Down
13 changes: 1 addition & 12 deletions nextjs/components/NavBar/index.tsx
@@ -1,25 +1,16 @@
import type { ChannelSerialized } from 'lib/channel';
import { sortByChannelName } from './utilities';
import { Permissions } from 'types/shared';
import { SerializedUser } from 'serializers/user';
import DesktopNavBar from './Desktop';
import MobileNavBar from './Mobile';

interface Props {
channels: ChannelSerialized[];
currentUser?: SerializedUser | null;
channelName: string;
permissions: Permissions;
token: string | null;
}

export default function NavBar({
channelName,
currentUser,
channels,
permissions,
token,
}: Props) {
export default function NavBar({ channelName, channels, permissions }: Props) {
const sortedChannels = sortByChannelName(channels);

return (
Expand All @@ -28,9 +19,7 @@ export default function NavBar({
<DesktopNavBar
channels={sortedChannels}
channelName={channelName}
currentUser={currentUser}
permissions={permissions}
token={token}
/>
</div>
<div className="lg:hidden">
Expand Down
6 changes: 0 additions & 6 deletions nextjs/components/Pages/ChannelsPage/ChannelPage.tsx
Expand Up @@ -10,15 +10,13 @@ export default function ChannelPage({
channels,
currentChannel,
currentCommunity,
currentUser,
settings,
channelName,
isSubDomainRouting,
nextCursor,
pathCursor,
isBot,
permissions,
token,
}: ChannelViewProps) {
// reusing the type between few layers causes problems
// we should explicitly define the type per component
Expand All @@ -31,7 +29,6 @@ export default function ChannelPage({
return (
<PageLayout
currentChannel={currentChannel}
currentUser={currentUser}
seo={{
...buildChannelSeo({
settings,
Expand All @@ -45,22 +42,19 @@ export default function ChannelPage({
settings={settings}
isSubDomainRouting={isSubDomainRouting}
permissions={permissions}
token={token}
>
<ComponentToRender
threads={threads}
pinnedThreads={pinnedThreads}
currentChannel={currentChannel}
currentCommunity={currentCommunity}
currentUser={currentUser}
settings={settings}
channelName={channelName}
isSubDomainRouting={isSubDomainRouting}
nextCursor={nextCursor}
pathCursor={pathCursor}
isBot={isBot}
permissions={permissions}
token={token}
key={settings.communityName + channelName}
/>
</PageLayout>
Expand Down
3 changes: 0 additions & 3 deletions nextjs/components/Pages/ChannelsPage/index.tsx
Expand Up @@ -3,7 +3,6 @@ import type { Settings } from 'serializers/account/settings';
import ChannelPage from './ChannelPage';
import type { SerializedAttachment, Permissions } from 'types/shared';
import type { SerializedThread } from 'serializers/thread';
import type { SerializedUser } from 'serializers/user';
import type { ChannelSerialized } from 'lib/channel';
import { SerializedAccount } from 'serializers/account';
import { SerializedReaction } from 'serializers/reaction';
Expand Down Expand Up @@ -34,13 +33,11 @@ export type messageWithAuthor = messages & {
};

export type ChannelViewProps = {
token: string | null;
settings: Settings;
channelName: string;
channels?: ChannelSerialized[];
currentChannel: ChannelSerialized;
currentCommunity: SerializedAccount | null;
currentUser: SerializedUser | null;
threads: SerializedThread[];
pinnedThreads: SerializedThread[];
isSubDomainRouting: boolean;
Expand Down
17 changes: 6 additions & 11 deletions nextjs/components/Pages/Feed/Content/index.tsx
Expand Up @@ -2,7 +2,6 @@ import { useState, useRef, useCallback } from 'react';
import SidebarLayout from 'components/layout/shared/SidebarLayout';
import { ThreadState } from '@prisma/client';
import { SerializedThread } from 'serializers/thread';
import { SerializedUser } from 'serializers/user';
import { Settings } from 'serializers/account/settings';
import { Permissions, Scope } from 'types/shared';
import Header from '../Header';
Expand All @@ -25,13 +24,9 @@ import { SerializedMessage } from 'serializers/message';
import { manageSelections } from '../utilities/selection';

interface Props {
communityId: string;
communityName: string;
currentUser: SerializedUser;
isSubDomainRouting: boolean;
permissions: Permissions;
settings: Settings;
token: string | null;
}

const debouncedFetch = debounce(
Expand All @@ -51,13 +46,9 @@ const debouncedFetch = debounce(
);

export default function Feed({
communityId,
communityName,
currentUser,
isSubDomainRouting,
permissions,
settings,
token,
}: Props) {
const [feed, setFeed] = useState<FeedResponse>({ threads: [], total: 0 });
const [state, setState] = useState<ThreadState>(ThreadState.OPEN);
Expand All @@ -71,6 +62,10 @@ export default function Feed({
const { startSignUp } = useJoinContext();
const { isShiftPressed } = useKeyboard();

const token = permissions.token || null;
const currentUser = permissions.user || null;
const { communityId, communityName } = settings;

useThreadWebsockets({
id: thread?.id,
token,
Expand Down Expand Up @@ -120,8 +115,8 @@ export default function Feed({
scope === Scope.Participant &&
!messages.find(
(m) =>
m.author?.id === currentUser.id ||
m.mentions.find((me) => me.id === currentUser.id)
m.author?.id === currentUser?.id ||
m.mentions.find((me) => me.id === currentUser?.id)
)
);
}
Expand Down
15 changes: 0 additions & 15 deletions nextjs/components/Pages/Feed/index.tsx
@@ -1,50 +1,35 @@
import React from 'react';
import PageLayout from 'components/layout/PageLayout';
import { channels } from '@prisma/client';
import { SerializedUser } from 'serializers/user';
import { Settings } from 'serializers/account/settings';
import { Permissions } from 'types/shared';

import Content from './Content';

interface Props {
channels: channels[];
communityName: string;
communityId: string;
currentUser: SerializedUser;
isSubDomainRouting: boolean;
permissions: Permissions;
settings: Settings;
token: string | null;
}

export default function Feed({
channels,
communityName,
communityId,
currentUser,
isSubDomainRouting,
permissions,
settings,
token,
}: Props) {
return (
<PageLayout
channels={channels}
isSubDomainRouting={isSubDomainRouting}
permissions={permissions}
settings={settings}
token={token}
currentUser={currentUser}
>
<Content
communityId={communityId}
communityName={communityName}
isSubDomainRouting={isSubDomainRouting}
permissions={permissions}
settings={settings}
currentUser={currentUser}
token={token}
/>
</PageLayout>
);
Expand Down
3 changes: 0 additions & 3 deletions nextjs/components/Pages/Metrics/index.tsx
Expand Up @@ -12,7 +12,6 @@ interface Props {
currentCommunity: SerializedAccount;
permissions: Permissions;
settings: Settings;
token: string | null;
}

const stats = [
Expand All @@ -26,15 +25,13 @@ export default function Metrics({
currentCommunity,
settings,
permissions,
token,
}: Props) {
return (
<PageLayout
channels={channels}
permissions={permissions}
settings={settings}
isSubDomainRouting={false}
token={token}
className="w-full"
>
<Header />
Expand Down
13 changes: 3 additions & 10 deletions nextjs/components/Pages/ThreadPage/Content/index.tsx
@@ -1,4 +1,4 @@
import { useEffect, useState, useRef } from 'react';
import { useState, useRef } from 'react';
import { Thread } from 'components/Thread';
import { scrollToBottom } from 'utilities/scroll';
import { ThreadState } from '@prisma/client';
Expand All @@ -9,7 +9,6 @@ import { sendMessageWrapper } from './sendMessageWrapper';
import { toast } from 'components/Toast';
import { SerializedThread } from 'serializers/thread';
import { SerializedAccount } from 'serializers/account';
import { SerializedUser } from 'serializers/user';
import type { Settings } from 'serializers/account/settings';
import type { ChannelSerialized } from 'lib/channel';
import { Permissions } from 'types/shared';
Expand All @@ -18,35 +17,29 @@ interface Props {
thread: SerializedThread;
currentChannel: ChannelSerialized;
currentCommunity: SerializedAccount | null;
currentUser: SerializedUser | null;
threadUrl: string | null;
isSubDomainRouting: boolean;
settings: Settings;
permissions: Permissions;
token: string | null;
}

export default function Content({
thread: initialThread,
currentChannel,
currentCommunity,
currentUser,
threadUrl,
isSubDomainRouting,
settings,
permissions,
token,
}: Props) {
const [thread, setThread] = useState(initialThread);
const [allUsers] = useUsersContext();
const { startSignUp } = useJoinContext();

const incrementId = thread.incrementId;
const token = permissions.token || null;
const currentUser = permissions.user;

const ref = useRef<HTMLDivElement>(null);
useEffect(() => {
fetch(`/api/count?incrementId=${incrementId}`, { method: 'PUT' });
}, [incrementId]);

useThreadWebsockets({
id: thread.id,
Expand Down
6 changes: 0 additions & 6 deletions nextjs/components/Pages/ThreadPage/ThreadPage.tsx
Expand Up @@ -9,12 +9,10 @@ export function ThreadPage({
channels,
currentChannel,
currentCommunity,
currentUser,
threadUrl,
isSubDomainRouting,
settings,
permissions,
token,
}: ThreadByIdProp) {
const ref = useRef<HTMLDivElement>(null);

Expand All @@ -31,24 +29,20 @@ export function ThreadPage({
}),
}}
currentChannel={currentChannel}
currentUser={currentUser}
channels={channels}
settings={settings}
isSubDomainRouting={isSubDomainRouting}
permissions={permissions}
innerRef={ref}
token={token}
>
<Content
thread={thread}
currentChannel={currentChannel}
currentCommunity={currentCommunity}
currentUser={currentUser}
threadUrl={threadUrl}
isSubDomainRouting={isSubDomainRouting}
settings={settings}
permissions={permissions}
token={token}
/>
</PageLayout>
);
Expand Down

1 comment on commit b72f89b

@vercel
Copy link

@vercel vercel bot commented on b72f89b Oct 21, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.