summaryrefslogtreecommitdiff
path: root/gui/src/components/nostr/User.tsx
blob: a9e9e2f5fada31213f28acd0c50573e29df1d0a1 (plain)
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import useLocalState from "@/state/state";
import { useState } from "react";
import Icon from "@/components/Icon";
import toast from "react-hot-toast";
import type { UserType } from "@/types/nostrill";
import type { FC } from "@/types/trill";
import Composer from "../composer/Composer";
import PostList from "@/components/feed/PostList";

export default function NostrUser({
  user,
  userString,
  feed,
  isFollowLoading,
  setIsFollowLoading,
  isAccessLoading,
  setIsAccessLoading,
}: {
  user: UserType;
  userString: string;
  feed: FC | undefined;
  isFollowLoading: boolean;
  setIsFollowLoading: (b: boolean) => void;
  isAccessLoading: boolean;
  setIsAccessLoading: (b: boolean) => void;
}) {
  const { api } = useLocalState((s) => ({
    api: s.api,
  }));
  const [fc, setFC] = useState<FC>();

  // Show empty state with resync option when no feed data

  async function refetch() {
    //
  }
  async function handleFollow() {
    if (!api) return;

    setIsFollowLoading(true);
    try {
      if (feed) {
        await api.unfollow(user);
      } else {
        await api.follow(user);
        toast.success(`Follow request sent to ${userString}`);
      }
    } catch (error) {
      toast.error(`Failed to ${!!feed ? "unfollow" : "follow"} ${userString}`);
      setIsFollowLoading(false);
      console.error("Follow error:", error);
    }
  }
  async function handleRequestAccess() {
    if (!api) return;

    setIsAccessLoading(true);
    // try {
    //   const res = await api.peekFeed(user.urbit);
    //   toast.success(`Access request sent to ${user.urbit}`);
    //   addNotification({
    //     type: "access_request",
    //     from: userString,
    //     message: `Access request sent to ${userString}`,
    //   });
    //   if ("error" in res) toast.error(res.error);
    //   else {
    //     console.log("peeked", res.ok.feed);
    //     setFC(res.ok.feed);
    //     if (res.ok.profile) addProfile(userString, res.ok.profile);
    //   }
    // } catch (error) {
    //   toast.error(`Failed to request access from ${user.urbit}`);
    //   console.error("Access request error:", error);
    // } finally {
    //   setIsAccessLoading(false);
    // }
  }
  return (
    <>
      <div className="user-actions">
        <button
          onClick={handleFollow}
          disabled={isFollowLoading}
          className={`action-btn ${!!feed ? "" : "follow"}`}
        >
          {isFollowLoading ? (
            <>
              <Icon name="settings" size={16} />
              {!!feed ? "Unfollowing..." : "Following..."}
            </>
          ) : (
            <>
              <Icon name={!!feed ? "bell" : "pals"} size={16} />
              {!!feed ? "Unfollow" : "Follow"}
            </>
          )}
        </button>

        {(!feed || !feed.feed || Object.keys(feed.feed).length === 0) && (
          <button
            onClick={handleRequestAccess}
            disabled={isAccessLoading}
            className="action-btn access"
          >
            {isAccessLoading ? (
              <>
                <Icon name="settings" size={16} />
                Fetching...
              </>
            ) : (
              <>
                <Icon name="key" size={16} />
                Fetch Feed
              </>
            )}
          </button>
        )}
      </div>
      {(feed || fc) && (
        <div id="feed-proper">
          <Composer />
          <PostList data={(feed || fc)!} refetch={refetch} />
        </div>
      )}
    </>
  );
}