File size: 7,989 Bytes
55d7c10
 
 
 
 
 
 
 
 
 
715230e
55d7c10
 
 
c121e6a
55d7c10
c121e6a
 
 
 
 
 
 
 
 
55d7c10
 
 
 
 
 
715230e
 
 
 
55d7c10
 
 
 
 
a6d1411
2f80fef
 
 
55d7c10
 
2f80fef
55d7c10
 
 
 
 
 
 
 
 
 
 
 
 
 
 
715230e
55d7c10
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
715230e
 
 
 
 
 
 
 
55d7c10
 
 
 
c121e6a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
55d7c10
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a14caa5
 
 
55d7c10
 
 
715230e
 
 
55d7c10
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
715230e
a6d1411
55d7c10
 
c121e6a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
55d7c10
 
715230e
 
 
 
 
 
 
 
55d7c10
 
 
 
 
 
 
 
 
 
 
715230e
 
 
55d7c10
 
 
 
 
 
 
 
715230e
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
import React, { useState, useMemo } from "react";
import { Input } from "./ui/input";
import {
  Dialog,
  DialogContent,
  DialogHeader,
  DialogTitle,
  DialogTrigger,
} from "./ui/dialog";
import { Button } from "./ui/button";
import { fetchAllAuthorsData, fetchOrganizationData } from "../utils/authors";
import { generateCalendarData } from "../utils/calendar";
import Heatmap from "./Heatmap";
import { ModelData } from "../types/heatmap";
import { LEADERBOARD_SIZE } from "../constants/organizations";

interface UserSearchDialogProps {
  candidateAuthors: string[];
  minActivityCount: number;
}

const UserSearchDialog: React.FC<UserSearchDialogProps> = ({
  candidateAuthors,
  minActivityCount,
}) => {
  const [isOpen, setIsOpen] = useState(false);
  const [isLoading, setIsLoading] = useState(false);
  const [searchInput, setSearchInput] = useState("");
  const [searchedData, setSearchedData] = useState<ModelData[] | null>(null);
  const [isCopied, setIsCopied] = useState(false);
  const [currentSearchTerm, setCurrentSearchTerm] = useState("");
  const [userInfo, setUserInfo] = useState<{
    fullName: string;
    avatarUrl: string | null;
  } | null>(null);

  const handleSearch = async () => {
    if (searchInput.trim()) {
      setIsLoading(true);
      try {
        const authorInfo = await fetchOrganizationData([searchInput.trim()]);
        const resolvedName =
          authorInfo.authorsData?.[0]?.author || searchInput.trim();
        const authorData = await fetchAllAuthorsData([resolvedName]);
        setSearchedData(authorData);
        setUserInfo(authorInfo);
        setCurrentSearchTerm(resolvedName);
      } catch (error) {
        console.error("Error fetching data for searched user:", error);
        setSearchedData(null);
        setUserInfo(null);
        setCurrentSearchTerm("");
      }
      setIsLoading(false);
    } else {
      setSearchedData(null);
      setUserInfo(null);
      setCurrentSearchTerm("");
    }
  };

  const handleKeyPress = (e: React.KeyboardEvent<HTMLInputElement>) => {
    if (e.key === "Enter") {
      handleSearch();
    }
  };

  const handleInputChange = (e: React.ChangeEvent<HTMLInputElement>) => {
    setSearchInput(e.target.value);
  };

  const getIframeCode = (username: string) => {
    return `<iframe
  src="https://cfahlgren1-model-release-heatmap.hf.space/${username}"
  class="w-full h-[300px]"
  frameborder="0"
></iframe>`;
  };

  const handleCopyCode = () => {
    const iframeCode = getIframeCode(searchInput);
    navigator.clipboard.writeText(iframeCode).then(() => {
      setIsCopied(true);
      setTimeout(() => setIsCopied(false), 2000);
    });
  };

  const searchedHeatmapData = useMemo(() => {
    if (searchedData && searchedData.length > 0 && userInfo) {
      return generateCalendarData(searchedData, [
        {
          authors: [currentSearchTerm],
          color: "#0088cc",
          fullName: userInfo.fullName,
          avatarUrl: userInfo.avatarUrl,
        },
      ])[currentSearchTerm];
    }
    return null;
  }, [searchedData, currentSearchTerm, userInfo]);

  const suggestionBanner = useMemo(() => {
    if (!searchedHeatmapData || !currentSearchTerm) return null;
    const isTracked = candidateAuthors.some(
      (a) => a.toLowerCase() === currentSearchTerm.toLowerCase()
    );
    if (isTracked) return null;
    const totalActivity = searchedHeatmapData.reduce(
      (sum, day) => sum + day.count,
      0
    );
    if (totalActivity < minActivityCount) return null;
    const title = encodeURIComponent(
      `Add ${currentSearchTerm} to tracked organizations`
    );
    const description = encodeURIComponent(
      `${currentSearchTerm} has ${totalActivity} new repos in the last year and qualifies for the top 16.`
    );
    const url = `https://huggingface.co/spaces/v1an1/model-release-heatmap/discussions/new?title=${title}&description=${description}`;
    return { totalActivity, url };
  }, [searchedHeatmapData, currentSearchTerm, candidateAuthors, minActivityCount]);

  const handleDialogOpenChange = (open: boolean) => {
    setIsOpen(open);
    if (!open) {
      setSearchInput("");
      setSearchedData(null);
      setCurrentSearchTerm("");
      setIsLoading(false);
      setIsCopied(false);
      setUserInfo(null);
    }
  };

  return (
    <Dialog open={isOpen} onOpenChange={handleDialogOpenChange}>
      <DialogTrigger asChild>
        <button className="text-sm text-foreground hover:text-blue-500 transition-colors duration-200">
          Search
        </button>
      </DialogTrigger>
      <DialogContent className="w-full max-w-[95vw] sm:max-w-4xl p-4 sm:p-6 max-h-[90vh] flex flex-col">
        <DialogHeader>
          <DialogTitle className="text-lg sm:text-xl mb-4">
            Get your Hugging Face Heatmap
          </DialogTitle>
        </DialogHeader>
        <div className="flex-grow overflow-y-auto">
          <div className="grid gap-4 py-4">
            <div className="flex items-center space-x-2 p-2 bg-background rounded-md">
              <Input
                type="text"
                placeholder="Enter username"
                value={searchInput}
                onChange={handleInputChange}
                onKeyDown={handleKeyPress}
                className="flex-grow"
              />
            </div>
            {isLoading ? (
              <p className="text-center">Loading...</p>
            ) : searchedHeatmapData && userInfo ? (
              <div className="mt-4 space-y-4">
                <div className="overflow-x-auto pb-2">
                  <Heatmap
                    data={searchedHeatmapData}
                    color="#FF9D00"
                    providerName={currentSearchTerm}
                    fullName={userInfo.fullName}
                    avatarUrl={userInfo.avatarUrl || ""}
                    authorId={currentSearchTerm}
                  />
                </div>
                {suggestionBanner && (
                  <div className="rounded-lg border border-blue-500/40 bg-blue-500/10 px-4 py-3 flex flex-col sm:flex-row sm:items-center gap-2">
                    <p className="text-sm text-foreground flex-grow">
                      <strong>{currentSearchTerm}</strong> has{" "}
                      {suggestionBanner.totalActivity} repos this year and
                      qualifies for the top {LEADERBOARD_SIZE}!
                    </p>
                    <a
                      href={suggestionBanner.url}
                      target="_blank"
                      rel="noopener noreferrer"
                      className="text-sm font-medium text-blue-500 hover:text-blue-400 whitespace-nowrap transition-colors"
                    >
                      Request to add &rarr;
                    </a>
                  </div>
                )}
                <div>
                  <div className="flex justify-between items-center mb-2">
                    <h3 className="font-semibold text-sm sm:text-base">
                      Embed in iFrame
                    </h3>
                    <Button
                      onClick={handleCopyCode}
                      variant="link"
                      size="sm"
                    >
                      {isCopied ? "Copied!" : "Copy"}
                    </Button>
                  </div>
                  <div className="overflow-x-auto">
                    <pre className="bg-secondary p-2 rounded text-xs whitespace-pre-wrap break-all">
                      <code>{getIframeCode(searchInput)}</code>
                    </pre>
                  </div>
                </div>
              </div>
            ) : searchedData !== null && searchedData.length === 0 ? (
              <p className="text-center text-slate-500 text-sm italic">
                User or Organization not found
              </p>
            ) : null}
          </div>
        </div>
      </DialogContent>
    </Dialog>
  );
};

export default UserSearchDialog;