// RigShareItem.swift // Lyrebird // // Copyright (c) 2026 Joshua Parmenter / realizedsound // SPDX-License-Identifier: MIT import Foundation import CoreTransferable import UniformTypeIdentifiers import LyrebirdShoeGaze /// A `Transferable` wrapper that lets a `ShareLink` send a single rig as a `.shoegaze` document. /// /// Holds the already-encoded document bytes (a value type, so the wrapper is `Sendable` as /// `Transferable` requires). The encode happens when the share menu is built for the focused row — /// cheap, one rig — via the throwing ``init(rig:)``. /// /// Uses `FileRepresentation` (not `DataRepresentation`): the share sheet — AirDrop, Messages, /// Mail, Notes, Files — only offers its full service list when handed a real file on disk. The /// bytes are written to a temporary `.shoegaze` when a destination is picked. Sharing raw /// data instead collapses the macOS menu to just Copy / Edit Extensions. struct RigShareItem: Transferable, Sendable { /// Filesystem-safe base name for the shared file (no extension). let filenameBase: String /// Encoded `.shoegaze` document bytes. let data: Data /// Encodes `entry` as a single-rig `.shoegaze` document. /// /// - Parameter entry: The library entry to share. /// - Throws: Any `JSONEncoder` error from ``ShoeGazeDocument/data()``. init(rig entry: RigEntry) throws { self.filenameBase = shareFilenameBase(entry.name) self.data = try ShoeGazeDocument(rig: entry).data() } static var transferRepresentation: some TransferRepresentation { FileRepresentation(exportedContentType: .shoeGaze) { item in SentTransferredFile(try writeShoeGazeShareFile(item.data, base: item.filenameBase)) } .suggestedFileName { item in "\(item.filenameBase).\(ShoeGazeDocument.fileExtension)" } } } /// Reduces a user-supplied name to a filesystem-safe base for a shared file, mirroring the preset /// stores' sanitisation at the UI boundary (the core sanitiser is module-internal to /// `LyrebirdShoeGaze`). Path separators and whitespace become underscores; an empty result falls /// back to a generic name. Shared by ``RigShareItem`` and ``BoardShareItem``. func shareFilenameBase(_ name: String) -> String { let forbidden: Set = ["/", "\\", ":", "\0"] let mapped: String = String(name.map { character -> Character in (character.isWhitespace || forbidden.contains(character)) ? "_" : character }) let trimmed: String = mapped.trimmingCharacters(in: CharacterSet(charactersIn: "_")) return trimmed.isEmpty ? "ShoeGaze" : trimmed } /// Writes `data` to a uniquely-named temporary `.shoegaze` file and returns its URL, for a /// `FileRepresentation` to hand the share sheet a real file. A fresh per-share subdirectory avoids /// collisions when the same name is shared twice. Shared by ``RigShareItem`` and ``BoardShareItem``. func writeShoeGazeShareFile(_ data: Data, base: String) throws -> URL { let directory: URL = FileManager.default.temporaryDirectory .appendingPathComponent("ShoeGazeShare-\(UUID().uuidString)", isDirectory: true) try FileManager.default.createDirectory(at: directory, withIntermediateDirectories: true) let url: URL = directory .appendingPathComponent(base) .appendingPathExtension(ShoeGazeDocument.fileExtension) try data.write(to: url, options: .atomic) return url }