Skip to content

Commit

Permalink
Catchup perf (#530)
Browse files Browse the repository at this point in the history
* More memo;

* Memo the nav strucs;

* Added videoprocessor docs;
  • Loading branch information
stef-coenen authored Jan 17, 2025
1 parent ee9be6b commit 94ef7bf
Show file tree
Hide file tree
Showing 12 changed files with 813 additions and 713 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { CommunityHistory } from '../channel/CommunityHistory';
import { ActionLink, COMMUNITY_ROOT_PATH, useDotYouClientContext } from '@homebase-id/common-app';
import { ExternalLink } from '@homebase-id/common-app/icons';
import { useCommunityMetadata } from '../../../hooks/community/useCommunityMetadata';
import { useMemo } from 'react';

export const CommunityChannelCatchup = ({
community,
Expand All @@ -14,29 +15,52 @@ export const CommunityChannelCatchup = ({
channel: ChannelWithRecentMessage;
}) => {
const communityId = community.fileMetadata.appData.uniqueId;
const channelLink = `${COMMUNITY_ROOT_PATH}/${community.fileMetadata.senderOdinId}/${communityId}/${channel.fileMetadata.appData.uniqueId}`;
const channelLink = useMemo(
() =>
`${COMMUNITY_ROOT_PATH}/${community.fileMetadata.senderOdinId}/${communityId}/${channel.fileMetadata.appData.uniqueId}`,
[community, communityId, channel]
);
const { data: metadata } = useCommunityMetadata({
odinId: community.fileMetadata.senderOdinId,
communityId,
}).single;
const channelLastRead =
metadata?.fileMetadata.appData.content.channelLastReadTime[
channel.fileMetadata.appData.uniqueId as string
];

const channelLastRead = useMemo(
() =>
metadata?.fileMetadata.appData.content.channelLastReadTime[
channel.fileMetadata.appData.uniqueId as string
],
[metadata, channel]
);

const loggedInIdentity = useDotYouClientContext().getLoggedInIdentity();
if (!channel.lastMessage) return null;

const todayDate = new Date(channel.lastMessage?.fileMetadata.created);
todayDate.setHours(0, 0, 0, 0);
const defaultMaxAge = todayDate.getTime();
const defaultMaxAge = useMemo(() => {
if (!channel.lastMessage) return 0;

const todayDate = new Date(channel.lastMessage?.fileMetadata.created);
todayDate.setHours(0, 0, 0, 0);
return todayDate.getTime();
}, [channel]);

const maxAge =
(channel.lastMessage?.fileMetadata.originalAuthor !== loggedInIdentity &&
channelLastRead &&
channelLastRead < defaultMaxAge &&
channelLastRead) ||
defaultMaxAge;
const maxAge = useMemo(
() =>
(channel.lastMessage?.fileMetadata.originalAuthor !== loggedInIdentity &&
channelLastRead &&
channelLastRead < defaultMaxAge &&
channelLastRead) ||
defaultMaxAge,
[channel, channelLastRead, defaultMaxAge, loggedInIdentity]
);

const showOptions = useMemo(() => {
return {
count: 5,
targetLink: channelLink,
};
}, [channelLink]);

if (!channel.lastMessage) return null;

return (
<div className="pb-3 last-of-type:pb-0">
Expand All @@ -58,10 +82,7 @@ export const CommunityChannelCatchup = ({
community={community}
channel={channel}
alignTop={true}
maxShowOptions={{
count: 5,
targetLink: channelLink,
}}
maxShowOptions={showOptions}
maxAge={maxAge}
/>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@ export const CommunityThreadCatchup = ({
communityId,
}).single;

const threadsLastRead = metadata?.fileMetadata.appData.content.threadsLastReadTime;
const threadsLastRead = useMemo(
() => metadata?.fileMetadata.appData.content.threadsLastReadTime,
[metadata]
);

const { data: channel } = useCommunityChannel({
odinId: community.fileMetadata.senderOdinId,
Expand All @@ -58,16 +61,21 @@ export const CommunityThreadCatchup = ({

const loggedInIdentity = useDotYouClientContext().getLoggedInIdentity();

const todayDate = new Date(threadMeta.lastMessageCreated);
todayDate.setHours(0, 0, 0, 0);
const defaultMaxAge = todayDate.getTime();
const defaultMaxAge = useMemo(() => {
const todayDate = new Date(threadMeta.lastMessageCreated);
todayDate.setHours(0, 0, 0, 0);
return todayDate.getTime();
}, [threadMeta]);

const maxAge =
(threadMeta.lastAuthor !== loggedInIdentity &&
threadsLastRead &&
threadsLastRead < defaultMaxAge &&
threadsLastRead) ||
defaultMaxAge;
const maxAge = useMemo(
() =>
(threadMeta.lastAuthor !== loggedInIdentity &&
threadsLastRead &&
threadsLastRead < defaultMaxAge &&
threadsLastRead) ||
defaultMaxAge,
[threadMeta, threadsLastRead, defaultMaxAge, loggedInIdentity]
);

if (!channel || !originMessage) return null;

Expand Down
143 changes: 143 additions & 0 deletions packages/apps/community-app/src/templates/Community/CommunitiesNav.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
import {
COMMUNITY_ROOT_PATH,
Sidenav,
ErrorBoundary,
t,
ActionGroup,
getOdinIdColor,
} from '@homebase-id/common-app';
import { RadioTower, MagnifyingGlass, Plus, Loader, Ellipsis } from '@homebase-id/common-app/icons';
import { HomebaseFile } from '@homebase-id/js-lib/core';
import { stringGuidsEqual } from '@homebase-id/js-lib/helpers';
import { useState } from 'react';
import { useMatch, useParams, useNavigate, Link } from 'react-router-dom';
import { useCommunities } from '../../hooks/community/useCommunities';
import { CommunityDefinition } from '../../providers/CommunityDefinitionProvider';

export const CommunitiesNav = ({ togglePin }: { togglePin: (newVal?: boolean) => void }) => {
const isReactNative = window.localStorage.getItem('client_type')?.startsWith('react-native');

const rootChatMatch = useMatch({ path: COMMUNITY_ROOT_PATH });
const communityHomeChatMatch = useMatch({
path: `${COMMUNITY_ROOT_PATH}/:odinKey/:communityKey`,
end: true,
});
const isRoot = !!rootChatMatch || !!communityHomeChatMatch;
const isActive = !!rootChatMatch;

return (
<>
{!isReactNative ? <Sidenav disablePinning={true} hideMobileDrawer={!isRoot} /> : null}
<div
className={`${isActive ? 'translate-x-full' : 'translate-x-0'} fixed bottom-0 left-[-100%] top-0 z-[1] flex h-[100dvh] w-full flex-shrink-0 flex-col bg-page-background transition-transform lg:relative lg:left-0 lg:max-w-[4rem] lg:translate-x-0`}
>
<ErrorBoundary>
<div className="absolute inset-0 flex flex-grow flex-col md:pl-[calc(env(safe-area-inset-left)+4.3rem)] lg:items-center lg:pl-0">
<div className="flex flex-row items-center gap-2 px-4 pb-2 pt-4 md:static">
<RadioTower className="h-7 w-7" />
<span className="lg:hidden">{t('Homebase Community')}</span>
</div>
<CommunitiesList togglePin={togglePin} />
</div>
</ErrorBoundary>
</div>
</>
);
};

const CommunitiesList = ({ togglePin }: { togglePin: (newVal?: boolean) => void }) => {
const [isEnableDiscovery, setIsEnableDiscovery] = useState(false);
const {
data: communities,
isFetched,
refetch: refetchCommunities,
isRefetching: isRefetchingCommunities,
} = useCommunities(isEnableDiscovery).all;
const { communityKey: activeCommunityId } = useParams();

const navigate = useNavigate();

if (!isFetched) return null;

return (
<>
{communities?.map((conversation) => (
<CommunityListItem
key={conversation.fileId}
community={conversation}
isActive={stringGuidsEqual(activeCommunityId, conversation.fileMetadata.appData.uniqueId)}
togglePin={togglePin}
/>
))}

<div className={`px-2 py-2`}>
<ActionGroup
options={[
{
label: 'Discover accessible communities',
onClick: () => {
setIsEnableDiscovery(true);

setTimeout(() => refetchCommunities(), 100);
},
icon: MagnifyingGlass,
},
{
label: 'Create a new community',
onClick: () => navigate(`${COMMUNITY_ROOT_PATH}/new`),
icon: Plus,
},
]}
type={'mute'}
size={'none'}
className={`flex w-full items-center justify-center rounded-2xl p-[0.606rem] lg:aspect-square`}
>
{isRefetchingCommunities && isEnableDiscovery ? (
<Loader className="h-6 w-6" />
) : (
<Ellipsis className="h-6 w-6" />
)}
</ActionGroup>
</div>
</>
);
};

const CommunityListItem = ({
community,
isActive,
togglePin,
}: {
community: HomebaseFile<CommunityDefinition>;
isActive: boolean;
togglePin: (newVal?: boolean) => void;
}) => {
return (
<div className={`w-full px-2 py-2 ${isActive ? 'bg-primary/20' : ''}`}>
<Link
to={`${COMMUNITY_ROOT_PATH}/${community.fileMetadata.senderOdinId}/${community.fileMetadata.appData.uniqueId}`}
className="flex aspect-auto flex-row items-center gap-4 rounded-lg rounded-l-2xl border bg-background lg:block lg:aspect-square lg:rounded-none lg:border-0 lg:bg-transparent"
onClick={(e) => {
if (isActive) {
togglePin();
e.stopPropagation();
e.preventDefault();
return;
} else {
togglePin(true);
}
}}
>
<span
className="flex aspect-square w-16 flex-row items-center justify-center rounded-2xl p-2 text-lg uppercase leading-none text-white hover:shadow-md lg:w-full"
style={{
backgroundColor: getOdinIdColor(community.fileMetadata.appData.content.title).darkTheme,
}}
>
{community.fileMetadata.appData.content.title.slice(0, 2)}
</span>
<span className="text-lg lg:hidden">{community.fileMetadata.appData.content.title}</span>
</Link>
</div>
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ export const CommunityChannelDetail = () => {
}

if (!community || !channelDsr) return <NotFound />;

return (
<ErrorBoundary>
<div className="h-full w-20 flex-grow bg-background">
Expand Down
Loading

0 comments on commit 94ef7bf

Please sign in to comment.