Skip to content

Commit

Permalink
refactor(vue): add use id composable (#80)
Browse files Browse the repository at this point in the history
  • Loading branch information
nmathew98 committed Mar 2, 2024
1 parent 91ac076 commit 4af9f64
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 4 deletions.
3 changes: 3 additions & 0 deletions packages/vue/src/use-id/use-id.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const useId = () => {
return Math.random().toString(36).substring(2);
};
3 changes: 2 additions & 1 deletion packages/vue/src/use-qwery/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import type {
UseQweryOptions,
UseQweryReturnWithSuspense,
} from "./types";
import { useId } from "../use-id/use-id";

export const useQwery = <
I extends InitialValue,
Expand Down Expand Up @@ -36,7 +37,7 @@ export const useQwery = <
dispatch: null,
});

const id = Math.random().toString(36).substring(2);
const id = useId();
const abortController = new AbortController();

const updateCRDT = (value: CRDT<any>) => {
Expand Down
10 changes: 7 additions & 3 deletions packages/vue/src/use-remember-scroll/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ import {
ExecutionEnvironment,
useExecutionEnvironment,
} from "../use-execution-environment";
import { useId } from "../use-id/use-id";

export const useRememberScroll = () => {
const id = useId();
const { executionEnvironment } = useExecutionEnvironment();

if (executionEnvironment !== ExecutionEnvironment.Browser) {
Expand All @@ -15,7 +17,7 @@ export const useRememberScroll = () => {
const currentPath = window.location.pathname;

sessionStorage.setItem(
currentPath,
`${id}.${currentPath}`,
JSON.stringify({
scrollX: window.scrollX,
scrollY: window.scrollY,
Expand All @@ -26,8 +28,10 @@ export const useRememberScroll = () => {
onMounted(() => {
const currentPath = window.location.pathname;

const savedScroll = sessionStorage.getItem(currentPath)
? JSON.parse(sessionStorage.getItem(currentPath) as string)
const savedScroll = sessionStorage.getItem(`${id}.${currentPath}`)
? JSON.parse(
sessionStorage.getItem(`${id}.${currentPath}`) as string,
)
: null;

if (!savedScroll) {
Expand Down

0 comments on commit 4af9f64

Please sign in to comment.