mirror of
https://github.com/xCyanGrizzly/DragonsStash.git
synced 2026-05-11 06:11:15 +00:00
Fix channels not active after selection and add Fetch Channels button to Channels tab
Co-authored-by: xCyanGrizzly <53275238+xCyanGrizzly@users.noreply.github.com>
This commit is contained in:
@@ -2,8 +2,10 @@
|
|||||||
|
|
||||||
import { useState, useTransition } from "react";
|
import { useState, useTransition } from "react";
|
||||||
import { toast } from "sonner";
|
import { toast } from "sonner";
|
||||||
|
import { Download } from "lucide-react";
|
||||||
import { getChannelColumns } from "./channel-columns";
|
import { getChannelColumns } from "./channel-columns";
|
||||||
import { DestinationCard } from "./destination-card";
|
import { DestinationCard } from "./destination-card";
|
||||||
|
import { ChannelPickerDialog } from "./channel-picker-dialog";
|
||||||
import {
|
import {
|
||||||
deleteChannel,
|
deleteChannel,
|
||||||
toggleChannelActive,
|
toggleChannelActive,
|
||||||
@@ -12,18 +14,24 @@ import {
|
|||||||
} from "../actions";
|
} from "../actions";
|
||||||
import { DataTable } from "@/components/shared/data-table";
|
import { DataTable } from "@/components/shared/data-table";
|
||||||
import { DeleteDialog } from "@/components/shared/delete-dialog";
|
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";
|
import { useDataTable } from "@/hooks/use-data-table";
|
||||||
|
|
||||||
interface ChannelsTabProps {
|
interface ChannelsTabProps {
|
||||||
channels: ChannelRow[];
|
channels: ChannelRow[];
|
||||||
globalDestination: GlobalDestination;
|
globalDestination: GlobalDestination;
|
||||||
|
accounts: AccountRow[];
|
||||||
}
|
}
|
||||||
|
|
||||||
export function ChannelsTab({ channels, globalDestination }: ChannelsTabProps) {
|
export function ChannelsTab({ channels, globalDestination, accounts }: ChannelsTabProps) {
|
||||||
const [isPending, startTransition] = useTransition();
|
const [isPending, startTransition] = useTransition();
|
||||||
const [deleteId, setDeleteId] = useState<string | null>(null);
|
const [deleteId, setDeleteId] = useState<string | null>(null);
|
||||||
const [rescanId, setRescanId] = 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({
|
const columns = getChannelColumns({
|
||||||
onToggleActive: (id) => {
|
onToggleActive: (id) => {
|
||||||
@@ -76,19 +84,41 @@ export function ChannelsTab({ channels, globalDestination }: ChannelsTabProps) {
|
|||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const handleFetchChannels = () => {
|
||||||
|
if (authenticatedAccounts.length === 1) {
|
||||||
|
setFetchChannelsAccountId(authenticatedAccounts[0].id);
|
||||||
|
} else if (authenticatedAccounts.length > 1) {
|
||||||
|
// Use the first authenticated account by default
|
||||||
|
setFetchChannelsAccountId(authenticatedAccounts[0].id);
|
||||||
|
} else {
|
||||||
|
toast.error("No authenticated accounts available. Add and authenticate an account first.");
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="space-y-4">
|
<div className="space-y-4">
|
||||||
<DestinationCard destination={globalDestination} />
|
<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 && (
|
{channels.length > 0 && (
|
||||||
<p className="text-xs text-muted-foreground">
|
<p className="text-xs text-muted-foreground">
|
||||||
Source channels are added per-account via the "Fetch Channels" button on the Accounts tab.
|
Channels discovered via "Fetch Channels" are automatically activated as sources.
|
||||||
</p>
|
</p>
|
||||||
)}
|
)}
|
||||||
|
|
||||||
<DataTable
|
<DataTable
|
||||||
table={table}
|
table={table}
|
||||||
emptyMessage="No channels yet. Use "Fetch Channels" on an account to discover and add source channels."
|
emptyMessage="No channels yet. Click "Fetch Channels" above to discover and add source channels."
|
||||||
/>
|
/>
|
||||||
|
|
||||||
<DeleteDialog
|
<DeleteDialog
|
||||||
@@ -109,6 +139,14 @@ export function ChannelsTab({ channels, globalDestination }: ChannelsTabProps) {
|
|||||||
onConfirm={handleRescan}
|
onConfirm={handleRescan}
|
||||||
isLoading={isPending}
|
isLoading={isPending}
|
||||||
/>
|
/>
|
||||||
|
|
||||||
|
<ChannelPickerDialog
|
||||||
|
accountId={fetchChannelsAccountId}
|
||||||
|
open={!!fetchChannelsAccountId}
|
||||||
|
onOpenChange={(open) => {
|
||||||
|
if (!open) setFetchChannelsAccountId(null);
|
||||||
|
}}
|
||||||
|
/>
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -53,7 +53,7 @@ export function TelegramAdmin({
|
|||||||
<AccountsTab accounts={accounts} />
|
<AccountsTab accounts={accounts} />
|
||||||
</TabsContent>
|
</TabsContent>
|
||||||
<TabsContent value="channels">
|
<TabsContent value="channels">
|
||||||
<ChannelsTab channels={channels} globalDestination={globalDestination} />
|
<ChannelsTab channels={channels} globalDestination={globalDestination} accounts={accounts} />
|
||||||
</TabsContent>
|
</TabsContent>
|
||||||
<TabsContent value="sends">
|
<TabsContent value="sends">
|
||||||
<BotSendsTab history={sendHistory} />
|
<BotSendsTab history={sendHistory} />
|
||||||
|
|||||||
@@ -453,7 +453,7 @@ export async function saveChannelSelections(
|
|||||||
try {
|
try {
|
||||||
let linked = 0;
|
let linked = 0;
|
||||||
for (const ch of channels) {
|
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({
|
const channel = await prisma.telegramChannel.upsert({
|
||||||
where: { telegramId: BigInt(ch.telegramId) },
|
where: { telegramId: BigInt(ch.telegramId) },
|
||||||
create: {
|
create: {
|
||||||
@@ -461,11 +461,12 @@ export async function saveChannelSelections(
|
|||||||
title: ch.title,
|
title: ch.title,
|
||||||
type: "SOURCE",
|
type: "SOURCE",
|
||||||
isForum: ch.isForum,
|
isForum: ch.isForum,
|
||||||
isActive: false,
|
isActive: true,
|
||||||
},
|
},
|
||||||
update: {
|
update: {
|
||||||
title: ch.title,
|
title: ch.title,
|
||||||
isForum: ch.isForum,
|
isForum: ch.isForum,
|
||||||
|
isActive: true,
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user