-
Notifications
You must be signed in to change notification settings - Fork 353
Expand file tree
/
Copy pathjwt-input.component.tsx
More file actions
120 lines (107 loc) · 4.36 KB
/
jwt-input.component.tsx
File metadata and controls
120 lines (107 loc) · 4.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
"use client";
import React, { useEffect, useState } from "react";
import { CardComponent } from "@/features/common/components/card/card.component";
import { JwtEditorComponent } from "@/features/decoder/components/jwt-editor.component";
import { DEFAULT_JWT } from "@/features/decoder/services/token-decoder.service";
import { useDecoderStore } from "@/features/decoder/services/decoder.store";
import { TokenDecoderSignatureValidationComponent } from "@/features/decoder/components/token-decoder-signature-validation.component";
import { HomeDictionaryModel } from "@/features/localization/models/home-dictionary.model";
import { useDebuggerStore } from "@/features/debugger/services/debugger.store";
import { extractJwt } from "@/features/common/services/utils";
import { dataTestidDictionary } from "@/libs/testing/data-testid.dictionary";
import { CardToolbarComponent } from "@/features/common/components/card-toolbar/card-toolbar.component";
import { CardToolbarCopyButtonComponent } from "@/features/common/components/card-toolbar-buttons/card-toolbar-copy-button/card-toolbar-copy-button.component";
import { CardToolbarClearButtonComponent } from "@/features/common/components/card-toolbar-buttons/card-toolbar-clear-button/card-toolbar-clear-button.component";
import styles from "./jwt-input.module.scss";
import { CheckboxComponent } from "@/features/common/components/checkbox/checkbox.component";
type JwtInputComponentProps = {
languageCode: string;
dictionary: HomeDictionaryModel["decoder"]["jwtEditor"];
};
export const JwtInputComponent: React.FC<JwtInputComponentProps> = ({
languageCode,
dictionary,
}) => {
const [autoFocusEnabled, setAutofocusEnabled] = useState<boolean | undefined>(
undefined
);
const handleJwtChange$ = useDecoderStore((state) => state.handleJwtChange);
const jwt$ = useDecoderStore((state) => state.jwt);
const decodeErrors$ = useDecoderStore((state) => state.decodingErrors);
const decoderInputs$ = useDebuggerStore((state) => state.decoderInputs$);
const [token, setToken] = useState<string>(
decoderInputs$.jwt || DEFAULT_JWT.token
);
const clearValue = async () => {
handleJwtChange$("");
};
const handleJwtChange = async (newValue: string) => {
const cleanValue = extractJwt(newValue);
setToken(cleanValue);
handleJwtChange$(cleanValue);
};
const handleCheckboxChange = (selected: boolean) => {
localStorage.setItem("autofocus-enabled", JSON.stringify(selected));
setAutofocusEnabled(selected);
};
useEffect(() => {
const saved = localStorage.getItem("autofocus-enabled");
setAutofocusEnabled(saved ? !!JSON.parse(saved) : false);
}, []);
useEffect(() => {
setToken(jwt$);
}, [jwt$]);
return (
<>
<div style={{ display: "flex", justifyContent: "space-between" }}>
<span className={styles.headline}>{dictionary.headline}</span>
<CheckboxComponent
isSelected={autoFocusEnabled}
onChange={(e) => handleCheckboxChange(e)}
>
<span className={styles.checkbox__label}>{dictionary.autoFocusLabel}</span>
</CheckboxComponent>
</div>
<CardComponent
id={dataTestidDictionary.decoder.jwtEditor.id}
languageCode={languageCode}
title={dictionary.title}
compactTitle={dictionary.compactTitle}
options={{ fullWidth: true, noPadding: true }}
messages={{
success: [dictionary.successMessage],
errors: decodeErrors$,
}}
hasHeaderIcon
slots={{
notification: (
<TokenDecoderSignatureValidationComponent
id={dataTestidDictionary.decoder.jwtEditor.notificationBar.id}
/>
),
toolbar: (
<CardToolbarComponent ariaLabel={"JWT editor toolbar"}>
<CardToolbarCopyButtonComponent
languageCode={languageCode}
value={token}
/>
<CardToolbarClearButtonComponent
onPress={clearValue}
isDisabled={!token}
languageCode={languageCode}
/>
</CardToolbarComponent>
),
}}
>
{autoFocusEnabled !== undefined ? (
<JwtEditorComponent
token={token}
handleJwtChange={handleJwtChange}
autoFocus={autoFocusEnabled}
/>
) : null}
</CardComponent>
</>
);
};