Skip to content
This repository has been archived by the owner on Nov 26, 2024. It is now read-only.

Commit

Permalink
Merge pull request #17 from claustra01/feature/websocket-base
Browse files Browse the repository at this point in the history
WebSocketの基盤とか、角度センサーとか色々
  • Loading branch information
claustra01 authored Aug 10, 2024
2 parents 802641e + dcd9def commit a88a668
Show file tree
Hide file tree
Showing 11 changed files with 190 additions and 10 deletions.
1 change: 1 addition & 0 deletions .env.sample
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
VITE_HOST_NAME=
7 changes: 1 addition & 6 deletions cspell.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,5 @@
"ignorePaths": ["node_modules/**", "*.svg"],
"version": "0.2",
"language": "en",
"words": [
"NATSUMATSURI",
"Dela",
"Yuji",
"Syuku"
]
"words": ["NATSUMATSURI", "Dela", "Yuji", "Syuku", "zustand"]
}
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
"dependencies": {
"react": "^18.3.1",
"react-dom": "^18.3.1",
"react-router-dom": "^6.26.0"
"react-router-dom": "^6.26.0",
"zustand": "^4.5.4"
},
"devDependencies": {
"@biomejs/biome": "1.8.3",
Expand Down
34 changes: 31 additions & 3 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

49 changes: 49 additions & 0 deletions src/hooks/useOrientation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { useCallback, useEffect, useState } from "react";

export interface Orientation {
alpha: number;
beta: number;
gamma: number;
}

export const useOrientation = () => {
const [initialOrientation, setInitialOrientation] =
useState<Orientation | null>(null);
const [orientationDiff, setOrientationDiff] = useState<Orientation>({
alpha: 0,
beta: 0,
gamma: 0,
});

const handleOrientation = useCallback(
(event: DeviceOrientationEvent) => {
if (!initialOrientation) {
setInitialOrientation({
alpha: event.alpha || 0,
beta: event.beta || 0,
gamma: event.gamma || 0,
});
} else {
setOrientationDiff({
alpha: (event.alpha || 0) - initialOrientation.alpha,
beta: (event.beta || 0) - initialOrientation.beta,
gamma: (event.gamma || 0) - initialOrientation.gamma,
});
}
},
[initialOrientation],
);

const reset = useCallback(() => {
setInitialOrientation(null);
}, []);

useEffect(() => {
window.addEventListener("deviceorientation", handleOrientation);
return () => {
window.removeEventListener("deviceorientation", handleOrientation);
};
}, [handleOrientation]);

return { orientationDiff, reset };
};
2 changes: 2 additions & 0 deletions src/store/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { useSocketRefStore } from "./useSocketRefStore";
export { useUUIDStore } from "./useUUIDStore";
15 changes: 15 additions & 0 deletions src/store/useSocketRefStore.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import type { MutableRefObject } from "react";
import { create } from "zustand";

type State = {
socketRef: MutableRefObject<WebSocket | undefined> | null;
};

type Action = {
setRef: (ref: MutableRefObject<WebSocket | undefined>) => void;
};

export const useSocketRefStore = create<State & Action>()((set) => ({
socketRef: null,
setRef: (ref) => set(() => ({ socketRef: ref })),
}));
23 changes: 23 additions & 0 deletions src/store/useUUIDStore.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { create } from "zustand";
import { persist } from "zustand/middleware";
import { generateUUID } from "../utils/uuid";

type State = {
uuid: string;
};

type Action = {
updateUUID: () => void;
};

export const useUUIDStore = create<State & Action>()(
persist(
(set) => ({
uuid: generateUUID(),
updateUUID: () => set(() => ({ uuid: generateUUID() })),
}),
{
name: "user-uuid",
},
),
);
30 changes: 30 additions & 0 deletions src/type/schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
export interface Schema {
id: string;
interval: number;
angle: angle;
acceleration: acceleration;
distance: distance;
message_type: message_type;
}

interface angle {
x: number;
y: number;
}

interface acceleration {
x: number;
y: number;
z: number;
}

interface distance {
x: number;
y: number;
z: number;
}

export enum message_type {
pointer = "pointer",
action = "action",
}
31 changes: 31 additions & 0 deletions src/utils/parmission.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/**
* 角度・加速度センサーのパーミッションをリクエストする関数
*/
export const requestPermission = async () => {
// @ts-expect-error only safari property
if (typeof DeviceMotionEvent.requestPermission === "function") {
try {
// @ts-expect-error only safari property
const permissionState = await DeviceMotionEvent.requestPermission();
if (permissionState !== "granted") {
console.warn("Device Motion permission denied.");
}
} catch (error) {
console.error("Error requesting Device Motion permission:", error);
}
}

// @ts-expect-error only safari property
if (typeof DeviceOrientationEvent.requestPermission === "function") {
try {
const permissionState =
// @ts-expect-error only safari property
await DeviceOrientationEvent.requestPermission();
if (permissionState !== "granted") {
console.warn("Device Orientation permission denied.");
}
} catch (error) {
console.error("Error requesting Device Orientation permission:", error);
}
}
};
5 changes: 5 additions & 0 deletions src/utils/uuid.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export const generateUUID = (): string => {
const timestamp = Date.now().toString(16);
const randomHex = Math.floor(Math.random() * 0xffffff).toString(16);
return `${timestamp}-${randomHex}`;
};

0 comments on commit a88a668

Please sign in to comment.