Download submit.js from pollen-robotics/reachy-mini-skins: direct link, hf CLI and curl.
- Browser
- Download file 6.7 kB
-
https://huggingface.co/spaces/pollen-robotics/reachy-mini-skins/resolve/main/submit.js
- Command line
-
hf download hf://spaces/pollen-robotics/reachy-mini-skins/submit.js
-
curl -L -o submit.js https://huggingface.co/spaces/pollen-robotics/reachy-mini-skins/resolve/main/submit.js
6.7 kB
| import { | |
| oauthLoginUrl, | |
| oauthHandleRedirectIfPresent, | |
| commit, | |
| } from "https://cdn.jsdelivr.net/npm/@huggingface/hub@latest/+esm"; | |
| const REPO = { type: "space", name: "pollen-robotics/reachy-mini-skins" }; | |
| // --- OAuth --- | |
| let oauthToken = null; | |
| async function init() { | |
| // Check if returning from OAuth redirect | |
| const result = await oauthHandleRedirectIfPresent(); | |
| if (result) { | |
| oauthToken = result.accessToken; | |
| // User just logged in — open the modal directly | |
| openModal(); | |
| setStatus("Logged in! Fill in the details and submit.", "success"); | |
| } | |
| } | |
| async function loginIfNeeded() { | |
| if (oauthToken) return true; | |
| // Redirect to HF login | |
| window.location.href = await oauthLoginUrl(); | |
| return false; // won't reach here | |
| } | |
| // --- Modal open/close --- | |
| const modal = document.getElementById("submit-modal"); | |
| const form = document.getElementById("submit-form"); | |
| const statusEl = document.getElementById("submit-status"); | |
| const submitBtn = document.getElementById("submit-btn"); | |
| document.addEventListener("click", async (e) => { | |
| if (e.target.closest("#open-submit")) { | |
| // Login first, then open modal | |
| const loggedIn = await loginIfNeeded(); | |
| if (loggedIn) openModal(); | |
| } | |
| }); | |
| document.getElementById("submit-close").addEventListener("click", closeModal); | |
| modal.addEventListener("click", (e) => { | |
| if (e.target === modal) closeModal(); | |
| }); | |
| function openModal() { | |
| modal.classList.add("active"); | |
| document.body.style.overflow = "hidden"; | |
| } | |
| function closeModal() { | |
| modal.classList.remove("active"); | |
| document.body.style.overflow = ""; | |
| } | |
| // --- Character count --- | |
| const descInput = document.getElementById("skin-description"); | |
| const descCount = document.getElementById("desc-count"); | |
| descInput.addEventListener("input", () => { | |
| descCount.textContent = descInput.value.length; | |
| }); | |
| // --- File handling with previews --- | |
| let selectedFiles = []; | |
| const fileInput = document.getElementById("skin-files"); | |
| const previewsEl = document.getElementById("file-previews"); | |
| const dropZone = document.getElementById("file-drop-zone"); | |
| fileInput.addEventListener("change", () => { | |
| addFiles(fileInput.files); | |
| fileInput.value = ""; | |
| }); | |
| dropZone.addEventListener("dragover", (e) => { | |
| e.preventDefault(); | |
| dropZone.classList.add("dragover"); | |
| }); | |
| dropZone.addEventListener("dragleave", () => { | |
| dropZone.classList.remove("dragover"); | |
| }); | |
| dropZone.addEventListener("drop", (e) => { | |
| e.preventDefault(); | |
| dropZone.classList.remove("dragover"); | |
| addFiles(e.dataTransfer.files); | |
| }); | |
| function addFiles(fileList) { | |
| for (const file of fileList) { | |
| if (!file.type.startsWith("image/") && !file.type.startsWith("video/")) continue; | |
| selectedFiles.push(file); | |
| } | |
| renderPreviews(); | |
| } | |
| function renderPreviews() { | |
| previewsEl.innerHTML = ""; | |
| selectedFiles.forEach((file, i) => { | |
| const div = document.createElement("div"); | |
| div.className = "file-preview"; | |
| if (file.type.startsWith("video/")) { | |
| const video = document.createElement("video"); | |
| video.src = URL.createObjectURL(file); | |
| video.muted = true; | |
| div.appendChild(video); | |
| } else { | |
| const img = document.createElement("img"); | |
| img.src = URL.createObjectURL(file); | |
| div.appendChild(img); | |
| } | |
| const removeBtn = document.createElement("button"); | |
| removeBtn.className = "file-preview-remove"; | |
| removeBtn.type = "button"; | |
| removeBtn.textContent = "\u00d7"; | |
| removeBtn.addEventListener("click", () => { | |
| selectedFiles.splice(i, 1); | |
| renderPreviews(); | |
| }); | |
| div.appendChild(removeBtn); | |
| previewsEl.appendChild(div); | |
| }); | |
| } | |
| // --- Slugify --- | |
| function slugify(text) { | |
| return text | |
| .toLowerCase() | |
| .normalize("NFD") | |
| .replace(/[\u0300-\u036f]/g, "") | |
| .replace(/[^a-z0-9]+/g, "-") | |
| .replace(/^-+|-+$/g, ""); | |
| } | |
| function getSelectedTags() { | |
| return Array.from(document.querySelectorAll('input[name="tags"]:checked')).map(cb => cb.value); | |
| } | |
| // --- Submit --- | |
| form.addEventListener("submit", async (e) => { | |
| e.preventDefault(); | |
| const name = document.getElementById("skin-name").value.trim(); | |
| const description = document.getElementById("skin-description").value.trim(); | |
| const tags = getSelectedTags(); | |
| const link = document.getElementById("skin-link").value.trim(); | |
| if (!name || !description) { | |
| setStatus("Please fill in the name and description.", "error"); | |
| return; | |
| } | |
| if (selectedFiles.length === 0) { | |
| setStatus("Please upload at least one image or video.", "error"); | |
| return; | |
| } | |
| if (!oauthToken) { | |
| setStatus("Please log in first.", "error"); | |
| return; | |
| } | |
| setLoading(true); | |
| setStatus(""); | |
| try { | |
| const slug = slugify(name); | |
| const folderPath = `assets/${slug}`; | |
| // Build skin.json | |
| const skinJson = { | |
| name, | |
| description, | |
| tags, | |
| media: selectedFiles.map((file) => ({ | |
| type: file.type.startsWith("video/") ? "video" : "image", | |
| file: file.name, | |
| })), | |
| official: false, | |
| }; | |
| const isDiscord = link.includes("discord.com") || link.includes("discord.gg"); | |
| skinJson.link = { type: isDiscord ? "discord" : "github", url: link }; | |
| // Build commit operations | |
| const operations = [ | |
| { | |
| operation: "addOrUpdate", | |
| path: `${folderPath}/skin.json`, | |
| content: new Blob([JSON.stringify(skinJson, null, 2)], { type: "application/json" }), | |
| }, | |
| ...selectedFiles.map((file) => ({ | |
| operation: "addOrUpdate", | |
| path: `${folderPath}/${file.name}`, | |
| content: file, | |
| })), | |
| ]; | |
| const result = await commit({ | |
| repo: REPO, | |
| accessToken: oauthToken, | |
| title: `Add community skin: ${name}`, | |
| description: `New skin submission "${name}" with ${selectedFiles.length} file(s).`, | |
| operations, | |
| isPullRequest: true, | |
| }); | |
| const prUrl = result?.pullRequestUrl || `https://huggingface.co/spaces/${REPO.name}/discussions`; | |
| setStatus(`Skin submitted! <a href="${prUrl}" target="_blank">View your pull request</a>`, "success"); | |
| form.reset(); | |
| selectedFiles = []; | |
| renderPreviews(); | |
| } catch (err) { | |
| console.error("Submit error:", err); | |
| setStatus(`Error: ${err.message || "Failed to submit. Please try again."}`, "error"); | |
| } finally { | |
| setLoading(false); | |
| } | |
| }); | |
| function setLoading(loading) { | |
| submitBtn.disabled = loading; | |
| submitBtn.querySelector(".submit-btn-text").style.display = loading ? "none" : ""; | |
| submitBtn.querySelector(".submit-btn-loading").style.display = loading ? "" : "none"; | |
| } | |
| function setStatus(html, type) { | |
| statusEl.innerHTML = html; | |
| statusEl.className = "submit-status" + (type ? ` ${type}` : ""); | |
| } | |
| // Init on page load | |
| init(); | |