feat: persistent multi-share configuration (shares.json + localStorage)
Daemon side: - internal/shares: new package reads/writes shares.json next to identity - proto: add ShareEntry type, add_share/remove_share/list_shares IPC commands, shares_list event, path field on FileEntry - netmgr: load shares.json on startup; ScanAllShares combines legacy ShareDir with shares.json entries (recursive walk with relative paths) - mesh: ScanFiles callback lets manager inject multi-share scanning without mesh knowing about shares.json - ipc: handle add_share, remove_share, list_shares commands Browser side: - ShareManager component replaces FolderPicker: shows list of named shares with remove/re-pick buttons; persists share records to waste_shares in localStorage (name, global flag, networkId) - FolderPicker retained for internal use; Sidebar now uses ShareManager Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -125,6 +125,18 @@ details summary { color: var(--muted); font-size: 12px; cursor: pointer; }
|
||||
.file-entry-dl { background: none; color: var(--accent); font-size: 14px; padding: 1px 4px; flex-shrink: 0; }
|
||||
.file-entry-dl:hover { background: rgba(124,106,247,0.15); }
|
||||
|
||||
/* ── share manager ── */
|
||||
.share-manager { width: 100%; display: flex; flex-direction: column; gap: 4px; }
|
||||
.share-list { list-style: none; display: flex; flex-direction: column; gap: 2px; margin-bottom: 2px; }
|
||||
.share-item { display: flex; align-items: center; gap: 4px; font-size: 12px; padding: 2px 0; }
|
||||
.share-icon { flex-shrink: 0; font-size: 11px; }
|
||||
.share-name { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; color: var(--text); }
|
||||
.share-scope { font-size: 10px; color: var(--muted); flex-shrink: 0; }
|
||||
.share-repick { background: none; color: var(--muted); font-size: 12px; padding: 0 3px; flex-shrink: 0; }
|
||||
.share-repick:hover { color: var(--accent); background: none; }
|
||||
.share-remove { background: none; color: var(--muted); font-size: 11px; padding: 0 3px; flex-shrink: 0; }
|
||||
.share-remove:hover { color: #e06060; background: none; }
|
||||
|
||||
/* ── folder picker ── */
|
||||
.folder-picker { width: 100%; display: flex; flex-direction: column; gap: 4px; }
|
||||
.folder-picker-btn { background: var(--surface); color: var(--muted); border: 1px solid var(--border); font-size: 12px; padding: 5px 10px; border-radius: 4px; width: 100%; text-align: left; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
|
||||
|
||||
118
web/src/components/ShareManager.tsx
Normal file
118
web/src/components/ShareManager.tsx
Normal file
@@ -0,0 +1,118 @@
|
||||
import { useState, useEffect, useRef } from 'react'
|
||||
import { useWaste } from '../store'
|
||||
|
||||
interface ShareRecord {
|
||||
name: string // display name (folder name picked by user)
|
||||
global: boolean // true = all networks
|
||||
networkId?: string
|
||||
}
|
||||
|
||||
const STORAGE_KEY = 'waste_shares'
|
||||
|
||||
function loadShares(): ShareRecord[] {
|
||||
try {
|
||||
return JSON.parse(localStorage.getItem(STORAGE_KEY) ?? '[]')
|
||||
} catch {
|
||||
return []
|
||||
}
|
||||
}
|
||||
|
||||
function saveShares(shares: ShareRecord[]) {
|
||||
localStorage.setItem(STORAGE_KEY, JSON.stringify(shares))
|
||||
}
|
||||
|
||||
export function ShareManager() {
|
||||
const { activeNetworkId, setSharedFiles, sharedFilesByNetwork } = useWaste()
|
||||
const [shares, setShares] = useState<ShareRecord[]>(loadShares)
|
||||
const [includeSubfolders, setIncludeSubfolders] = useState(true)
|
||||
const inputRef = useRef<HTMLInputElement>(null)
|
||||
|
||||
// Count files currently shared on active network
|
||||
const current = activeNetworkId ? sharedFilesByNetwork[activeNetworkId] : undefined
|
||||
const fileCount = current?.size ?? 0
|
||||
|
||||
function persist(next: ShareRecord[]) {
|
||||
setShares(next)
|
||||
saveShares(next)
|
||||
}
|
||||
|
||||
function addShare(files: FileList, folderName: string) {
|
||||
const map = new Map<string, File>()
|
||||
for (let i = 0; i < files.length; i++) {
|
||||
const f = files[i]
|
||||
const rel = (f as File & { webkitRelativePath?: string }).webkitRelativePath
|
||||
const path = rel ? rel.split('/').slice(1).join('/') : f.name
|
||||
if (!includeSubfolders && path.includes('/')) continue
|
||||
map.set(path, f)
|
||||
}
|
||||
setSharedFiles(map)
|
||||
|
||||
const record: ShareRecord = {
|
||||
name: folderName,
|
||||
global: true,
|
||||
networkId: activeNetworkId ?? undefined,
|
||||
}
|
||||
persist([...shares.filter(s => s.name !== folderName), record])
|
||||
}
|
||||
|
||||
function removeShare(name: string) {
|
||||
persist(shares.filter(s => s.name !== name))
|
||||
// Clear the in-memory share if it matches
|
||||
setSharedFiles(new Map())
|
||||
}
|
||||
|
||||
function onChange(e: React.ChangeEvent<HTMLInputElement>) {
|
||||
const fileList = e.target.files
|
||||
if (!fileList || fileList.length === 0) return
|
||||
// Get folder name from first file's path
|
||||
const first = fileList[0] as File & { webkitRelativePath?: string }
|
||||
const folderName = first.webkitRelativePath?.split('/')[0] ?? 'folder'
|
||||
addShare(fileList, folderName)
|
||||
e.target.value = ''
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="share-manager">
|
||||
<input
|
||||
ref={inputRef}
|
||||
type="file"
|
||||
// @ts-expect-error webkitdirectory is non-standard
|
||||
webkitdirectory=""
|
||||
multiple
|
||||
style={{ display: 'none' }}
|
||||
onChange={onChange}
|
||||
/>
|
||||
|
||||
{shares.length > 0 && (
|
||||
<ul className="share-list">
|
||||
{shares.map(s => (
|
||||
<li key={s.name} className="share-item">
|
||||
<span className="share-icon">📁</span>
|
||||
<span className="share-name" title={s.name}>{s.name}</span>
|
||||
<span className="share-scope">{s.global ? 'all nets' : 'this net'}</span>
|
||||
<button className="share-repick" onClick={() => inputRef.current?.click()} title="Re-pick folder">↺</button>
|
||||
<button className="share-remove" onClick={() => removeShare(s.name)} title="Remove share">✕</button>
|
||||
</li>
|
||||
))}
|
||||
</ul>
|
||||
)}
|
||||
|
||||
<button
|
||||
className="folder-picker-btn"
|
||||
onClick={() => inputRef.current?.click()}
|
||||
title="Share a folder with peers on this network"
|
||||
>
|
||||
{fileCount > 0 ? `${fileCount} file${fileCount !== 1 ? 's' : ''} shared` : '+ Share folder'}
|
||||
</button>
|
||||
|
||||
<label className="folder-picker-subfolders">
|
||||
<input
|
||||
type="checkbox"
|
||||
checked={includeSubfolders}
|
||||
onChange={e => setIncludeSubfolders(e.target.checked)}
|
||||
/>
|
||||
include subfolders
|
||||
</label>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
@@ -1,7 +1,7 @@
|
||||
import { useState } from 'react'
|
||||
import { useWaste } from '../store'
|
||||
import type { PeerStatus } from '../store'
|
||||
import { FolderPicker } from './FolderPicker'
|
||||
import { ShareManager } from './ShareManager'
|
||||
import { Transfers } from './Transfers'
|
||||
|
||||
function makeYawCard(id: string, alias: string): string {
|
||||
@@ -124,7 +124,7 @@ export function Sidebar() {
|
||||
{adapterMode === 'browser' && (
|
||||
<div className="sidebar-section">
|
||||
<span className="sidebar-label">Sharing</span>
|
||||
<div style={{ padding: '4px 12px' }}><FolderPicker /></div>
|
||||
<div style={{ padding: '4px 12px' }}><ShareManager /></div>
|
||||
</div>
|
||||
)}
|
||||
|
||||
|
||||
@@ -32,6 +32,11 @@ export interface FileEntry {
|
||||
path?: string // relative path including filename, e.g. "docs/report.pdf"
|
||||
}
|
||||
|
||||
export interface ShareEntry {
|
||||
path: string
|
||||
networks: string[] // ["*"] = global
|
||||
}
|
||||
|
||||
export interface FileOffer {
|
||||
xid: string
|
||||
name: string
|
||||
@@ -56,6 +61,9 @@ export type IpcMsgType =
|
||||
| 'get_file_list'
|
||||
| 'export_identity'
|
||||
| 'import_identity'
|
||||
| 'add_share'
|
||||
| 'remove_share'
|
||||
| 'list_shares'
|
||||
// events
|
||||
| 'state_snapshot'
|
||||
| 'message_received'
|
||||
@@ -71,6 +79,7 @@ export type IpcMsgType =
|
||||
| 'invite_generated'
|
||||
| 'identity_exported'
|
||||
| 'identity_imported'
|
||||
| 'shares_list'
|
||||
| 'peer_status'
|
||||
| 'error'
|
||||
|
||||
@@ -109,6 +118,8 @@ export interface IpcMessage {
|
||||
error_message?: string
|
||||
invite?: string
|
||||
files?: FileEntry[]
|
||||
shares?: ShareEntry[]
|
||||
networks_filter?: string[] // for add_share
|
||||
// peer_status
|
||||
conn_state?: PeerConnState
|
||||
candidate_type?: CandidateType
|
||||
|
||||
Reference in New Issue
Block a user