Skip to content

Commit

Permalink
refactor: renterd libs extract route variables
Browse files Browse the repository at this point in the history
  • Loading branch information
alexfreska committed Apr 15, 2024
1 parent e54efd3 commit 3f50d41
Show file tree
Hide file tree
Showing 13 changed files with 324 additions and 154 deletions.
4 changes: 2 additions & 2 deletions apps/renterd/config/routes.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { busStateKey } from '@siafoundation/renterd-react'
import { busStateRoute } from '@siafoundation/renterd-types'

export const routes = {
home: '/',
Expand Down Expand Up @@ -38,4 +38,4 @@ export const routes = {
login: '/login',
}

export const connectivityRoute = busStateKey
export const connectivityRoute = busStateRoute
10 changes: 5 additions & 5 deletions libs/react-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@
"version": "1.1.0",
"license": "MIT",
"peerDependencies": {
"react": "^18.2.0",
"react": "^18.2.0"
},
"dependencies": {
"@siafoundation/next": "^0.1.3",
"@siafoundation/request": "0.0.0",
"swr": "^2.1.1",
"axios": "^0.27.2",
"use-local-storage-state": "^18.3.3",
"@siafoundation/next": "^0.1.3",
"@siafoundation/request": "0.0.0"
},
"dependencies": {
"detect-gpu": "^5.0.34"
},
"types": "./src/index.d.ts"
Expand Down
2 changes: 1 addition & 1 deletion libs/react-core/src/useDelete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ import {
buildRouteWithParams,
InternalCallbackArgs,
mergeInternalCallbackArgs,
RequestParams,
Response,
InternalHookArgsCallback,
mergeInternalHookArgsCallback,
After,
getPathFromKey,
} from './request'
import { useAppSettings } from './useAppSettings'
import { RequestParams } from '@siafoundation/request'

type DeleteFunc<Params extends RequestParams, Result> = {
delete: (
Expand Down
2 changes: 1 addition & 1 deletion libs/react-core/src/useGet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import {
buildRouteWithParams,
InternalCallbackArgs,
mergeInternalCallbackArgs,
RequestParams,
Response,
mergeInternalHookArgsCallback,
InternalHookArgsSwr,
Expand All @@ -18,6 +17,7 @@ import {
import { SWRError } from './types'
import { useAppSettings } from './useAppSettings'
import { keyOrNull } from './utils'
import { RequestParams } from '@siafoundation/request'

export function useGetSwr<Params extends RequestParams, Result>(
args: InternalHookArgsSwr<Params, Result>
Expand Down
2 changes: 1 addition & 1 deletion libs/react-core/src/usePatch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import {
InternalCallbackArgs,
InternalHookArgsCallback,
mergeInternalCallbackArgs,
RequestParams,
Response,
mergeInternalHookArgsCallback,
getPathFromKey,
Expand All @@ -22,6 +21,7 @@ import { useAppSettings } from './useAppSettings'
import { useMemo } from 'react'
import { keyOrNull } from './utils'
import { SWRError } from './types'
import { RequestParams } from '@siafoundation/request'

export function usePatchSwr<Params extends RequestParams, Payload, Result>(
args: InternalHookArgsWithPayloadSwr<Params, Payload, Result>
Expand Down
2 changes: 1 addition & 1 deletion libs/react-core/src/usePost.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import {
buildRouteWithParams,
InternalCallbackArgs,
mergeInternalCallbackArgs,
RequestParams,
Response,
InternalHookArgsCallback,
mergeInternalHookArgsCallback,
Expand All @@ -22,6 +21,7 @@ import { SWRError } from './types'
import { useAppSettings } from './useAppSettings'
import { keyOrNull } from './utils'
import { useWorkflows } from './workflows'
import { RequestParams } from '@siafoundation/request'

export function usePostSwr<Params extends RequestParams, Payload, Result>(
args: InternalHookArgsWithPayloadSwr<Params, Payload, Result>
Expand Down
2 changes: 1 addition & 1 deletion libs/react-core/src/usePut.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import {
InternalCallbackArgs,
InternalHookArgsCallback,
mergeInternalCallbackArgs,
RequestParams,
Response,
mergeInternalHookArgsCallback,
getPathFromKey,
Expand All @@ -22,6 +21,7 @@ import { useAppSettings } from './useAppSettings'
import { useMemo } from 'react'
import { keyOrNull } from './utils'
import { SWRError } from './types'
import { RequestParams } from '@siafoundation/request'

export function usePutSwr<Params extends RequestParams, Payload, Result>(
args: InternalHookArgsWithPayloadSwr<Params, Payload, Result>
Expand Down
38 changes: 20 additions & 18 deletions libs/renterd-react/src/autopilot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,26 +22,27 @@ import {
AutopilotTriggerParams,
AutopilotTriggerPayload,
AutopilotTriggerResponse,
autopilotConfigRoute,
autopilotHostsRoute,
autopilotStateRoute,
autopilotTriggerRoute,
} from '@siafoundation/renterd-types'

const autopilotStateKey = '/autopilot/state'

export function useAutopilotState(
args?: HookArgsSwr<AutopilotStateParams, AutopilotStateResponse>
) {
return useGetSwr({
...args,
route: autopilotStateKey,
route: autopilotStateRoute,
})
}

const autopilotConfigKey = '/autopilot/config'
export function useAutopilotConfig(
args?: HookArgsSwr<AutopilotConfigParams, AutopilotConfigResponse>
) {
return useGetSwr({
...args,
route: autopilotConfigKey,
route: autopilotConfigRoute,
})
}

Expand All @@ -52,20 +53,21 @@ export function useAutopilotConfigUpdate(
AutopilotConfigUpdateResponse
>
) {
return usePutFunc({ ...args, route: autopilotConfigKey }, async (mutate) => {
mutate((key) => key === autopilotConfigKey)
// might need a delay before revalidating status which returns whether
// or not autopilot is configured
const func = async () => {
await delay(1000)
mutate((key) => key === autopilotStateKey)
return usePutFunc(
{ ...args, route: autopilotConfigRoute },
async (mutate) => {
mutate((key) => key === autopilotConfigRoute)
// might need a delay before revalidating status which returns whether
// or not autopilot is configured
const func = async () => {
await delay(1000)
mutate((key) => key === autopilotStateRoute)
}
func()
}
func()
})
)
}

export const autopilotHostsKey = '/autopilot/hosts'

export function useAutopilotHostsSearch(
args?: HookArgsWithPayloadSwr<
AutopilotHostsSearchParams,
Expand All @@ -75,7 +77,7 @@ export function useAutopilotHostsSearch(
) {
return usePostSwr({
...args,
route: autopilotHostsKey,
route: autopilotHostsRoute,
})
}

Expand All @@ -88,6 +90,6 @@ export function useAutopilotTrigger(
) {
return usePostFunc({
...args,
route: '/autopilot/trigger',
route: autopilotTriggerRoute,
})
}
Loading

0 comments on commit 3f50d41

Please sign in to comment.