GuideComponentsHooksFunctionsTypesTokensSource code on GitHub
Appearance
Language
Pages
Reference

Hooks

The hooks of hodi:sdk give a plugin's component its texts, its routes, its links and the server's settings. Each hook knows which plugin calls it: it only works in that plugin's components.

usePluginTranslation()

This hook gives the plugin's texts, in the person's language. It reads them in the plugin's locales/ files, never in Hodi's.

web/index.tsx
import { usePluginTranslation } from 'hodi:sdk'

const { t, language } = usePluginTranslation()
t('greeting', { name: 'Anna' })
Returns?: optional
t(key: string, options?) => string

Returns a key's text. options fills its variables, such as {{name}}.

language'fr' | 'en'

The person's language.

usePluginApi()

This hook returns a function that calls the plugin's routes. api('/hello') requests /api/plugins/<id>/hello: the plugin never writes the whole address. Two other hooks do the same for the two other kinds of routes, which the server context explains.

web/index.tsx
import { usePluginApi, usePluginPublicApi, usePluginServerApi } from 'hodi:sdk'

const api = usePluginApi()
const answer = await api('/hello', { method: 'POST' })
const { greeting } = await answer.json()
Hooks?: optional
usePluginApi()(path, init?) => Promise<Response>

Calls the plugin's routes, for the household's profiles.

usePluginServerApi()(path, init?) => Promise<Response>

Calls the plugin's serverRoutes. Only the server's admin can call them.

usePluginPublicApi()(path, init?) => Promise<Response>

Calls the plugin's publicRoutes, even before any sign-in.

The returned function takes the same options as fetch, plus one: proof, the proof askToConfirm() received when the person typed their password again. A route marked proof: true only runs with it.

This hook gives the addresses of Hodi's and the plugin's screens. A plugin never writes them by hand: they stay right even when Hodi changes its addresses.

web/index.tsx
import { Button, navigate, usePluginLinks } from 'hodi:sdk'

const links = usePluginLinks()
<Button href={links.page('week/3')}>This week</Button>
<Button onClick={() => navigate(links.frontPage)}>Home</Button>
Returns?: optional
frontPagestring

The front page's address.

article(id: string) => string

The address of an article in the reader, by its id.

settingsstring

The address of Settings.

pluginsstring

The address of the plugin's page in Server › Plugins. Only the server's admin can open it.

page(path?: string) => string

The address of the plugin's page in Settings, or of one of its screens with path.

serverPage(section: string, path?: string) => string

The address of one of the plugin's sections in the Server space, or of one of its screens with path.

To go to one of these addresses from code, after an action for example, navigate(href) does the same as a click on a link.

usePluginPath()

This hook says which screen the plugin's page should show. It returns what follows the page's address: '' at its root, 'week/3' for the address links.page('week/3') gives.

web/index.tsx
import { usePluginPath } from 'hodi:sdk'

const path = usePluginPath()
return path.startsWith('week/') ? <Week n={Number(path.slice(5))} /> : <Home />
Returns?: optional
pathstring

What follows the address of the plugin's page, or '' at its root.

useServerSettings()

This hook gives the durations and limits the admin chose in Server › Content and Backups, read-only. A plugin uses it to write the real figures, in its legal pages for example. It is null until the server has answered.

web/index.tsx
import { useServerSettings } from 'hodi:sdk'

const settings = useServerSettings()
settings && t('privacy.kept', { days: settings.articleDays })
Returns?: optional
articleDaysnumber

The number of days Hodi keeps an article.

videoDaysnumber

The number of days Hodi keeps a video.

textDaysnumber

The number of days Hodi keeps the text of an article read.

backupCopiesnumber

The number of database copies Hodi keeps, one per night.

maxSourcesnumber | null

The number of sources a household can follow, or null when there is no limit.