shell: the shell frame, createShell(site) (moved from wayBetter for X)

packages/shell/src/frame (waybetter/shell/frame): the whole shell of an app. An app describes its
site (ShellSite: names and pages, account rules, search, address chips and completions, its panel
tab, the new-tab page, palette items, deck column kinds and flags and an optional compose dock, the
media panel's composer rule, a theme store, overlays, the native features it serves) and passes
its Tauri API and i18n instance; createShell returns the root component.

- Shell.tsx: dockview host (tabs in the titlebar row, splits, drag-link drops, shortcuts, context
  menu, deck back-and-forth, session restore, workspace on start, overlay raise); state.ts (pane
  placeholders, tabs_sync one-in-flight via syncGate, shell_raise layer, profiles, layout).
- WebPanel (address bar), NewTabPage, CommandPalette + paletteModel (frame groups; site items),
  DeckView + AddColumnDialog + deckStore (site column kinds), PaneTheme + themeStore
  (createThemeStore over the app's ThemeAdapter) + chromeTheme (ChromeTheme input) + themeScopes,
  Titlebar + TitlebarExtras + icons + brand (wB mark data URI), AccountSwitcher + accountModel,
  workspaces + workspaceModel, backupModel, syncHost, devHost + devModel, pluginCommandStore,
  MediaPanel + mediaModel, searchInput, hooks, bridge (installPreviewMocks), i18n, boot.ts
  (bootScript: the first-frame boot.js), CSS themed with --wb-* tokens.
- paneModel: paneVisibility, raiseSupported, cssRgb; deckModel: isSourcePage.
- Includes the live-QA titlebar fix (dock ResizeObserver + rAF titlebar marks, dock sized by its
  insets).
- tests/frame.test.ts: every frame key has English and is used, palette order with a site,
  address fallback, layout gate, boot script, chrome variables, switcher rules, workspaces,
  deck settings with site flags.
- npm package 0.4.0: exports ./shell/frame, ./shell/frame/*, ./shell/frame/*.css (copied by the
  build); dockview and acorn optional peers; root devDependencies dockview, acorn.
This commit is contained in:
CRTkafa 2026-09-29 12:47:10 +07:00
parent 60b3e57a0a
commit 322f854753
49 changed files with 6442 additions and 6 deletions

View file

@ -0,0 +1,113 @@
import { useEffect, useState } from 'react';
import { createPortal } from 'react-dom';
import { invoke, listen } from './bridge.ts';
import { useT } from './hooks.ts';
import { attachTarget, clipPreview, folderLabel, relativeTime, type ClipItem, type GalleryImage, type MediaSettings } from './mediaModel.ts';
import { site } from './site.ts';
import { isWeb, setOverlay, shell, type WebParams } from './state.ts';
const getError = (error: unknown) => error instanceof Error ? error.message : String(error);
function targetTab() {
const api = shell.api;
const media = site().media;
if (!api || !media) return null;
const tabs = api.panels.filter(isWeb).map(panel => ({ id: panel.id, url: (panel.params as WebParams).url }));
return attachTarget(tabs, api.activePanel?.id, media.canAttach);
}
/** Lazily loads one preview (a data URL read natively, only from inside a gallery folder). */
function Thumb({ command, args, alt }: { command: string; args: Record<string, unknown>; alt: string }) {
const [src, setSrc] = useState<string | null>(null);
useEffect(() => {
let live = true;
invoke<string>(command, args).then(url => { if (live) setSrc(url); }).catch(() => {});
return () => { live = false; };
}, [command, JSON.stringify(args)]);
return src ? <img src={src} alt={alt} loading="lazy" draggable={false}/> : <span className="bt-media-thumb-empty"/>;
}
/**
* Media side panel: recent images from the owner's gallery folders and the local clipboard
* history. Choosing an item attaches the image to (or inserts the text into) the composer of the
* active site tab (`site.media.canAttach`). Nothing is posted; the user reviews and sends it themselves.
*/
export function MediaPanel({ onClose }: { onClose: () => void }) {
const t = useT();
const siteName = site().name;
const [tab, setTab] = useState<'gallery' | 'clipboard'>('gallery');
const [settings, setSettings] = useState<MediaSettings | null>(null);
const [images, setImages] = useState<GalleryImage[] | null>(null);
const [clips, setClips] = useState<ClipItem[] | null>(null);
const [error, setError] = useState('');
const refresh = () => {
invoke<MediaSettings>('media_get').then(setSettings).catch(err => setError(getError(err)));
invoke<GalleryImage[]>('gallery_list', { limit: 60 }).then(setImages).catch(err => setError(getError(err)));
invoke<ClipItem[]>('clip_list').then(setClips).catch(err => setError(getError(err)));
};
useEffect(() => {
setOverlay(1);
invoke('shell_focus').catch(() => {});
refresh();
const unlisten = listen('clipboard-changed', () => { invoke<ClipItem[]>('clip_list').then(setClips).catch(() => {}); });
const onKey = (event: KeyboardEvent) => { if (event.key === 'Escape') onClose(); };
window.addEventListener('keydown', onKey);
return () => { setOverlay(-1); window.removeEventListener('keydown', onKey); unlisten.then(dispose => dispose()).catch(() => {}); };
}, []);
const deliver = (command: 'tab_attach_image' | 'tab_insert_text', args: Record<string, unknown>) => {
const target = targetTab();
if (!target) { setError(t('wb.media.noComposer', { site: siteName })); return; }
onClose();
shell.api?.getPanel(target.id)?.api.setActive();
// Wait until the tab webview is shown again (the overlay hid it), then attach. Never posts.
setTimeout(() => {
invoke(command, { id: target.id, ...args }).catch(err => console.warn('[shell] media attach failed', err));
}, 150);
};
const addFolder = () => invoke<MediaSettings>('gallery_add_folder').then(next => { setSettings(next); refresh(); }).catch(err => setError(getError(err)));
const removeClip = (id: string) => invoke<ClipItem[]>('clip_remove', { id }).then(setClips).catch(err => setError(getError(err)));
return createPortal(<div className="bt-palette-backdrop bt-media-backdrop" onMouseDown={onClose}>
<aside className="bt-media" role="dialog" aria-label={t('wb.media.title')} onMouseDown={event => event.stopPropagation()}>
<header className="bt-media-head">
<div className="bt-media-tabs" role="tablist">
<button role="tab" aria-selected={tab === 'gallery'} className={tab === 'gallery' ? 'active' : ''} onClick={() => setTab('gallery')}>{t('wb.media.gallery')}</button>
<button role="tab" aria-selected={tab === 'clipboard'} className={tab === 'clipboard' ? 'active' : ''} onClick={() => setTab('clipboard')}>{t('wb.media.clipboard')}</button>
</div>
<button className="bt-icon bt-media-close" aria-label={t('wb.common.close')} title={t('wb.media.closeTitle')} onClick={onClose}><svg viewBox="0 0 24 24" aria-hidden="true"><path d="M6 6l12 12M18 6L6 18"/></svg></button>
</header>
{error && <div className="bt-media-error" role="alert">{error}</div>}
{tab === 'gallery' && <div className="bt-media-body">
{settings && settings.folders.length === 0 && <div className="bt-media-empty"><p>{t('wb.media.pickFolders')}</p><button className="button small" onClick={() => void addFolder()}>{t('wb.media.addFolder')}</button></div>}
{settings && settings.folders.length > 0 && <div className="bt-media-folders">{settings.folders.map(folder => <span key={folder} className="pill" title={folder}>{folderLabel(folder)}</span>)}<button className="bt-media-link" onClick={() => void addFolder()}>{t('wb.media.add')}</button></div>}
{images && images.length > 0 && <div className="bt-media-grid">
{images.map(image => <button key={image.path} className="bt-media-tile" title={t('wb.media.attachTitle', { name: image.name })} onClick={() => deliver('tab_attach_image', { source: { kind: 'gallery', path: image.path } })}>
<Thumb command="gallery_thumb" args={{ path: image.path }} alt={image.name}/>
<span className="bt-media-caption">{relativeTime(image.modifiedMs)}</span>
</button>)}
</div>}
{settings && settings.folders.length > 0 && images?.length === 0 && <div className="bt-media-empty"><p>{t('wb.media.noImages')}</p></div>}
</div>}
{tab === 'clipboard' && <div className="bt-media-body">
{settings && !settings.clipboardEnabled && <div className="bt-media-empty"><p>{t('wb.media.clipOff', { count: settings.clipboardMax })}</p></div>}
{clips && clips.length === 0 && settings?.clipboardEnabled && <div className="bt-media-empty"><p>{t('wb.media.clipEmpty')}</p></div>}
{clips && clips.length > 0 && <ul className="bt-media-clips">
{clips.map(item => <li key={item.id} className="bt-media-clip">
<button className="bt-media-clip-main" title={t('wb.media.putIn')} onClick={() => item.kind === 'image'
? deliver('tab_attach_image', { source: { kind: 'clip', id: item.id } })
: deliver('tab_insert_text', { text: item.text ?? '' })}>
{item.kind === 'image' ? <span className="bt-media-clip-thumb"><Thumb command="clip_thumb" args={{ id: item.id }} alt={t('wb.media.copiedImage')}/></span> : <span className="bt-media-clip-text">{clipPreview(item)}</span>}
<small>{relativeTime(item.at)}</small>
</button>
<button className="bt-icon" aria-label={t('wb.media.removeFromHistory')} title={t('wb.common.remove')} onClick={() => void removeClip(item.id)}><svg viewBox="0 0 24 24" aria-hidden="true"><path d="M6 6l12 12M18 6L6 18"/></svg></button>
</li>)}
</ul>}
</div>}
<footer className="bt-palette-foot"><span>{t('wb.media.clickToAttach', { site: siteName })}</span><span>{t('wb.media.youPost')}</span></footer>
</aside>
</div>, document.body);
}