refactor Pattern atoms structure.
This commit is contained in:
parent
2bb0cc35f9
commit
1c48bb36d3
|
@ -1,9 +1,10 @@
|
||||||
import { invoke } from '@tauri-apps/api/core';
|
import { invoke } from '@tauri-apps/api/core';
|
||||||
import dayjs from 'dayjs';
|
import dayjs from 'dayjs';
|
||||||
import { atom } from 'jotai';
|
import { atom, useSetAtom } from 'jotai';
|
||||||
import { atomFamily, atomWithRefresh } from 'jotai/utils';
|
import { atomWithDefault, atomWithRefresh } from 'jotai/utils';
|
||||||
import { get, reduce } from 'lodash-es';
|
import { get, reduce } from 'lodash-es';
|
||||||
import { v4 } from 'uuid';
|
import { v4 } from 'uuid';
|
||||||
|
import { NotificationType, ToastDuration, useNotification } from '../components/Notifications';
|
||||||
|
|
||||||
export enum FrequencyShifting {
|
export enum FrequencyShifting {
|
||||||
/**
|
/**
|
||||||
|
@ -120,18 +121,34 @@ export function totalDuration(pattern: Pattern): number {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
export const PatternsAtom = atomFamily((keyword: string) =>
|
export const SearchKeywordAtom = atom<string | null>(null);
|
||||||
atomWithRefresh(async () => {
|
export const PatternsAtom = atomWithRefresh(async (get) => {
|
||||||
try {
|
try {
|
||||||
|
const keyword = get(SearchKeywordAtom);
|
||||||
|
if (keyword === null) {
|
||||||
|
return [];
|
||||||
|
}
|
||||||
const patterns = await invoke<Pattern[]>('list_patterns', { keyword });
|
const patterns = await invoke<Pattern[]>('list_patterns', { keyword });
|
||||||
return patterns;
|
return patterns;
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error('[retrieving pattern list]', e);
|
console.error('[retrieving pattern list]', e);
|
||||||
}
|
}
|
||||||
return [];
|
return [];
|
||||||
}),
|
});
|
||||||
);
|
export const SelectedPatternIdAtom = atomWithDefault<string | null>(null);
|
||||||
export const CurrentPatternAtom = atom<Pattern | null>(null);
|
export const CurrentPatternAtom = atom<Pattern | null>(async (get) => {
|
||||||
|
try {
|
||||||
|
const patternId = get(SelectedPatternIdAtom);
|
||||||
|
if (patternId === null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
const pattern = await invoke('get_pattern', { patternId });
|
||||||
|
return pattern;
|
||||||
|
} catch (e) {
|
||||||
|
console.error('[retrieving pattern]', e);
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
});
|
||||||
export const PulsesInCurrentPatternAtom = atom(
|
export const PulsesInCurrentPatternAtom = atom(
|
||||||
(get) => get(CurrentPatternAtom)?.pulses ?? [],
|
(get) => get(CurrentPatternAtom)?.pulses ?? [],
|
||||||
(get, set, pulse: Pulse) => {
|
(get, set, pulse: Pulse) => {
|
||||||
|
@ -155,3 +172,25 @@ export const CurrentPatternDuration = atom((get) => {
|
||||||
if (!currentPattern) return 0;
|
if (!currentPattern) return 0;
|
||||||
return totalDuration(currentPattern);
|
return totalDuration(currentPattern);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
export function useSavePattern() {
|
||||||
|
const refreshPatterns = useSetAtom(PatternsAtom);
|
||||||
|
const { showToast } = useNotification();
|
||||||
|
|
||||||
|
const savePattern = async (pattern: Pattern) => {
|
||||||
|
try {
|
||||||
|
await invoke('save_pattern', { pattern });
|
||||||
|
refreshPatterns();
|
||||||
|
} catch (error) {
|
||||||
|
console.error('[save pattern]', error);
|
||||||
|
showToast(
|
||||||
|
NotificationType.ERROR,
|
||||||
|
'Failed to save pattern. Please try again.',
|
||||||
|
'material-symbols-light:error-outline',
|
||||||
|
ToastDuration.MEDIUM,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
return savePattern;
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user