Spaces:
Sleeping
Sleeping
Esteves Enzo
commited on
Commit
·
76ea2b9
1
Parent(s):
40c26ae
persistant store for authorization
Browse files- components/editor/main/index.tsx +0 -1
- components/editor/main/request.tsx +23 -2
- components/input/input.tsx +5 -2
- utils/axios.ts +0 -1
- utils/storage +0 -0
- utils/usePersistentState.ts +22 -0
components/editor/main/index.tsx
CHANGED
|
@@ -2,7 +2,6 @@
|
|
| 2 |
import { useState } from "react";
|
| 3 |
|
| 4 |
import { ApiRoute } from "@/utils/type";
|
| 5 |
-
import axios from "@/utils/axios";
|
| 6 |
|
| 7 |
import { Endpoint } from "./endpoint";
|
| 8 |
import { Request } from "./request";
|
|
|
|
| 2 |
import { useState } from "react";
|
| 3 |
|
| 4 |
import { ApiRoute } from "@/utils/type";
|
|
|
|
| 5 |
|
| 6 |
import { Endpoint } from "./endpoint";
|
| 7 |
import { Request } from "./request";
|
components/editor/main/request.tsx
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
|
|
| 1 |
import { Toggle } from "@/components/input/toggle";
|
| 2 |
import { TextInput } from "@/components/input/input";
|
| 3 |
-
import {
|
| 4 |
|
| 5 |
export const Request = ({
|
| 6 |
parameters,
|
|
@@ -11,11 +12,17 @@ export const Request = ({
|
|
| 11 |
children: React.ReactElement;
|
| 12 |
onChange: (key: string, value: string | boolean) => void;
|
| 13 |
}) => {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
return (
|
| 15 |
<div className="h-full bg-slate-900 p-5">
|
| 16 |
{children}
|
| 17 |
{parameters && (
|
| 18 |
-
<div className="mt-6 grid grid-cols-2 gap-6">
|
| 19 |
<p className="text-slate-200 uppercase text-xs font-semibold col-span-2">
|
| 20 |
Optional parameters
|
| 21 |
</p>
|
|
@@ -45,6 +52,20 @@ export const Request = ({
|
|
| 45 |
))}
|
| 46 |
</div>
|
| 47 |
)}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
</div>
|
| 49 |
);
|
| 50 |
};
|
|
|
|
| 1 |
+
"use client";
|
| 2 |
import { Toggle } from "@/components/input/toggle";
|
| 3 |
import { TextInput } from "@/components/input/input";
|
| 4 |
+
import { usePersistentState } from "@/utils/usePersistentState";
|
| 5 |
|
| 6 |
export const Request = ({
|
| 7 |
parameters,
|
|
|
|
| 12 |
children: React.ReactElement;
|
| 13 |
onChange: (key: string, value: string | boolean) => void;
|
| 14 |
}) => {
|
| 15 |
+
const [headers, setHeaders] = usePersistentState("headers", {
|
| 16 |
+
Authorization: "hf_api_key",
|
| 17 |
+
});
|
| 18 |
+
|
| 19 |
+
console.log(headers);
|
| 20 |
+
|
| 21 |
return (
|
| 22 |
<div className="h-full bg-slate-900 p-5">
|
| 23 |
{children}
|
| 24 |
{parameters && (
|
| 25 |
+
<div className="mt-6 grid grid-cols-2 gap-6 w-full">
|
| 26 |
<p className="text-slate-200 uppercase text-xs font-semibold col-span-2">
|
| 27 |
Optional parameters
|
| 28 |
</p>
|
|
|
|
| 52 |
))}
|
| 53 |
</div>
|
| 54 |
)}
|
| 55 |
+
<div className="mt-8 grid grid-cols-1 gap-6 w-full">
|
| 56 |
+
<p className="text-slate-200 uppercase text-xs font-semibold">
|
| 57 |
+
Headers
|
| 58 |
+
</p>
|
| 59 |
+
<TextInput
|
| 60 |
+
value={headers?.Authorization}
|
| 61 |
+
label="Authorization"
|
| 62 |
+
placeholder="Authorization"
|
| 63 |
+
onlyAlphaNumeric={false}
|
| 64 |
+
onChange={(Authorization) =>
|
| 65 |
+
setHeaders({ ...headers, Authorization })
|
| 66 |
+
}
|
| 67 |
+
/>
|
| 68 |
+
</div>
|
| 69 |
</div>
|
| 70 |
);
|
| 71 |
};
|
components/input/input.tsx
CHANGED
|
@@ -5,20 +5,23 @@ interface Props {
|
|
| 5 |
onChange: (value: string) => void;
|
| 6 |
placeholder?: string;
|
| 7 |
label?: string;
|
|
|
|
| 8 |
}
|
| 9 |
|
| 10 |
export const TextInput: React.FC<Props> = ({
|
| 11 |
value,
|
| 12 |
onChange,
|
| 13 |
placeholder,
|
|
|
|
| 14 |
label,
|
| 15 |
}) => {
|
| 16 |
const handleInputChange = (event: ChangeEvent<HTMLInputElement>) => {
|
| 17 |
const newValue = event.target.value;
|
| 18 |
// Only allow numbers or strings
|
| 19 |
-
if (/^[0-9a-zA-Z]*$/.test(newValue)) {
|
| 20 |
-
onChange(newValue);
|
| 21 |
}
|
|
|
|
| 22 |
};
|
| 23 |
|
| 24 |
return (
|
|
|
|
| 5 |
onChange: (value: string) => void;
|
| 6 |
placeholder?: string;
|
| 7 |
label?: string;
|
| 8 |
+
onlyAlphaNumeric?: boolean;
|
| 9 |
}
|
| 10 |
|
| 11 |
export const TextInput: React.FC<Props> = ({
|
| 12 |
value,
|
| 13 |
onChange,
|
| 14 |
placeholder,
|
| 15 |
+
onlyAlphaNumeric = true,
|
| 16 |
label,
|
| 17 |
}) => {
|
| 18 |
const handleInputChange = (event: ChangeEvent<HTMLInputElement>) => {
|
| 19 |
const newValue = event.target.value;
|
| 20 |
// Only allow numbers or strings
|
| 21 |
+
if (onlyAlphaNumeric && /^[0-9a-zA-Z]*$/.test(newValue)) {
|
| 22 |
+
return onChange(newValue);
|
| 23 |
}
|
| 24 |
+
onChange(newValue);
|
| 25 |
};
|
| 26 |
|
| 27 |
return (
|
utils/axios.ts
CHANGED
|
@@ -4,7 +4,6 @@ const axios = redaxios.create({
|
|
| 4 |
baseURL: process.env.NEXT_PUBLIC_APP_APIURL,
|
| 5 |
headers: {
|
| 6 |
'Content-Type': 'application/json',
|
| 7 |
-
Authorization: `Bearer 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`,
|
| 8 |
},
|
| 9 |
});
|
| 10 |
|
|
|
|
| 4 |
baseURL: process.env.NEXT_PUBLIC_APP_APIURL,
|
| 5 |
headers: {
|
| 6 |
'Content-Type': 'application/json',
|
|
|
|
| 7 |
},
|
| 8 |
});
|
| 9 |
|
utils/storage
ADDED
|
File without changes
|
utils/usePersistentState.ts
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { useEffect, useState } from "react";
|
| 2 |
+
import axios from "@/utils/axios";
|
| 3 |
+
|
| 4 |
+
export const usePersistentState = (key: string, defaultValue: any) => {
|
| 5 |
+
const [state, setState] = useState(() => {
|
| 6 |
+
if (typeof window !== 'undefined') {
|
| 7 |
+
const persistedState = localStorage?.getItem(key);
|
| 8 |
+
if (persistedState) {
|
| 9 |
+
axios.defaults.headers = {...JSON.parse(persistedState), Authorization: `Bearer ${JSON.parse(persistedState).Authorization}`}
|
| 10 |
+
}
|
| 11 |
+
return persistedState !== null ? JSON.parse(persistedState) : defaultValue;
|
| 12 |
+
}
|
| 13 |
+
return defaultValue;
|
| 14 |
+
});
|
| 15 |
+
|
| 16 |
+
useEffect(() => {
|
| 17 |
+
localStorage.setItem(key, JSON.stringify(state));
|
| 18 |
+
axios.defaults.headers = {...state, Authorization: `Bearer ${state.Authorization}`}
|
| 19 |
+
}, [key, state]);
|
| 20 |
+
|
| 21 |
+
return [state, setState];
|
| 22 |
+
}
|