Merge pull request #15 from xCyanGrizzly/copilot/fix-channel-tab-issues

Fix inactive source channels and add Fetch Channels button to Channels tab
This commit is contained in:
xCyanGrizzly
2026-03-05 23:50:03 +01:00
committed by GitHub
3 changed files with 43 additions and 7 deletions

View File

@@ -2,8 +2,10 @@
import { useState, useTransition } from "react";
import { toast } from "sonner";
import { Download } from "lucide-react";
import { getChannelColumns } from "./channel-columns";
import { DestinationCard } from "./destination-card";
import { ChannelPickerDialog } from "./channel-picker-dialog";
import {
deleteChannel,
toggleChannelActive,
@@ -12,18 +14,24 @@ import {
} from "../actions";
import { DataTable } from "@/components/shared/data-table";
import { DeleteDialog } from "@/components/shared/delete-dialog";
import type { ChannelRow, GlobalDestination } from "@/lib/telegram/admin-queries";
import { Button } from "@/components/ui/button";
import type { AccountRow, ChannelRow, GlobalDestination } from "@/lib/telegram/admin-queries";
import { useDataTable } from "@/hooks/use-data-table";
interface ChannelsTabProps {
channels: ChannelRow[];
globalDestination: GlobalDestination;
accounts: AccountRow[];
}
export function ChannelsTab({ channels, globalDestination }: ChannelsTabProps) {
export function ChannelsTab({ channels, globalDestination, accounts }: ChannelsTabProps) {
const [isPending, startTransition] = useTransition();
const [deleteId, setDeleteId] = useState<string | null>(null);
const [rescanId, setRescanId] = useState<string | null>(null);
const [fetchChannelsAccountId, setFetchChannelsAccountId] = useState<string | null>(null);
// Find the first authenticated account for "Fetch Channels"
const authenticatedAccounts = accounts.filter((a) => a.authState === "AUTHENTICATED" && a.isActive);
const columns = getChannelColumns({
onToggleActive: (id) => {
@@ -76,19 +84,38 @@ export function ChannelsTab({ channels, globalDestination }: ChannelsTabProps) {
});
};
const handleFetchChannels = () => {
if (authenticatedAccounts.length > 0) {
setFetchChannelsAccountId(authenticatedAccounts[0].id);
} else {
toast.error("No authenticated accounts available. Add and authenticate an account first.");
}
};
return (
<div className="space-y-4">
<DestinationCard destination={globalDestination} />
<div className="flex items-center gap-2">
<Button
variant="outline"
onClick={handleFetchChannels}
disabled={authenticatedAccounts.length === 0}
>
<Download className="mr-2 h-4 w-4" />
Fetch Channels
</Button>
</div>
{channels.length > 0 && (
<p className="text-xs text-muted-foreground">
Source channels are added per-account via the &quot;Fetch Channels&quot; button on the Accounts tab.
Channels discovered via &quot;Fetch Channels&quot; are automatically activated as sources.
</p>
)}
<DataTable
table={table}
emptyMessage="No channels yet. Use &quot;Fetch Channels&quot; on an account to discover and add source channels."
emptyMessage="No channels yet. Click &quot;Fetch Channels&quot; above to discover and add source channels."
/>
<DeleteDialog
@@ -109,6 +136,14 @@ export function ChannelsTab({ channels, globalDestination }: ChannelsTabProps) {
onConfirm={handleRescan}
isLoading={isPending}
/>
<ChannelPickerDialog
accountId={fetchChannelsAccountId}
open={!!fetchChannelsAccountId}
onOpenChange={(open) => {
if (!open) setFetchChannelsAccountId(null);
}}
/>
</div>
);
}

View File

@@ -53,7 +53,7 @@ export function TelegramAdmin({
<AccountsTab accounts={accounts} />
</TabsContent>
<TabsContent value="channels">
<ChannelsTab channels={channels} globalDestination={globalDestination} />
<ChannelsTab channels={channels} globalDestination={globalDestination} accounts={accounts} />
</TabsContent>
<TabsContent value="sends">
<BotSendsTab history={sendHistory} />

View File

@@ -453,7 +453,7 @@ export async function saveChannelSelections(
try {
let linked = 0;
for (const ch of channels) {
// Upsert the channel record (new channels default to disabled)
// Upsert the channel record and activate it (user explicitly selected it)
const channel = await prisma.telegramChannel.upsert({
where: { telegramId: BigInt(ch.telegramId) },
create: {
@@ -461,11 +461,12 @@ export async function saveChannelSelections(
title: ch.title,
type: "SOURCE",
isForum: ch.isForum,
isActive: false,
isActive: true,
},
update: {
title: ch.title,
isForum: ch.isForum,
isActive: true,
},
});