Skip to content

Commit

Permalink
feat: share config
Browse files Browse the repository at this point in the history
  • Loading branch information
alexfreska committed Jan 5, 2024
1 parent f5b4463 commit fa1297b
Show file tree
Hide file tree
Showing 17 changed files with 257 additions and 73 deletions.
6 changes: 6 additions & 0 deletions .changeset/many-tigers-guess.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'hostd': minor
'renterd': minor
---

The configuration page now has menu options to download or copy an image of the current configuration for easier sharing.
6 changes: 1 addition & 5 deletions apps/hostd/components/Config/AnnounceButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,7 @@ export function AnnounceButton() {
)

return (
<Button
variant="accent"
tip="Announce host address"
onClick={triggerConfirm}
>
<Button tip="Announce host address" onClick={triggerConfirm}>
<Bullhorn16 />
Announce
</Button>
Expand Down
37 changes: 37 additions & 0 deletions apps/hostd/components/Config/ConfigActions.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { Text, Button } from '@siafoundation/design-system'
import { Reset16, Save16 } from '@siafoundation/react-icons'
import { AnnounceButton } from './AnnounceButton'
import { useConfig } from '../../contexts/config'
import { ConfigContextMenu } from './ConfigContextMenu'

export function ConfigActions() {
const { changeCount, revalidateAndResetForm, form, onSubmit } = useConfig()
return (
<div className="flex items-center gap-2">
{!!changeCount && (
<Text size="12" color="subtle">
{changeCount === 1 ? '1 change' : `${changeCount} changes`}
</Text>
)}
<Button
tip="Reset all changes"
icon="contrast"
disabled={!changeCount}
onClick={revalidateAndResetForm}
>
<Reset16 />
</Button>
<Button
tip="Save all changes"
variant="accent"
disabled={!form.formState.isDirty || form.formState.isSubmitting}
onClick={onSubmit}
>
<Save16 />
Save changes
</Button>
<AnnounceButton />
<ConfigContextMenu />
</div>
)
}
49 changes: 49 additions & 0 deletions apps/hostd/components/Config/ConfigContextMenu.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import {
DropdownMenu,
DropdownMenuItem,
Button,
DropdownMenuLeftSlot,
DropdownMenuLabel,
} from '@siafoundation/design-system'
import {
Copy16,
Download16,
OverflowMenuHorizontal16,
} from '@siafoundation/react-icons'
import { useConfig } from '../../contexts/config'

export function ConfigContextMenu() {
const { takeScreenshot } = useConfig()
return (
<DropdownMenu
trigger={
<Button>
<OverflowMenuHorizontal16 />
</Button>
}
contentProps={{ align: 'end' }}
>
<DropdownMenuLabel>Actions</DropdownMenuLabel>
<DropdownMenuItem
onSelect={() => {
takeScreenshot({ name: 'config image', copy: true })
}}
>
<DropdownMenuLeftSlot>
<Copy16 />
</DropdownMenuLeftSlot>
Copy image of configuration
</DropdownMenuItem>
<DropdownMenuItem
onSelect={() => {
takeScreenshot({ name: 'config', download: true })
}}
>
<DropdownMenuLeftSlot>
<Download16 />
</DropdownMenuLeftSlot>
Download image of configuration
</DropdownMenuItem>
</DropdownMenu>
)
}
56 changes: 10 additions & 46 deletions apps/hostd/components/Config/index.tsx
Original file line number Diff line number Diff line change
@@ -1,31 +1,18 @@
import { Text, Button, ConfigurationPanel } from '@siafoundation/design-system'
import {
Reset16,
Save16,
Warning16,
CheckmarkFilled16,
} from '@siafoundation/react-icons'
import { Text, ConfigurationPanel } from '@siafoundation/design-system'
import { Warning16, CheckmarkFilled16 } from '@siafoundation/react-icons'
import { HostdSidenav } from '../HostdSidenav'
import { routes } from '../../config/routes'
import { useDialog } from '../../contexts/dialog'
import { HostdAuthedLayout } from '../../components/HostdAuthedLayout'
import { AnnounceButton } from './AnnounceButton'
import { useConfig } from '../../contexts/config'
import { ConfigNav } from './ConfigNav'
import { StateConnError } from './StateConnError'
import { ConfigActions } from './ConfigActions'

export function Config() {
const { openDialog } = useDialog()
const {
fields,
settings,
dynDNSCheck,
changeCount,
revalidateAndResetForm,
form,
onSubmit,
remoteError,
} = useConfig()
const { fields, settings, dynDNSCheck, form, remoteError, configRef } =
useConfig()
return (
<HostdAuthedLayout
title="Configuration"
Expand Down Expand Up @@ -54,39 +41,16 @@ export function Config() {
)
) : null
}
actions={
<div className="flex items-center gap-2">
{!!changeCount && (
<Text size="12" color="subtle">
{changeCount === 1 ? '1 change' : `${changeCount} changes`}
</Text>
)}
<Button
tip="Reset all changes"
icon="contrast"
disabled={!changeCount}
onClick={revalidateAndResetForm}
>
<Reset16 />
</Button>
<Button
tip="Save all changes"
variant="accent"
disabled={!form.formState.isDirty || form.formState.isSubmitting}
onClick={onSubmit}
>
<Save16 />
Save changes
</Button>
<AnnounceButton />
</div>
}
actions={<ConfigActions />}
openSettings={() => openDialog('settings')}
>
{remoteError ? (
<StateConnError />
) : (
<div className="p-6 flex flex-col gap-16 max-w-screen-xl">
<div
ref={configRef}
className="p-6 flex flex-col gap-16 max-w-screen-xl"
>
<ConfigurationPanel
title="Host"
category="host"
Expand Down
18 changes: 17 additions & 1 deletion apps/hostd/contexts/config/index.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { createContext, useContext } from 'react'
import { createContext, useContext, useRef } from 'react'
import {
triggerErrorToast,
useOnInvalid,
useFormInit,
useFormServerSynced,
useFormChangeCount,
nodeToImage,
} from '@siafoundation/design-system'
import { useCallback, useMemo } from 'react'
import { transformDown } from './transform'
Expand Down Expand Up @@ -85,6 +86,19 @@ export function useConfigMain() {
[form, onValid, onInvalid]
)

const configRef = useRef()
const takeScreenshot = useCallback(
async (props: {
name: string
quality?: number
copy?: boolean
download?: boolean
}) => {
nodeToImage(configRef.current, props)
},
[]
)

return {
fields,
settings,
Expand All @@ -96,6 +110,8 @@ export function useConfigMain() {
showAdvanced,
setShowAdvanced,
remoteError,
takeScreenshot,
configRef,
}
}

Expand Down
1 change: 0 additions & 1 deletion apps/hostd/contexts/metrics/useNowAtInterval.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ export function useNowAtInterval(dataInterval: DataInterval) {
setNow(new Date().getTime())
const i = setInterval(() => {
setNow(new Date().getTime())
console.log('reset time range')
}, getDataIntervalInMs(dataInterval))
return () => clearInterval(i)
}, [dataInterval])
Expand Down
2 changes: 2 additions & 0 deletions apps/renterd/components/Config/ConfigActions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
} from '@siafoundation/design-system'
import { Reset16, Save16, Settings16 } from '@siafoundation/react-icons'
import { useConfig } from '../../contexts/config'
import { ConfigContextMenu } from './ConfigContextMenu'

export function ConfigActions() {
const {
Expand Down Expand Up @@ -72,6 +73,7 @@ export function ConfigActions() {
</div>
</Popover>
</ControlGroup>
<ConfigContextMenu />
</div>
)
}
49 changes: 49 additions & 0 deletions apps/renterd/components/Config/ConfigContextMenu.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import {
DropdownMenu,
DropdownMenuItem,
Button,
DropdownMenuLeftSlot,
DropdownMenuLabel,
} from '@siafoundation/design-system'
import {
Copy16,
Download16,
OverflowMenuHorizontal16,
} from '@siafoundation/react-icons'
import { useConfig } from '../../contexts/config'

export function ConfigContextMenu() {
const { takeScreenshot } = useConfig()
return (
<DropdownMenu
trigger={
<Button>
<OverflowMenuHorizontal16 />
</Button>
}
contentProps={{ align: 'end' }}
>
<DropdownMenuLabel>Actions</DropdownMenuLabel>
<DropdownMenuItem
onSelect={() => {
takeScreenshot({ name: 'config image', copy: true })
}}
>
<DropdownMenuLeftSlot>
<Copy16 />
</DropdownMenuLeftSlot>
Copy image of configuration
</DropdownMenuItem>
<DropdownMenuItem
onSelect={() => {
takeScreenshot({ name: 'config', download: true })
}}
>
<DropdownMenuLeftSlot>
<Download16 />
</DropdownMenuLeftSlot>
Download image of configuration
</DropdownMenuItem>
</DropdownMenu>
)
}
7 changes: 5 additions & 2 deletions apps/renterd/components/Config/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { StateConnError } from './StateConnError'

export function Config() {
const { openDialog } = useDialog()
const { form, fields, remoteError } = useConfig()
const { form, fields, remoteError, configRef } = useConfig()

return (
<RenterdAuthedLayout
Expand All @@ -26,7 +26,10 @@ export function Config() {
{remoteError ? (
<StateConnError />
) : (
<div className="px-5 py-6 flex flex-col gap-16 max-w-screen-xl">
<div
ref={configRef}
className="px-5 py-6 flex flex-col gap-16 max-w-screen-xl"
>
<ConfigurationPanel
title="Storage"
category="storage"
Expand Down
18 changes: 17 additions & 1 deletion apps/renterd/contexts/config/index.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React, { createContext, useContext } from 'react'
import React, { useRef, createContext, useContext } from 'react'
import {
nodeToImage,
triggerErrorToast,
useFormChangeCount,
useFormInit,
Expand Down Expand Up @@ -199,6 +200,19 @@ export function useConfigMain() {
[form, onValid, onInvalid]
)

const configRef = useRef()
const takeScreenshot = useCallback(
async (props: {
name: string
quality?: number
copy?: boolean
download?: boolean
}) => {
nodeToImage(configRef.current, props)
},
[]
)

return {
onSubmit,
revalidateAndResetForm,
Expand All @@ -215,6 +229,8 @@ export function useConfigMain() {
showAdvanced,
setShowAdvanced,
remoteError,
configRef,
takeScreenshot,
}
}

Expand Down
1 change: 1 addition & 0 deletions libs/design-system/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
"clipboard-polyfill": "^4.0.1",
"@visx/xychart": "^2.18.0",
"react-dropzone": "^14.2.3",
"html-to-image": "^1.11.11",
"react-number-format": "^5.3.1",
"@radix-ui/react-radio-group": "^1.0.0",
"@radix-ui/react-accordion": "^1.0.0",
Expand Down
1 change: 1 addition & 0 deletions libs/design-system/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,5 +177,6 @@ export * from './lib/chartStats'
export * from './lib/ipRegex'
export * from './lib/getOs'
export * from './lib/countryEmoji'
export * from './lib/nodeToImage'

export { colors } from './config/colors'
Loading

0 comments on commit fa1297b

Please sign in to comment.