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

refine manage channels screen #4319

Merged
merged 5 commits into from
Jan 10, 2025
Merged
Show file tree
Hide file tree
Changes from 4 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
43 changes: 33 additions & 10 deletions packages/app/hooks/useGroupContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,20 @@ export const useGroupContext = ({
const groupNavSectionsWithChannels = useMemo(() => {
return groupNavSections.map((section) => ({
...section,
channels: groupChannels.filter((channel) =>
section.channels.map((c) => c.channelId).includes(channel.id)
),
channels: groupChannels
.filter((channel) =>
section.channels.map((c) => c.channelId).includes(channel.id)
)
.sort((a, b) => {
const aIndex =
section.channels.find((c) => c.channelId === a.id)?.channelIndex ??
0;
const bIndex =
section.channels.find((c) => c.channelId === b.id)?.channelIndex ??
0;

return aIndex - bIndex;
}),
}));
}, [groupNavSections, groupChannels]);

Expand Down Expand Up @@ -164,7 +175,7 @@ export const useGroupContext = ({
async (channelId: string, navSectionId: string, index: number) => {
if (group) {
await store.moveChannel({
group,
groupId: group.id,
channelId,
navSectionId,
index,
Expand All @@ -176,13 +187,25 @@ export const useGroupContext = ({

const moveChannelToNavSection = useCallback(
async (channelId: string, navSectionId: string) => {
if (group) {
await store.addChannelToNavSection({
group,
channelId,
navSectionId,
});
if (!group) return;

// Find current section for the channel
const currentSection = group.navSections?.find((section) =>
section.channels?.some((channel) => channel.channelId === channelId)
);

if (!currentSection) {
console.error('Channel not found in any section');
return;
}

// Use addChannelToNavSection which handles both adding to new section
// and removing from the previous section
await store.addChannelToNavSection({
groupId: group.id,
channelId,
navSectionId,
});
},
[group]
);
Expand Down
4 changes: 4 additions & 0 deletions packages/shared/src/api/groupsApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -743,6 +743,7 @@ export type GroupChannelDelete = {
export type GroupChannelNavSectionAdd = {
type: 'addChannelToNavSection';
channelId: string;
groupId: string;
navSectionId: string;
sectionId: string;
};
Expand Down Expand Up @@ -778,6 +779,7 @@ export type GroupnavSectionMoveChannel = {
type: 'moveChannel';
navSectionId: string;
sectionId: string;
groupId: string;
channelId: string;
index: number;
};
Expand Down Expand Up @@ -1205,6 +1207,7 @@ export const toGroupUpdate = (
type: 'moveChannel',
navSectionId,
sectionId,
groupId,
channelId: zoneDelta['mov-nest'].nest,
index: zoneDelta['mov-nest'].idx,
};
Expand Down Expand Up @@ -1262,6 +1265,7 @@ export const toGroupUpdate = (
return {
type: 'addChannelToNavSection',
channelId,
groupId,
navSectionId: `${groupId}-${zoneId}`,
sectionId: zoneId,
};
Expand Down
34 changes: 31 additions & 3 deletions packages/shared/src/db/queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -673,7 +673,7 @@ export const updateGroup = createWriteQuery(
async (group: Partial<Group> & { id: string }, ctx: QueryCtx) => {
return ctx.db.update($groups).set(group).where(eq($groups.id, group.id));
},
['groups']
['groups', 'channels', 'groupNavSections', 'groupNavSectionChannels']
);

export const deleteGroup = createWriteQuery(
Expand Down Expand Up @@ -1604,7 +1604,34 @@ export const addNavSectionToGroup = createWriteQuery(
set: conflictUpdateSetAll($groupNavSections),
});
},
['groupNavSections']
['groups','groupNavSections', 'groupNavSectionChannels']
);

export const updateNavSectionChannel = createWriteQuery(
'updateNavSectionChannel',
async (
{
channelId,
groupNavSectionId,
channelIndex,
}: {
channelId: string;
groupNavSectionId: string;
channelIndex: number;
},
ctx: QueryCtx
) => {
return ctx.db
.update($groupNavSectionChannels)
.set({ channelIndex })
.where(
and(
eq($groupNavSectionChannels.channelId, channelId),
eq($groupNavSectionChannels.groupNavSectionId, groupNavSectionId)
)
);
},
['groupNavSectionChannels']
);

export const updateNavSection = createWriteQuery(
Expand Down Expand Up @@ -1656,7 +1683,7 @@ export const addChannelToNavSection = createWriteQuery(
})
.onConflictDoNothing();
},
['groupNavSectionChannels']
['groupNavSectionChannels', 'groups']
);

export const deleteChannelFromNavSection = createWriteQuery(
Expand Down Expand Up @@ -2765,6 +2792,7 @@ export const getGroup = createReadQuery(
'channels',
'groupJoinRequests',
'groupMemberBans',
'groupNavSectionChannels'
]
);

Expand Down
Loading
Loading