Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 17 additions & 17 deletions src/options/hooks/useHash.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
import { useCallback, useEffect, useState } from 'react'

import { useRef } from "react";
import { useCallback, useEffect, useState } from 'react';
export const useHash = () => {
const [hashId, setHashId] = useState('')
const [hashNewUrl, setHashNewUrl] = useState<string | undefined>()

const hashId = useRef('');
const hashNewUrl = useRef<string | undefined>();
const handler = useCallback(() => {
const h = window.location.hash.substring(1).split('=')
setHashId(h[0])
setHashNewUrl(h.slice(1).join('=')) // in case the url contains a colon i need to join the rest of the array
}, [])

const h = window.location.hash.substring(1).split('=');
hashId.current = h[0];
hashNewUrl.current = h.slice(1).join('='); // in case the url contains a colon i need to join the rest of the array
}, []);
useEffect(() => {
handler() // run the handler on mount to set the initial state

window.addEventListener('hashchange', handler)
return () => window.removeEventListener('hashchange', handler)
}, [])
handler(); // run the handler on mount to set the initial state

return { hashId, hashNewUrl }
}
window.addEventListener('hashchange', handler);
return () => window.removeEventListener('hashchange', handler);
}, []);
return {
hashId: hashId.current,
hashNewUrl: hashNewUrl.current
};
};
20 changes: 9 additions & 11 deletions src/options/hooks/useProtos.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
import { useEffect, useState } from 'react'
import { CSSProto } from '../../types'

import { useRef } from "react";
import { useEffect, useState } from 'react';
import { CSSProto } from '../../types';
export const useProtos = () => {
// detect chrome.storage.local change callback

const [protos, setProtos] = useState<CSSProto[]>([])

const protos = useRef<CSSProto[]>([]);
useEffect(() => {
// get protos from storage
chrome.storage.onChanged.addListener((changes, areaName) => {
if (areaName == 'local' && changes['css-protos']) {
setProtos(changes.protos.newValue)
protos.current = changes.protos.newValue;
}
})
}, [])

return protos
}
});
}, []);
return protos.current;
};
Loading