mirror of
https://github.com/mountain-loop/yaak.git
synced 2026-08-26 13:24:48 +02:00
Add a typed platform package to decouple the frontend from Tauri (#539)
Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5
parent
0068be9ffc
commit
2383f06e71
@@ -1,9 +1,9 @@
|
||||
import { event } from "@tauri-apps/api";
|
||||
import { platform } from "@yaakapp-internal/platform";
|
||||
import { useFastMutation } from "./useFastMutation";
|
||||
|
||||
export function useCancelHttpResponse(id: string | null) {
|
||||
return useFastMutation<void>({
|
||||
mutationKey: ["cancel_http_response", id],
|
||||
mutationFn: () => event.emit(`cancel_http_response_${id}`),
|
||||
mutationFn: () => platform.emit(`cancel_http_response_${id}`),
|
||||
});
|
||||
}
|
||||
|
||||
@@ -3,13 +3,13 @@ import { InlineCode } from "@yaakapp-internal/ui";
|
||||
import { showAlert } from "../lib/alert";
|
||||
import { appInfo } from "../lib/appInfo";
|
||||
import { minPromiseMillis } from "../lib/minPromiseMillis";
|
||||
import { invokeCmd } from "../lib/tauri";
|
||||
import { rpc } from "../lib/rpc";
|
||||
|
||||
export function useCheckForUpdates() {
|
||||
return useMutation({
|
||||
mutationKey: ["check_for_updates"],
|
||||
mutationFn: async () => {
|
||||
const hasUpdate: boolean = await minPromiseMillis(invokeCmd("cmd_check_for_updates"), 500);
|
||||
const hasUpdate: boolean = await minPromiseMillis(rpc("cmd_check_for_updates"), 500);
|
||||
if (!hasUpdate) {
|
||||
showAlert({
|
||||
id: "no-updates",
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { invokeCmd } from "../lib/tauri";
|
||||
import { rpc } from "../lib/rpc";
|
||||
import { useFastMutation } from "./useFastMutation";
|
||||
|
||||
export function useDeleteGrpcConnections(requestId?: string) {
|
||||
@@ -6,7 +6,7 @@ export function useDeleteGrpcConnections(requestId?: string) {
|
||||
mutationKey: ["delete_grpc_connections", requestId],
|
||||
mutationFn: async () => {
|
||||
if (requestId === undefined) return;
|
||||
await invokeCmd("cmd_delete_all_grpc_connections", { requestId });
|
||||
await rpc("cmd_delete_all_grpc_connections", { requestId });
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { invokeCmd } from "../lib/tauri";
|
||||
import { rpc } from "../lib/rpc";
|
||||
import { useFastMutation } from "./useFastMutation";
|
||||
|
||||
export function useDeleteHttpResponses(requestId?: string) {
|
||||
@@ -6,7 +6,7 @@ export function useDeleteHttpResponses(requestId?: string) {
|
||||
mutationKey: ["delete_http_responses", requestId],
|
||||
mutationFn: async () => {
|
||||
if (requestId === undefined) return;
|
||||
await invokeCmd("cmd_delete_all_http_responses", { requestId });
|
||||
await rpc("cmd_delete_all_http_responses", { requestId });
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
@@ -8,7 +8,7 @@ import { showAlert } from "../lib/alert";
|
||||
import { showConfirmDelete } from "../lib/confirm";
|
||||
import { jotaiStore } from "../lib/jotai";
|
||||
import { pluralizeCount } from "../lib/pluralize";
|
||||
import { invokeCmd } from "../lib/tauri";
|
||||
import { rpc } from "../lib/rpc";
|
||||
import { activeWorkspaceIdAtom } from "./useActiveWorkspace";
|
||||
import { useFastMutation } from "./useFastMutation";
|
||||
|
||||
@@ -45,7 +45,7 @@ export function useDeleteSendHistory() {
|
||||
if (!confirmed) return false;
|
||||
|
||||
const workspaceId = jotaiStore.get(activeWorkspaceIdAtom);
|
||||
await invokeCmd("cmd_delete_send_history", { workspaceId });
|
||||
await rpc("cmd_delete_send_history", { workspaceId });
|
||||
return true;
|
||||
},
|
||||
});
|
||||
|
||||
@@ -6,7 +6,7 @@ import type {
|
||||
GetFolderActionsResponse,
|
||||
} from "@yaakapp-internal/plugins";
|
||||
import { useMemo } from "react";
|
||||
import { invokeCmd } from "../lib/tauri";
|
||||
import { rpc } from "../lib/rpc";
|
||||
import { usePluginsKey } from "./usePlugins";
|
||||
|
||||
export type CallableFolderAction = Pick<FolderAction, "label" | "icon"> & {
|
||||
@@ -30,7 +30,7 @@ export function useFolderActions() {
|
||||
}
|
||||
|
||||
export async function getFolderActions() {
|
||||
const responses = await invokeCmd<GetFolderActionsResponse[]>("cmd_folder_actions");
|
||||
const responses = await rpc<GetFolderActionsResponse[]>("cmd_folder_actions");
|
||||
const actions = responses.flatMap((r) =>
|
||||
r.actions.map((a, i) => ({
|
||||
label: a.label,
|
||||
@@ -41,7 +41,7 @@ export async function getFolderActions() {
|
||||
pluginRefId: r.pluginRefId,
|
||||
args: { folder },
|
||||
};
|
||||
await invokeCmd("cmd_call_folder_action", { req: payload });
|
||||
await rpc("cmd_call_folder_action", { req: payload });
|
||||
},
|
||||
})),
|
||||
);
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
import { useMutation, useQuery } from "@tanstack/react-query";
|
||||
import { emit } from "@tauri-apps/api/event";
|
||||
import { platform } from "@yaakapp-internal/platform";
|
||||
import type { GrpcConnection, GrpcRequest } from "@yaakapp-internal/models";
|
||||
import { flushAllModelWrites } from "@yaakapp-internal/models";
|
||||
import { jotaiStore } from "../lib/jotai";
|
||||
import { minPromiseMillis } from "../lib/minPromiseMillis";
|
||||
import { invokeCmd } from "../lib/tauri";
|
||||
import { rpc } from "../lib/rpc";
|
||||
import { activeEnvironmentIdAtom, useActiveEnvironment } from "./useActiveEnvironment";
|
||||
import { useDebouncedValue } from "@yaakapp-internal/ui";
|
||||
|
||||
@@ -25,7 +25,7 @@ export function useGrpc(
|
||||
mutationKey: ["grpc_go", conn?.id],
|
||||
mutationFn: async () => {
|
||||
await flushAllModelWrites(); // The backend reads the request from the DB
|
||||
return invokeCmd<void>("cmd_grpc_go", {
|
||||
return rpc<void>("cmd_grpc_go", {
|
||||
requestId,
|
||||
environmentId: environment?.id,
|
||||
protoFiles,
|
||||
@@ -36,17 +36,17 @@ export function useGrpc(
|
||||
const send = useMutation({
|
||||
mutationKey: ["grpc_send", conn?.id],
|
||||
mutationFn: ({ message }: { message: string }) =>
|
||||
emit(`grpc_client_msg_${conn?.id ?? "none"}`, { Message: message }),
|
||||
platform.emit(`grpc_client_msg_${conn?.id ?? "none"}`, { Message: message }),
|
||||
});
|
||||
|
||||
const cancel = useMutation({
|
||||
mutationKey: ["grpc_cancel", conn?.id ?? "n/a"],
|
||||
mutationFn: () => emit(`grpc_client_msg_${conn?.id ?? "none"}`, "Cancel"),
|
||||
mutationFn: () => platform.emit(`grpc_client_msg_${conn?.id ?? "none"}`, "Cancel"),
|
||||
});
|
||||
|
||||
const commit = useMutation({
|
||||
mutationKey: ["grpc_commit", conn?.id ?? "n/a"],
|
||||
mutationFn: () => emit(`grpc_client_msg_${conn?.id ?? "none"}`, "Commit"),
|
||||
mutationFn: () => platform.emit(`grpc_client_msg_${conn?.id ?? "none"}`, "Commit"),
|
||||
});
|
||||
|
||||
const debouncedUrl = useDebouncedValue<string>(req?.url ?? "", 1000);
|
||||
@@ -61,7 +61,7 @@ export function useGrpc(
|
||||
queryFn: () => {
|
||||
const environmentId = jotaiStore.get(activeEnvironmentIdAtom);
|
||||
return minPromiseMillis<ReflectResponseService[]>(
|
||||
invokeCmd("cmd_grpc_reflect", { requestId, protoFiles, environmentId }),
|
||||
rpc("cmd_grpc_reflect", { requestId, protoFiles, environmentId }),
|
||||
300,
|
||||
);
|
||||
},
|
||||
|
||||
@@ -6,7 +6,7 @@ import type {
|
||||
GrpcRequestAction,
|
||||
} from "@yaakapp-internal/plugins";
|
||||
import { useMemo } from "react";
|
||||
import { invokeCmd } from "../lib/tauri";
|
||||
import { rpc } from "../lib/rpc";
|
||||
import { getGrpcProtoFiles } from "./useGrpcProtoFiles";
|
||||
import { usePluginsKey } from "./usePlugins";
|
||||
|
||||
@@ -33,7 +33,7 @@ export function useGrpcRequestActions() {
|
||||
}
|
||||
|
||||
export async function getGrpcRequestActions() {
|
||||
const responses = await invokeCmd<GetGrpcRequestActionsResponse[]>("cmd_grpc_request_actions");
|
||||
const responses = await rpc<GetGrpcRequestActionsResponse[]>("cmd_grpc_request_actions");
|
||||
|
||||
return responses.flatMap((r) =>
|
||||
r.actions.map((a, i) => ({
|
||||
@@ -46,7 +46,7 @@ export async function getGrpcRequestActions() {
|
||||
pluginRefId: r.pluginRefId,
|
||||
args: { grpcRequest, protoFiles },
|
||||
};
|
||||
await invokeCmd("cmd_call_grpc_request_action", { req: payload });
|
||||
await rpc("cmd_call_grpc_request_action", { req: payload });
|
||||
},
|
||||
})),
|
||||
);
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { type } from "@tauri-apps/plugin-os";
|
||||
import { platform } from "@yaakapp-internal/platform";
|
||||
import { debounce } from "@yaakapp-internal/lib";
|
||||
import { settingsAtom } from "@yaakapp-internal/models";
|
||||
import { atom, useAtomValue } from "jotai";
|
||||
@@ -102,7 +102,7 @@ const defaultHotkeysOther: Record<HotkeyAction, string[]> = {
|
||||
|
||||
/** Get the default hotkeys for the current platform */
|
||||
export const defaultHotkeys: Record<HotkeyAction, string[]> =
|
||||
type() === "macos" ? defaultHotkeysMac : defaultHotkeysOther;
|
||||
platform.osType() === "macos" ? defaultHotkeysMac : defaultHotkeysOther;
|
||||
|
||||
/** Atom that provides the effective hotkeys by merging defaults with user settings */
|
||||
export const hotkeysAtom = atom((get) => {
|
||||
@@ -318,7 +318,7 @@ export function getHotkeyScope(action: HotkeyAction): string {
|
||||
}
|
||||
|
||||
export function formatHotkeyString(trigger: string): string[] {
|
||||
const os = type();
|
||||
const os = platform.osType();
|
||||
const parts = trigger.split("+");
|
||||
const labelParts: string[] = [];
|
||||
|
||||
|
||||
@@ -3,7 +3,7 @@ import type { GetHttpAuthenticationSummaryResponse } from "@yaakapp-internal/plu
|
||||
import { atom, useAtomValue } from "jotai";
|
||||
import { useState } from "react";
|
||||
import { jotaiStore } from "../lib/jotai";
|
||||
import { invokeCmd } from "../lib/tauri";
|
||||
import { rpc } from "../lib/rpc";
|
||||
import { showErrorToast } from "../lib/toast";
|
||||
import { usePluginsKey } from "./usePlugins";
|
||||
|
||||
@@ -31,7 +31,7 @@ export function useSubscribeHttpAuthentication() {
|
||||
placeholderData: (prev) => prev, // Keep previous data on refetch
|
||||
queryFn: async () => {
|
||||
try {
|
||||
const result = await invokeCmd<GetHttpAuthenticationSummaryResponse[]>(
|
||||
const result = await rpc<GetHttpAuthenticationSummaryResponse[]>(
|
||||
"cmd_get_http_authentication_summaries",
|
||||
);
|
||||
setNumResults(result.length);
|
||||
|
||||
@@ -11,7 +11,7 @@ import type { GetHttpAuthenticationConfigResponse, JsonPrimitive } from "@yaakap
|
||||
import { useAtomValue } from "jotai";
|
||||
import { md5 } from "js-md5";
|
||||
import { useState } from "react";
|
||||
import { invokeCmd } from "../lib/tauri";
|
||||
import { rpc } from "../lib/rpc";
|
||||
import { activeEnvironmentIdAtom } from "./useActiveEnvironment";
|
||||
import { activeWorkspaceIdAtom } from "./useActiveWorkspace";
|
||||
|
||||
@@ -48,7 +48,7 @@ export function useHttpAuthenticationConfig(
|
||||
placeholderData: (prev) => prev, // Keep previous data on refetch
|
||||
queryFn: async () => {
|
||||
if (authName == null || authName === "inherit") return null;
|
||||
const config = await invokeCmd<GetHttpAuthenticationConfigResponse>(
|
||||
const config = await rpc<GetHttpAuthenticationConfigResponse>(
|
||||
"cmd_get_http_authentication_config",
|
||||
{
|
||||
authName,
|
||||
@@ -65,7 +65,7 @@ export function useHttpAuthenticationConfig(
|
||||
call: async (
|
||||
model: HttpRequest | GrpcRequest | WebsocketRequest | Folder | Workspace,
|
||||
) => {
|
||||
await invokeCmd("cmd_call_http_authentication_action", {
|
||||
await rpc("cmd_call_http_authentication_action", {
|
||||
pluginRefId: config.pluginRefId,
|
||||
actionIndex: i,
|
||||
authName,
|
||||
|
||||
@@ -6,7 +6,7 @@ import type {
|
||||
HttpRequestAction,
|
||||
} from "@yaakapp-internal/plugins";
|
||||
import { useMemo } from "react";
|
||||
import { invokeCmd } from "../lib/tauri";
|
||||
import { rpc } from "../lib/rpc";
|
||||
import { usePluginsKey } from "./usePlugins";
|
||||
|
||||
export type CallableHttpRequestAction = Pick<HttpRequestAction, "label" | "icon"> & {
|
||||
@@ -30,7 +30,7 @@ export function useHttpRequestActions() {
|
||||
}
|
||||
|
||||
export async function getHttpRequestActions() {
|
||||
const responses = await invokeCmd<GetHttpRequestActionsResponse[]>("cmd_http_request_actions");
|
||||
const responses = await rpc<GetHttpRequestActionsResponse[]>("cmd_http_request_actions");
|
||||
const actions = responses.flatMap((r) =>
|
||||
r.actions.map((a, i) => ({
|
||||
label: a.label,
|
||||
@@ -41,7 +41,7 @@ export async function getHttpRequestActions() {
|
||||
pluginRefId: r.pluginRefId,
|
||||
args: { httpRequest },
|
||||
};
|
||||
await invokeCmd("cmd_call_http_request_action", { req: payload });
|
||||
await rpc("cmd_call_http_request_action", { req: payload });
|
||||
},
|
||||
})),
|
||||
);
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { useQuery } from "@tanstack/react-query";
|
||||
import type { HttpResponse } from "@yaakapp-internal/models";
|
||||
import { invokeCmd } from "../lib/tauri";
|
||||
import { rpc } from "../lib/rpc";
|
||||
|
||||
export function useHttpRequestBody(response: HttpResponse | null) {
|
||||
return useQuery({
|
||||
@@ -18,7 +18,7 @@ export async function getRequestBodyText(response: HttpResponse | null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const data = await invokeCmd<number[] | null>("cmd_http_request_body", {
|
||||
const data = await rpc<number[] | null>("cmd_http_request_body", {
|
||||
responseId: response.id,
|
||||
});
|
||||
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
import { invoke } from "@tauri-apps/api/core";
|
||||
import type { HttpResponse, HttpResponseEvent } from "@yaakapp-internal/models";
|
||||
import {
|
||||
httpResponseEventsAtom,
|
||||
@@ -8,6 +7,7 @@ import {
|
||||
import { useAtomValue } from "jotai";
|
||||
import { useEffect } from "react";
|
||||
import { fireAndForget } from "../lib/fireAndForget";
|
||||
import { rpc } from "../lib/rpc";
|
||||
|
||||
export function useHttpResponseEvents(response: HttpResponse | null) {
|
||||
const allEvents = useAtomValue(httpResponseEventsAtom);
|
||||
@@ -20,7 +20,7 @@ export function useHttpResponseEvents(response: HttpResponse | null) {
|
||||
|
||||
// Fetch events from database, filtering out events from other responses and merging atomically
|
||||
fireAndForget(
|
||||
invoke<HttpResponseEvent[]>("cmd_get_http_response_events", { responseId: response.id }).then(
|
||||
rpc<HttpResponseEvent[]>("cmd_get_http_response_events", { responseId: response.id }).then(
|
||||
(events) =>
|
||||
mergeModelsInStore("http_response_event", events, (e) => e.responseId === response.id),
|
||||
),
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { useQuery, useQueryClient } from "@tanstack/react-query";
|
||||
import { invoke } from "@tauri-apps/api/core";
|
||||
|
||||
import type { GraphQlIntrospection, HttpRequest } from "@yaakapp-internal/models";
|
||||
import type { GraphQLSchema, IntrospectionQuery } from "graphql";
|
||||
import { buildClientSchema, getIntrospectionQuery } from "graphql";
|
||||
@@ -9,6 +9,7 @@ import { getResponseBodyText } from "../lib/responseBody";
|
||||
import { sendEphemeralRequest } from "../lib/sendEphemeralRequest";
|
||||
import { useActiveEnvironment } from "./useActiveEnvironment";
|
||||
import { useDebouncedValue } from "@yaakapp-internal/ui";
|
||||
import { rpc } from "../lib/rpc";
|
||||
|
||||
const introspectionRequestBody = JSON.stringify({
|
||||
query: getIntrospectionQuery(),
|
||||
@@ -32,7 +33,7 @@ export function useIntrospectGraphQL(
|
||||
|
||||
const upsertIntrospection = useCallback(
|
||||
async (content: string | null) => {
|
||||
const v = await invoke<GraphQlIntrospection>("models_upsert_graphql_introspection", {
|
||||
const v = await rpc<GraphQlIntrospection>("models_upsert_graphql_introspection", {
|
||||
requestId: baseRequest.id,
|
||||
workspaceId: baseRequest.workspaceId,
|
||||
content: content ?? "",
|
||||
@@ -119,7 +120,7 @@ function useIntrospectionResult(request: HttpRequest) {
|
||||
return useQuery({
|
||||
queryKey: ["introspection", request.id],
|
||||
queryFn: async () =>
|
||||
invoke<GraphQlIntrospection | null>("models_get_graphql_introspection", {
|
||||
rpc<GraphQlIntrospection | null>("models_get_graphql_introspection", {
|
||||
requestId: request.id,
|
||||
}),
|
||||
});
|
||||
|
||||
@@ -1,28 +0,0 @@
|
||||
import type { EventCallback, EventName } from "@tauri-apps/api/event";
|
||||
import { listen } from "@tauri-apps/api/event";
|
||||
import { getCurrentWebviewWindow } from "@tauri-apps/api/webviewWindow";
|
||||
import { useEffect, useRef } from "react";
|
||||
|
||||
export function useListenToTauriEvent<T>(event: EventName, fn: EventCallback<T>) {
|
||||
const handlerRef = useRef(fn);
|
||||
useEffect(() => {
|
||||
handlerRef.current = fn;
|
||||
}, [fn]);
|
||||
|
||||
useEffect(() => {
|
||||
return listenToTauriEvent<T>(event, (p) => handlerRef.current(p));
|
||||
}, [event]);
|
||||
}
|
||||
|
||||
export function listenToTauriEvent<T>(event: EventName, fn: EventCallback<T>) {
|
||||
const unsubPromise = listen<T>(
|
||||
event,
|
||||
fn,
|
||||
// Listen to `emit_all()` events or events specific to the current window
|
||||
{ target: { label: getCurrentWebviewWindow().label, kind: "Window" } },
|
||||
);
|
||||
|
||||
return () => {
|
||||
unsubPromise.then((unsub) => unsub()).catch(console.error);
|
||||
};
|
||||
}
|
||||
@@ -1,4 +1,3 @@
|
||||
import { invoke } from "@tauri-apps/api/core";
|
||||
import type { GrpcConnection, GrpcEvent } from "@yaakapp-internal/models";
|
||||
import {
|
||||
grpcConnectionsAtom,
|
||||
@@ -11,6 +10,7 @@ import { useEffect, useMemo } from "react";
|
||||
import { fireAndForget } from "../lib/fireAndForget";
|
||||
import { atomWithKVStorage } from "../lib/atoms/atomWithKVStorage";
|
||||
import { activeRequestIdAtom } from "./useActiveRequestId";
|
||||
import { rpc } from "../lib/rpc";
|
||||
|
||||
const pinnedGrpcConnectionIdsAtom = atomWithKVStorage<Record<string, string | null>>(
|
||||
"pinned-grpc-connection-ids",
|
||||
@@ -71,7 +71,7 @@ export function useGrpcEvents(connectionId: string | null) {
|
||||
|
||||
// Fetch events from database, filtering out events from other connections and merging atomically
|
||||
fireAndForget(
|
||||
invoke<GrpcEvent[]>("models_grpc_events", { connectionId }).then((events) =>
|
||||
rpc<GrpcEvent[]>("models_grpc_events", { connectionId }).then((events) =>
|
||||
mergeModelsInStore("grpc_event", events, (e) => e.connectionId === connectionId),
|
||||
),
|
||||
);
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
import { invoke } from "@tauri-apps/api/core";
|
||||
import type { WebsocketConnection, WebsocketEvent } from "@yaakapp-internal/models";
|
||||
import {
|
||||
mergeModelsInStore,
|
||||
@@ -12,6 +11,7 @@ import { fireAndForget } from "../lib/fireAndForget";
|
||||
import { atomWithKVStorage } from "../lib/atoms/atomWithKVStorage";
|
||||
import { jotaiStore } from "../lib/jotai";
|
||||
import { activeRequestIdAtom } from "./useActiveRequestId";
|
||||
import { rpc } from "../lib/rpc";
|
||||
|
||||
const pinnedWebsocketConnectionIdAtom = atomWithKVStorage<Record<string, string | null>>(
|
||||
"pinned-websocket-connection-ids",
|
||||
@@ -58,7 +58,7 @@ export function useWebsocketEvents(connectionId: string | null) {
|
||||
|
||||
// Fetch events from database, filtering out events from other connections and merging atomically
|
||||
fireAndForget(
|
||||
invoke<WebsocketEvent[]>("models_websocket_events", { connectionId }).then((events) =>
|
||||
rpc<WebsocketEvent[]>("models_websocket_events", { connectionId }).then((events) =>
|
||||
mergeModelsInStore("websocket_event", events, (e) => e.connectionId === connectionId),
|
||||
),
|
||||
);
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
import { platform } from "@yaakapp-internal/platform";
|
||||
import { useEffect, useRef } from "react";
|
||||
|
||||
/** Subscribe to a backend event for the lifetime of the component. */
|
||||
export function usePlatformEvent<T>(event: string, fn: (payload: T) => void) {
|
||||
const handlerRef = useRef(fn);
|
||||
useEffect(() => {
|
||||
handlerRef.current = fn;
|
||||
}, [fn]);
|
||||
|
||||
useEffect(() => platform.listen<T>(event, (payload) => handlerRef.current(payload)), [event]);
|
||||
}
|
||||
@@ -4,7 +4,7 @@ import { pluginsAtom } from "@yaakapp-internal/models";
|
||||
import type { PluginMetadata } from "@yaakapp-internal/plugins";
|
||||
import { useAtomValue } from "jotai";
|
||||
import { queryClient } from "../lib/queryClient";
|
||||
import { invokeCmd } from "../lib/tauri";
|
||||
import { rpc } from "../lib/rpc";
|
||||
|
||||
function pluginInfoKey(id: string | null, plugin: Plugin | null) {
|
||||
return ["plugin_info", id ?? "n/a", plugin?.updatedAt ?? "n/a"];
|
||||
@@ -19,7 +19,7 @@ export function usePluginInfo(id: string | null) {
|
||||
placeholderData: (prev) => prev, // Keep previous data on refetch
|
||||
queryFn: () => {
|
||||
if (id == null) return null;
|
||||
return invokeCmd<PluginMetadata>("cmd_plugin_info", { id });
|
||||
return rpc<PluginMetadata>("cmd_plugin_info", { id });
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
@@ -3,7 +3,7 @@ import { changeModelStoreWorkspace, pluginsAtom } from "@yaakapp-internal/models
|
||||
import { useAtomValue } from "jotai";
|
||||
import { jotaiStore } from "../lib/jotai";
|
||||
import { minPromiseMillis } from "../lib/minPromiseMillis";
|
||||
import { invokeCmd } from "../lib/tauri";
|
||||
import { rpc } from "../lib/rpc";
|
||||
import { activeWorkspaceIdAtom } from "./useActiveWorkspace";
|
||||
import { useDebouncedValue } from "@yaakapp-internal/ui";
|
||||
import { invalidateAllPluginInfo } from "./usePluginInfo";
|
||||
@@ -26,7 +26,7 @@ export function useRefreshPlugins() {
|
||||
mutationFn: async () => {
|
||||
await minPromiseMillis(
|
||||
(async () => {
|
||||
await invokeCmd("cmd_reload_plugins");
|
||||
await rpc("cmd_reload_plugins");
|
||||
const workspaceId = jotaiStore.get(activeWorkspaceIdAtom);
|
||||
await changeModelStoreWorkspace(workspaceId); // Force refresh models
|
||||
invalidateAllPluginInfo();
|
||||
|
||||
@@ -2,7 +2,7 @@ import { useQuery } from "@tanstack/react-query";
|
||||
import type { RenderPurpose } from "@yaakapp-internal/plugins";
|
||||
import { useAtomValue } from "jotai";
|
||||
import { minPromiseMillis } from "../lib/minPromiseMillis";
|
||||
import { invokeCmd } from "../lib/tauri";
|
||||
import { rpc } from "../lib/rpc";
|
||||
import { useActiveEnvironment } from "./useActiveEnvironment";
|
||||
import { activeWorkspaceIdAtom } from "./useActiveWorkspace";
|
||||
|
||||
@@ -49,7 +49,7 @@ export async function renderTemplate({
|
||||
purpose: RenderPurpose;
|
||||
ignoreError?: boolean;
|
||||
}): Promise<string> {
|
||||
return invokeCmd("cmd_render_template", {
|
||||
return rpc("cmd_render_template", {
|
||||
template,
|
||||
workspaceId,
|
||||
environmentId,
|
||||
@@ -67,5 +67,5 @@ export async function decryptTemplate({
|
||||
workspaceId: string;
|
||||
environmentId: string | null;
|
||||
}): Promise<string> {
|
||||
return invokeCmd("cmd_decrypt_template", { template, workspaceId, environmentId });
|
||||
return rpc("cmd_decrypt_template", { template, workspaceId, environmentId });
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { getCurrentWebviewWindow } from "@tauri-apps/api/webviewWindow";
|
||||
import { platform } from "@yaakapp-internal/platform";
|
||||
import type { ModelPayload } from "@yaakapp-internal/models";
|
||||
import { atom, useAtomValue } from "jotai";
|
||||
import { generateId } from "../lib/generateId";
|
||||
@@ -6,26 +6,24 @@ import { jotaiStore } from "../lib/jotai";
|
||||
|
||||
const requestUpdateKeyAtom = atom<Record<string, string>>({});
|
||||
|
||||
getCurrentWebviewWindow()
|
||||
.listen<ModelPayload[]>("model_writes", ({ payload: payloads }) => {
|
||||
const changedIds: string[] = [];
|
||||
for (const payload of payloads) {
|
||||
if (payload.change.type !== "upsert") continue;
|
||||
platform.listen<ModelPayload[]>("model_writes", (payloads) => {
|
||||
const changedIds: string[] = [];
|
||||
for (const payload of payloads) {
|
||||
if (payload.change.type !== "upsert") continue;
|
||||
|
||||
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")
|
||||
) {
|
||||
changedIds.push(payload.model.id);
|
||||
}
|
||||
if (
|
||||
(payload.model.model === "http_request" ||
|
||||
payload.model.model === "grpc_request" ||
|
||||
payload.model.model === "websocket_request") &&
|
||||
((payload.updateSource.type === "window" &&
|
||||
payload.updateSource.label !== platform.window.label) ||
|
||||
payload.updateSource.type !== "window")
|
||||
) {
|
||||
changedIds.push(payload.model.id);
|
||||
}
|
||||
if (changedIds.length > 0) wasUpdatedExternally(changedIds);
|
||||
})
|
||||
.catch(console.error);
|
||||
}
|
||||
if (changedIds.length > 0) wasUpdatedExternally(changedIds);
|
||||
});
|
||||
|
||||
export function wasUpdatedExternally(changedRequestIds: string | string[]) {
|
||||
const ids = Array.isArray(changedRequestIds) ? changedRequestIds : [changedRequestIds];
|
||||
|
||||
@@ -1,13 +1,13 @@
|
||||
import { save } from "@tauri-apps/plugin-dialog";
|
||||
import type { HttpResponse } from "@yaakapp-internal/models";
|
||||
import { getModel } from "@yaakapp-internal/models";
|
||||
import mime from "mime";
|
||||
import slugify from "slugify";
|
||||
import { InlineCode } from "@yaakapp-internal/ui";
|
||||
import { getContentTypeFromHeaders } from "../lib/model_util";
|
||||
import { invokeCmd } from "../lib/tauri";
|
||||
import { rpc } from "../lib/rpc";
|
||||
import { showToast } from "../lib/toast";
|
||||
import { useFastMutation } from "./useFastMutation";
|
||||
import { platform } from "@yaakapp-internal/platform";
|
||||
|
||||
export function useSaveResponse(response: HttpResponse | null) {
|
||||
return useFastMutation({
|
||||
@@ -21,11 +21,11 @@ export function useSaveResponse(response: HttpResponse | null) {
|
||||
const contentType = getContentTypeFromHeaders(response.headers) ?? "unknown";
|
||||
const ext = mime.getExtension(contentType);
|
||||
const slug = slugify(request.name || "response", { lower: true });
|
||||
const filepath = await save({
|
||||
const filepath = await platform.dialog.save({
|
||||
defaultPath: ext ? `${slug}.${ext}` : slug,
|
||||
title: "Save Response",
|
||||
});
|
||||
await invokeCmd("cmd_save_response", { responseId: response.id, filepath });
|
||||
await rpc("cmd_save_response", { responseId: response.id, filepath });
|
||||
showToast({
|
||||
message: (
|
||||
<>
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import type { HttpResponse } from "@yaakapp-internal/models";
|
||||
import { flushAllModelWrites } from "@yaakapp-internal/models";
|
||||
import { invokeCmd } from "../lib/tauri";
|
||||
import { rpc } from "../lib/rpc";
|
||||
import { getActiveCookieJar } from "./useActiveCookieJar";
|
||||
import { getActiveEnvironment } from "./useActiveEnvironment";
|
||||
import { createFastMutation, useFastMutation } from "./useFastMutation";
|
||||
@@ -12,7 +12,7 @@ async function sendAnyHttpRequestById(id: string | null): Promise<HttpResponse |
|
||||
|
||||
await flushAllModelWrites();
|
||||
|
||||
return invokeCmd("cmd_send_http_request", {
|
||||
return rpc("cmd_send_http_request", {
|
||||
requestId: id,
|
||||
environmentId: getActiveEnvironment()?.id,
|
||||
cookieJarId: getActiveCookieJar()?.id,
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
import { type } from "@tauri-apps/plugin-os";
|
||||
import { platform } from "@yaakapp-internal/platform";
|
||||
import { useIsFullscreen } from "@yaakapp-internal/ui";
|
||||
|
||||
export function useStoplightsVisible() {
|
||||
const fullscreen = useIsFullscreen();
|
||||
const stoplightsVisible = type() === "macos" && !fullscreen;
|
||||
const stoplightsVisible = platform.osType() === "macos" && !fullscreen;
|
||||
return stoplightsVisible;
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { getCurrentWebviewWindow } from "@tauri-apps/api/webviewWindow";
|
||||
import { platform } from "@yaakapp-internal/platform";
|
||||
import { settingsAtom } from "@yaakapp-internal/models";
|
||||
import { useAtomValue } from "jotai";
|
||||
import { useEffect } from "react";
|
||||
@@ -11,7 +11,7 @@ export function useSyncFontSizeSetting() {
|
||||
}
|
||||
|
||||
const { interfaceScale, editorFontSize } = settings;
|
||||
getCurrentWebviewWindow().setZoom(interfaceScale).catch(console.error);
|
||||
platform.window.setZoom(interfaceScale).catch(console.error);
|
||||
document.documentElement.style.setProperty("--editor-font-size", `${editorFontSize}px`);
|
||||
}, [settings]);
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { useHotKey } from "./useHotKey";
|
||||
import { useListenToTauriEvent } from "./useListenToTauriEvent";
|
||||
import { usePlatformEvent } from "./usePlatformEvent";
|
||||
import { useZoom } from "./useZoom";
|
||||
|
||||
export function useSyncZoomSetting() {
|
||||
@@ -8,9 +8,9 @@ export function useSyncZoomSetting() {
|
||||
// shortcuts for Windows/Linux
|
||||
const zoom = useZoom();
|
||||
useHotKey("app.zoom_in", zoom.zoomIn);
|
||||
useListenToTauriEvent("zoom_in", zoom.zoomIn);
|
||||
usePlatformEvent("zoom_in", zoom.zoomIn);
|
||||
useHotKey("app.zoom_out", zoom.zoomOut);
|
||||
useListenToTauriEvent("zoom_out", zoom.zoomOut);
|
||||
usePlatformEvent("zoom_out", zoom.zoomOut);
|
||||
useHotKey("app.zoom_reset", zoom.zoomReset);
|
||||
useListenToTauriEvent("zoom_reset", zoom.zoomReset);
|
||||
usePlatformEvent("zoom_reset", zoom.zoomReset);
|
||||
}
|
||||
|
||||
@@ -12,7 +12,7 @@ import {
|
||||
import type { GetTemplateFunctionConfigResponse, JsonPrimitive } from "@yaakapp-internal/plugins";
|
||||
import { useAtomValue } from "jotai";
|
||||
import { md5 } from "js-md5";
|
||||
import { invokeCmd } from "../lib/tauri";
|
||||
import { rpc } from "../lib/rpc";
|
||||
import { activeEnvironmentIdAtom } from "./useActiveEnvironment";
|
||||
import { activeWorkspaceIdAtom } from "./useActiveWorkspace";
|
||||
|
||||
@@ -63,7 +63,7 @@ export async function getTemplateFunctionConfig(
|
||||
model: HttpRequest | GrpcRequest | WebsocketRequest | Folder | Workspace,
|
||||
environmentId: string | undefined,
|
||||
) {
|
||||
const config = await invokeCmd<GetTemplateFunctionConfigResponse>(
|
||||
const config = await rpc<GetTemplateFunctionConfigResponse>(
|
||||
"cmd_template_function_config",
|
||||
{
|
||||
functionName,
|
||||
|
||||
@@ -6,7 +6,7 @@ import type {
|
||||
import { atom, useAtomValue, useSetAtom } from "jotai";
|
||||
import { useMemo, useState } from "react";
|
||||
import type { TwigCompletionOption } from "../components/core/Editor/twig/completion";
|
||||
import { invokeCmd } from "../lib/tauri";
|
||||
import { rpc } from "../lib/rpc";
|
||||
import { usePluginsKey } from "./usePlugins";
|
||||
|
||||
const templateFunctionsAtom = atom<TemplateFunction[]>([]);
|
||||
@@ -49,7 +49,7 @@ export function useSubscribeTemplateFunctions() {
|
||||
refetchInterval: numFns > 0 ? Number.POSITIVE_INFINITY : 1000,
|
||||
refetchOnMount: true,
|
||||
queryFn: async () => {
|
||||
const result = await invokeCmd<GetTemplateFunctionSummaryResponse[]>(
|
||||
const result = await rpc<GetTemplateFunctionSummaryResponse[]>(
|
||||
"cmd_template_function_summaries",
|
||||
);
|
||||
setNumFns(result.length);
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { useQuery } from "@tanstack/react-query";
|
||||
import type { Tokens } from "@yaakapp-internal/templates";
|
||||
import { invokeCmd } from "../lib/tauri";
|
||||
import { rpc } from "../lib/rpc";
|
||||
|
||||
export function useTemplateTokensToString(tokens: Tokens) {
|
||||
return useQuery<string>({
|
||||
@@ -11,5 +11,5 @@ export function useTemplateTokensToString(tokens: Tokens) {
|
||||
}
|
||||
|
||||
export async function templateTokensToString(tokens: Tokens): Promise<string> {
|
||||
return invokeCmd("cmd_template_tokens_to_string", { tokens });
|
||||
return rpc("cmd_template_tokens_to_string", { tokens });
|
||||
}
|
||||
|
||||
@@ -6,7 +6,7 @@ import type {
|
||||
WebsocketRequestAction,
|
||||
} from "@yaakapp-internal/plugins";
|
||||
import { useMemo } from "react";
|
||||
import { invokeCmd } from "../lib/tauri";
|
||||
import { rpc } from "../lib/rpc";
|
||||
import { usePluginsKey } from "./usePlugins";
|
||||
|
||||
export type CallableWebSocketRequestAction = Pick<WebsocketRequestAction, "label" | "icon"> & {
|
||||
@@ -30,7 +30,7 @@ export function useWebsocketRequestActions() {
|
||||
}
|
||||
|
||||
export async function getWebsocketRequestActions() {
|
||||
const responses = await invokeCmd<GetWebsocketRequestActionsResponse[]>(
|
||||
const responses = await rpc<GetWebsocketRequestActionsResponse[]>(
|
||||
"cmd_websocket_request_actions",
|
||||
);
|
||||
const actions = responses.flatMap((r) =>
|
||||
@@ -43,7 +43,7 @@ export async function getWebsocketRequestActions() {
|
||||
pluginRefId: r.pluginRefId,
|
||||
args: { websocketRequest },
|
||||
};
|
||||
await invokeCmd("cmd_call_websocket_request_action", { req: payload });
|
||||
await rpc("cmd_call_websocket_request_action", { req: payload });
|
||||
},
|
||||
})),
|
||||
);
|
||||
|
||||
@@ -1,18 +1,11 @@
|
||||
import { getCurrentWebviewWindow } from "@tauri-apps/api/webviewWindow";
|
||||
import { platform } from "@yaakapp-internal/platform";
|
||||
import { useEffect, useState } from "react";
|
||||
import { fireAndForget } from "../lib/fireAndForget";
|
||||
|
||||
export function useWindowFocus() {
|
||||
const [visible, setVisible] = useState(true);
|
||||
|
||||
useEffect(() => {
|
||||
const unlisten = getCurrentWebviewWindow().onFocusChanged((e) => {
|
||||
setVisible(e.payload);
|
||||
});
|
||||
|
||||
return () => {
|
||||
fireAndForget(unlisten.then((fn) => fn()));
|
||||
};
|
||||
return platform.window.onFocusChanged(setVisible);
|
||||
}, []);
|
||||
|
||||
return visible;
|
||||
|
||||
@@ -6,7 +6,7 @@ import type {
|
||||
WorkspaceAction,
|
||||
} from "@yaakapp-internal/plugins";
|
||||
import { useMemo } from "react";
|
||||
import { invokeCmd } from "../lib/tauri";
|
||||
import { rpc } from "../lib/rpc";
|
||||
import { usePluginsKey } from "./usePlugins";
|
||||
|
||||
export type CallableWorkspaceAction = Pick<WorkspaceAction, "label" | "icon"> & {
|
||||
@@ -30,7 +30,7 @@ export function useWorkspaceActions() {
|
||||
}
|
||||
|
||||
export async function getWorkspaceActions() {
|
||||
const responses = await invokeCmd<GetWorkspaceActionsResponse[]>("cmd_workspace_actions");
|
||||
const responses = await rpc<GetWorkspaceActionsResponse[]>("cmd_workspace_actions");
|
||||
const actions = responses.flatMap((r) =>
|
||||
r.actions.map((a, i) => ({
|
||||
label: a.label,
|
||||
@@ -41,7 +41,7 @@ export async function getWorkspaceActions() {
|
||||
pluginRefId: r.pluginRefId,
|
||||
args: { workspace },
|
||||
};
|
||||
await invokeCmd("cmd_call_workspace_action", { req: payload });
|
||||
await rpc("cmd_call_workspace_action", { req: payload });
|
||||
},
|
||||
})),
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user