aboutsummaryrefslogtreecommitdiffstats
path: root/apps/web/components/dashboard/lists/RssLink.tsx
blob: 1be486811afa92c1f46849fa11e9291a0d79f877 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
"use client";

import { useMemo } from "react";
import { Button } from "@/components/ui/button";
import { CopyBtnV2 } from "@/components/ui/copy-button";
import { Input } from "@/components/ui/input";
import { Label } from "@/components/ui/label";
import { Switch } from "@/components/ui/switch";
import { useClientConfig } from "@/lib/clientConfig";
import { api } from "@/lib/trpc";
import { Loader2, RotateCcw } from "lucide-react";
import { useTranslation } from "react-i18next";

export default function RssLink({ listId }: { listId: string }) {
  const { t } = useTranslation();
  const clientConfig = useClientConfig();
  const apiUtils = api.useUtils();

  const { mutate: regenRssToken, isPending: isRegenPending } =
    api.lists.regenRssToken.useMutation({
      onSuccess: () => {
        apiUtils.lists.getRssToken.invalidate({ listId });
      },
    });
  const { mutate: clearRssToken, isPending: isClearPending } =
    api.lists.clearRssToken.useMutation({
      onSuccess: () => {
        apiUtils.lists.getRssToken.invalidate({ listId });
      },
    });
  const { data: rssToken, isLoading: isTokenLoading } =
    api.lists.getRssToken.useQuery({ listId });

  const rssUrl = useMemo(() => {
    if (!rssToken || !rssToken.token) {
      return null;
    }
    return `${clientConfig.publicApiUrl}/v1/rss/lists/${listId}?token=${rssToken.token}`;
  }, [rssToken]);

  const rssEnabled = rssUrl !== null;

  return (
    <>
      {/* RSS Feed Toggle */}
      <div className="flex items-center justify-between">
        <div className="space-y-1">
          <Label htmlFor="rss-toggle" className="text-sm font-medium">
            {t("lists.rss.title")}
          </Label>
          <p className="text-xs text-muted-foreground">
            {t("lists.rss.description")}
          </p>
        </div>
        <Switch
          id="rss-toggle"
          checked={rssEnabled}
          onCheckedChange={(checked) =>
            checked ? regenRssToken({ listId }) : clearRssToken({ listId })
          }
          disabled={
            isTokenLoading ||
            isClearPending ||
            isRegenPending ||
            !!clientConfig.demoMode
          }
        />
      </div>
      {/* RSS URL - only show when RSS is enabled */}
      {rssEnabled && (
        <div className="space-y-3">
          <Label className="text-sm font-medium">
            {t("lists.rss.feed_url")}
          </Label>
          <div className="flex items-center space-x-2">
            <Input value={rssUrl} readOnly className="flex-1 text-sm" />
            <CopyBtnV2 getStringToCopy={() => rssUrl} />
            <Button
              variant="outline"
              size="sm"
              onClick={() => regenRssToken({ listId })}
              disabled={isRegenPending}
            >
              {isRegenPending ? (
                <Loader2 className="h-4 w-4 animate-spin" />
              ) : (
                <RotateCcw className="h-4 w-4" />
              )}
            </Button>
          </div>
        </div>
      )}
    </>
  );
}