Spaces:
Running
Running
Create SearchPage.jsx
Browse files- SearchPage.jsx +138 -0
SearchPage.jsx
ADDED
|
@@ -0,0 +1,138 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import React, { useState, useEffect } from 'react';
|
| 2 |
+
import axios from 'axios';
|
| 3 |
+
|
| 4 |
+
const filterOptions = ["real", "ai", "futanari", "vanilla"];
|
| 5 |
+
|
| 6 |
+
const SearchPage = () => {
|
| 7 |
+
const [query, setQuery] = useState('');
|
| 8 |
+
const [filters, setFilters] = useState({
|
| 9 |
+
real: true, ai: true, futanari: false, vanilla: false,
|
| 10 |
+
});
|
| 11 |
+
const [result, setResult] = useState(null);
|
| 12 |
+
const [history, setHistory] = useState([]);
|
| 13 |
+
|
| 14 |
+
const token = localStorage.getItem("token");
|
| 15 |
+
|
| 16 |
+
const runSearch = async (customQuery) => {
|
| 17 |
+
try {
|
| 18 |
+
const res = await axios.post(
|
| 19 |
+
"https://your-hf-space.hf.space/search",
|
| 20 |
+
{ query: customQuery || query, filters },
|
| 21 |
+
{ headers: { Authorization: `Bearer ${token}` } }
|
| 22 |
+
);
|
| 23 |
+
setResult(res.data);
|
| 24 |
+
} catch {
|
| 25 |
+
alert("Error or Unauthorized.");
|
| 26 |
+
}
|
| 27 |
+
};
|
| 28 |
+
|
| 29 |
+
useEffect(() => {
|
| 30 |
+
const fetchHistory = async () => {
|
| 31 |
+
try {
|
| 32 |
+
const res = await axios.get("https://your-hf-space.hf.space/history", {
|
| 33 |
+
headers: { Authorization: `Bearer ${token}` },
|
| 34 |
+
});
|
| 35 |
+
setHistory(res.data);
|
| 36 |
+
} catch {
|
| 37 |
+
// ignore
|
| 38 |
+
}
|
| 39 |
+
};
|
| 40 |
+
fetchHistory();
|
| 41 |
+
}, [token]);
|
| 42 |
+
|
| 43 |
+
const handleCheckbox = (e) => {
|
| 44 |
+
const { name, checked } = e.target;
|
| 45 |
+
setFilters((prev) => ({ ...prev, [name]: checked }));
|
| 46 |
+
};
|
| 47 |
+
|
| 48 |
+
const clearHistory = async () => {
|
| 49 |
+
if (!window.confirm("Are you sure you want to clear your search history?")) return;
|
| 50 |
+
try {
|
| 51 |
+
await axios.delete("https://your-hf-space.hf.space/history", {
|
| 52 |
+
headers: { Authorization: `Bearer ${token}` },
|
| 53 |
+
});
|
| 54 |
+
setHistory([]);
|
| 55 |
+
} catch {
|
| 56 |
+
alert("Could not clear history.");
|
| 57 |
+
}
|
| 58 |
+
};
|
| 59 |
+
|
| 60 |
+
return (
|
| 61 |
+
<div className="max-w-2xl mx-auto p-6">
|
| 62 |
+
<h1 className="text-3xl font-bold mb-6">NSFW AI Search</h1>
|
| 63 |
+
<div className="mb-4">
|
| 64 |
+
<h3 className="text-sm font-semibold mb-1">Filters:</h3>
|
| 65 |
+
<div className="flex flex-wrap gap-4">
|
| 66 |
+
{filterOptions.map((f) => (
|
| 67 |
+
<label key={f} className="flex items-center gap-1 text-sm">
|
| 68 |
+
<input
|
| 69 |
+
type="checkbox"
|
| 70 |
+
name={f}
|
| 71 |
+
checked={filters[f]}
|
| 72 |
+
onChange={handleCheckbox}
|
| 73 |
+
className="accent-purple-600"
|
| 74 |
+
/>
|
| 75 |
+
{f.charAt(0).toUpperCase() + f.slice(1)}
|
| 76 |
+
</label>
|
| 77 |
+
))}
|
| 78 |
+
</div>
|
| 79 |
+
</div>
|
| 80 |
+
<div className="flex space-x-4 mb-4">
|
| 81 |
+
<input
|
| 82 |
+
type="text"
|
| 83 |
+
placeholder="Enter fantasy or prompt"
|
| 84 |
+
className="flex-1 px-4 py-2 border rounded-lg"
|
| 85 |
+
value={query} onChange={e => setQuery(e.target.value)}
|
| 86 |
+
/>
|
| 87 |
+
<button
|
| 88 |
+
className="bg-purple-600 text-white px-4 py-2 rounded-lg hover:bg-purple-700"
|
| 89 |
+
onClick={() => runSearch()}
|
| 90 |
+
>
|
| 91 |
+
Search
|
| 92 |
+
</button>
|
| 93 |
+
</div>
|
| 94 |
+
{result && (
|
| 95 |
+
<div className="bg-white rounded-xl p-4 shadow-md space-y-3">
|
| 96 |
+
<h2 className="text-xl font-bold">Preview:</h2>
|
| 97 |
+
<img src={result.ai_preview} alt="AI preview" className="w-full rounded-xl" />
|
| 98 |
+
<h2 className="text-xl font-bold mt-4">Links:</h2>
|
| 99 |
+
<ul>
|
| 100 |
+
{result.results.map((r, idx) => (
|
| 101 |
+
<li key={idx}>
|
| 102 |
+
<a href={r.url} target="_blank" className="text-blue-500 underline">{r.title}</a>
|
| 103 |
+
</li>
|
| 104 |
+
))}
|
| 105 |
+
</ul>
|
| 106 |
+
</div>
|
| 107 |
+
)}
|
| 108 |
+
<div className="mt-8">
|
| 109 |
+
<div className="flex justify-between items-center mb-2">
|
| 110 |
+
<h2 className="text-xl font-semibold">Search History</h2>
|
| 111 |
+
<button
|
| 112 |
+
onClick={clearHistory}
|
| 113 |
+
className="text-sm bg-red-500 text-white px-3 py-1 rounded hover:bg-red-600"
|
| 114 |
+
>
|
| 115 |
+
Clear History
|
| 116 |
+
</button>
|
| 117 |
+
</div>
|
| 118 |
+
<ul className="space-y-1 text-sm">
|
| 119 |
+
{history.map((h, i) => (
|
| 120 |
+
<li
|
| 121 |
+
key={i}
|
| 122 |
+
onClick={() => {
|
| 123 |
+
setQuery(h.query);
|
| 124 |
+
runSearch(h.query);
|
| 125 |
+
}}
|
| 126 |
+
className="text-blue-600 cursor-pointer hover:underline flex justify-between items-center"
|
| 127 |
+
>
|
| 128 |
+
<span>{h.query}</span>
|
| 129 |
+
<span className="text-gray-400 text-xs">{new Date(h.timestamp).toLocaleString()}</span>
|
| 130 |
+
</li>
|
| 131 |
+
))}
|
| 132 |
+
</ul>
|
| 133 |
+
</div>
|
| 134 |
+
</div>
|
| 135 |
+
);
|
| 136 |
+
};
|
| 137 |
+
|
| 138 |
+
export default SearchPage;
|