Skip to content

Commit

Permalink
fix: correctly handle unicode
Browse files Browse the repository at this point in the history
fixes #17
  • Loading branch information
theSuess committed Dec 18, 2024
1 parent 78d37f2 commit dac8195
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
14 changes: 10 additions & 4 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,16 @@ function App() {

const converterEnabled = !!process.env.REACT_APP_CONVERT_ENDPOINT;

const shareLink = useMemo(
() => `${window.location}?c=${btoa(model)}`,
[model],
);
const bytesToBase64 = (bytes: Uint8Array) => {
const binString = Array.from(bytes, (byte) =>
String.fromCodePoint(byte),
).join("");
return btoa(binString);
};

const shareLink = useMemo(() => {
return `${window.location}?c=${encodeURIComponent(bytesToBase64(new TextEncoder().encode(model)))}`;
}, [model]);

const copyLink = () => {
navigator.clipboard.writeText(shareLink);
Expand Down
7 changes: 6 additions & 1 deletion src/state.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,12 @@ export const ModelProvider = ({ children }: React.PropsWithChildren) => {
const urlModel = new URLSearchParams(document.location.search).get("c");
const localStorageModel = localStorage.getItem("config.alloy");
let initialModel = "";
if (urlModel) initialModel = atob(urlModel);
const base64ToBytes = (data: string) => {
const binString = atob(decodeURIComponent(data));
return Uint8Array.from(binString, (m) => m.codePointAt(0) ?? 0);
};
if (urlModel)
initialModel = new TextDecoder().decode(base64ToBytes(urlModel));
else if (localStorageModel) initialModel = localStorageModel;
if (initialModel === "") {
initialModel = `// Welcome to the Grafana Alloy Configurator!
Expand Down

0 comments on commit dac8195

Please sign in to comment.