Skip to content

Commit

Permalink
Merge branch 'main' into alert-refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
andnorda committed Jul 3, 2024
2 parents 6277c0b + bd2d031 commit f4f9cee
Show file tree
Hide file tree
Showing 34 changed files with 609 additions and 405 deletions.
3 changes: 0 additions & 3 deletions .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -1,4 +1 @@
#!/usr/bin/env sh
. "$(dirname -- "$0")/_/husky.sh"

bunx lint-staged --config package.json
Binary file modified bun.lockb
Binary file not shown.
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,10 @@
"dev": "concurrently \"bun run --cwd packages/client dev\" \"bun run --cwd packages/server dev\"",
"build": "bun run --cwd packages/client build && bun run --cwd packages/server build && bun run copy-assets",
"copy-assets": "mkdir -p packages/server/public && cp -r packages/client/dist/assets packages/server/public/ && cp -r packages/client/dist/~partytown packages/server/public/",
"prepare": "husky install",
"prepare": "husky",
"benchmarking": "./benchmarking/run",
"partytown": "partytown copylib packages/server/public/~partytown",
"clean": "rm -rf node_modules packages/**/node_modules bun.lockb packages/**/bun.lockb",
"postinstall": "husky install",
"playwright": "playwright test"
},
"keywords": [],
Expand Down
2 changes: 1 addition & 1 deletion packages/client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"rollup-plugin-minify-html-literals-v3": "1.3.3",
"vite-bundle-visualizer": "1.2.1",
"vite-tsconfig-paths": "4.2.0",
"vitest": "1.3.1",
"vitest": "1.6.0",
"msw": "2.3.0"
}
}
207 changes: 0 additions & 207 deletions packages/client/src/controllers/logout-warning.ts

This file was deleted.

43 changes: 16 additions & 27 deletions packages/client/src/csr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,39 +2,28 @@ import { CsrPayload } from "decorator-shared/types";
import { buildHtmlElement } from "./helpers/html-element-builder";

const findOrError = (id: string) => {
const el = document.getElementById(id);
const el = document.getElementById(`decorator-${id}`);

if (!el) {
throw new Error(
`Could not find element with id ${id}. See documentation on nav-dekoratoren on github.`,
);
throw new Error(`No elem:${id}. See github.com/navikt/decorator-next`);
}

return el as HTMLDivElement;
return el;
};

async function hydrate() {
const [header, footer, envEl] = [
"decorator-header",
"decorator-footer",
"decorator-env",
].map(findOrError);

const envUrl = envEl.dataset.src as string;

const response = await fetch(envUrl);
const elements = (await response.json()) as CsrPayload;

header.outerHTML = elements.header;
footer.outerHTML = elements.footer;

// Set decorator state before script evaulation
window.__DECORATOR_DATA__ = elements.data;

elements.scripts
.map(buildHtmlElement)
.forEach((script) => document.body.appendChild(script));
}
const hydrate = () =>
fetch(findOrError("env").dataset.src!)
.then((res) => res.json())
.then((elements: CsrPayload) => {
(["header", "footer"] as const).forEach(
(key) => (findOrError(key).outerHTML = elements[key]),
);
window.__DECORATOR_DATA__ = elements.data;

elements.scripts
.map(buildHtmlElement)
.forEach((script) => document.body.appendChild(script));
});

if (document.readyState === "loading") {
document.addEventListener("DOMContentLoaded", hydrate);
Expand Down
6 changes: 3 additions & 3 deletions packages/client/src/faro.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
* This adds information about decorator parameters to the Grafana faro. To aid with debugging.
* */
export function addFaroMetaData() {
if (!window.faro) return;
if (!window.faro) {
return;
}

window.faro.api.setSession({
attributes: {
decorator_env: JSON.stringify(window.__DECORATOR_DATA__.env),
decorator_params: JSON.stringify(window.__DECORATOR_DATA__.params),
},
});

console.log("Faro: Adding metadata");
}
43 changes: 43 additions & 0 deletions packages/client/src/helpers/auth.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { describe, expect, it } from "vitest";
import { SessionData, transformSessionToAuth } from "./auth";

describe("Auth helpers", () => {
describe("transformSessionToAuth", () => {
it("correctly uses expire_in_seconds when transforming to local time", () => {
const mockCurrentISODate = "2021-10-10T10:10:00.000Z";

// Note: Discrepancy between ISO dates and ends_in_seconds and expire_in_seconds
// This is intentional to test that the function does infact use the seconds and not the time stamps.
const mockSessionData: SessionData = {
session: {
created_at: "2021-10-10T10:00:00.000Z",
ends_at: "2021-10-10T16:00:00.000Z",
timeout_at: "2021-10-10T16:00:00.000Z",
ends_in_seconds: 3600,
active: true,
timeout_in_seconds: 3600,
},
tokens: {
expire_at: "2021-10-10T11:05:00.000Z",
refreshed_at: "2021-10-10T12:05:00.000Z",
expire_in_seconds: 1800,
next_auto_refresh_in_seconds: 1800,
refresh_cooldown: false,
refresh_cooldown_seconds: 0,
},
};

vi.useFakeTimers();
vi.setSystemTime(mockCurrentISODate);

const authData = transformSessionToAuth(mockSessionData);

expect(authData.sessionExpireAtLocal).toBe(
"2021-10-10T11:10:00.000Z",
);
expect(authData.tokenExpireAtLocal).toBe(
"2021-10-10T10:40:00.000Z",
);
});
});
});
Loading

0 comments on commit f4f9cee

Please sign in to comment.