File size: 5,500 Bytes
55d7c10 |
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 |
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 } from "../utils/authors";
import { generateCalendarData } from "../utils/calendar";
import Heatmap from "./Heatmap";
import { ModelData } from "../types/heatmap";
import { fetchAuthorData, fetchUserData } from "@/utils/authors";
const UserSearchDialog = () => {
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 authorData = await fetchAllAuthorsData([searchInput.trim()]);
const authorInfo = await fetchUserData([searchInput.trim()]);
setSearchedData(authorData);
setUserInfo(authorInfo);
setCurrentSearchTerm(searchInput.trim());
} 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 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 variant="outline">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 || ''}
/>
</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; |