-
-
Notifications
You must be signed in to change notification settings - Fork 362
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: split useStore into several small hooks based on zustand st…
…ores
- Loading branch information
Showing
22 changed files
with
635 additions
and
807 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,10 @@ | ||
import { AppQueue } from './app'; | ||
import { AppJob, AppQueue, Status } from './app'; | ||
|
||
export interface GetQueuesResponse { | ||
queues: AppQueue[]; | ||
} | ||
|
||
export interface GetJobResponse { | ||
job: AppJob; | ||
status: Status; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 3 additions & 3 deletions
6
packages/ui/src/components/QueueDropdownActions/QueueDropdownActions.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { matchPath } from 'react-router'; | ||
import { useLocation } from 'react-router-dom'; | ||
|
||
export function useActiveJobId(): string { | ||
const { pathname } = useLocation(); | ||
|
||
const match = matchPath<{ name: string; jobId: string }>(pathname, { | ||
path: ['/queue/:name/:jobId'], | ||
exact: false, | ||
strict: false, | ||
}); | ||
|
||
return decodeURIComponent(match?.params.jobId || ''); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,15 @@ | ||
import { AppQueue } from '@bull-board/api/dist/typings/app'; | ||
import { useActiveQueueName } from './useActiveQueueName'; | ||
import { Store } from './useStore'; | ||
import { QueuesState } from './useQueues'; | ||
|
||
export function useActiveQueue(data: Store['state']['data']): AppQueue | null { | ||
export function useActiveQueue(data: Pick<QueuesState, 'queues'>): AppQueue | null { | ||
const activeQueueName = useActiveQueueName(); | ||
|
||
if (!data) { | ||
if (!data.queues) { | ||
return null; | ||
} | ||
|
||
const activeQueue = data.queues?.find((q) => q.name === activeQueueName); | ||
const activeQueue = data.queues.find((q) => q.name === activeQueueName); | ||
|
||
return activeQueue || null; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,45 +1,50 @@ | ||
import { useState } from 'react'; | ||
import { create } from 'zustand'; | ||
import { ConfirmProps } from '../components/ConfirmModal/ConfirmModal'; | ||
|
||
interface ConfirmState { | ||
promise: { resolve: (value: unknown) => void; reject: () => void } | null; | ||
opts: { title?: string; description?: string }; | ||
opts: { title?: string; description?: string } | null; | ||
setState(state: Omit<ConfirmState, 'setState'>): void; | ||
} | ||
|
||
export interface ConfirmApi { | ||
confirmProps: ConfirmProps; | ||
openConfirm: (opts?: ConfirmState['opts']) => Promise<unknown>; | ||
} | ||
|
||
export function useConfirm(): ConfirmApi { | ||
const [confirmData, setConfirmData] = useState<ConfirmState | null>(null); | ||
const useConfirmStore = create<ConfirmState>((set) => ({ | ||
opts: null, | ||
promise: null, | ||
setState: (state) => set(() => ({ ...state })), | ||
})); | ||
|
||
function openConfirm(opts: ConfirmState['opts'] = {}) { | ||
return new Promise((resolve, reject) => { | ||
setConfirmData({ promise: { resolve, reject }, opts }); | ||
}); | ||
} | ||
export function useConfirm(): ConfirmApi { | ||
const { promise, opts, setState } = useConfirmStore((state) => state); | ||
|
||
return { | ||
confirmProps: { | ||
open: !!confirmData?.promise, | ||
title: confirmData?.opts.title || 'Are you sure?', | ||
description: confirmData?.opts.description || '', | ||
onCancel: () => { | ||
setConfirmData({ | ||
opts: { title: confirmData?.opts.title, description: confirmData?.opts.description }, | ||
open: !!promise, | ||
title: opts?.title || 'Are you sure?', | ||
description: opts?.description || '', | ||
onCancel: function onCancel() { | ||
setState({ | ||
opts: { title: opts?.title, description: opts?.description }, | ||
promise: null, | ||
}); | ||
confirmData?.promise?.reject(); | ||
promise?.reject(); | ||
}, | ||
onConfirm: () => { | ||
setConfirmData({ | ||
opts: { title: confirmData?.opts.title, description: confirmData?.opts.description }, | ||
onConfirm: function onConfirm() { | ||
setState({ | ||
opts: { title: opts?.title, description: opts?.description }, | ||
promise: null, | ||
}); | ||
confirmData?.promise?.resolve(undefined); | ||
promise?.resolve(undefined); | ||
}, | ||
}, | ||
openConfirm, | ||
openConfirm: function openConfirm(opts: ConfirmState['opts'] = {}) { | ||
return new Promise((resolve, reject) => { | ||
setState({ promise: { resolve, reject }, opts }); | ||
}); | ||
}, | ||
}; | ||
} |
Oops, something went wrong.