mirror of
https://github.com/mountain-loop/yaak.git
synced 2026-03-11 17:46:41 -05:00
Generalized frontend model store (#193)
This commit is contained in:
@@ -1,16 +1,32 @@
|
||||
import { getCurrentWebviewWindow } from '@tauri-apps/api/webviewWindow';
|
||||
import type { ModelPayload } from '@yaakapp-internal/models';
|
||||
import { atom, useAtomValue } from 'jotai';
|
||||
import { generateId } from '../lib/generateId';
|
||||
import { jotaiStore } from '../lib/jotai';
|
||||
|
||||
const keyAtom = atom<Record<string, string>>({});
|
||||
const requestUpdateKeyAtom = atom<Record<string, string>>({});
|
||||
|
||||
getCurrentWebviewWindow()
|
||||
.listen<ModelPayload>('upserted_model', ({ payload }) => {
|
||||
if (
|
||||
(payload.model.model === 'http_request' ||
|
||||
payload.model.model === 'grpc_request' ||
|
||||
payload.model.model === 'websocket_request') &&
|
||||
((payload.updateSource.type === 'window' &&
|
||||
payload.updateSource.label !== getCurrentWebviewWindow().label) ||
|
||||
payload.updateSource.type !== 'window')
|
||||
) {
|
||||
wasUpdatedExternally(payload.model.id);
|
||||
}
|
||||
})
|
||||
.catch(console.error);
|
||||
|
||||
export function wasUpdatedExternally(changedRequestId: string) {
|
||||
jotaiStore.set(requestUpdateKeyAtom, (m) => ({ ...m, [changedRequestId]: generateId() }));
|
||||
}
|
||||
|
||||
export function useRequestUpdateKey(requestId: string | null) {
|
||||
const keys = useAtomValue(keyAtom);
|
||||
const keys = useAtomValue(requestUpdateKeyAtom);
|
||||
const key = keys[requestId ?? 'n/a'];
|
||||
return {
|
||||
updateKey: `${requestId}::${key ?? 'default'}`,
|
||||
wasUpdatedExternally: (changedRequestId: string) => {
|
||||
jotaiStore.set(keyAtom, (m) => ({ ...m, [changedRequestId]: generateId() }));
|
||||
},
|
||||
};
|
||||
return `${requestId}::${key ?? 'default'}`;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user