rl-core-front 0.3.0 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/_services/api/schema.d.ts +43 -132
- package/src/_utils/filter.ts +15 -34
- package/src/components/app-shell.tsx +51 -1
- package/src/components/brand-header.tsx +23 -8
- package/src/components/brand-panel.tsx +41 -0
- package/src/components/index.ts +1 -0
- package/src/components/ui/button.tsx +5 -0
- package/src/components/ui/confirm-dialog.tsx +3 -1
- package/src/components/ui/data-table.tsx +13 -4
- package/src/components/ui/dialog.tsx +120 -21
- package/src/components/ui/filter-sheet.tsx +47 -25
- package/src/components/ui/index.ts +1 -1
- package/src/components/ui/segmented-control.tsx +74 -0
- package/src/contexts/brand-context.tsx +15 -0
- package/src/contexts/i18n-context.tsx +69 -4
- package/src/features/audit/audit-screen.tsx +13 -140
- package/src/features/audit/components/audit-diff.tsx +156 -0
- package/src/features/audit/components/index.ts +3 -0
- package/src/features/audit/components/record-audit-button.tsx +176 -0
- package/src/features/audit/enums/audit-data-action.enum.ts +20 -0
- package/src/features/audit/hooks/use-audit-trail.ts +9 -2
- package/src/features/audit/services/audit.service.ts +43 -1
- package/src/features/login/login-screen.tsx +86 -40
- package/src/features/logs/enums/error-type.enum.ts +21 -0
- package/src/features/logs/enums/request-outcome.enum.ts +14 -0
- package/src/features/logs/hooks/use-request-logs.ts +26 -3
- package/src/features/logs/logs-screen.tsx +2 -14
- package/src/features/logs/panels/index.ts +0 -1
- package/src/features/logs/panels/request-logs-panel.tsx +177 -25
- package/src/features/logs/services/logs.service.ts +13 -51
- package/src/features/notifications/components/notification-item.tsx +10 -8
- package/src/features/notifications/enums/notification-type.enum.ts +8 -0
- package/src/features/profile/profile-screen.tsx +2 -1
- package/src/features/rbac/panels/groups-panel.tsx +20 -7
- package/src/features/users/components/user-form-dialog.tsx +16 -7
- package/src/features/users/hooks/use-users.ts +9 -2
- package/src/hooks/use-filters.ts +25 -2
- package/src/hooks/use-list-query.ts +14 -1
- package/src/i18n/messages/en.ts +17 -5
- package/src/i18n/messages/pt.ts +18 -5
- package/src/index.ts +12 -1
- package/src/components/ui/filter-chips.tsx +0 -88
- package/src/features/logs/hooks/use-error-logs.ts +0 -65
- package/src/features/logs/panels/error-logs-panel.tsx +0 -183
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
"use client";
|
|
2
2
|
|
|
3
3
|
import * as DialogPrimitive from "@radix-ui/react-dialog";
|
|
4
|
-
import { X } from "lucide-react";
|
|
4
|
+
import { Maximize2, Minimize2, X } from "lucide-react";
|
|
5
5
|
import * as React from "react";
|
|
6
6
|
|
|
7
7
|
import { cn } from "#core/lib/utils";
|
|
@@ -26,28 +26,127 @@ const DialogOverlay = React.forwardRef<
|
|
|
26
26
|
));
|
|
27
27
|
DialogOverlay.displayName = DialogPrimitive.Overlay.displayName;
|
|
28
28
|
|
|
29
|
+
/**
|
|
30
|
+
* Escala de largura do Tailwind, em ordem. Ampliar anda dois degraus: `md` vira
|
|
31
|
+
* `xl`, `2xl` vira `4xl`. O modal cresce mantendo a proporção que já tinha, em
|
|
32
|
+
* vez de saltar para a tela inteira — um formulário de duas colunas esticado a
|
|
33
|
+
* 100% fica pior, não melhor. Um degrau só passava despercebido.
|
|
34
|
+
*/
|
|
35
|
+
const WIDTH_SCALE: readonly string[] = [
|
|
36
|
+
"max-w-3xs",
|
|
37
|
+
"max-w-2xs",
|
|
38
|
+
"max-w-xs",
|
|
39
|
+
"max-w-sm",
|
|
40
|
+
"max-w-md",
|
|
41
|
+
"max-w-lg",
|
|
42
|
+
"max-w-xl",
|
|
43
|
+
"max-w-2xl",
|
|
44
|
+
"max-w-3xl",
|
|
45
|
+
"max-w-4xl",
|
|
46
|
+
"max-w-5xl",
|
|
47
|
+
"max-w-6xl",
|
|
48
|
+
"max-w-7xl",
|
|
49
|
+
];
|
|
50
|
+
|
|
51
|
+
/** Largura de quem não declara nenhuma. */
|
|
52
|
+
const DEFAULT_WIDTH = "max-w-lg";
|
|
53
|
+
|
|
54
|
+
/** Quantos degraus o botão sobe de uma vez. */
|
|
55
|
+
const EXPAND_STEPS = 2;
|
|
56
|
+
|
|
57
|
+
/** Só para quem já está no topo da escala: daí não há degrau, resta a tela. */
|
|
58
|
+
const FULL_WIDTH = "max-w-none";
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* A largura do modal ampliado.
|
|
62
|
+
*
|
|
63
|
+
* Lê a **última** `max-w-*` do `className` porque é ela que o `tailwind-merge`
|
|
64
|
+
* deixa valer. Largura fora da escala (`max-w-[42rem]`) não tem degrau a subir:
|
|
65
|
+
* amplia para a tela toda.
|
|
66
|
+
*/
|
|
67
|
+
const expandedWidth = (className?: string): string => {
|
|
68
|
+
const declared = (className ?? "")
|
|
69
|
+
.split(/\s+/)
|
|
70
|
+
.filter((item) => item.startsWith("max-w-"));
|
|
71
|
+
const current = declared[declared.length - 1] ?? DEFAULT_WIDTH;
|
|
72
|
+
const index = WIDTH_SCALE.indexOf(current);
|
|
73
|
+
const last = WIDTH_SCALE.length - 1;
|
|
74
|
+
|
|
75
|
+
// Fora da escala não há degrau a subir, e chutar um da escala encolheria um
|
|
76
|
+
// `max-w-[90rem]`. Amplia para a tela, que é o que o botão promete.
|
|
77
|
+
if (index === -1 || index === last) {
|
|
78
|
+
return FULL_WIDTH;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
// Perto do topo, dois degraus passariam do fim: para no último em vez de
|
|
82
|
+
// cair em tela cheia, que é justamente o salto que a escala evita.
|
|
83
|
+
return WIDTH_SCALE[Math.min(index + EXPAND_STEPS, last)];
|
|
84
|
+
};
|
|
85
|
+
|
|
86
|
+
export interface DialogContentProps
|
|
87
|
+
extends React.ComponentPropsWithoutRef<typeof DialogPrimitive.Content> {
|
|
88
|
+
/**
|
|
89
|
+
* Botão de ampliar ao lado do fechar.
|
|
90
|
+
*
|
|
91
|
+
* Ligado por padrão: ampliar um degrau não atrapalha modal nenhum. Desligue
|
|
92
|
+
* com `expandable={false}` onde o tamanho é parte do desenho — confirmação
|
|
93
|
+
* curta, por exemplo, que esticada só fica com mais vazio.
|
|
94
|
+
*/
|
|
95
|
+
expandable?: boolean;
|
|
96
|
+
}
|
|
97
|
+
|
|
29
98
|
const DialogContent = React.forwardRef<
|
|
30
99
|
React.ElementRef<typeof DialogPrimitive.Content>,
|
|
31
|
-
|
|
32
|
-
>(({ className, children, ...props }, ref) =>
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
)
|
|
100
|
+
DialogContentProps
|
|
101
|
+
>(({ className, children, expandable = true, ...props }, ref) => {
|
|
102
|
+
// Reinicia a cada montagem: o Radix desmonta o conteúdo ao fechar, então
|
|
103
|
+
// reabrir traz o modal no tamanho normal — modal pequeno que volta ampliado
|
|
104
|
+
// surpreende quem o abriu.
|
|
105
|
+
const [expanded, setExpanded] = React.useState(false);
|
|
106
|
+
|
|
107
|
+
return (
|
|
108
|
+
<DialogPortal>
|
|
109
|
+
<DialogOverlay />
|
|
110
|
+
<DialogPrimitive.Content
|
|
111
|
+
ref={ref}
|
|
112
|
+
className={cn(
|
|
113
|
+
"fixed left-[50%] top-[50%] z-50 grid translate-x-[-50%] translate-y-[-50%] gap-4 border border-border bg-card p-6 shadow-lg data-[state=open]:animate-in data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=open]:fade-in-0 data-[state=closed]:zoom-out-95 data-[state=open]:zoom-in-95 overflow-y-auto",
|
|
114
|
+
"w-[calc(100%-2rem)] max-h-[calc(100%-2rem)] rounded-xl",
|
|
115
|
+
DEFAULT_WIDTH,
|
|
116
|
+
className,
|
|
117
|
+
// Depois do `className`: é o que faz a largura ampliada vencer a que
|
|
118
|
+
// o chamador declarou, e só enquanto estiver ampliado.
|
|
119
|
+
expanded && expandedWidth(className),
|
|
120
|
+
)}
|
|
121
|
+
{...props}
|
|
122
|
+
>
|
|
123
|
+
{children}
|
|
124
|
+
<div className="absolute right-4 top-4 flex items-center gap-2">
|
|
125
|
+
{expandable && (
|
|
126
|
+
<button
|
|
127
|
+
type="button"
|
|
128
|
+
onClick={() => setExpanded((value) => !value)}
|
|
129
|
+
className="rounded-sm opacity-70 transition-opacity hover:opacity-100 focus:outline-none focus:ring-2 focus:ring-ring"
|
|
130
|
+
>
|
|
131
|
+
{expanded ? (
|
|
132
|
+
<Minimize2 className="h-4 w-4" />
|
|
133
|
+
) : (
|
|
134
|
+
<Maximize2 className="h-4 w-4" />
|
|
135
|
+
)}
|
|
136
|
+
<span className="sr-only">
|
|
137
|
+
{expanded ? "Restaurar" : "Ampliar"}
|
|
138
|
+
</span>
|
|
139
|
+
</button>
|
|
140
|
+
)}
|
|
141
|
+
<DialogPrimitive.Close className="rounded-sm opacity-70 transition-opacity hover:opacity-100 focus:outline-none focus:ring-2 focus:ring-ring">
|
|
142
|
+
<X className="h-4 w-4" />
|
|
143
|
+
<span className="sr-only">Fechar</span>
|
|
144
|
+
</DialogPrimitive.Close>
|
|
145
|
+
</div>
|
|
146
|
+
</DialogPrimitive.Content>
|
|
147
|
+
</DialogPortal>
|
|
148
|
+
);
|
|
149
|
+
});
|
|
51
150
|
DialogContent.displayName = DialogPrimitive.Content.displayName;
|
|
52
151
|
|
|
53
152
|
/**
|
|
@@ -10,6 +10,7 @@ import { activeFilterCount } from "#core/_utils/filter";
|
|
|
10
10
|
import { Button } from "#core/components/ui/button";
|
|
11
11
|
import { FilterFieldControl } from "#core/components/ui/filter-field";
|
|
12
12
|
import { FilterGroupBlock } from "#core/components/ui/filter-group";
|
|
13
|
+
import { SegmentedControl } from "#core/components/ui/segmented-control";
|
|
13
14
|
import { Separator } from "#core/components/ui/separator";
|
|
14
15
|
import {
|
|
15
16
|
Sheet,
|
|
@@ -29,6 +30,17 @@ export interface FilterSheetProps {
|
|
|
29
30
|
open: boolean;
|
|
30
31
|
onOpenChange: (open: boolean) => void;
|
|
31
32
|
filters: FiltersState;
|
|
33
|
+
/**
|
|
34
|
+
* Campos que a barra da listagem já controla — somem do modo simples para a
|
|
35
|
+
* mesma escolha não existir em dois lugares.
|
|
36
|
+
*
|
|
37
|
+
* Continuam no catálogo, e isso não é detalhe: é o catálogo que serializa o
|
|
38
|
+
* filtro (`buildFilter` percorre o schema). Tirá-los de lá faria o atalho da
|
|
39
|
+
* barra parar de filtrar, sem erro nenhum. O modo avançado também segue com
|
|
40
|
+
* todos: é a saída de emergência, e esconder campo ali tornaria ilegível um
|
|
41
|
+
* filtro salvo que o use.
|
|
42
|
+
*/
|
|
43
|
+
hiddenFields?: string[];
|
|
32
44
|
}
|
|
33
45
|
|
|
34
46
|
/**
|
|
@@ -55,6 +67,7 @@ export function FilterSheet({
|
|
|
55
67
|
open,
|
|
56
68
|
onOpenChange,
|
|
57
69
|
filters,
|
|
70
|
+
hiddenFields = [],
|
|
58
71
|
}: FilterSheetProps): JSX.Element {
|
|
59
72
|
const { t } = useI18n();
|
|
60
73
|
const labelFor = useFieldLabel();
|
|
@@ -83,6 +96,21 @@ export function FilterSheet({
|
|
|
83
96
|
}
|
|
84
97
|
};
|
|
85
98
|
|
|
99
|
+
/**
|
|
100
|
+
* O que o painel não mostra, o painel não mexe.
|
|
101
|
+
*
|
|
102
|
+
* O desfecho de `/logs` é escolhido na barra e não aparece aqui dentro —
|
|
103
|
+
* então nem "Aplicar" nem "Limpar" podem desmarcá-lo. Filtrar por rota
|
|
104
|
+
* enquanto se olha o sucesso é somar uma condição à outra, não trocar de
|
|
105
|
+
* assunto.
|
|
106
|
+
*/
|
|
107
|
+
const keepHidden = (): FilterValues =>
|
|
108
|
+
Object.fromEntries(
|
|
109
|
+
hiddenFields
|
|
110
|
+
.filter((field) => values[field])
|
|
111
|
+
.map((field) => [field, values[field]]),
|
|
112
|
+
);
|
|
113
|
+
|
|
86
114
|
const apply = (): void => {
|
|
87
115
|
filters.setMode(draftMode);
|
|
88
116
|
if (draftMode === "advanced") {
|
|
@@ -90,18 +118,18 @@ export function FilterSheet({
|
|
|
90
118
|
filters.setTree(draftTree);
|
|
91
119
|
}
|
|
92
120
|
} else {
|
|
93
|
-
filters.setValues(draft);
|
|
121
|
+
filters.setValues({ ...draft, ...keepHidden() });
|
|
94
122
|
}
|
|
95
123
|
onOpenChange(false);
|
|
96
124
|
};
|
|
97
125
|
|
|
98
126
|
const clear = (): void => {
|
|
99
|
-
setDraft(
|
|
127
|
+
setDraft(keepHidden());
|
|
100
128
|
setDraftTree(initialTree(schema));
|
|
101
129
|
if (draftMode === "advanced") {
|
|
102
130
|
filters.setTree(initialTree(schema));
|
|
103
131
|
} else {
|
|
104
|
-
filters.setValues(
|
|
132
|
+
filters.setValues(keepHidden());
|
|
105
133
|
}
|
|
106
134
|
onOpenChange(false);
|
|
107
135
|
};
|
|
@@ -111,13 +139,17 @@ export function FilterSheet({
|
|
|
111
139
|
? draftTree
|
|
112
140
|
? countConditions(draftTree)
|
|
113
141
|
: 0
|
|
114
|
-
: activeFilterCount(draft);
|
|
142
|
+
: activeFilterCount(draft, hiddenFields);
|
|
115
143
|
|
|
116
|
-
const modes
|
|
117
|
-
{
|
|
118
|
-
{
|
|
144
|
+
const modes = [
|
|
145
|
+
{ value: "simple", label: t("filters.simple") },
|
|
146
|
+
{ value: "advanced", label: t("filters.advanced") },
|
|
119
147
|
];
|
|
120
148
|
|
|
149
|
+
const visibleFields = schema.filter(
|
|
150
|
+
(field) => !hiddenFields.includes(field.field),
|
|
151
|
+
);
|
|
152
|
+
|
|
121
153
|
return (
|
|
122
154
|
<Sheet open={open} onOpenChange={onOpenChange}>
|
|
123
155
|
<SheetContent
|
|
@@ -131,23 +163,13 @@ export function FilterSheet({
|
|
|
131
163
|
</SheetDescription>
|
|
132
164
|
</SheetHeader>
|
|
133
165
|
|
|
134
|
-
<
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
"px-3 py-1.5 text-sm transition-colors",
|
|
142
|
-
draftMode === item.id
|
|
143
|
-
? "bg-primary text-primary-foreground"
|
|
144
|
-
: "hover:bg-accent",
|
|
145
|
-
)}
|
|
146
|
-
>
|
|
147
|
-
{item.label}
|
|
148
|
-
</button>
|
|
149
|
-
))}
|
|
150
|
-
</div>
|
|
166
|
+
<SegmentedControl
|
|
167
|
+
fullWidth
|
|
168
|
+
label={t("filters.title")}
|
|
169
|
+
options={modes}
|
|
170
|
+
value={draftMode}
|
|
171
|
+
onValueChange={(value) => switchMode(value as FilterMode)}
|
|
172
|
+
/>
|
|
151
173
|
|
|
152
174
|
<SheetBody className="-mx-1 space-y-5 px-1">
|
|
153
175
|
{loading && <Spinner className="mx-auto mt-8" />}
|
|
@@ -160,7 +182,7 @@ export function FilterSheet({
|
|
|
160
182
|
|
|
161
183
|
{!loading && schema.length > 0 && draftMode === "simple" && (
|
|
162
184
|
<>
|
|
163
|
-
{
|
|
185
|
+
{visibleFields.map((field) => (
|
|
164
186
|
<FilterFieldControl
|
|
165
187
|
key={field.field}
|
|
166
188
|
field={field}
|
|
@@ -12,7 +12,6 @@ export * from "./date-range-picker";
|
|
|
12
12
|
export * from "./dialog";
|
|
13
13
|
export * from "./dropdown-menu";
|
|
14
14
|
export * from "./field";
|
|
15
|
-
export * from "./filter-chips";
|
|
16
15
|
export * from "./filter-field";
|
|
17
16
|
export * from "./filter-group";
|
|
18
17
|
export * from "./filter-rule";
|
|
@@ -20,6 +19,7 @@ export * from "./filter-sheet";
|
|
|
20
19
|
export * from "./input";
|
|
21
20
|
export * from "./label";
|
|
22
21
|
export * from "./row-actions";
|
|
22
|
+
export * from "./segmented-control";
|
|
23
23
|
export * from "./select";
|
|
24
24
|
export * from "./separator";
|
|
25
25
|
export * from "./sheet";
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
import * as React from "react";
|
|
4
|
+
|
|
5
|
+
import { cn } from "#core/lib/utils";
|
|
6
|
+
|
|
7
|
+
export interface SegmentedOption {
|
|
8
|
+
value: string;
|
|
9
|
+
label: string;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export interface SegmentedControlProps
|
|
13
|
+
extends Omit<React.HTMLAttributes<HTMLDivElement>, "onChange"> {
|
|
14
|
+
options: SegmentedOption[];
|
|
15
|
+
value: string;
|
|
16
|
+
onValueChange: (value: string) => void;
|
|
17
|
+
/** Rótulo acessível do grupo — o que essas opções escolhem. */
|
|
18
|
+
label: string;
|
|
19
|
+
/**
|
|
20
|
+
* Ocupa a largura toda, com as opções dividindo o espaço em partes iguais.
|
|
21
|
+
*
|
|
22
|
+
* Para quando o controle é o cabeçalho de um bloco — duas opções encostadas à
|
|
23
|
+
* esquerda de um painel largo ficam tortas. Solto, ele mede pelo conteúdo.
|
|
24
|
+
*/
|
|
25
|
+
fullWidth?: boolean;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Escolha única no formato das abas: uma opção acesa dentro de uma trilha.
|
|
30
|
+
*
|
|
31
|
+
* Tem a pele do `Tabs`, mas não é aba: aba troca de painel, isto escolhe um
|
|
32
|
+
* valor. Por isso são botões de alternância num `role="group"`, e não o
|
|
33
|
+
* `Tabs` do Radix — que emitiria `role="tablist"` com `aria-controls`
|
|
34
|
+
* apontando para um painel inexistente.
|
|
35
|
+
*
|
|
36
|
+
* Vale quando as opções cabem na linha e são poucas (até ~5). Passando disso,
|
|
37
|
+
* `Select` — trilha que quebra em duas linhas fica pior que uma lista.
|
|
38
|
+
*/
|
|
39
|
+
export const SegmentedControl = ({
|
|
40
|
+
options,
|
|
41
|
+
value,
|
|
42
|
+
onValueChange,
|
|
43
|
+
label,
|
|
44
|
+
fullWidth = false,
|
|
45
|
+
className,
|
|
46
|
+
...props
|
|
47
|
+
}: SegmentedControlProps): React.JSX.Element => (
|
|
48
|
+
<div
|
|
49
|
+
role="group"
|
|
50
|
+
aria-label={label}
|
|
51
|
+
className={cn(
|
|
52
|
+
"h-10 items-center justify-center rounded-lg bg-muted p-1 text-muted-foreground",
|
|
53
|
+
fullWidth ? "flex w-full" : "inline-flex",
|
|
54
|
+
className,
|
|
55
|
+
)}
|
|
56
|
+
{...props}
|
|
57
|
+
>
|
|
58
|
+
{options.map((option) => (
|
|
59
|
+
<button
|
|
60
|
+
key={option.value}
|
|
61
|
+
type="button"
|
|
62
|
+
aria-pressed={option.value === value}
|
|
63
|
+
onClick={() => onValueChange(option.value)}
|
|
64
|
+
className={cn(
|
|
65
|
+
"inline-flex items-center justify-center whitespace-nowrap rounded-md px-3 py-1.5 text-sm font-medium ring-offset-background transition-all focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring",
|
|
66
|
+
fullWidth && "flex-1",
|
|
67
|
+
option.value === value && "bg-card text-foreground shadow-sm",
|
|
68
|
+
)}
|
|
69
|
+
>
|
|
70
|
+
{option.label}
|
|
71
|
+
</button>
|
|
72
|
+
))}
|
|
73
|
+
</div>
|
|
74
|
+
);
|
|
@@ -33,6 +33,18 @@ export interface Brand {
|
|
|
33
33
|
logo?: ReactNode;
|
|
34
34
|
/** Frase curta sob a logo na tela de login. Opcional. */
|
|
35
35
|
tagline?: string;
|
|
36
|
+
/**
|
|
37
|
+
* Onde a marca fica na tela de login.
|
|
38
|
+
*
|
|
39
|
+
* - `"top"` (padrão): logo pequena acima do formulário, tudo num cartão só.
|
|
40
|
+
* - `"right"` / `"left"`: a marca ocupa metade da tela, com um divisor entre
|
|
41
|
+
* ela e o formulário. É para logo grande — espremida no topo do cartão ela
|
|
42
|
+
* sai ilegível, e aumentar o cartão para caber estragaria o formulário.
|
|
43
|
+
*
|
|
44
|
+
* Em tela estreita os dois lados viram empilhado com a marca em cima: meia
|
|
45
|
+
* tela de logo num celular não deixaria o formulário aparecer.
|
|
46
|
+
*/
|
|
47
|
+
loginLayout?: "top" | "left" | "right";
|
|
36
48
|
}
|
|
37
49
|
|
|
38
50
|
/** O que os componentes do core consomem: sem opcionais, tudo já resolvido. */
|
|
@@ -41,6 +53,7 @@ export interface BrandContextType {
|
|
|
41
53
|
mark: ReactNode;
|
|
42
54
|
logo: ReactNode;
|
|
43
55
|
tagline: string | null;
|
|
56
|
+
loginLayout: "top" | "left" | "right";
|
|
44
57
|
}
|
|
45
58
|
|
|
46
59
|
const BrandContext = createContext<BrandContextType | undefined>(undefined);
|
|
@@ -64,6 +77,7 @@ export function BrandProvider({
|
|
|
64
77
|
mark,
|
|
65
78
|
logo: brand?.logo ?? mark,
|
|
66
79
|
tagline: brand?.tagline ?? null,
|
|
80
|
+
loginLayout: brand?.loginLayout ?? "top",
|
|
67
81
|
};
|
|
68
82
|
}, [brand, t]);
|
|
69
83
|
|
|
@@ -88,6 +102,7 @@ export function useBrand(): BrandContextType {
|
|
|
88
102
|
mark: defaultMark,
|
|
89
103
|
logo: defaultMark,
|
|
90
104
|
tagline: null,
|
|
105
|
+
loginLayout: "top",
|
|
91
106
|
}
|
|
92
107
|
);
|
|
93
108
|
}
|
|
@@ -14,9 +14,45 @@ import { en } from "#core/i18n/messages/en";
|
|
|
14
14
|
import { pt } from "#core/i18n/messages/pt";
|
|
15
15
|
|
|
16
16
|
export type Locale = "pt" | "en";
|
|
17
|
-
const MESSAGES = { pt, en } as const;
|
|
18
17
|
const STORAGE_KEY = "locale";
|
|
19
18
|
|
|
19
|
+
/**
|
|
20
|
+
* Dicionário de um idioma: objeto aninhado de strings, como `i18n/messages`.
|
|
21
|
+
*
|
|
22
|
+
* O tipo é aberto de propósito — o projeto acrescenta as seções dele, e travar
|
|
23
|
+
* a forma aqui obrigaria o core a conhecer as chaves de quem o instala.
|
|
24
|
+
*/
|
|
25
|
+
export type Messages = Record<string, unknown>;
|
|
26
|
+
|
|
27
|
+
/** Dicionário do projeto, por idioma. Cada um é opcional. */
|
|
28
|
+
export type ProjectMessages = Partial<Record<Locale, Messages>>;
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Mescla o dicionário do projeto sobre o do core.
|
|
32
|
+
*
|
|
33
|
+
* É profundo porque as seções coincidem: um projeto que acrescenta
|
|
34
|
+
* `nav.tasks` não pode apagar o resto de `nav`, que é o que o shell usa. Em
|
|
35
|
+
* conflito, o projeto vence — é o que permite renomear "Logs" sem tocar no
|
|
36
|
+
* pacote.
|
|
37
|
+
*/
|
|
38
|
+
function deepMerge(base: Messages, override: Messages): Messages {
|
|
39
|
+
const result: Messages = { ...base };
|
|
40
|
+
for (const [key, value] of Object.entries(override)) {
|
|
41
|
+
const current = result[key];
|
|
42
|
+
const bothObjects =
|
|
43
|
+
current !== null &&
|
|
44
|
+
typeof current === "object" &&
|
|
45
|
+
!Array.isArray(current) &&
|
|
46
|
+
value !== null &&
|
|
47
|
+
typeof value === "object" &&
|
|
48
|
+
!Array.isArray(value);
|
|
49
|
+
result[key] = bothObjects
|
|
50
|
+
? deepMerge(current as Messages, value as Messages)
|
|
51
|
+
: value;
|
|
52
|
+
}
|
|
53
|
+
return result;
|
|
54
|
+
}
|
|
55
|
+
|
|
20
56
|
export interface I18nContextType {
|
|
21
57
|
locale: Locale;
|
|
22
58
|
setLocale: (l: Locale) => void;
|
|
@@ -34,9 +70,35 @@ function resolve(obj: unknown, path: string): string | undefined {
|
|
|
34
70
|
}, obj) as string | undefined;
|
|
35
71
|
}
|
|
36
72
|
|
|
37
|
-
export
|
|
73
|
+
export interface I18nProviderProps {
|
|
74
|
+
children: React.ReactNode;
|
|
75
|
+
/**
|
|
76
|
+
* Dicionário do projeto, mesclado sobre o do core (§14: o core não sabe os
|
|
77
|
+
* textos de quem o instala).
|
|
78
|
+
*
|
|
79
|
+
* ```tsx
|
|
80
|
+
* <I18nProvider messages={{ pt: { nav: { tasks: "Tarefas" } } }}>
|
|
81
|
+
* ```
|
|
82
|
+
*/
|
|
83
|
+
messages?: ProjectMessages;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
export function I18nProvider({
|
|
87
|
+
children,
|
|
88
|
+
messages,
|
|
89
|
+
}: I18nProviderProps): JSX.Element {
|
|
38
90
|
const [locale, setLocaleState] = useState<Locale>("pt");
|
|
39
91
|
|
|
92
|
+
// Mescla uma vez por dicionário, não a cada tradução: `t` roda em todo render
|
|
93
|
+
// de toda tela que usa texto.
|
|
94
|
+
const dictionary = useMemo(
|
|
95
|
+
() => ({
|
|
96
|
+
pt: messages?.pt ? deepMerge(pt, messages.pt) : (pt as Messages),
|
|
97
|
+
en: messages?.en ? deepMerge(en, messages.en) : (en as Messages),
|
|
98
|
+
}),
|
|
99
|
+
[messages],
|
|
100
|
+
);
|
|
101
|
+
|
|
40
102
|
useEffect(() => {
|
|
41
103
|
const saved = (localStorage.getItem(STORAGE_KEY) as Locale) || null;
|
|
42
104
|
if (saved === "pt" || saved === "en") {setLocaleState(saved);}
|
|
@@ -50,15 +112,18 @@ export function I18nProvider({ children }: { children: React.ReactNode }): JSX.E
|
|
|
50
112
|
|
|
51
113
|
const t = useCallback(
|
|
52
114
|
(key: string, params?: Record<string, string | number>) => {
|
|
115
|
+
// Sem tradução no idioma corrente, cai no português; sem nenhuma, devolve
|
|
116
|
+
// a própria chave — é o que deixa `label: "Tarefas"` funcionar como texto
|
|
117
|
+
// cru em quem não quer cadastrar dicionário.
|
|
53
118
|
const raw =
|
|
54
|
-
resolve(
|
|
119
|
+
resolve(dictionary[locale], key) ?? resolve(dictionary.pt, key) ?? key;
|
|
55
120
|
if (!params) {return raw;}
|
|
56
121
|
return Object.entries(params).reduce(
|
|
57
122
|
(str, [k, v]) => str.replace(new RegExp(`\\{${k}\\}`, "g"), String(v)),
|
|
58
123
|
raw,
|
|
59
124
|
);
|
|
60
125
|
},
|
|
61
|
-
[locale],
|
|
126
|
+
[locale, dictionary],
|
|
62
127
|
);
|
|
63
128
|
|
|
64
129
|
const value = useMemo(
|