mirror of
https://github.com/mountain-loop/yaak.git
synced 2026-04-21 00:01:22 +02:00
Better listening for path changes
This commit is contained in:
@@ -13,15 +13,23 @@ import { modelsEq } from '../lib/models';
|
|||||||
import { useRecentRequests } from '../hooks/useRecentRequests';
|
import { useRecentRequests } from '../hooks/useRecentRequests';
|
||||||
import { useRecentWorkspaces } from '../hooks/useRecentWorkspaces';
|
import { useRecentWorkspaces } from '../hooks/useRecentWorkspaces';
|
||||||
import { useRecentEnvironments } from '../hooks/useRecentEnvironments';
|
import { useRecentEnvironments } from '../hooks/useRecentEnvironments';
|
||||||
|
import { useLocation } from 'react-router-dom';
|
||||||
|
import { useEffect } from 'react';
|
||||||
|
import { setPathname } from '../lib/persistPathname';
|
||||||
|
|
||||||
export function GlobalHooks() {
|
export function GlobalHooks() {
|
||||||
useRecentWorkspaces();
|
useRecentWorkspaces();
|
||||||
useRecentEnvironments();
|
useRecentEnvironments();
|
||||||
useRecentRequests();
|
useRecentRequests();
|
||||||
|
|
||||||
const queryClient = useQueryClient();
|
const queryClient = useQueryClient();
|
||||||
const { wasUpdatedExternally } = useRequestUpdateKey(null);
|
const { wasUpdatedExternally } = useRequestUpdateKey(null);
|
||||||
|
|
||||||
|
// Listen for location changes and update the pathname
|
||||||
|
const location = useLocation();
|
||||||
|
useEffect(() => {
|
||||||
|
setPathname(location.pathname);
|
||||||
|
}, [location.pathname]);
|
||||||
|
|
||||||
useListenToTauriEvent<Model>('created_model', ({ payload, windowLabel }) => {
|
useListenToTauriEvent<Model>('created_model', ({ payload, windowLabel }) => {
|
||||||
if (shouldIgnoreEvent(payload, windowLabel)) return;
|
if (shouldIgnoreEvent(payload, windowLabel)) return;
|
||||||
|
|
||||||
@@ -71,8 +79,9 @@ export function GlobalHooks() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (!shouldIgnoreModel(payload)) {
|
if (!shouldIgnoreModel(payload)) {
|
||||||
queryClient.setQueryData<Model[]>(queryKey, (values) =>
|
queryClient.setQueryData<Model[]>(
|
||||||
values?.map((v) => (modelsEq(v, payload) ? payload : v)),
|
queryKey,
|
||||||
|
(values) => values?.map((v) => (modelsEq(v, payload) ? payload : v)),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -1,21 +0,0 @@
|
|||||||
import { appWindow } from '@tauri-apps/api/window';
|
|
||||||
import { NAMESPACE_NO_SYNC, getKeyValue, setKeyValue } from './keyValueStore';
|
|
||||||
|
|
||||||
const key = ['window_pathname', appWindow.label];
|
|
||||||
const namespace = NAMESPACE_NO_SYNC;
|
|
||||||
const fallback = undefined;
|
|
||||||
|
|
||||||
export async function initPathnamePersistance() {
|
|
||||||
if (window.location.pathname === '/') {
|
|
||||||
const pathname = await getKeyValue<string | undefined>({ key, namespace, fallback });
|
|
||||||
if (pathname != null) {
|
|
||||||
window.location.replace(pathname);
|
|
||||||
}
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
window.addEventListener('pushstate', async () => {
|
|
||||||
const { pathname: value } = window.location;
|
|
||||||
await setKeyValue<string | undefined>({ key, namespace, value });
|
|
||||||
});
|
|
||||||
}
|
|
||||||
@@ -36,7 +36,7 @@ export async function getKeyValue<T>({
|
|||||||
return extractKeyValueOrFallback(kv, fallback);
|
return extractKeyValueOrFallback(kv, fallback);
|
||||||
}
|
}
|
||||||
|
|
||||||
export function extractKeyValue<T>(kv: KeyValue | null): T | undefined {
|
function extractKeyValue<T>(kv: KeyValue | null): T | undefined {
|
||||||
if (kv === null) return undefined;
|
if (kv === null) return undefined;
|
||||||
try {
|
try {
|
||||||
return JSON.parse(kv.value) as T;
|
return JSON.parse(kv.value) as T;
|
||||||
@@ -45,7 +45,7 @@ export function extractKeyValue<T>(kv: KeyValue | null): T | undefined {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export function extractKeyValueOrFallback<T>(kv: KeyValue | null, fallback: T): T {
|
function extractKeyValueOrFallback<T>(kv: KeyValue | null, fallback: T): T {
|
||||||
const v = extractKeyValue<T>(kv);
|
const v = extractKeyValue<T>(kv);
|
||||||
if (v === undefined) return fallback;
|
if (v === undefined) return fallback;
|
||||||
return v;
|
return v;
|
||||||
|
|||||||
21
src-web/lib/persistPathname.ts
Normal file
21
src-web/lib/persistPathname.ts
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
import { appWindow } from '@tauri-apps/api/window';
|
||||||
|
import { NAMESPACE_NO_SYNC, getKeyValue, setKeyValue } from './keyValueStore';
|
||||||
|
|
||||||
|
const key = ['window_pathname', appWindow.label];
|
||||||
|
const namespace = NAMESPACE_NO_SYNC;
|
||||||
|
const fallback = undefined;
|
||||||
|
|
||||||
|
export async function setPathname(value: string) {
|
||||||
|
await setKeyValue<string | undefined>({ key, namespace, value });
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function maybeRestorePathname() {
|
||||||
|
if (window.location.pathname !== '/') {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const pathname = await getKeyValue<string | undefined>({ key, namespace, fallback });
|
||||||
|
if (pathname != null) {
|
||||||
|
window.location.replace(pathname);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -4,9 +4,9 @@ import { App } from './components/App';
|
|||||||
import { getKeyValue } from './lib/keyValueStore';
|
import { getKeyValue } from './lib/keyValueStore';
|
||||||
import { getPreferredAppearance, setAppearance } from './lib/theme/window';
|
import { getPreferredAppearance, setAppearance } from './lib/theme/window';
|
||||||
import './main.css';
|
import './main.css';
|
||||||
import { initPathnamePersistance } from './lib/initPathnamePersistance';
|
import { maybeRestorePathname } from './lib/persistPathname';
|
||||||
|
|
||||||
await initPathnamePersistance();
|
await maybeRestorePathname();
|
||||||
|
|
||||||
setAppearance(
|
setAppearance(
|
||||||
await getKeyValue({
|
await getKeyValue({
|
||||||
|
|||||||
Reference in New Issue
Block a user