@tacoshell/shell 0.1.0 → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,63 @@
1
+ import { useMemo, type ReactNode } from 'react';
2
+ import { createFramework } from '../../core/bus.ts';
3
+ import { unmet } from '../../core/nav.ts';
4
+ import { createPage } from '../../core/page.ts';
5
+ import * as services from '../../core/services.ts';
6
+ import type { RegistrySettingsTab, RegistryView } from '../../types.ts';
7
+ import { useShell, useStore } from '../hooks.ts';
8
+ import { Icon } from '../Icon.tsx';
9
+ import { Boundary } from '../ViewHost.tsx';
10
+
11
+ /** The address of a module's tab on a core Settings page: `#/settings/<page>/<module>/<tab>` with the pair as one encoded segment. */
12
+ export const tabHref = (page: string, t: Pick<RegistrySettingsTab, 'module' | 'name'>) => `#/settings/${page}/${encodeURIComponent(`${t.module}/${t.name}`)}`;
13
+ export const tabKey = (t: Pick<RegistrySettingsTab, 'module' | 'name'>) => `${t.module}/${t.name}`;
14
+
15
+ /** The tabs modules added to a core Settings page (ADR-072), in order; one whose services aren't usable stays out, like a panel. */
16
+ export function useContributedTabs(page: string): RegistrySettingsTab[] {
17
+ const { registry } = useShell();
18
+ useStore(services.states);
19
+ return (registry.settingsTabs ?? [])
20
+ .filter((t) => t.into === page && !unmet({ requires: t.requires } as unknown as RegistryView, services.isUsable).length)
21
+ .sort((a, b) => a.order - b.order || a.module.localeCompare(b.module) || a.name.localeCompare(b.name));
22
+ }
23
+
24
+ /** The tab strip above a Settings page's title (like a view's bar): the page's own tab(s) first, then the modules'. Not shown when there is only the page's own tab. */
25
+ export function TabStrip({ core, contributed, active, page, coreHref }: { core: { id: string; label: string; href: string }[]; contributed: RegistrySettingsTab[]; active: string; page: string; coreHref?: string }) {
26
+ void coreHref;
27
+ if (!contributed.length && core.length < 2) return null;
28
+ return (
29
+ <nav className="page-tabs settings-tabs" aria-label="Sections">
30
+ {core.map((c) => <a key={c.id} className={active === c.id ? 'on' : ''} href={c.href} data-tab={c.id}>{c.label}</a>)}
31
+ {contributed.map((t) => <a key={tabKey(t)} className={active === tabKey(t) ? 'on' : ''} href={tabHref(page, t)} data-tab={tabKey(t)}><Icon icon={t.icon} />{t.label}</a>)}
32
+ </nav>
33
+ );
34
+ }
35
+
36
+ /** A module's tab, rendered with the module's own framework (datasets, send, storage, log…). */
37
+ export function ContributedTab({ tab }: { tab: RegistrySettingsTab }) {
38
+ const input = useShell();
39
+ const page = useMemo(() => createPage(`${tab.module}/settings:${tab.name}`), [tab.module, tab.name]);
40
+ const framework = useMemo(() => createFramework(tab.module, page), [tab.module, page]);
41
+ const Tab = input.settingsTabs?.[tabKey(tab)];
42
+ return (
43
+ <div className="page" data-settings-tab={tabKey(tab)}>
44
+ <h1>{tab.label}</h1>
45
+ {Tab ? <Boundary name={`Settings tab ${tab.label} (${tab.module})`}><Tab framework={framework} /></Boundary> : <p className="muted">This tab has no component.</p>}
46
+ </div>
47
+ );
48
+ }
49
+
50
+ /**
51
+ * Wraps a core Settings page that modules may add tabs to. With no contributed tab nothing changes. With some, a strip appears above the page: its own tab
52
+ * ("Datasets") then each contributed one; the address picks the tab (`#/settings/datasets/<module>%2F<tab>`).
53
+ */
54
+ export function SettingsTabbed({ page, coreLabel, tab, children }: { page: string; coreLabel: string; tab?: string; children: ReactNode }) {
55
+ const contributed = useContributedTabs(page);
56
+ const current = contributed.find((t) => tabKey(t) === tab);
57
+ return (
58
+ <>
59
+ <TabStrip page={page} core={[{ id: 'core', label: coreLabel, href: `#/settings/${page}` }]} contributed={contributed} active={current ? tabKey(current) : 'core'} />
60
+ {current ? <ContributedTab tab={current} /> : children}
61
+ </>
62
+ );
63
+ }
@@ -0,0 +1,36 @@
1
+ import { useEffect, useState } from 'react';
2
+ import { createFramework } from '../core/bus.ts';
3
+ import * as log from '../core/log.ts';
4
+ import { collect, type Collected, type ContribKind } from '../core/contrib.ts';
5
+ import { NO_CONTEXT, type Page } from '../core/page.ts';
6
+ import * as services from '../core/services.ts';
7
+ import { unmet } from '../core/nav.ts';
8
+ import type { RegistryView } from '../types.ts';
9
+ import { useShell, useStore } from './hooks.ts';
10
+
11
+ const noExposed = { get: () => NO_CONTEXT, subscribe: () => () => {} };
12
+
13
+ /**
14
+ * What other features contribute (Related or Actions) for the page being viewed. Starts as soon as the page opens and again whenever the feature
15
+ * changes what it exposes; the list fills in as contributors answer.
16
+ */
17
+ export function useContributions(kind: ContribKind, page: Page | null | undefined) {
18
+ const { registry, handlers } = useShell();
19
+ useStore(services.states);
20
+ const exposed = useStore(page?.exposed ?? noExposed);
21
+ const [items, setItems] = useState<Collected[]>([]);
22
+ const key = JSON.stringify(exposed);
23
+ useEffect(() => {
24
+ setItems([]);
25
+ const current = page ? registry.views.find((v) => `${v.module}/${v.name}` === page.key) : undefined;
26
+ if (!page || !current) return;
27
+ let stop = false;
28
+ void collect({
29
+ reg: registry, kind, current, context: exposed, handlers: handlers ?? {}, cancelled: () => stop, onItems: (all) => { if (!stop) setItems(all); },
30
+ usable: (v: RegistryView) => !unmet(v, services.isUsable).length,
31
+ env: (v: RegistryView) => ({ framework: createFramework(v.module, undefined, v.name), log: log.logger(`${v.label} › contribute`) }),
32
+ });
33
+ return () => { stop = true; };
34
+ }, [page, kind, key, registry]);
35
+ return { items, exposed };
36
+ }