mirror of
https://github.com/mountain-loop/yaak.git
synced 2026-02-18 21:57:44 +01:00
29 lines
940 B
TypeScript
29 lines
940 B
TypeScript
import { useNavigate, useSearch } from '@tanstack/react-router';
|
|
import { useCallback } from 'react';
|
|
import { useEnvironments } from './useEnvironments';
|
|
|
|
export function useActiveEnvironment() {
|
|
const [id, setId] = useActiveEnvironmentId();
|
|
const { subEnvironments } = useEnvironments();
|
|
const environment = subEnvironments.find((w) => w.id === id) ?? null;
|
|
return [environment, setId] as const;
|
|
}
|
|
|
|
export const QUERY_ENVIRONMENT_ID = 'environment_id';
|
|
|
|
function useActiveEnvironmentId() {
|
|
// NOTE: This query param is accessed from Rust side, so do not change
|
|
const { environment_id: id} = useSearch({ strict: false });
|
|
const navigate = useNavigate({ from: '/workspaces/$workspaceId' });
|
|
|
|
const setId = useCallback(
|
|
(environmentId: string | null) =>
|
|
navigate({
|
|
search: (prev) => ({ ...prev, environment_id: environmentId ?? undefined }),
|
|
}),
|
|
[navigate],
|
|
);
|
|
|
|
return [id, setId] as const;
|
|
}
|