Skip to content

Commit

Permalink
use sha1 to generate colors constantly
Browse files Browse the repository at this point in the history
  • Loading branch information
rouk1 committed Jul 26, 2024
1 parent ca22fdf commit bac0926
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 7 deletions.
19 changes: 12 additions & 7 deletions frontend/src/components/FileTreeItem.vue
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
<script setup lang="ts">
import { computed } from "vue";
import { computed, onBeforeMount, ref } from "vue";
import { useRouter } from "vue-router";
import { type FileTreeNode } from "./FileTree.vue";
import { type FileTreeNode } from "@/components/FileTree.vue";
import { remap, sha1 } from "@/services/utils";
const router = useRouter();
const props = defineProps<FileTreeNode>();
const randomColor = ref("");
const hasChildren = computed(() => props.children?.length);
const label = computed(() => {
const segment = props.uri.split("/");
return segment[segment.length - 1];
});
const randomColor = computed(() => {
const hue = Math.random() * 360;
return `background-color: hsl(${hue}deg 97 75);`;
});
function onClick() {
router.push({
name: "dashboard",
Expand All @@ -25,6 +23,13 @@ function onClick() {
},
});
}
onBeforeMount(async () => {
const hash = await sha1(props.uri);
const hashAsNumber = Number(`0x${hash.slice(0, 7)}`);
const hue = remap(hashAsNumber, 0x0000000, 0xffff, 0, 360);
randomColor.value = `background-color: hsl(${hue}deg 97 75);`;
});
</script>

<template>
Expand Down
17 changes: 17 additions & 0 deletions frontend/src/services/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,20 @@ export function sleep(delay: number): Promise<void> {
export function isString(s: any) {
return typeof s === "string" || s instanceof String;
}

export function remap(
x: number,
fromMin: number,
fromMax: number,
toMin: number,
toMax: number
): number {
return ((x - fromMin) * (toMax - toMin)) / (fromMax - fromMin) + toMin;
}

export async function sha1(message: string) {
const msgUint8 = new TextEncoder().encode(message);
const hashBuffer = await crypto.subtle.digest("SHA-1", msgUint8);

Check failure on line 21 in frontend/src/services/utils.ts

View workflow job for this annotation

GitHub Actions / build

Unhandled error

TypeError: Cannot read properties of undefined (reading 'digest') ❯ Module.sha1 src/services/utils.ts:21:42 ❯ src/components/FileTreeItem.vue:28:22 ❯ node_modules/@vue/runtime-core/dist/runtime-core.cjs.js:1807:40 ❯ callWithErrorHandling node_modules/@vue/runtime-core/dist/runtime-core.cjs.js:196:19 ❯ callWithAsyncErrorHandling node_modules/@vue/runtime-core/dist/runtime-core.cjs.js:203:17 ❯ Array.hook.__weh.hook.__weh node_modules/@vue/runtime-core/dist/runtime-core.cjs.js:1787:19 ❯ Object.invokeArrayFns node_modules/@vue/shared/dist/shared.cjs.js:85:11 ❯ ReactiveEffect.componentUpdateFn [as fn] node_modules/@vue/runtime-core/dist/runtime-core.cjs.js:5036:18 ❯ ReactiveEffect.run node_modules/@vue/reactivity/dist/reactivity.cjs.js:181:19 ❯ instance.update node_modules/@vue/runtime-core/dist/runtime-core.cjs.js:5211:16 This error originated in "tests/components/FileTree.spec.ts" test file. It doesn't mean the error was thrown inside the file itself, but while it was running.

Check failure on line 21 in frontend/src/services/utils.ts

View workflow job for this annotation

GitHub Actions / build

Unhandled error

TypeError: Cannot read properties of undefined (reading 'digest') ❯ Module.sha1 src/services/utils.ts:21:42 ❯ src/components/FileTreeItem.vue:28:22 ❯ node_modules/@vue/runtime-core/dist/runtime-core.cjs.js:1807:40 ❯ callWithErrorHandling node_modules/@vue/runtime-core/dist/runtime-core.cjs.js:196:19 ❯ callWithAsyncErrorHandling node_modules/@vue/runtime-core/dist/runtime-core.cjs.js:203:17 ❯ Array.hook.__weh.hook.__weh node_modules/@vue/runtime-core/dist/runtime-core.cjs.js:1787:19 ❯ Object.invokeArrayFns node_modules/@vue/shared/dist/shared.cjs.js:85:11 ❯ ReactiveEffect.componentUpdateFn [as fn] node_modules/@vue/runtime-core/dist/runtime-core.cjs.js:5036:18 ❯ ReactiveEffect.run node_modules/@vue/reactivity/dist/reactivity.cjs.js:181:19 ❯ instance.update node_modules/@vue/runtime-core/dist/runtime-core.cjs.js:5211:16 This error originated in "tests/components/FileTree.spec.ts" test file. It doesn't mean the error was thrown inside the file itself, but while it was running.

Check failure on line 21 in frontend/src/services/utils.ts

View workflow job for this annotation

GitHub Actions / build

Unhandled error

TypeError: Cannot read properties of undefined (reading 'digest') ❯ Module.sha1 src/services/utils.ts:21:42 ❯ src/components/FileTreeItem.vue:28:22 ❯ node_modules/@vue/runtime-core/dist/runtime-core.cjs.js:1807:40 ❯ callWithErrorHandling node_modules/@vue/runtime-core/dist/runtime-core.cjs.js:196:19 ❯ callWithAsyncErrorHandling node_modules/@vue/runtime-core/dist/runtime-core.cjs.js:203:17 ❯ Array.hook.__weh.hook.__weh node_modules/@vue/runtime-core/dist/runtime-core.cjs.js:1787:19 ❯ Object.invokeArrayFns node_modules/@vue/shared/dist/shared.cjs.js:85:11 ❯ ReactiveEffect.componentUpdateFn [as fn] node_modules/@vue/runtime-core/dist/runtime-core.cjs.js:5036:18 ❯ ReactiveEffect.run node_modules/@vue/reactivity/dist/reactivity.cjs.js:181:19 ❯ instance.update node_modules/@vue/runtime-core/dist/runtime-core.cjs.js:5211:16 This error originated in "tests/components/FileTree.spec.ts" test file. It doesn't mean the error was thrown inside the file itself, but while it was running.

Check failure on line 21 in frontend/src/services/utils.ts

View workflow job for this annotation

GitHub Actions / build

Unhandled error

TypeError: Cannot read properties of undefined (reading 'digest') ❯ Module.sha1 src/services/utils.ts:21:42 ❯ src/components/FileTreeItem.vue:28:22 ❯ node_modules/@vue/runtime-core/dist/runtime-core.cjs.js:1807:40 ❯ callWithErrorHandling node_modules/@vue/runtime-core/dist/runtime-core.cjs.js:196:19 ❯ callWithAsyncErrorHandling node_modules/@vue/runtime-core/dist/runtime-core.cjs.js:203:17 ❯ Array.hook.__weh.hook.__weh node_modules/@vue/runtime-core/dist/runtime-core.cjs.js:1787:19 ❯ Object.invokeArrayFns node_modules/@vue/shared/dist/shared.cjs.js:85:11 ❯ ReactiveEffect.componentUpdateFn [as fn] node_modules/@vue/runtime-core/dist/runtime-core.cjs.js:5036:18 ❯ ReactiveEffect.run node_modules/@vue/reactivity/dist/reactivity.cjs.js:181:19 ❯ instance.update node_modules/@vue/runtime-core/dist/runtime-core.cjs.js:5211:16 This error originated in "tests/components/FileTree.spec.ts" test file. It doesn't mean the error was thrown inside the file itself, but while it was running.

Check failure on line 21 in frontend/src/services/utils.ts

View workflow job for this annotation

GitHub Actions / build

Unhandled error

TypeError: Cannot read properties of undefined (reading 'digest') ❯ Module.sha1 src/services/utils.ts:21:42 ❯ src/components/FileTreeItem.vue:28:22 ❯ node_modules/@vue/runtime-core/dist/runtime-core.cjs.js:1807:40 ❯ callWithErrorHandling node_modules/@vue/runtime-core/dist/runtime-core.cjs.js:196:19 ❯ callWithAsyncErrorHandling node_modules/@vue/runtime-core/dist/runtime-core.cjs.js:203:17 ❯ Array.hook.__weh.hook.__weh node_modules/@vue/runtime-core/dist/runtime-core.cjs.js:1787:19 ❯ Object.invokeArrayFns node_modules/@vue/shared/dist/shared.cjs.js:85:11 ❯ ReactiveEffect.componentUpdateFn [as fn] node_modules/@vue/runtime-core/dist/runtime-core.cjs.js:5036:18 ❯ ReactiveEffect.run node_modules/@vue/reactivity/dist/reactivity.cjs.js:181:19 ❯ instance.update node_modules/@vue/runtime-core/dist/runtime-core.cjs.js:5211:16 This error originated in "tests/components/FileTree.spec.ts" test file. It doesn't mean the error was thrown inside the file itself, but while it was running.

Check failure on line 21 in frontend/src/services/utils.ts

View workflow job for this annotation

GitHub Actions / build

Unhandled error

TypeError: Cannot read properties of undefined (reading 'digest') ❯ Module.sha1 src/services/utils.ts:21:42 ❯ src/components/FileTreeItem.vue:28:22 ❯ node_modules/@vue/runtime-core/dist/runtime-core.cjs.js:1807:40 ❯ callWithErrorHandling node_modules/@vue/runtime-core/dist/runtime-core.cjs.js:196:19 ❯ callWithAsyncErrorHandling node_modules/@vue/runtime-core/dist/runtime-core.cjs.js:203:17 ❯ Array.hook.__weh.hook.__weh node_modules/@vue/runtime-core/dist/runtime-core.cjs.js:1787:19 ❯ Object.invokeArrayFns node_modules/@vue/shared/dist/shared.cjs.js:85:11 ❯ ReactiveEffect.componentUpdateFn [as fn] node_modules/@vue/runtime-core/dist/runtime-core.cjs.js:5036:18 ❯ ReactiveEffect.run node_modules/@vue/reactivity/dist/reactivity.cjs.js:181:19 ❯ instance.update node_modules/@vue/runtime-core/dist/runtime-core.cjs.js:5211:16 This error originated in "tests/components/FileTree.spec.ts" test file. It doesn't mean the error was thrown inside the file itself, but while it was running.

Check failure on line 21 in frontend/src/services/utils.ts

View workflow job for this annotation

GitHub Actions / build

Unhandled error

TypeError: Cannot read properties of undefined (reading 'digest') ❯ Module.sha1 src/services/utils.ts:21:42 ❯ src/components/FileTreeItem.vue:28:22 ❯ node_modules/@vue/runtime-core/dist/runtime-core.cjs.js:1807:40 ❯ callWithErrorHandling node_modules/@vue/runtime-core/dist/runtime-core.cjs.js:196:19 ❯ callWithAsyncErrorHandling node_modules/@vue/runtime-core/dist/runtime-core.cjs.js:203:17 ❯ Array.hook.__weh.hook.__weh node_modules/@vue/runtime-core/dist/runtime-core.cjs.js:1787:19 ❯ Object.invokeArrayFns node_modules/@vue/shared/dist/shared.cjs.js:85:11 ❯ ReactiveEffect.componentUpdateFn [as fn] node_modules/@vue/runtime-core/dist/runtime-core.cjs.js:5036:18 ❯ ReactiveEffect.run node_modules/@vue/reactivity/dist/reactivity.cjs.js:181:19 ❯ instance.update node_modules/@vue/runtime-core/dist/runtime-core.cjs.js:5211:16 This error originated in "tests/components/FileTree.spec.ts" test file. It doesn't mean the error was thrown inside the file itself, but while it was running.

Check failure on line 21 in frontend/src/services/utils.ts

View workflow job for this annotation

GitHub Actions / build

Unhandled error

TypeError: Cannot read properties of undefined (reading 'digest') ❯ Module.sha1 src/services/utils.ts:21:42 ❯ src/components/FileTreeItem.vue:28:22 ❯ node_modules/@vue/runtime-core/dist/runtime-core.cjs.js:1807:40 ❯ callWithErrorHandling node_modules/@vue/runtime-core/dist/runtime-core.cjs.js:196:19 ❯ callWithAsyncErrorHandling node_modules/@vue/runtime-core/dist/runtime-core.cjs.js:203:17 ❯ Array.hook.__weh.hook.__weh node_modules/@vue/runtime-core/dist/runtime-core.cjs.js:1787:19 ❯ Object.invokeArrayFns node_modules/@vue/shared/dist/shared.cjs.js:85:11 ❯ ReactiveEffect.componentUpdateFn [as fn] node_modules/@vue/runtime-core/dist/runtime-core.cjs.js:5036:18 ❯ ReactiveEffect.run node_modules/@vue/reactivity/dist/reactivity.cjs.js:181:19 ❯ instance.update node_modules/@vue/runtime-core/dist/runtime-core.cjs.js:5211:16 This error originated in "tests/components/FileTree.spec.ts" test file. It doesn't mean the error was thrown inside the file itself, but while it was running.

Check failure on line 21 in frontend/src/services/utils.ts

View workflow job for this annotation

GitHub Actions / build

Unhandled error

TypeError: Cannot read properties of undefined (reading 'digest') ❯ Module.sha1 src/services/utils.ts:21:42 ❯ src/components/FileTreeItem.vue:28:22 ❯ node_modules/@vue/runtime-core/dist/runtime-core.cjs.js:1807:40 ❯ callWithErrorHandling node_modules/@vue/runtime-core/dist/runtime-core.cjs.js:196:19 ❯ callWithAsyncErrorHandling node_modules/@vue/runtime-core/dist/runtime-core.cjs.js:203:17 ❯ Array.hook.__weh.hook.__weh node_modules/@vue/runtime-core/dist/runtime-core.cjs.js:1787:19 ❯ Object.invokeArrayFns node_modules/@vue/shared/dist/shared.cjs.js:85:11 ❯ ReactiveEffect.componentUpdateFn [as fn] node_modules/@vue/runtime-core/dist/runtime-core.cjs.js:5036:18 ❯ ReactiveEffect.run node_modules/@vue/reactivity/dist/reactivity.cjs.js:181:19 ❯ instance.update node_modules/@vue/runtime-core/dist/runtime-core.cjs.js:5211:16 This error originated in "tests/components/FileTree.spec.ts" test file. It doesn't mean the error was thrown inside the file itself, but while it was running.

Check failure on line 21 in frontend/src/services/utils.ts

View workflow job for this annotation

GitHub Actions / build

Unhandled error

TypeError: Cannot read properties of undefined (reading 'digest') ❯ Module.sha1 src/services/utils.ts:21:42 ❯ src/components/FileTreeItem.vue:28:22 ❯ node_modules/@vue/runtime-core/dist/runtime-core.cjs.js:1807:40 ❯ callWithErrorHandling node_modules/@vue/runtime-core/dist/runtime-core.cjs.js:196:19 ❯ callWithAsyncErrorHandling node_modules/@vue/runtime-core/dist/runtime-core.cjs.js:203:17 ❯ Array.hook.__weh.hook.__weh node_modules/@vue/runtime-core/dist/runtime-core.cjs.js:1787:19 ❯ Object.invokeArrayFns node_modules/@vue/shared/dist/shared.cjs.js:85:11 ❯ ReactiveEffect.componentUpdateFn [as fn] node_modules/@vue/runtime-core/dist/runtime-core.cjs.js:5036:18 ❯ ReactiveEffect.run node_modules/@vue/reactivity/dist/reactivity.cjs.js:181:19 ❯ instance.update node_modules/@vue/runtime-core/dist/runtime-core.cjs.js:5211:16 This error originated in "tests/components/FileTree.spec.ts" test file. It doesn't mean the error was thrown inside the file itself, but while it was running.
const hashArray = Array.from(new Uint8Array(hashBuffer));
return hashArray.map((b) => b.toString(16).padStart(2, "0")).join("");
}

0 comments on commit bac0926

Please sign in to comment.