Skip to content

Commit

Permalink
rough flattened nav
Browse files Browse the repository at this point in the history
  • Loading branch information
dnbrwstr committed Nov 27, 2024
1 parent 48549cd commit 393d10f
Show file tree
Hide file tree
Showing 24 changed files with 222 additions and 323 deletions.
6 changes: 0 additions & 6 deletions apps/tlon-mobile/src/hooks/useNotificationListener.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,12 +154,6 @@ export default function useNotificationListener() {
}

const routeStack: RouteStack = [{ name: 'ChatList' }];
if (channel.groupId && !channelSwitcherEnabled) {
routeStack.push({
name: 'GroupChannels',
params: { groupId: channel.groupId },
});
}
const screenName = screenNameFromChannelId(channelId);
routeStack.push({
name: screenName,
Expand Down
2 changes: 1 addition & 1 deletion packages/app/features/channels/ChannelMembersScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ type Props = NativeStackScreenProps<RootStackParamList, 'ChannelMembers'>;

export function ChannelMembersScreen(props: Props) {
const { channelId } = props.route.params;
const channelQuery = store.useChannelWithRelations({
const channelQuery = store.useChannel({
id: channelId,
});

Expand Down
12 changes: 0 additions & 12 deletions packages/app/features/top/ChannelScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -319,16 +319,6 @@ export default function ChannelScreen(props: Props) {

const canUpload = useCanUpload();

const isFocused = useIsFocused();

const { data: pins } = store.usePins({
enabled: isFocused,
});

const pinnedItems = useMemo(() => {
return pins ?? [];
}, [pins]);

const chatOptionsNavProps = useChatSettingsNavigation();

const handleGoToUserProfile = useCallback(
Expand All @@ -350,8 +340,6 @@ export default function ChannelScreen(props: Props) {

return (
<ChatOptionsProvider
groupId={group?.id}
pinned={pinnedItems}
useGroup={store.useGroup}
onPressInvite={(group) => {
setInviteSheetGroup(group);
Expand Down
4 changes: 2 additions & 2 deletions packages/app/features/top/ChannelSearchScreen.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { NativeStackScreenProps } from '@react-navigation/native-stack';
import { useChannelSearch, useChannelWithRelations } from '@tloncorp/shared';
import { useChannel, useChannelSearch } from '@tloncorp/shared';
import type * as db from '@tloncorp/shared/db';
import { Button, SearchBar, SearchResults, XStack, YStack } from '@tloncorp/ui';
import { useCallback, useState } from 'react';
Expand All @@ -13,7 +13,7 @@ type Props = NativeStackScreenProps<RootStackParamList, 'ChannelSearch'>;
export default function ChannelSearchScreen(props: Props) {
const channelId = props.route.params.channelId;
const groupId = props.route.params.groupId;
const channelQuery = useChannelWithRelations({
const channelQuery = useChannel({
id: channelId,
});

Expand Down
66 changes: 13 additions & 53 deletions packages/app/features/top/ChatListScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,11 @@ import {
import type { NativeStackScreenProps } from '@react-navigation/native-stack';
import { createDevLogger } from '@tloncorp/shared';
import * as db from '@tloncorp/shared/db';
import * as logic from '@tloncorp/shared/logic';
import * as store from '@tloncorp/shared/store';
import {
AddGroupSheet,
ChatList,
ChatOptionsProvider,
ChatOptionsSheet,
ChatOptionsSheetMethods,
GroupPreviewAction,
GroupPreviewSheet,
InviteUsersSheet,
Expand All @@ -29,7 +26,6 @@ import { TLON_EMPLOYEE_GROUP } from '../../constants';
import { useChatSettingsNavigation } from '../../hooks/useChatSettingsNavigation';
import { useCurrentUserId } from '../../hooks/useCurrentUser';
import { useGroupActions } from '../../hooks/useGroupActions';
import { useFeatureFlag } from '../../lib/featureFlags';
import type { RootStackParamList } from '../../navigation/types';
import { screenNameFromChannelId } from '../../navigation/utils';
import { identifyTlonEmployee } from '../../utils/posthog';
Expand All @@ -53,19 +49,6 @@ export function ChatListScreenView({
const [addGroupOpen, setAddGroupOpen] = useState(false);
const [screenTitle, setScreenTitle] = useState('Home');
const [inviteSheetGroup, setInviteSheetGroup] = useState<db.Group | null>();
const chatOptionsSheetRef = useRef<ChatOptionsSheetMethods>(null);
const [longPressedChat, setLongPressedChat] = useState<db.Chat | null>(null);
const chatOptionsGroupId = useMemo(() => {
return longPressedChat?.type === 'group'
? longPressedChat.group.id
: undefined;
}, [longPressedChat]);

const chatOptionsChannelId = useMemo(() => {
return longPressedChat?.type === 'channel'
? longPressedChat.channel.id
: undefined;
}, [longPressedChat]);

const [activeTab, setActiveTab] = useState<'all' | 'groups' | 'messages'>(
'all'
Expand All @@ -77,10 +60,6 @@ export function ChatListScreenView({

const [showSearchInput, setShowSearchInput] = useState(false);
const isFocused = useIsFocused();
const { data: pins } = store.usePins({
enabled: isFocused,
});
const pinned = useMemo(() => pins ?? [], [pins]);

const { data: chats } = store.useCurrentChats({
enabled: isFocused,
Expand Down Expand Up @@ -172,44 +151,30 @@ export function ChatListScreenView({
}
}, []);

const [isChannelSwitcherEnabled] = useFeatureFlag('channelSwitcher');

const onPressChat = useCallback(
(item: db.Chat) => {
if (item.type === 'group' && item.isPending) {
setSelectedGroupId(item.id);
} else if (item.type === 'group' && !isChannelSwitcherEnabled) {
navigation.navigate('GroupChannels', { groupId: item.group.id });
} else if (item.type === 'group') {
if (!item.group.channels?.length) {
throw new Error('cant open group with no channels');
async (item: db.Chat) => {
if (item.type === 'group') {
if (item.isPending) {
setSelectedGroupId(item.id);
} else {
const mostRecentChannel = item.group.channels?.[0];
if (!mostRecentChannel) {
throw new Error("Can't open group: no channels");
}
navigation.navigate('Channel', {
channelId: mostRecentChannel.id,
});
}
navigation.navigate('Channel', {
channelId: item.group.channels[0].id,
groupId: item.group.id,
});
} else {
const screenName = screenNameFromChannelId(item.id);
navigation.navigate(screenName, {
channelId: item.id,
});
}
},
[isChannelSwitcherEnabled, navigation]
[navigation]
);

const onLongPressChat = useCallback((item: db.Chat) => {
if (item.isPending) {
return;
}
setLongPressedChat(item);
chatOptionsSheetRef.current?.open(
item.id,
item.type === 'channel' ? item.channel.type : 'group',
item.unreadCount
);
}, []);

const handleGroupPreviewSheetOpenChange = useCallback((open: boolean) => {
if (!open) {
setSelectedGroupId(null);
Expand Down Expand Up @@ -298,9 +263,6 @@ export function ChatListScreenView({
useGroup={store.useGroupPreview}
>
<ChatOptionsProvider
channelId={chatOptionsChannelId}
groupId={chatOptionsGroupId}
pinned={pinned}
{...useChatSettingsNavigation()}
onPressInvite={(group) => {
setInviteSheetGroup(group);
Expand Down Expand Up @@ -329,7 +291,6 @@ export function ChatListScreenView({
pinned={resolvedChats.pinned}
unpinned={resolvedChats.unpinned}
pending={resolvedChats.pending}
onLongPressItem={onLongPressChat}
onPressItem={onPressChat}
onSectionChange={handleSectionChange}
showSearchInput={showSearchInput}
Expand All @@ -343,7 +304,6 @@ export function ChatListScreenView({
open={splashVisible}
onOpenChange={handleWelcomeOpenChange}
/>
<ChatOptionsSheet ref={chatOptionsSheetRef} />
<GroupPreviewSheet
open={!!selectedGroup}
onOpenChange={handleGroupPreviewSheetOpenChange}
Expand Down
13 changes: 1 addition & 12 deletions packages/app/features/top/GroupChannelsScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
GroupChannelsScreenView,
InviteUsersSheet,
} from '@tloncorp/ui';
import { useCallback, useMemo, useState } from 'react';
import { useCallback, useState } from 'react';

import { useChatSettingsNavigation } from '../../hooks/useChatSettingsNavigation';
import { useGroupContext } from '../../hooks/useGroupContext';
Expand All @@ -30,11 +30,7 @@ export function GroupChannelsScreenContent({
groupId: string;
}) {
const navigation = useNavigation<NavigationProp<RootStackParamList>>();

const isFocused = useIsFocused();
const { data: pins } = store.usePins({
enabled: isFocused,
});
const [inviteSheetGroup, setInviteSheetGroup] = useState<db.Group | null>(
null
);
Expand All @@ -43,10 +39,6 @@ export function GroupChannelsScreenContent({
group?.id ?? ''
);

const pinnedItems = useMemo(() => {
return pins ?? [];
}, [pins]);

const handleChannelSelected = useCallback(
(channel: db.Channel) => {
navigation.navigate('Channel', {
Expand Down Expand Up @@ -79,9 +71,6 @@ export function GroupChannelsScreenContent({

return (
<ChatOptionsProvider
groupId={id}
pinned={pinnedItems}
useGroup={store.useGroup}
onPressInvite={(group) => {
setInviteSheetGroup(group);
}}
Expand Down
3 changes: 1 addition & 2 deletions packages/app/hooks/useChannelNavigation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ import { useCallback } from 'react';
import { RootStackParamList } from '../navigation/types';

export const useChannelNavigation = ({ channelId }: { channelId: string }) => {
// Model context
const channelQuery = store.useChannelWithRelations({
const channelQuery = store.useChannel({
id: channelId,
});

Expand Down
6 changes: 3 additions & 3 deletions packages/app/hooks/useGroupActions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,19 @@ import { useCallback } from 'react';
import { useGroupNavigation } from './useGroupNavigation';

export const useGroupActions = () => {
const { goToHome, goToGroupChannels } = useGroupNavigation();
const { goToHome, goToGroup } = useGroupNavigation();

const performGroupAction = useCallback(
async (action: GroupPreviewAction, updatedGroup: db.Group) => {
if (action === 'goTo') {
goToGroupChannels(updatedGroup.id);
goToGroup(updatedGroup.id);
}

if (action === 'joined') {
goToHome();
}
},
[goToGroupChannels, goToHome]
[goToGroup, goToHome]
);

return {
Expand Down
4 changes: 2 additions & 2 deletions packages/app/hooks/useGroupNavigation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export const useGroupNavigation = () => {
[navigation]
);

const goToGroupChannels = useCallback(
const goToGroup = useCallback(
async (groupId: string) => {
resetToGroup(groupId);
},
Expand All @@ -48,6 +48,6 @@ export const useGroupNavigation = () => {
goToChannel,
goToHome,
goToContactHostedGroups,
goToGroupChannels,
goToGroup,
};
};
18 changes: 16 additions & 2 deletions packages/app/navigation/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
NavigationProp,
useNavigation,
} from '@react-navigation/native';
import * as db from '@tloncorp/shared/db';
import * as logic from '@tloncorp/shared/logic';
import * as store from '@tloncorp/shared/store';

Expand Down Expand Up @@ -83,14 +84,27 @@ export function useResetToDm() {
export function useResetToGroup() {
const reset = useTypedReset();

return function resetToGroup(groupId: string) {
return async function resetToGroup(groupId: string) {
const channelId = await getInitialGroupChannel(groupId);
reset([
{ name: 'ChatList' },
{ name: 'GroupChannels', params: { groupId } },
{ name: 'Channel', params: { channelId, groupId } },
]);
};
}

async function getInitialGroupChannel(groupId: string) {
const group = await db.getGroup({ id: groupId });
const channelId =
group?.channels?.sort(
(a, b) => (a.lastPostAt ?? 0) - (b.lastPostAt ?? 0)
)[0]?.id ?? group?.channels?.[0]?.id;
if (!channelId) {
throw new Error('unable to find default group channel');
}
return channelId;
}

export function screenNameFromChannelId(channelId: string) {
return logic.isDmChannelId(channelId)
? 'DM'
Expand Down
27 changes: 7 additions & 20 deletions packages/shared/src/db/keyValue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,26 +30,6 @@ export const BASE_VOLUME_SETTING_QUERY_KEY = ['volume', 'base'];
export const SHOW_BENEFITS_SHEET_QUERY_KEY = ['showBenefitsSheet'];
export const THEME_STORAGE_KEY = '@user_theme';

export type ChannelSortPreference = 'recency' | 'arranged';
export async function storeChannelSortPreference(
sortPreference: ChannelSortPreference
) {
try {
await AsyncStorage.setItem('channelSortPreference', sortPreference);
} catch (error) {
logger.error('storeChannelSortPreference', error);
}
}

export async function getChannelSortPreference() {
try {
const value = await AsyncStorage.getItem('channelSortPreference');
return (value ?? 'recency') as ChannelSortPreference;
} catch (error) {
logger.error('getChannelSortPreference', error);
}
}

export async function getActivitySeenMarker() {
const marker = await AsyncStorage.getItem('activitySeenMarker');
return Number(marker) ?? 1;
Expand Down Expand Up @@ -312,3 +292,10 @@ export const themeSettings = createStorageItem<ThemeName | null>({
key: THEME_STORAGE_KEY,
defaultValue: null,
});

export type ChannelSortPreference = 'recency' | 'arranged';

export const channelSortPreference = createStorageItem<ChannelSortPreference>({
key: 'channelSortPreference',
defaultValue: 'recency',
});
10 changes: 2 additions & 8 deletions packages/shared/src/db/queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1429,16 +1429,9 @@ export const getAllSingleDms = createReadQuery(
[]
);

export interface GetChannelWithRelations {
id: string;
}

export const getChannelWithRelations = createReadQuery(
'getChannelWithRelations',
async (
{ id }: GetChannelWithRelations,
ctx: QueryCtx
): Promise<Channel | null> => {
async ({ id }: { id: string }, ctx: QueryCtx): Promise<Channel | null> => {
const result = await ctx.db.query.channels.findFirst({
where: eq($channels.id, id),
with: {
Expand Down Expand Up @@ -2609,6 +2602,7 @@ export const getGroup = createReadQuery(
.findFirst({
where: (groups, { eq }) => eq(groups.id, id),
with: {
unread: true,
pin: true,
channels: {
where: (channels, { eq }) => eq(channels.currentUserIsMember, true),
Expand Down
Loading

0 comments on commit 393d10f

Please sign in to comment.