mirror of
https://github.com/mountain-loop/yaak.git
synced 2026-04-10 03:03:37 +02:00
Generalized frontend model store (#193)
This commit is contained in:
@@ -1,24 +1,25 @@
|
||||
import type { Folder } from '@yaakapp-internal/models';
|
||||
import { createWorkspaceModel } from '@yaakapp-internal/models';
|
||||
import { applySync, calculateSync } from '@yaakapp-internal/sync';
|
||||
import { Banner } from '../components/core/Banner';
|
||||
import { InlineCode } from '../components/core/InlineCode';
|
||||
import { VStack } from '../components/core/Stacks';
|
||||
import { getActiveWorkspaceId } from '../hooks/useActiveWorkspace';
|
||||
import { activeWorkspaceIdAtom } from '../hooks/useActiveWorkspace';
|
||||
import { createFastMutation } from '../hooks/useFastMutation';
|
||||
import { showConfirm } from '../lib/confirm';
|
||||
import { resolvedModelNameWithFolders } from '../lib/resolvedModelName';
|
||||
import { jotaiStore } from '../lib/jotai';
|
||||
import { pluralizeCount } from '../lib/pluralize';
|
||||
import { showPrompt } from '../lib/prompt';
|
||||
import { invokeCmd } from '../lib/tauri';
|
||||
import { resolvedModelNameWithFolders } from '../lib/resolvedModelName';
|
||||
|
||||
export const createFolder = createFastMutation<
|
||||
Folder | null,
|
||||
void,
|
||||
void,
|
||||
Partial<Pick<Folder, 'name' | 'sortPriority' | 'folderId'>>
|
||||
>({
|
||||
mutationKey: ['create_folder'],
|
||||
mutationFn: async (patch) => {
|
||||
const workspaceId = getActiveWorkspaceId();
|
||||
const workspaceId = jotaiStore.get(activeWorkspaceIdAtom);
|
||||
if (workspaceId == null) {
|
||||
throw new Error("Cannot create folder when there's no active workspace");
|
||||
}
|
||||
@@ -33,13 +34,13 @@ export const createFolder = createFastMutation<
|
||||
confirmText: 'Create',
|
||||
placeholder: 'Name',
|
||||
});
|
||||
if (name == null) return null;
|
||||
if (name == null) throw new Error('No name provided to create folder');
|
||||
|
||||
patch.name = name;
|
||||
}
|
||||
|
||||
patch.sortPriority = patch.sortPriority || -Date.now();
|
||||
return invokeCmd<Folder>('cmd_update_folder', { folder: { workspaceId, ...patch } });
|
||||
await createWorkspaceModel({ model: 'folder', workspaceId, ...patch });
|
||||
},
|
||||
});
|
||||
|
||||
|
||||
@@ -1,10 +0,0 @@
|
||||
import type { WebsocketConnection } from '@yaakapp-internal/models';
|
||||
import { deleteWebsocketConnection as cmdDeleteWebsocketConnection } from '@yaakapp-internal/ws';
|
||||
import { createFastMutation } from '../hooks/useFastMutation';
|
||||
|
||||
export const deleteWebsocketConnection = createFastMutation({
|
||||
mutationKey: ['delete_websocket_connection'],
|
||||
mutationFn: async function (connection: WebsocketConnection) {
|
||||
return cmdDeleteWebsocketConnection(connection.id);
|
||||
},
|
||||
});
|
||||
@@ -1,26 +0,0 @@
|
||||
import type { WebsocketRequest } from '@yaakapp-internal/models';
|
||||
import { deleteWebsocketRequest as cmdDeleteWebsocketRequest } from '@yaakapp-internal/ws';
|
||||
import { InlineCode } from '../components/core/InlineCode';
|
||||
import { createFastMutation } from '../hooks/useFastMutation';
|
||||
import { showConfirmDelete } from '../lib/confirm';
|
||||
import { resolvedModelName } from '../lib/resolvedModelName';
|
||||
|
||||
export const deleteWebsocketRequest = createFastMutation({
|
||||
mutationKey: ['delete_websocket_request'],
|
||||
mutationFn: async (request: WebsocketRequest) => {
|
||||
const confirmed = await showConfirmDelete({
|
||||
id: 'delete-websocket-request',
|
||||
title: 'Delete WebSocket Request',
|
||||
description: (
|
||||
<>
|
||||
Permanently delete <InlineCode>{resolvedModelName(request)}</InlineCode>?
|
||||
</>
|
||||
),
|
||||
});
|
||||
if (!confirmed) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return cmdDeleteWebsocketRequest(request.id);
|
||||
},
|
||||
});
|
||||
@@ -1,17 +0,0 @@
|
||||
import { duplicateWebsocketRequest as cmdDuplicateWebsocketRequest } from '@yaakapp-internal/ws';
|
||||
import { createFastMutation } from '../hooks/useFastMutation';
|
||||
import { router } from '../lib/router';
|
||||
|
||||
export const duplicateWebsocketRequest = createFastMutation({
|
||||
mutationKey: ['delete_websocket_connection'],
|
||||
mutationFn: async function (requestId: string) {
|
||||
return cmdDuplicateWebsocketRequest(requestId);
|
||||
},
|
||||
onSuccess: async (request) => {
|
||||
await router.navigate({
|
||||
to: '/workspaces/$workspaceId',
|
||||
params: { workspaceId: request.workspaceId },
|
||||
search: (prev) => ({ ...prev, request_id: request.id }),
|
||||
});
|
||||
},
|
||||
});
|
||||
@@ -1,13 +1,14 @@
|
||||
import { SettingsTab } from '../components/Settings/SettingsTab';
|
||||
import { getActiveWorkspaceId } from '../hooks/useActiveWorkspace';
|
||||
import { activeWorkspaceIdAtom } from '../hooks/useActiveWorkspace';
|
||||
import { createFastMutation } from '../hooks/useFastMutation';
|
||||
import { jotaiStore } from '../lib/jotai';
|
||||
import { router } from '../lib/router';
|
||||
import { invokeCmd } from '../lib/tauri';
|
||||
|
||||
export const openSettings = createFastMutation<void, string, SettingsTab | null>({
|
||||
mutationKey: ['open_settings'],
|
||||
mutationFn: async function (tab) {
|
||||
const workspaceId = getActiveWorkspaceId();
|
||||
const workspaceId = jotaiStore.get(activeWorkspaceIdAtom);
|
||||
if (workspaceId == null) return;
|
||||
|
||||
const location = router.buildLocation({
|
||||
|
||||
@@ -1,12 +1,13 @@
|
||||
import { WorkspaceSettingsDialog } from '../components/WorkspaceSettingsDialog';
|
||||
import { getActiveWorkspaceId } from '../hooks/useActiveWorkspace';
|
||||
import { activeWorkspaceIdAtom } from '../hooks/useActiveWorkspace';
|
||||
import { createFastMutation } from '../hooks/useFastMutation';
|
||||
import { showDialog } from '../lib/dialog';
|
||||
import { jotaiStore } from '../lib/jotai';
|
||||
|
||||
export const openWorkspaceSettings = createFastMutation<void, string, { openSyncMenu?: boolean }>({
|
||||
mutationKey: ['open_workspace_settings'],
|
||||
async mutationFn({ openSyncMenu }) {
|
||||
const workspaceId = getActiveWorkspaceId();
|
||||
const workspaceId = jotaiStore.get(activeWorkspaceIdAtom);
|
||||
showDialog({
|
||||
id: 'workspace-settings',
|
||||
title: 'Workspace Settings',
|
||||
|
||||
@@ -1,25 +0,0 @@
|
||||
import type { WebsocketRequest } from '@yaakapp-internal/models';
|
||||
import { upsertWebsocketRequest as cmdUpsertWebsocketRequest } from '@yaakapp-internal/ws';
|
||||
import { differenceInMilliseconds } from 'date-fns';
|
||||
import { createFastMutation } from '../hooks/useFastMutation';
|
||||
import { router } from '../lib/router';
|
||||
|
||||
export const upsertWebsocketRequest = createFastMutation<
|
||||
WebsocketRequest,
|
||||
void,
|
||||
Parameters<typeof cmdUpsertWebsocketRequest>[0]
|
||||
>({
|
||||
mutationKey: ['upsert_websocket_request'],
|
||||
mutationFn: (request) => cmdUpsertWebsocketRequest(request),
|
||||
onSuccess: async (request) => {
|
||||
const isNew = differenceInMilliseconds(new Date(), request.createdAt + 'Z') < 100;
|
||||
|
||||
if (isNew) {
|
||||
await router.navigate({
|
||||
to: '/workspaces/$workspaceId',
|
||||
params: { workspaceId: request.workspaceId },
|
||||
search: (prev) => ({ ...prev, request_id: request.id }),
|
||||
});
|
||||
}
|
||||
},
|
||||
});
|
||||
@@ -1,12 +0,0 @@
|
||||
import type { Workspace } from '@yaakapp-internal/models';
|
||||
import { createFastMutation } from '../hooks/useFastMutation';
|
||||
import { invokeCmd } from '../lib/tauri';
|
||||
|
||||
export const upsertWorkspace = createFastMutation<
|
||||
Workspace,
|
||||
void,
|
||||
Workspace | Partial<Omit<Workspace, 'id'>>
|
||||
>({
|
||||
mutationKey: ['upsert_workspace'],
|
||||
mutationFn: (workspace) => invokeCmd<Workspace>('cmd_update_workspace', { workspace }),
|
||||
});
|
||||
@@ -1,19 +0,0 @@
|
||||
import type { WorkspaceMeta } from '@yaakapp-internal/models';
|
||||
import { createFastMutation } from '../hooks/useFastMutation';
|
||||
import { workspaceMetaAtom } from '../hooks/useWorkspaceMeta';
|
||||
import { jotaiStore } from '../lib/jotai';
|
||||
import { invokeCmd } from '../lib/tauri';
|
||||
|
||||
export const upsertWorkspaceMeta = createFastMutation<
|
||||
WorkspaceMeta,
|
||||
unknown,
|
||||
WorkspaceMeta | (Partial<Omit<WorkspaceMeta, 'id'>> & { workspaceId: string })
|
||||
>({
|
||||
mutationKey: ['update_workspace_meta'],
|
||||
mutationFn: async (patch) => {
|
||||
const workspaceMeta = jotaiStore.get(workspaceMetaAtom);
|
||||
return invokeCmd<WorkspaceMeta>('cmd_update_workspace_meta', {
|
||||
workspaceMeta: { ...workspaceMeta, ...patch },
|
||||
});
|
||||
},
|
||||
});
|
||||
Reference in New Issue
Block a user