[Refactor] Separate Logics directory.
This commit is contained in:
@@ -1,27 +0,0 @@
|
||||
export const scrollToBottom = (ref, smooth = false) => {
|
||||
const element = ref.current;
|
||||
const scroll_height = element.scrollHeight - element.clientHeight;
|
||||
|
||||
if (smooth) {
|
||||
const duration = 300; // スクロールにかける時間(ミリ秒)
|
||||
const start_time = performance.now();
|
||||
const scroll_top = element.scrollTop;
|
||||
|
||||
const scroll = (current_time) => {
|
||||
const elapsed = current_time - start_time;
|
||||
const progress = Math.min(elapsed / duration, 1);
|
||||
const ease_in_out_quad = (t) => t < 0.5
|
||||
? 2 * t * t
|
||||
: -1 + (4 - 2 * t) * t;
|
||||
element.scrollTop = scroll_top + (scroll_height - scroll_top) * ease_in_out_quad(progress);
|
||||
|
||||
if (progress < 1) {
|
||||
requestAnimationFrame(scroll);
|
||||
}
|
||||
};
|
||||
|
||||
requestAnimationFrame(scroll);
|
||||
} else {
|
||||
element.scrollTop = scroll_height;
|
||||
}
|
||||
};
|
||||
@@ -1,81 +0,0 @@
|
||||
import { getCurrent } from "@tauri-apps/api/window";
|
||||
|
||||
import {
|
||||
useTranslationStatus,
|
||||
useTranscriptionSendStatus,
|
||||
useTranscriptionReceiveStatus,
|
||||
useForegroundStatus,
|
||||
} from "@store";
|
||||
|
||||
import { useStdoutToPython } from "./useStdoutToPython";
|
||||
|
||||
export const useMainFunction = () => {
|
||||
const {
|
||||
currentTranslationStatus,
|
||||
updateTranslationStatus,
|
||||
asyncUpdateTranslationStatus,
|
||||
} = useTranslationStatus();
|
||||
const {
|
||||
currentTranscriptionSendStatus,
|
||||
updateTranscriptionSendStatus,
|
||||
asyncUpdateTranscriptionSendStatus,
|
||||
} = useTranscriptionSendStatus();
|
||||
const {
|
||||
currentTranscriptionReceiveStatus,
|
||||
updateTranscriptionReceiveStatus,
|
||||
asyncUpdateTranscriptionReceiveStatus,
|
||||
} = useTranscriptionReceiveStatus();
|
||||
const {
|
||||
currentForegroundStatus,
|
||||
updateForegroundStatus,
|
||||
} = useForegroundStatus();
|
||||
|
||||
const { asyncStdoutToPython } = useStdoutToPython();
|
||||
|
||||
const asyncPending = () => new Promise(() => {});
|
||||
return {
|
||||
toggleTranslation: () => {
|
||||
asyncStdoutToPython({id: "/controller/callback_toggle_translation", data: !currentTranslationStatus.data});
|
||||
asyncUpdateTranslationStatus(asyncPending);
|
||||
},
|
||||
currentTranslationStatus: currentTranslationStatus,
|
||||
updateTranslationStatus: (payload) => {
|
||||
updateTranslationStatus(payload.data);
|
||||
},
|
||||
|
||||
toggleTranscriptionSend: () => {
|
||||
asyncStdoutToPython({id: "/controller/callback_toggle_transcription_send", data: !currentTranscriptionSendStatus.data});
|
||||
asyncUpdateTranscriptionSendStatus(asyncPending);
|
||||
},
|
||||
currentTranscriptionSendStatus: currentTranscriptionSendStatus,
|
||||
updateTranscriptionSendStatus: (payload) => {
|
||||
updateTranscriptionSendStatus(payload.data);
|
||||
},
|
||||
|
||||
toggleTranscriptionReceive: () => {
|
||||
asyncStdoutToPython({id: "/controller/callback_toggle_transcription_receive", data: !currentTranscriptionReceiveStatus.data});
|
||||
asyncUpdateTranscriptionReceiveStatus(asyncPending);
|
||||
},
|
||||
currentTranscriptionReceiveStatus: currentTranscriptionReceiveStatus,
|
||||
updateTranscriptionReceiveStatus: (payload) => {
|
||||
updateTranscriptionReceiveStatus(payload.data);
|
||||
},
|
||||
|
||||
toggleForeground: () => {
|
||||
const main_window = getCurrent();
|
||||
const is_foreground_enabled = !currentForegroundStatus.data;
|
||||
main_window.setAlwaysOnTop(is_foreground_enabled);
|
||||
updateForegroundStatus(is_foreground_enabled);
|
||||
|
||||
},
|
||||
currentForegroundStatus: currentForegroundStatus,
|
||||
};
|
||||
};
|
||||
|
||||
const asyncTestFunction = (...args) => {
|
||||
return new Promise((resolve) => {
|
||||
setTimeout(() => {
|
||||
resolve(...args);
|
||||
}, 3000);
|
||||
});
|
||||
};
|
||||
@@ -1,55 +0,0 @@
|
||||
import {
|
||||
useMessageLogsStatus,
|
||||
} from "@store";
|
||||
|
||||
import { useStdoutToPython } from "./useStdoutToPython";
|
||||
|
||||
export const useMessage = () => {
|
||||
const { currentMessageLogsStatus, addMessageLogsStatus, updateMessageLogsStatus } = useMessageLogsStatus();
|
||||
const { asyncStdoutToPython } = useStdoutToPython();
|
||||
|
||||
return {
|
||||
sendMessage: (message) => {
|
||||
asyncStdoutToPython({id: "send_message", data: message});
|
||||
const uuid = crypto.randomUUID();
|
||||
const date = new Date().toLocaleTimeString(
|
||||
"ja-JP",
|
||||
{hour12: false, hour: "2-digit", minute:"2-digit"},
|
||||
);
|
||||
|
||||
addMessageLogsStatus({
|
||||
id: uuid,
|
||||
category: "sent",
|
||||
status: "pending",
|
||||
created_at: date,
|
||||
messages: {
|
||||
original: message,
|
||||
translated: [
|
||||
message,
|
||||
],
|
||||
},
|
||||
});
|
||||
|
||||
setTimeout(() => {
|
||||
const updateItemById = (id) => (prevItems) => {
|
||||
return prevItems.map(item => {
|
||||
if (item.id === id) {
|
||||
item.status = "ok";
|
||||
}
|
||||
return item;
|
||||
});
|
||||
};
|
||||
updateMessageLogsStatus(updateItemById(uuid));
|
||||
}, 3000);
|
||||
},
|
||||
currentMessageLogsStatus: currentMessageLogsStatus,
|
||||
};
|
||||
};
|
||||
|
||||
// const asyncTestFunction = (...args) => {
|
||||
// return new Promise((resolve) => {
|
||||
// setTimeout(() => {
|
||||
// resolve(...args);
|
||||
// }, 3000);
|
||||
// });
|
||||
// };
|
||||
@@ -1,53 +0,0 @@
|
||||
import { Command } from "@tauri-apps/api/shell";
|
||||
import { store } from "@store";
|
||||
|
||||
import { useMainFunction } from "./useMainFunction";
|
||||
|
||||
export const useStartPython = () => {
|
||||
const {
|
||||
updateTranslationStatus,
|
||||
updateTranscriptionSendStatus,
|
||||
updateTranscriptionReceiveStatus,
|
||||
} = useMainFunction();
|
||||
|
||||
const routes = {
|
||||
"/controller/callback_toggle_translation": updateTranslationStatus,
|
||||
"/controller/callback_toggle_transcription_send": updateTranscriptionSendStatus,
|
||||
"/controller/callback_toggle_transcription_receive": updateTranscriptionReceiveStatus,
|
||||
};
|
||||
|
||||
const receiveRoutes = (parsed_data) => {
|
||||
if (parsed_data.status === "ok") {
|
||||
const route = routes[parsed_data.id];
|
||||
if (route) {
|
||||
route({ data: parsed_data.data });
|
||||
} else {
|
||||
console.error(`Invalid path: ${parsed_data.id}`);
|
||||
}
|
||||
} else {
|
||||
console.log("Received data status is not 'ok'.", parsed_data);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
const asyncStartPython = async () => {
|
||||
const command = Command.sidecar("bin/test");
|
||||
command.on("error", error => console.error(`error: "${error}"`));
|
||||
command.stdout.on("data", (line) => {
|
||||
let parsed_data = "";
|
||||
try {
|
||||
parsed_data = JSON.parse(line);
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
parsed_data = line;
|
||||
}
|
||||
console.log("from python:", parsed_data);
|
||||
receiveRoutes(parsed_data);
|
||||
});
|
||||
command.stderr.on("data", line => console.error("stderr:", line));
|
||||
const backend_subprocess = await command.spawn();
|
||||
store.backend_subprocess = backend_subprocess;
|
||||
};
|
||||
|
||||
return { asyncStartPython };
|
||||
};
|
||||
@@ -1,17 +0,0 @@
|
||||
import { store } from "@store";
|
||||
|
||||
export const useStdoutToPython = () => {
|
||||
const asyncStdoutToPython = async (value) => {
|
||||
// send to python
|
||||
const backend_subprocess = store.backend_subprocess;
|
||||
if (backend_subprocess) {
|
||||
await backend_subprocess.write(JSON.stringify(value) + "\n").then(() => {
|
||||
}).catch((err) => {
|
||||
console.log(err);
|
||||
});
|
||||
} else {
|
||||
console.error("Backend subprocess is not found.", backend_subprocess);
|
||||
}
|
||||
};
|
||||
return { asyncStdoutToPython };
|
||||
};
|
||||
Reference in New Issue
Block a user