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.
import { usePluginTranslation } from 'hodi:sdk'
const { t, language } = usePluginTranslation()
t('greeting', { name: 'Anna' })t(key: string, options?) => stringReturns 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.
import { usePluginApi, usePluginPublicApi, usePluginServerApi } from 'hodi:sdk'
const api = usePluginApi()
const answer = await api('/hello', { method: 'POST' })
const { greeting } = await answer.json()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.
usePluginLinks()
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.
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>frontPagestringThe front page's address.
article(id: string) => stringThe address of an article in the reader, by its id.
settingsstringThe address of Settings.
pluginsstringThe address of the plugin's page in Server › Plugins. Only the server's admin can open it.
page(path?: string) => stringThe address of the plugin's page in Settings, or of one of its screens with path.
serverPage(section: string, path?: string) => stringThe 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.
import { usePluginPath } from 'hodi:sdk'
const path = usePluginPath()
return path.startsWith('week/') ? <Week n={Number(path.slice(5))} /> : <Home />pathstringWhat 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.
import { useServerSettings } from 'hodi:sdk'
const settings = useServerSettings()
settings && t('privacy.kept', { days: settings.articleDays })articleDaysnumberThe number of days Hodi keeps an article.
videoDaysnumberThe number of days Hodi keeps a video.
textDaysnumberThe number of days Hodi keeps the text of an article read.
backupCopiesnumberThe number of database copies Hodi keeps, one per night.
maxSourcesnumber | nullThe number of sources a household can follow, or null when there is no limit.