First version running with npm run dev
This commit is contained in:
commit
f90ec06a4f
28 files changed
+12042
No files matched your search
@@ -0,0 +1,50 @@
|
||||
import { ref, computed } from "vue";
|
||||
import type { Language } from "~/types/translation";
|
||||
import { supportedLanguages, getLanguageByCode } from "~/utils/languages";
|
||||
|
||||
export function useLanguages() {
|
||||
const languages = ref<Language[]>(supportedLanguages);
|
||||
const searchQuery = ref("");
|
||||
|
||||
const filteredLanguages = computed(() => {
|
||||
if (!searchQuery.value.trim()) {
|
||||
return languages.value;
|
||||
}
|
||||
|
||||
const query = searchQuery.value.toLowerCase();
|
||||
return languages.value.filter(
|
||||
(lang) =>
|
||||
lang.name.toLowerCase().includes(query) ||
|
||||
lang.nativeName?.toLowerCase().includes(query) ||
|
||||
lang.code.toLowerCase().includes(query),
|
||||
);
|
||||
});
|
||||
|
||||
const getLanguageName = (code: string): string => {
|
||||
const lang = getLanguageByCode(code);
|
||||
return lang?.name || code;
|
||||
};
|
||||
|
||||
const getLanguageNativeName = (code: string): string => {
|
||||
const lang = getLanguageByCode(code);
|
||||
return lang?.nativeName || lang?.name || code;
|
||||
};
|
||||
|
||||
const search = (query: string) => {
|
||||
searchQuery.value = query;
|
||||
};
|
||||
|
||||
const clearSearch = () => {
|
||||
searchQuery.value = "";
|
||||
};
|
||||
|
||||
return {
|
||||
languages,
|
||||
filteredLanguages,
|
||||
searchQuery,
|
||||
search,
|
||||
clearSearch,
|
||||
getLanguageName,
|
||||
getLanguageNativeName,
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,91 @@
|
||||
import { ref, computed, onMounted } from "vue";
|
||||
|
||||
interface OllamaStatus {
|
||||
available: boolean;
|
||||
running: boolean;
|
||||
modelExists: boolean;
|
||||
message: string;
|
||||
}
|
||||
|
||||
export function useOllamaStatus() {
|
||||
const status = ref<OllamaStatus>({
|
||||
available: false,
|
||||
running: false,
|
||||
modelExists: false,
|
||||
message: "Checking Ollama status...",
|
||||
});
|
||||
|
||||
const isLoading = ref(true);
|
||||
const isChecking = ref(false);
|
||||
|
||||
async function checkStatus() {
|
||||
isChecking.value = true;
|
||||
isLoading.value = true;
|
||||
|
||||
try {
|
||||
const response = await fetch("/api/tags", {
|
||||
method: "GET",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
status.value = {
|
||||
available: false,
|
||||
running: false,
|
||||
modelExists: false,
|
||||
message: "Ollama service is not running",
|
||||
};
|
||||
isLoading.value = false;
|
||||
isChecking.value = false;
|
||||
return;
|
||||
}
|
||||
|
||||
const data = await response.json();
|
||||
|
||||
status.value = {
|
||||
available: data.available,
|
||||
running: data.running,
|
||||
modelExists: data.modelExists,
|
||||
message: data.modelExists
|
||||
? "Ollama is ready"
|
||||
: `Model "${data.model}" not found. Run: ollama pull ${data.model}`,
|
||||
};
|
||||
} catch (error) {
|
||||
status.value = {
|
||||
available: false,
|
||||
running: false,
|
||||
modelExists: false,
|
||||
message:
|
||||
"Cannot connect to Ollama. Ensure it is running on the configured host.",
|
||||
};
|
||||
} finally {
|
||||
isLoading.value = false;
|
||||
isChecking.value = false;
|
||||
}
|
||||
}
|
||||
|
||||
const isReady = computed(
|
||||
() => status.value.available && status.value.modelExists,
|
||||
);
|
||||
|
||||
const errorMessage = computed(() =>
|
||||
status.value.modelExists && status.value.available
|
||||
? null
|
||||
: status.value.message,
|
||||
);
|
||||
|
||||
onMounted(() => {
|
||||
checkStatus();
|
||||
});
|
||||
|
||||
return {
|
||||
status,
|
||||
isLoading,
|
||||
isChecking,
|
||||
isReady,
|
||||
errorMessage,
|
||||
checkStatus,
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
import { ref, onMounted } from "vue";
|
||||
|
||||
const STORAGE_KEY = "quick-ling-settings";
|
||||
const DEFAULT_MODEL = "translategemma:4b";
|
||||
|
||||
interface Settings {
|
||||
model: string;
|
||||
}
|
||||
|
||||
const model = ref(DEFAULT_MODEL);
|
||||
|
||||
function loadSettings(): void {
|
||||
if (typeof localStorage !== "undefined") {
|
||||
try {
|
||||
const stored = localStorage.getItem(STORAGE_KEY);
|
||||
if (stored) {
|
||||
const parsed: Settings = JSON.parse(stored);
|
||||
if (parsed.model) {
|
||||
model.value = parsed.model;
|
||||
}
|
||||
}
|
||||
} catch {
|
||||
// ignore parse errors
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function saveSettings(): void {
|
||||
if (typeof localStorage !== "undefined") {
|
||||
const settings: Settings = { model: model.value };
|
||||
localStorage.setItem(STORAGE_KEY, JSON.stringify(settings));
|
||||
}
|
||||
}
|
||||
|
||||
export function useSettings() {
|
||||
function setModel(newModel: string): void {
|
||||
model.value = newModel;
|
||||
saveSettings();
|
||||
}
|
||||
|
||||
onMounted(() => {
|
||||
loadSettings();
|
||||
});
|
||||
|
||||
return {
|
||||
model,
|
||||
setModel,
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
import { ref, watch, onMounted } from 'vue';
|
||||
|
||||
type Theme = 'light' | 'dark';
|
||||
|
||||
const STORAGE_KEY = 'quick-ling-theme';
|
||||
|
||||
const theme = ref<Theme>('light');
|
||||
|
||||
function applyTheme(t: Theme): void {
|
||||
if (typeof document !== 'undefined') {
|
||||
document.documentElement.setAttribute('data-theme', t);
|
||||
}
|
||||
}
|
||||
|
||||
function getSystemPreference(): Theme {
|
||||
if (typeof window !== 'undefined' && window.matchMedia) {
|
||||
return window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light';
|
||||
}
|
||||
return 'light';
|
||||
}
|
||||
|
||||
function loadSavedTheme(): Theme | null {
|
||||
if (typeof localStorage !== 'undefined') {
|
||||
const saved = localStorage.getItem(STORAGE_KEY);
|
||||
if (saved === 'light' || saved === 'dark') {
|
||||
return saved;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
function saveTheme(t: Theme): void {
|
||||
if (typeof localStorage !== 'undefined') {
|
||||
localStorage.setItem(STORAGE_KEY, t);
|
||||
}
|
||||
}
|
||||
|
||||
export function useTheme() {
|
||||
function toggleTheme(): void {
|
||||
theme.value = theme.value === 'light' ? 'dark' : 'light';
|
||||
}
|
||||
|
||||
function setTheme(t: Theme): void {
|
||||
theme.value = t;
|
||||
}
|
||||
|
||||
watch(theme, (newTheme) => {
|
||||
applyTheme(newTheme);
|
||||
saveTheme(newTheme);
|
||||
});
|
||||
|
||||
onMounted(() => {
|
||||
const saved = loadSavedTheme();
|
||||
const initial = saved ?? getSystemPreference();
|
||||
theme.value = initial;
|
||||
applyTheme(initial);
|
||||
|
||||
if (typeof window !== 'undefined' && window.matchMedia) {
|
||||
const mediaQuery = window.matchMedia('(prefers-color-scheme: dark)');
|
||||
mediaQuery.addEventListener('change', (e) => {
|
||||
if (!loadSavedTheme()) {
|
||||
theme.value = e.matches ? 'dark' : 'light';
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
return {
|
||||
theme,
|
||||
toggleTheme,
|
||||
setTheme,
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
import { ref } from "vue";
|
||||
|
||||
interface Toast {
|
||||
id: number;
|
||||
message: string;
|
||||
type: "success" | "error" | "info";
|
||||
}
|
||||
|
||||
const toasts = ref<Toast[]>([]);
|
||||
let nextId = 0;
|
||||
|
||||
export function useToast() {
|
||||
function show(
|
||||
message: string,
|
||||
type: Toast["type"] = "success",
|
||||
duration = 2500,
|
||||
): void {
|
||||
const id = nextId++;
|
||||
toasts.value.push({ id, message, type });
|
||||
setTimeout(() => {
|
||||
dismiss(id);
|
||||
}, duration);
|
||||
}
|
||||
|
||||
function dismiss(id: number): void {
|
||||
const index = toasts.value.findIndex((t) => t.id === id);
|
||||
if (index !== -1) {
|
||||
toasts.value.splice(index, 1);
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
toasts,
|
||||
show,
|
||||
dismiss,
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,196 @@
|
||||
import { ref, computed } from "vue";
|
||||
import type { Ref } from "vue";
|
||||
import type { TranslationResponse } from "~/types/translation";
|
||||
import { supportedLanguages } from "~/utils/languages";
|
||||
|
||||
interface UseTranslationOptions {
|
||||
initialSource?: string;
|
||||
initialTarget?: string;
|
||||
model?: Ref<string>;
|
||||
}
|
||||
|
||||
function detectBrowserLanguage(): string {
|
||||
if (typeof navigator !== "undefined") {
|
||||
const browserLang = navigator.language.toLowerCase();
|
||||
const langCode = browserLang.split("-")[0];
|
||||
|
||||
const matchedLang = supportedLanguages.find(
|
||||
(lang) => lang.code === langCode,
|
||||
);
|
||||
if (matchedLang) {
|
||||
return matchedLang.code;
|
||||
}
|
||||
|
||||
if (browserLang.startsWith("en")) {
|
||||
return "en";
|
||||
}
|
||||
}
|
||||
return "en";
|
||||
}
|
||||
|
||||
const detectedLang = detectBrowserLanguage();
|
||||
const defaultSource = detectedLang;
|
||||
const defaultTarget = detectedLang === "en" ? "es" : "en";
|
||||
|
||||
export function useTranslation(options: UseTranslationOptions = {}) {
|
||||
const sourceText = ref("");
|
||||
const translatedText = ref("");
|
||||
const sourceLang = ref(options.initialSource || defaultSource);
|
||||
const targetLang = ref(options.initialTarget || defaultTarget);
|
||||
const isLoading = ref(false);
|
||||
const error = ref<string | null>(null);
|
||||
const charCount = computed(() => sourceText.value.length);
|
||||
const wordCount = computed(() => {
|
||||
const text = sourceText.value.trim();
|
||||
return text ? text.split(/\s+/).length : 0;
|
||||
});
|
||||
|
||||
const isReadyToTranslate = computed(() => {
|
||||
return sourceText.value.trim().length > 0 && !isLoading.value;
|
||||
});
|
||||
|
||||
async function translate() {
|
||||
if (!isReadyToTranslate.value) {
|
||||
return;
|
||||
}
|
||||
|
||||
isLoading.value = true;
|
||||
error.value = null;
|
||||
translatedText.value = "";
|
||||
|
||||
try {
|
||||
const response = await fetch("/api/translate", {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
body: JSON.stringify({
|
||||
text: sourceText.value,
|
||||
source: sourceLang.value,
|
||||
target: targetLang.value,
|
||||
...(options.model ? { model: options.model.value } : {}),
|
||||
}),
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
const errorBody = await response.json().catch(() => ({}));
|
||||
throw new Error(
|
||||
errorBody.statusMessage || "Translation request failed",
|
||||
);
|
||||
}
|
||||
|
||||
const result: TranslationResponse = await response.json();
|
||||
translatedText.value = result.translatedText;
|
||||
} catch (e) {
|
||||
if (e instanceof Error) {
|
||||
error.value = e.message;
|
||||
} else {
|
||||
error.value = "An unknown error occurred";
|
||||
}
|
||||
} finally {
|
||||
isLoading.value = false;
|
||||
}
|
||||
}
|
||||
|
||||
async function translateWithStreaming(
|
||||
options: {
|
||||
onChunk?: (chunk: string) => void;
|
||||
onComplete?: (fullText: string) => void;
|
||||
} = {},
|
||||
) {
|
||||
if (!isReadyToTranslate.value) {
|
||||
return;
|
||||
}
|
||||
|
||||
isLoading.value = true;
|
||||
error.value = null;
|
||||
translatedText.value = "";
|
||||
|
||||
try {
|
||||
const response = await fetch("/api/translate-stream", {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
body: JSON.stringify({
|
||||
text: sourceText.value,
|
||||
source: sourceLang.value,
|
||||
target: targetLang.value,
|
||||
...(options.model ? { model: options.model.value } : {}),
|
||||
}),
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
const errorBody = await response.json().catch(() => ({}));
|
||||
throw new Error(
|
||||
errorBody.statusMessage || "Streaming translation request failed",
|
||||
);
|
||||
}
|
||||
|
||||
if (!response.body) {
|
||||
throw new Error("No response body");
|
||||
}
|
||||
|
||||
const reader = response.body.getReader();
|
||||
const decoder = new TextDecoder();
|
||||
let accumulatedText = "";
|
||||
|
||||
while (true) {
|
||||
const { done, value } = await reader.read();
|
||||
if (done) break;
|
||||
|
||||
const chunk = decoder.decode(value);
|
||||
const lines = chunk.split("\n").filter((line) => line.trim());
|
||||
|
||||
for (const line of lines) {
|
||||
try {
|
||||
const parsed = JSON.parse(line);
|
||||
if (parsed.response) {
|
||||
accumulatedText += parsed.response;
|
||||
translatedText.value = accumulatedText;
|
||||
options.onChunk?.(parsed.response);
|
||||
}
|
||||
} catch {}
|
||||
}
|
||||
}
|
||||
|
||||
options.onComplete?.(accumulatedText);
|
||||
} catch (e) {
|
||||
if (e instanceof Error) {
|
||||
error.value = e.message;
|
||||
} else {
|
||||
error.value = "An unknown error occurred";
|
||||
}
|
||||
} finally {
|
||||
isLoading.value = false;
|
||||
}
|
||||
}
|
||||
|
||||
function swapLanguages() {
|
||||
const temp = sourceLang.value;
|
||||
sourceLang.value = targetLang.value;
|
||||
targetLang.value = temp;
|
||||
}
|
||||
|
||||
function clearInput() {
|
||||
sourceText.value = "";
|
||||
translatedText.value = "";
|
||||
error.value = null;
|
||||
}
|
||||
|
||||
return {
|
||||
sourceText,
|
||||
translatedText,
|
||||
sourceLang,
|
||||
targetLang,
|
||||
isLoading,
|
||||
error,
|
||||
charCount,
|
||||
wordCount,
|
||||
isReadyToTranslate,
|
||||
translate,
|
||||
translateWithStreaming,
|
||||
swapLanguages,
|
||||
clearInput,
|
||||
};
|
||||
}
|
||||
Reference in new issue
Block a user