Files
commafeed/commafeed-client/src/components/settings/CustomCodeSettings.tsx

95 lines
3.3 KiB
TypeScript
Raw Normal View History

2023-05-05 14:03:28 +02:00
import { Trans } from "@lingui/macro"
2023-06-22 14:37:56 +02:00
import { Box, Button, Group, Input, Stack, useMantineTheme } from "@mantine/core"
2023-05-05 14:03:28 +02:00
import { useForm } from "@mantine/form"
2023-06-22 14:37:56 +02:00
import { Editor } from "@monaco-editor/react"
2023-05-05 14:56:53 +02:00
import { client, errorToStrings } from "app/client"
2023-05-05 14:55:03 +02:00
import { redirectToSelectedSource } from "app/slices/redirect"
import { useAppDispatch, useAppSelector } from "app/store"
2023-05-05 14:56:53 +02:00
import { Alert } from "components/Alert"
2023-05-05 14:03:28 +02:00
import { useEffect } from "react"
import { useAsyncCallback } from "react-async-hook"
import { TbDeviceFloppy } from "react-icons/tb"
interface FormData {
customCss: string
customJs: string
2023-05-05 14:03:28 +02:00
}
export function CustomCodeSettings() {
2023-05-05 14:03:28 +02:00
const settings = useAppSelector(state => state.user.settings)
2023-06-22 14:37:56 +02:00
const theme = useMantineTheme()
2023-05-05 14:03:28 +02:00
const dispatch = useAppDispatch()
2023-06-22 14:37:56 +02:00
const editorTheme = theme.colorScheme === "dark" ? "vs-dark" : "light"
2023-05-05 14:03:28 +02:00
const form = useForm<FormData>()
const { setValues } = form
const saveCustomCode = useAsyncCallback(
2023-05-05 14:47:15 +02:00
async (d: FormData) => {
if (!settings) return
await client.user.saveSettings({
...settings,
customCss: d.customCss,
customJs: d.customJs,
})
2023-05-05 14:03:28 +02:00
},
2023-05-05 14:47:15 +02:00
{
onSuccess: () => {
window.location.reload()
},
}
)
2023-05-05 14:03:28 +02:00
useEffect(() => {
if (!settings) return
2023-05-05 14:03:28 +02:00
setValues({
customCss: settings.customCss,
customJs: settings.customJs,
2023-05-05 14:03:28 +02:00
})
}, [setValues, settings])
2023-05-05 14:03:28 +02:00
return (
2023-05-05 14:56:53 +02:00
<>
{saveCustomCode.error && (
2023-05-05 14:56:53 +02:00
<Box mb="md">
<Alert messages={errorToStrings(saveCustomCode.error)} />
2023-05-05 14:56:53 +02:00
</Box>
)}
2023-05-05 14:03:28 +02:00
<form onSubmit={form.onSubmit(saveCustomCode.execute)}>
2023-05-05 14:56:53 +02:00
<Stack>
2023-06-22 14:37:56 +02:00
<Input.Wrapper description={<Trans>Custom CSS rules that will be applied</Trans>}>
<Editor
height="30vh"
defaultLanguage="css"
theme={editorTheme}
options={{ minimap: { enabled: false } }}
{...form.getInputProps("customCss")}
/>
</Input.Wrapper>
2023-05-05 14:56:53 +02:00
2023-06-22 14:37:56 +02:00
<Input.Wrapper description={<Trans>Custom JS code that will be executed on page load</Trans>}>
<Editor
height="30vh"
defaultLanguage="javascript"
theme={editorTheme}
options={{ minimap: { enabled: false } }}
{...form.getInputProps("customJs")}
/>
</Input.Wrapper>
2023-05-05 14:56:53 +02:00
<Group>
<Button variant="default" onClick={() => dispatch(redirectToSelectedSource())}>
<Trans>Cancel</Trans>
</Button>
<Button type="submit" leftIcon={<TbDeviceFloppy size={16} />} loading={saveCustomCode.loading}>
2023-05-05 14:56:53 +02:00
<Trans>Save</Trans>
</Button>
</Group>
</Stack>
</form>
</>
2023-05-05 14:03:28 +02:00
)
}