Files
zeekayGitHubCursor Agentzhanzo-agentHanzo Devhanzo-platform[bot] <201622097+hanzo-platform[bot]@users.noreply.github.com>
fcedaff5b5 Localize built-in UI copy and the docs site (#266)
* @hanzo/ui 8.30.0: every word the package authors is the reader's language

- The consumer installs every peer of the train it pins
- @hanzo/ui 8.30.0: every word the package authors is the reader's language
- i18n: one English fallback, the footer's doors through t, and a typed header cell
- agents: the elements i18n touched draw with gui props, not style
- i18n: @hanzo/ui/product/pure still depends on nothing
- i18n: the framework's collections, records and media say their words in the reader's language
- i18n: one English fallback for the pure modules that build a sentence
- i18n: the builder workspace says its words in the reader's language
- i18n: settings, the catalogue shelf and the backdrop presets
- i18n: the masthead's tree is marked for translation and drawn through t
- i18n: the product layer says its words in the reader's language
- i18n: mockups and market widgets say their words in the reader's language
- i18n: the gui controls say their words in the reader's language
- picker: each choosable option wears the field's own ink
- consent: the bar's fit width is a gui prop
- i18n: onboarding, chat, the model picker, sign-in, consent and the corner speak the reader's language
- i18n: @hanzo/ui peer-depends on @hanzo/i18n

* feat(i18n): localize built-in UI copy and the docs site

English stays the default. A locale on Hanzo or LocaleProvider switches
built-in labels, chat copy, and dates, and Arabic sets dir=rtl. Missing
keys render in English. The docs site adds a language switcher, prefixed
routes, hreflang, and a localized sitemap.

Co-authored-by: z <[email protected]>

* docs: translate every page into the 16 target locales

Each English doc now has a locale file under data/i18n. Code fences,
previews, links, and product names stay as written. A missing file still
falls back to English.

Co-authored-by: z <[email protected]>

* Unfinished: these files still contain <<<<<<< / ======= / >>>>>>> conflict markers on origin/agent/i18n-locales-0366: pk…

Run: sess_498ba60a49b5870a63d042e97f565728

* No conflict markers remain. Now: pull origin agent/i18n-locales-0366 (merge), run pnpm install --frozen-lockfile (if it…

Run: sess_0a7901763139b331895f48f02df95b94

* CI on agent/i18n-locales-0366 head 362214f fails @hanzo/ui tests 1-8, ui browser/vite/webpack. Concrete errors: (a) pkg/…

Run: sess_79bff23928c2986ee61768a243fc6dfa

* fix(ui): Dial RING from look; drop duplicate useI18n in Masthead

Unblocks pkg/ui build on #266: RING lives in ./look (not ./ring); Masthead had a duplicate const { t } = useI18n() after the #254/#287 merge.

* fix(ui): rtl.test ALLOWED counts for Masthead/DiffViewer/SlideOver

Align ALLOWED physical-side budgets with current source after the i18n merge: Masthead 2, DiffViewer 5, SlideOver 1.

* fix(ui): RepoSelect test expects default note + troubleshoot

Co-authored-by: Hanzo Dev <[email protected]>

---------

Co-authored-by: Cursor Agent <[email protected]>
Co-authored-by: z <[email protected]>
Co-authored-by: hanzo-agent <[email protected]>
Co-authored-by: Hanzo Dev <[email protected]>
Co-authored-by: hanzo-platform[bot] <201622097+hanzo-platform[bot]@users.noreply.github.com>
2026-11-05 04:11:17 -08:00

246 lines
9.2 KiB
TypeScript

import { createContext, useContext, useEffect, useMemo, useState, type ReactNode } from 'react'
import { Accordion, H4, Input, Paragraph, SizableText, Text, View, XStack, YStack } from '@hanzo/gui'
import { useTranslate } from '@hanzo/ui'
import { ChevronDown } from '@hanzogui/lucide-icons-2'
import type { Href } from 'one'
import { Link, usePathname } from 'one'
import { useBarePath, useLocalHref } from './locale'
export type Item = { name: string; title: string; href: string }
export type Section = { title: string; items: Item[] }
export type Heading = { id: string; title: string; level: 2 | 3 }
/** The bar every Hanzo surface wears, and the measure the frame shares with it. */
export const HEADER = 60
export const WIDTH = 1280
/** A heading's anchor: lowercased, one dash per run of anything else. */
export const slug = (s: string) =>
s
.toLowerCase()
.replace(/[^a-z0-9]+/g, '-')
.replace(/^-|-$/g, '')
/** The filter's element id: the header, ⌘K and the sidebar all name the one input. */
const FILTER = 'filter'
type Menu = {
open: boolean
setOpen: (open: boolean) => void
query: string
setQuery: (query: string) => void
/** Show the sidebar where it is hidden, and put the cursor in its filter. */
focus: () => void
}
const Context = createContext<Menu>({
open: false,
setOpen: () => {},
query: '',
setQuery: () => {},
focus: () => {},
})
export const useMenu = () => useContext(Context)
/** The menu and the filter, shared by the header that drives them and the
* sidebar that shows them. ⌘K is the filter's key everywhere on the page. */
export function Docs({ children }: { children: ReactNode }) {
const [open, setOpen] = useState(false)
const [query, setQuery] = useState('')
const value = useMemo<Menu>(() => {
const focus = () => {
setOpen(true)
setTimeout(() => document.getElementById(FILTER)?.focus(), 0)
}
return { open, setOpen, query, setQuery, focus }
}, [open, query])
useEffect(() => {
const key = (e: KeyboardEvent) => {
if ((e.metaKey || e.ctrlKey) && e.key.toLowerCase() === 'k') {
e.preventDefault()
value.focus()
}
}
window.addEventListener('keydown', key)
return () => window.removeEventListener('keydown', key)
}, [value])
return <Context.Provider value={value}>{children}</Context.Provider>
}
/**
* The docs frame: sidebar, page, quick nav — three grid columns from 1280,
* two from 1024, and below that the page alone, the sidebar folded behind the
* header's menu button.
*/
export function Frame({ sections, headings = [], children }: { sections: Section[]; headings?: Heading[]; children: ReactNode }) {
return (
<View display="grid" gridTemplateColumns="minmax(0, 1fr)" $lg={{ gridTemplateColumns: '280px minmax(0, 1fr)' }} $xl={{ gridTemplateColumns: '280px minmax(0, 1fr) 220px' }} width="100%" maxW={WIDTH} self="center" grow={1}>
<Sidebar sections={sections} />
<YStack render="main" minW={0} px="$4" py="$6" $lg={{ px: '$6', py: '$8' }}>
<YStack render="article" width="100%" maxW="var(--container-wide)" gap="$7">
{children}
</YStack>
</YStack>
<QuickNav headings={headings} />
</View>
)
}
function Sidebar({ sections }: { sections: Section[] }) {
const t = useTranslate()
const local = useLocalHref()
const { open, setOpen, query, setQuery } = useMenu()
const path = useBarePath()
const q = query.trim().toLowerCase()
const shown = q
? sections
.map((s) => ({
...s,
items: s.items.filter((i) => i.title.toLowerCase().includes(q) || i.name.includes(q)),
}))
.filter((s) => s.items.length)
: sections
const current = sections.find((s) => s.items.some((i) => i.href === path))?.title
const [opened, setOpened] = useState<string[]>(current ? [current] : sections.slice(0, 1).map((s) => s.title))
useEffect(() => {
if (current) setOpened((v) => (v.includes(current) ? v : [...v, current]))
}, [current])
// A route change closes the menu that led to it.
useEffect(() => setOpen(false), [path, setOpen])
return (
<YStack
render="aside"
className="rail"
display={open ? 'flex' : 'none'}
position="fixed"
t={HEADER}
l={0}
r={0}
b={0}
z={90}
bg="$background"
px="$4"
pt="$4"
pb="$10"
gap="$3"
$lg={{
display: 'flex',
position: 'sticky',
l: 'auto',
r: 'auto',
b: 'auto',
self: 'flex-start',
height: `calc(100vh - ${HEADER}px)`,
width: '100%',
bg: 'transparent',
pl: 0,
pr: '$3',
}}
>
<Input id={FILTER} value={query} onChangeText={setQuery} placeholder={t('Filter')} aria-label={t('Filter the catalog')} size="$3" ml="$3" fontFamily="$mono" />
<Accordion type="multiple" value={q ? shown.map((s) => s.title) : opened} onValueChange={setOpened}>
{shown.map((s) => (
<Accordion.Item key={s.title} value={s.title}>
<Accordion.Trigger unstyled bg="transparent" borderWidth={0} rounded="$3" ml="$2" hoverStyle={{ bg: '$color2' }} pressStyle={{ bg: '$color1' }}>
{({ open }: { open: boolean }) => (
<XStack py="$2" px="$3" justify="space-between" items="center" width="100%">
<Text fontSize={13} fontWeight="600" color="$color12">
{s.title}
</Text>
<YStack rotate={open ? '180deg' : '0deg'} animateOnly={['transform']}>
<ChevronDown color="$color8" size={14} />
</YStack>
</XStack>
)}
</Accordion.Trigger>
<Accordion.HeightAnimator overflow="hidden">
<Accordion.Content unstyled bg="transparent" exitStyle={{ opacity: 0 }}>
<YStack py="$1">
{s.items.map((i) => (
<NavItem key={i.href} item={{ ...i, href: local(i.href) }} active={i.href === path} />
))}
</YStack>
</Accordion.Content>
</Accordion.HeightAnimator>
</Accordion.Item>
))}
</Accordion>
{q && shown.length === 0 ? (
<Paragraph size="$2" color="$color10" px="$3">
{t('Nothing named {query}.', { query })}
</Paragraph>
) : null}
</YStack>
)
}
function NavItem({ item, active }: { item: Item; active: boolean }) {
return (
<Link href={item.href as Href} style={{ textDecorationLine: 'none' }}>
<XStack items="center" px="$4" py={6} position="relative" rounded="$3" hoverStyle={{ bg: '$color2' }} pressStyle={{ bg: '$color3' }}>
<YStack position="absolute" t={4} b={4} l={0} width={3} rounded="$2" bg="$color12" opacity={active ? 1 : 0} />
<SizableText fontFamily="$mono" size="$3" letterSpacing={-0.25} opacity={active ? 1 : 0.65} hoverStyle={{ opacity: 0.85 }} {...(active && { fontWeight: '700' })}>
{item.title}
</SizableText>
</XStack>
</Link>
)
}
/** The page's own headings, the one the reader is at marked, kept by an observer
* whose reading line sits 70% down the screen. */
function QuickNav({ headings }: { headings: Heading[] }) {
const t = useTranslate()
const [active, setActive] = useState(0)
const path = usePathname()
useEffect(() => setActive(0), [path])
useEffect(() => {
if (!headings.length) return
const seen = new Set<string>()
const io = new IntersectionObserver(
(entries) => {
for (const e of entries) e.isIntersecting ? seen.add(e.target.id) : seen.delete(e.target.id)
if (seen.size) {
let last = -1
seen.forEach(
(id) =>
(last = Math.max(
last,
headings.findIndex((h) => h.id === id)
))
)
if (last !== -1) setActive(last)
} else if (window.scrollY < 100) setActive(0)
},
{ rootMargin: '100% 0px -30% 0px', threshold: 0 }
)
for (const h of headings) {
const el = document.getElementById(h.id)
if (el) io.observe(el)
}
return () => io.disconnect()
}, [headings])
return (
<YStack render="aside" display="none" $xl={{ display: 'flex' }} position="sticky" t={HEADER} self="flex-start" pt="$8" pb="$10" pl="$4">
{headings.length > 0 ? (
<YStack render="nav" aria-labelledby="quick-nav" gap="$3">
<H4 id="quick-nav" fontFamily="$mono" size="$2" letterSpacing={1} color="$color10">
{t('On this page')}
</H4>
<YStack borderLeftWidth={1} borderColor="$borderColor">
{headings.map((h, i) => (
<a key={h.id} href={`#${h.id}`} onClick={() => setActive(i)} style={{ textDecoration: 'none' }}>
<Paragraph render="span" size={h.level === 2 ? '$3' : '$2'} ml={-1} pl={h.level === 2 ? 12 : 24} py="$1" borderLeftWidth={2} borderColor={i === active ? '$color12' : 'transparent'} color={i === active ? '$color12' : h.level === 2 ? '$color11' : '$color10'} fontWeight={h.level === 2 ? '500' : '400'} cursor="pointer" hoverStyle={{ color: '$color12' }}>
{h.title}
</Paragraph>
</a>
))}
</YStack>
</YStack>
) : null}
</YStack>
)
}