Skip to content
For the complete documentation index, see llms.txt. This page is also available as Markdown.

Script Config (React)

Complete guide for building an in-game admin settings panel using dirk-cfx-react's SettingsPanel, useForm, and createScriptConfig — connected to dirk_lib's scriptConfig Lua module.

This page covers the React/NUI side of the script config system. For the Lua side (registration, watchers, DB persistence), see dirk_lib → scriptConfig.


Overview

The script config system lets you build a full admin configuration panel for any FiveM resource with:

  • Live settings that apply without server restart

  • Undo / redo with full edit history

  • Version conflict detection (optimistic concurrency)

  • Change audit log with admin attribution

  • JSON editor for bulk/advanced edits

  • Reset to defaults with confirmation

The architecture flows like this:

schema.json + defaults.json
        ↓
dirk_lib server (registerScriptConfig → MySQL)
        ↕ NUI callbacks (get / update / history / reset)
dirk-cfx-react (createScriptConfig → useForm → SettingsPanel)
        ↕ React component sections
Admin UI in-game

Step-by-Step Setup

1. Define Your Schema

Create schema.json in your resource root. This is a JSON Schema that defines every setting, its type, and its default value.

Schema extensions:

Extension
Purpose

x-serverOnly

Never sent to client — server-only secrets

x-renamedFrom

Auto-migrate old key path to new key path

x-arrayKey

Merge arrays by identity field instead of index

defaults.json is optional — if provided, it supplies the initial default values. Otherwise defaults are derived from the "default" fields in schema.json.

2. Register on the Lua Side

See dirk_lib → scriptConfig for full details. The minimal setup:

src/server/scriptConfig.lua

src/client/scriptConfig.lua

3. Create the TypeScript Config Store

In your resource's web/src/stores/, create a file that defines your settings types, Zod schema, and calls createScriptConfig:

4. Build the Admin Panel

Create web/src/components/Admin/main.tsx with your nav items and section routing:

5. Build Section Components

Each admin section uses useFormField and useFormActions from dirk-cfx-react to bind to the form state managed by SettingsPanel:


API Reference

createScriptConfig<T>(defaultValue)

Factory function that creates a Zustand store and NUI integration for your settings type. Call once at module level.

Returns:

Property
Type
Description

store

StoreApi<T>

Zustand store — use as a hook for reactive reads

updateSettings

(data: Partial<T>) => Promise<NuiResponse<T>>

Send partial update to server via NUI

getHistory

(params?) => Promise<HistoryResponse>

Fetch paginated change history

useScriptConfigHooks

() => void

Call inside your admin component to register NUI listeners

SettingsPanel

The main admin UI shell. Renders a full-screen modal with sidebar navigation, undo/redo, save/discard, history, JSON editor, and reset.

Prop
Type
Required
Description

navItems

NavItem[]

yes

Sidebar navigation items { id, icon, label }

title

string

yes

Panel title

subtitle

string

no

Panel subtitle

open

boolean

yes

Whether the panel is visible

onClose

() => void

no

Close handler (defaults to fetchNui("CLOSE_ADMIN_SECTION"))

children

(activeTab: string) => ReactNode

yes

Render function receiving the active tab ID

defaultScriptConfig

T

yes

Default settings object for reset

schema

{ safeParse }

no

Zod schema for JSON editor validation

resetConfirmText

string

no

Text user must type to confirm reset

width

string

no

Panel width (default: "70vh")

height

string

no

Panel height (default: "85vh")

Built-in sidebar features:

  • Undo / Redo — Step through edit history

  • Save — Calls updateSettings with all pending changes, shows unsaved count badge

  • History — Paginated audit log modal with search/filter

  • Discard — Revert to last saved state

  • Manual Edit (JSON) — Full JSON editor with schema validation

  • Reset Defaults — Wipe to defaults with confirmation modal

useForm

Advanced form state hook with deep nesting, undo/redo, and Zod validation. Used internally by SettingsPanel but can be used standalone.

Key methods:

Method
Description

setValue(path, value)

Set a nested value (e.g. "basic.debug")

getValue(path)

Get a nested value

reset()

Reset to initial values

reinitialize(newValues)

Reset with new initial values

undo()

Undo last change

redo()

Redo undone change

validate()

Run validation, returns { success, errors }

getInputProps(path)

Returns { value, onChange, error } for Mantine inputs

isDirty()

Whether any values have changed

getChangedFields()

Array of paths that have been modified

Context Hooks

These hooks access the form state within FormProvider (set up by SettingsPanel):

Hook
Returns
Description

useFormField(path)

T[path]

Reactive value at a nested path

useFormFields(...paths)

[T[p1], T[p2], ...]

Reactive values at multiple paths

useFormActions()

{ setValue, reset, ... }

All form mutation methods

useFormError(path)

string | undefined

Validation error for a field

useFormErrors()

Record<string, string>

All current validation errors


NUI Callback Reference

The settings system communicates with dirk_lib through these NUI callbacks (handled automatically):

Callback
Direction
Purpose

NUI_READY

UI → Lua

Signals that the NUI is loaded and ready

GET_SETTINGS

UI → Lua

Fetches server config (currency, colours, etc.)

GET_LOCALES

UI → Lua

Fetches locale strings

GET_ITEMS

UI → Lua

Fetches inventory item definitions

OPEN_ADMIN_SECTION

Lua → UI

Opens the admin panel

CLOSE_ADMIN_SECTION

UI → Lua

Closes the admin panel

UPDATE_SCRIPT_SETTINGS

UI → Lua

Sends settings update with expected version

GET_SCRIPT_SETTINGS_HISTORY

UI → Lua

Fetches paginated change history

All NUI callbacks are registered automatically by dirk_lib's scriptConfig module. You don't need to set them up manually.


Version Conflict Handling

When two admins edit settings simultaneously:

  1. Admin A opens the panel → receives settings + client_version hash

  2. Admin B saves a change → server's version hash updates

  3. Admin A tries to save → sends their expectedVersion

  4. Server detects mismatch → returns success: false with latestData

  5. UI automatically merges the latest data back into the form

  6. Admin A can review and re-save

This prevents silent overwrites and ensures all changes are tracked.

Last updated