Decuga for React and Next.js
Three ways to use it, from least to most work: drop in a widget (a complete, themable component), draw the changelog in your own markup with ChangelogFeed (rendered on the server, so search engines index it), or build everything yourself on the hooks. They work together, so you can start with a widget and replace one page later.
Needs React 18 or newer. The package is in beta; see the SDKs overview for what you need first (a project id and the right plan).
Install
npm install @decuga/react @decuga/elements@decuga/elements holds the widgets' web components. You only need it if you use the widgets (not for ChangelogFeed or the hooks), and the package loads it in the browser after the first render.
Set the project once
Wrap the part of your app that uses Decuga in a provider. Every component and hook below it then knows the project.
import { DecugaProvider, DecugaRoadmap } from '@decuga/react'
export default function App() {
return (
<DecugaProvider projectId="YOUR_PROJECT_ID">
<DecugaRoadmap showHeader />
</DecugaProvider>
)
}Prefer no provider? Every component and hook also takes projectId directly. By default the visitor's sign-in is kept in session storage; pass storage="local" to keep them signed in between visits.
Ready-made widgets
The three widgets are complete: loading and error states, sign-in with an emailed code (or no sign-in at all when your own app gives the session, see helpdesk auto-login), forms, and validation. Each keeps its styles inside a shadow root.
import { DecugaChangelog, DecugaRoadmap, DecugaHelpdesk } from '@decuga/react'
<DecugaChangelog pageSize={5} />
<DecugaRoadmap
view="board"
showHeader
onVote={item => analytics.track('roadmap_vote', { item: item.title })}
/>
<DecugaHelpdesk
formId="OPTIONAL_FORM_ID" // skip the form picker
onSubmit={ticket => console.log(ticket.ticketNumber)}
/>Colours, font and corner radius come from CSS custom properties, which you can set on the component's className or style:
<DecugaRoadmap
theme="auto"
style={{
'--decuga-color-primary': '#0f766e',
'--decuga-radius': '6px',
'--decuga-font': 'Inter, sans-serif',
} as React.CSSProperties}
/>The web components guide lists every custom property and CSS part.
Helpdesk for customers signed in to your app
DecugaHelpdesk normally asks for the customer's email and an emailed code. If your app already signs them in, give the provider a session from your server and the widget opens on their requests, with no sign-in step and no sign-out button.
import { DecugaProvider, DecugaHelpdesk } from '@decuga/react'
// Made once, outside the component, so a new session is not asked for on every render
const getSession = () => fetch('/decuga-session', { method: 'POST', credentials: 'include' }).then(r => r.json())
export function Support() {
return (
<DecugaProvider projectId="YOUR_PROJECT_ID" session={getSession}>
<DecugaHelpdesk />
</DecugaProvider>
)
}session option is also accepted by useDecugaClient, and by a client you make yourself and pass as client.Changelog in your markup
ChangelogFeed draws release notes as plain HTML elements in your page, with no shadow DOM, so your CSS applies, it can be drawn on the server, and search engines read it. Class names start with decuga-.
import { ChangelogFeed } from '@decuga/react'
import '@decuga/client/changelog.css' // optional starter styles
export function Changelog() {
return <ChangelogFeed projectId="YOUR_PROJECT_ID" pageSize={5} />
}Draw an entry your own way with renderEntry. It gets the entry ready to draw (dates as text, only the sections that have content):
<ChangelogFeed
projectId="YOUR_PROJECT_ID"
renderEntry={entry => (
<article>
<h3>{entry.title} {entry.version && <small>{entry.version}</small>}</h3>
<time dateTime={entry.dateIso ?? undefined}>{entry.dateText}</time>
{entry.sections.map(s => (
<section key={s.key}>
<h4>{s.label}</h4>
<ul>{s.items.map((i, n) => <li key={n}>{i.title}</li>)}</ul>
</section>
))}
</article>
)}
/>Next.js and server rendering
To put your release notes in the HTML your server sends, fetch the first page on the server and hand it to ChangelogFeed. The first paint then contains the entries, and the browser asks for the rest only as the visitor scrolls.
import { DecugaClient } from '@decuga/client'
import { ChangelogFeed } from '@decuga/react'
import '@decuga/client/changelog.css'
const projectId = process.env.DECUGA_PROJECT_ID!
// Ask Decuga at most every five minutes, however many people visit
const decuga = new DecugaClient({
projectId,
fetch: (url, init) => fetch(url, { ...init, next: { revalidate: 300 } }),
})
export const metadata = { title: 'Release notes' }
export default async function Page() {
const initialPage = await decuga.changelog.page({ size: 5 })
return <ChangelogFeed projectId={projectId} initialPage={initialPage} locale="en-US" />
}- On the server, import
DecugaClientfrom@decuga/client, not from@decuga/react. The React package is marked as a client module. - Set
localewhen you render on the server (it defaults toen-US), so the server and the browser write the same date. Dates show the calendar day stored in Decuga, whatever the visitor's time zone, so the page hydrates cleanly. - The widgets (
DecugaChangelog,DecugaRoadmap,DecugaHelpdesk) draw an empty tag on the server and fill in when the page is live, so they are safe in the App Router and the Pages Router. - The hooks run in the browser. Use them in a Client Component (
'use client').
Hooks for your own UI
The hooks hold the data, the loading and error state, and the actions. You draw the markup.
useChangelog
'use client'
import { useChangelog, changelogEntryView } from '@decuga/react'
export function Notes() {
const { entries, hasMore, loading, error, loadMore } = useChangelog({ projectId: 'YOUR_PROJECT_ID', pageSize: 10 })
return (
<>
{entries.map(e => <h3 key={e.id}>{changelogEntryView(e).title}</h3>)}
{error && <p>Could not load more.</p>}
{hasMore && <button disabled={loading} onClick={loadMore}>Load more</button>}
</>
)
}useRoadmap
Voting needs a visitor who has verified their email. toggleVote rejects with a session-expired error when they have not, which is your cue to show your own sign-in.
'use client'
import { useRoadmap, isDecugaError } from '@decuga/react'
export function Roadmap() {
const { grouped, session, requestCode, verifyCode, toggleVote } = useRoadmap({ projectId: 'YOUR_PROJECT_ID' })
async function vote(item) {
try {
await toggleVote(item)
} catch (e) {
if (isDecugaError(e) && e.code === 'session-expired') openSignIn() // your UI: requestCode(email), then verifyCode(email, code)
}
}
return grouped.PLANNED.map(item => (
<button key={item.id} aria-pressed={item.hasVoted} onClick={() => vote(item)}>
{item.title} ({item.voteCount})
</button>
))
}useHelpdesk
It returns the portal settings, the forms the visitor may use, the signed-in customer, and helpdesk, the client part with submitTicket, sendCode, verifyCode, myTickets, ticket and comment. validateTicket and buildTicketRequest turn a filled-in form into the request the portal expects.
'use client'
import { useHelpdesk, validateTicket, buildTicketRequest } from '@decuga/react'
export function Contact() {
const { forms, helpdesk } = useHelpdesk({ projectId: 'YOUR_PROJECT_ID' })
const form = forms[0]
if (!form) return null
async function send(values: { email: string; description: string; fields: Record<string, string> }) {
const errors = validateTicket(form, values) // { email: '...', description: '...', <fieldId>: '...' }
if (Object.keys(errors).length) return errors
const ticket = await helpdesk.submitTicket(buildTicketRequest(form, values))
return ticket.ticketNumber
}
// ...your form
}Props reference
Shared by every widget
| Prop | Type | What it does |
|---|---|---|
projectId | string | The project. Not needed inside a provider. |
baseUrl | string | Where Decuga is served. Default https://decuga.com. |
storage | "session" | "local" | "memory" | Where a signed-in visitor is remembered. Default session. |
client | DecugaClient | A client you made yourself. |
theme | "light" | "dark" | "auto" | Colour scheme. Default light. |
locale | string | Language for dates, such as "en-GB". Default the visitor's browser. |
onError | (message) => void | Called when something could not be loaded or sent. |
Per widget
| Component | Props |
|---|---|
DecugaChangelog | pageSize (1 to 50, default 5), infiniteScroll (default true), initialPage, onLoad |
DecugaRoadmap | view ("board" or "list"), showHeader, suggestions (default true), onLoad, onVote, onSignIn, onSuggest |
DecugaHelpdesk | formId, showHeader (default true), tickets (default true), onSubmit, onSignIn |
ChangelogFeed | initialPage, pageSize, infiniteScroll, locale, className, renderEntry, empty |
Testing
Pass a client built with your own fetch, and your tests never touch the network:
import { DecugaClient, MemoryStorage } from '@decuga/react'
const client = new DecugaClient({
projectId: 'test',
storage: new MemoryStorage(),
fetch: async () => new Response(JSON.stringify({ items: [], page: 0, size: 5, total: 0, hasMore: false, publishedTotal: 0 })),
})
render(<ChangelogFeed client={client} />)