mirror of
https://github.com/paperclipai/paperclip.git
synced 2026-10-25 09:41:04 +01:00
Preserve scroll when opening file viewer
Co-Authored-By: Paperclip <noreply@paperclip.ing>
This commit is contained in:
1 parent
fec3c19a0e
commit
bce60bce8e
2 files changed
+15
-2
No files matched your search
@@ -2,10 +2,18 @@
|
||||
|
||||
import { describe, expect, it } from "vitest";
|
||||
import {
|
||||
FILE_VIEWER_NAVIGATE_OPTIONS,
|
||||
readFileViewerStateFromSearch,
|
||||
writeFileViewerStateToSearch,
|
||||
} from "./FileViewerContext";
|
||||
|
||||
describe("FILE_VIEWER_NAVIGATE_OPTIONS", () => {
|
||||
it("preserves page scroll when the viewer updates URL search params", () => {
|
||||
expect(FILE_VIEWER_NAVIGATE_OPTIONS.preventScrollReset).toBe(true);
|
||||
expect(FILE_VIEWER_NAVIGATE_OPTIONS.replace).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe("readFileViewerStateFromSearch", () => {
|
||||
it("returns null when no file param is present", () => {
|
||||
expect(readFileViewerStateFromSearch("")).toBeNull();
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { createContext, useContext, useCallback, useMemo, type ReactNode } from "react";
|
||||
import { useLocation, useNavigate } from "@/lib/router";
|
||||
import { useLocation, useNavigate, type NavigateOptions } from "@/lib/router";
|
||||
import type { WorkspaceFileSelector } from "@paperclipai/shared";
|
||||
import type { ParsedWorkspaceFileRef } from "@/lib/workspace-file-parser";
|
||||
|
||||
@@ -31,6 +31,11 @@ export interface FileViewerContextValue {
|
||||
|
||||
const FileViewerContext = createContext<FileViewerContextValue | null>(null);
|
||||
|
||||
export const FILE_VIEWER_NAVIGATE_OPTIONS = {
|
||||
replace: false,
|
||||
preventScrollReset: true,
|
||||
} satisfies NavigateOptions;
|
||||
|
||||
export function readFileViewerStateFromSearch(search: string): FileViewerUrlState | null {
|
||||
const params = new URLSearchParams(search);
|
||||
const path = params.get("file");
|
||||
@@ -100,7 +105,7 @@ export function FileViewerProvider({ issueId, children }: FileViewerProviderProp
|
||||
(nextSearch: string) => {
|
||||
navigate(
|
||||
{ pathname: location.pathname, hash: location.hash, search: nextSearch },
|
||||
{ state: location.state, replace: false },
|
||||
{ ...FILE_VIEWER_NAVIGATE_OPTIONS, state: location.state },
|
||||
);
|
||||
},
|
||||
[location.hash, location.pathname, location.state, navigate],
|
||||
|
||||
Reference in new issue
Block a user