Skip to content

Commit

Permalink
admin/custom: persist section
Browse files Browse the repository at this point in the history
  • Loading branch information
rejetto committed Apr 12, 2024
1 parent 40adce2 commit 7bac326
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 5 deletions.
14 changes: 10 additions & 4 deletions admin/src/CustomHtmlPage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { Save } from '@mui/icons-material'
import _ from 'lodash'
import { useDebounce } from 'usehooks-ts'
import { TextEditor } from './TextEditor';
import { state, useSnapState } from './state'

const names: any = {
top: "Top of HTML Body",
Expand All @@ -18,15 +19,15 @@ const names: any = {

export default function CustomHtmlPage() {
const { data, reload } = useApiEx<{ sections: Dict<string> }>('get_custom_html')
const [section, setSection] = useState('')
const { customHtmlSection: section } = useSnapState()
const [all, setAll] = useState<Dict<string>>({})
const [saved, setSaved] = useState({})
useEffect(() => data && setSaved(data?.sections), [data])
useEffect(() => setAll(saved), [saved])
const options = useMemo(() => {
const keys = _.sortBy(Object.keys(all), x => !isNaN(+x)) // http codes at the bottom
if (!keys.includes(section))
setSection(_.findKey(all, Boolean) || keys?.[0] || '') // prefer any key with content
if (keys.length && !keys.includes(section))
state.customHtmlSection = _.findKey(all, Boolean) || keys?.[0] || '' // prefer any key with content
return keys.map(x => ({
value: x,
label: (names[x] || prefix('HTTP ', HTTP_MESSAGES[x as any]) || _.startCase(x)) + (all[x]?.trim() ? ' *' : '')
Expand All @@ -40,7 +41,12 @@ export default function CustomHtmlPage() {
wikiLink('customization', "More help")
),
h(Box, { display: 'flex', alignItems: 'center', gap: 1, mb: 1 },
h(SelectField as Field<string>, { label: "Section", value: section, options, onChange: setSection }),
h(SelectField as Field<string>, {
label: "Section",
value: section,
options,
onChange: v => state.customHtmlSection = v
}),
reloadBtn(reload),
h(IconBtn, {
icon: Save,
Expand Down
2 changes: 1 addition & 1 deletion admin/src/FileForm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { createElement as h, forwardRef, ReactElement, ReactNode, useEffect, use
import { Alert, Box, Collapse, FormHelperText, Link, MenuItem, MenuList, useTheme } from '@mui/material'
import { BoolField, DisplayField, Field, FieldProps, Form, MultiSelectField, SelectField, StringField
} from '@hfs/mui-grid-form'
import { apiCall, UseApi, useApiEx } from './api'
import { apiCall, UseApi } from './api'
import { basename, defaultPerms, formatBytes, formatTimestamp, isEqualLax, isWhoObject, newDialog, objSameKeys,
onlyTruthy, prefix, VfsPerms, wantArray, Who, WhoObject, matches, HTTP_MESSAGES, xlate, md } from './misc'
import { Btn, IconBtn, LinkBtn, modifiedProps, useBreakpoint, wikiLink } from './mui'
Expand Down
2 changes: 2 additions & 0 deletions admin/src/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export const state = proxy<{
loginRequired: boolean | number
username: string
monitorOnlyFiles: boolean
customHtmlSection: string,
onlinePluginsColumns: Dict<boolean>
}>(Object.assign({
title: '',
Expand All @@ -26,6 +27,7 @@ export const state = proxy<{
loginRequired: false,
username: '',
monitorOnlyFiles: true,
customHtmlSection: '',
onlinePluginsColumns: {
version: false,
pushed_at: false,
Expand Down

0 comments on commit 7bac326

Please sign in to comment.