Commit 7745c72d authored by Vũ Hoàng Anh's avatar Vũ Hoàng Anh

refactor: set 172.16.2.210 as primary backend instead of dual-write

parent f55888b0
Pipeline #3442 canceled with stages
......@@ -5,32 +5,32 @@
"src": "../../../@crx/manifest",
"isEntry": true
},
"_api-client-DVp22mKG.js": {
"file": "assets/api-client-DVp22mKG.js",
"_api-client-Y5oDKlCr.js": {
"file": "assets/api-client-Y5oDKlCr.js",
"name": "api-client"
},
"src/background/service-worker.ts": {
"file": "assets/service-worker.ts-Co6uxu3b.js",
"file": "assets/service-worker.ts-DVsIrub1.js",
"name": "service-worker.ts",
"src": "src/background/service-worker.ts",
"isEntry": true,
"imports": [
"_api-client-DVp22mKG.js"
"_api-client-Y5oDKlCr.js"
]
},
"src/content/content-script.ts": {
"file": "assets/content-script.ts-CFoIDPlU.js",
"file": "assets/content-script.ts-Kz-gf73N.js",
"name": "content-script.ts",
"src": "src/content/content-script.ts",
"isEntry": true
},
"src/popup/popup.html": {
"file": "assets/popup-r4Xxxxmr.js",
"file": "assets/popup-bQfbpxZ8.js",
"name": "popup",
"src": "src/popup/popup.html",
"isEntry": true,
"imports": [
"_api-client-DVp22mKG.js"
"_api-client-Y5oDKlCr.js"
],
"css": [
"assets/popup-dYtSf2jU.css"
......
const l="http://160.191.50.138:3001";async function r(){return`${(await chrome.storage.local.get(["apiBaseUrl"])).apiBaseUrl||l}/api/v1`}async function h(){const e=await chrome.storage.local.get(["clerkSessionToken","authToken"]);return e.clerkSessionToken?e.clerkSessionToken:e.authToken||null}const y=45e3;async function f(){try{const s=(await chrome.storage.local.get(["clerkTokenSyncedAt"])).clerkTokenSyncedAt||0,n=Date.now()-s;if(n>y){console.log("[CuCu API] Token stale, refreshing...",{age:Math.round(n/1e3)+"s"});const t=await new Promise(a=>{chrome.runtime.sendMessage({type:"REFRESH_TOKEN"},o=>{a(o)})});t?.success&&t?.token?(await chrome.storage.local.set({clerkSessionToken:t.token,clerkTokenSyncedAt:Date.now()}),console.log("[CuCu API] ✅ Token refreshed",t.token.length,"chars")):console.log("[CuCu API] ⚠️ Token refresh failed:",t?.reason||"unknown")}}catch(e){console.log("[CuCu API] ❌ Token refresh error:",e?.message)}}async function c(){await f();const e=await h(),s={"Content-Type":"application/json"};return e&&(s.Authorization=`Bearer ${e}`),s}async function g(e){const s=await r(),n=await c(),t=await fetch(`${s}/memos`,{method:"POST",headers:n,body:JSON.stringify({content:e.content,tags:e.tags||[],visibility:e.visibility||"PRIVATE"})});if(!t.ok)throw await t.text(),new Error(`Failed to create memo (${t.status})`);return await t.json()}async function w(e,s){const n=await r(),t=await fetch(`${n}/memos`,{method:"POST",headers:{"Content-Type":"application/json",Authorization:`Bearer ${e}`},body:JSON.stringify({content:s.content,tags:s.tags||[],visibility:s.visibility||"PRIVATE"})});if(!t.ok){const a=await t.text();throw new Error(`Failed to save (${t.status}): ${a.substring(0,100)}`)}return await t.json()}async function d(e){try{const s=await c(),t=await fetch("http://172.16.2.210:5230/api/v1/memos",{method:"POST",headers:s,body:JSON.stringify({content:e.content,tags:e.tags||[],visibility:e.visibility||"PRIVATE"})});t.ok?console.log("[CuCu API] ✅ Successfully synced to secondary backend"):console.warn("[CuCu API] Secondary sync returned status:",t.status)}catch(s){console.warn("[CuCu API] ⚠️ Failed to sync to secondary backend:",s)}}async function k(){try{const e=await r(),s=await c(),n=await fetch(`${e}/memos`,{headers:s});if(!n.ok)return[];const t=await n.json(),a=new Set;for(const o of t){Array.isArray(o.tags)&&o.tags.forEach(i=>a.add(i));const u=(o.content||"").match(/#([a-zA-Z0-9_\u00C0-\u024F\u1E00-\u1EFF]+)/g);u&&u.forEach(i=>a.add(i.slice(1)))}return Array.from(a).sort()}catch{return[]}}async function T(){try{const e=await r(),s=await c(),n=await fetch(`${e}/shortcuts`,{headers:s});if(!n.ok)return[];const t=await n.json();return(Array.isArray(t)?t:t.shortcuts||[]).map(o=>({id:o.id,title:o.title||o.name||`Workspace ${o.id}`,filter:o.filter||""}))}catch{return[]}}async function m(){return(await chrome.storage.local.get(["recentTags"])).recentTags||[]}async function p(e){const s=[...new Set(e)].slice(0,20);await chrome.storage.local.set({recentTags:s})}export{T as a,g as b,w as c,d,k as f,m as g,p as s};
(function(){let i="",u="",d="";async function l(){for(let o=0;o<5;o++){try{const n=window.Clerk;if(n&&!n.user)return null;if(n?.session?.getToken){const s=await n.session.getToken();if(s)return s}}catch{}o<4&&await new Promise(n=>setTimeout(n,800))}return null}async function h(){const e=window.location.hostname;if(!(e==="160.191.50.138"||e==="localhost"||e.includes("opennotion")||e.includes("cucunote")||e.includes("cucu-note")))return;const o=await l();o&&chrome.runtime.sendMessage({type:"SYNC_AUTH",data:{clerkSessionToken:o}})}h();chrome.runtime.onMessage.addListener((e,t,o)=>{if(e.type==="GET_CLERK_TOKEN")return l().then(n=>{o({token:n})}),!0});document.addEventListener("mouseup",m);document.addEventListener("keydown",async e=>{if((e.key===" "||e.key==="Enter")&&i.length>0){const t=e.target;if(!t||t.tagName!=="INPUT"&&t.tagName!=="TEXTAREA"&&!t.isContentEditable){e.preventDefault(),e.stopPropagation(),await y();return}}});document.addEventListener("keyup",m);function m(){try{setTimeout(()=>{const e=window.getSelection();if(!e||e.rangeCount===0){i="",c();return}const t=e.toString().trim();if(t.length===0){i="",c();return}i=t,u=window.location.href,d=document.title,f()},50)}catch{}}async function y(){if(i)try{a("Đang lưu...","loading"),chrome.runtime.sendMessage({type:"SAVE_NOTE",data:{text:i,url:u,title:d}},e=>{if(chrome.runtime.lastError){a(`❌ Lỗi: ${chrome.runtime.lastError.message||"Extension error"}`,"error");return}if(e?.success)a("✅ Đã lưu vào CuCu Note!","success"),window.getSelection()?.removeAllRanges(),i="",c();else{const t=e?.error||"Không thể lưu note";a(`❌ ${t}`,"error")}})}catch(e){a(`❌ ${e?.message||"Lỗi khi lưu note"}`,"error")}}function f(){c();const e=document.createElement("div");if(e.id="cucu-quick-hint",e.textContent="💡 Nhấn Space hoặc Enter để lưu nhanh",e.style.cssText=`
position: fixed;
bottom: 20px;
right: 20px;
background: linear-gradient(135deg, #a0845c 0%, #8b6914 100%);
color: white;
padding: 12px 20px;
border-radius: 8px;
font-size: 14px;
font-weight: 500;
box-shadow: 0 4px 12px rgba(0,0,0,0.15);
z-index: 999999;
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
animation: cucuSlideUp 0.3s ease-out;
pointer-events: none;
`,!document.getElementById("cucu-hint-style")){const t=document.createElement("style");t.id="cucu-hint-style",t.textContent=`
@keyframes cucuSlideUp {
from {
opacity: 0;
transform: translateY(20px);
}
to {
opacity: 1;
transform: translateY(0);
}
}
`,document.head.appendChild(t)}try{document.body.appendChild(e)}catch{document.documentElement.appendChild(e)}setTimeout(()=>{c()},3e3)}function c(){const e=document.getElementById("cucu-quick-hint");if(e)try{e.remove()}catch{}}function a(e,t="success"){const o=document.getElementById("cucu-toast");if(o)try{o.remove()}catch{}const n=document.createElement("div");n.id="cucu-toast",n.textContent=e;const s={success:"linear-gradient(135deg, #10b981 0%, #059669 100%)",error:"linear-gradient(135deg, #ef4444 0%, #dc2626 100%)",loading:"linear-gradient(135deg, #a0845c 0%, #8b6914 100%)"};if(n.style.cssText=`
position: fixed;
top: 20px;
right: 20px;
background: ${s[t]};
color: white;
padding: 16px 24px;
border-radius: 12px;
font-size: 15px;
font-weight: 500;
box-shadow: 0 8px 24px rgba(0,0,0,0.2);
z-index: 999999;
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
animation: cucuToastSlide 0.4s ease-out;
max-width: 350px;
word-wrap: break-word;
`,!document.getElementById("cucu-toast-style")){const r=document.createElement("style");r.id="cucu-toast-style",r.textContent=`
@keyframes cucuToastSlide {
from {
opacity: 0;
transform: translateX(100%);
}
to {
opacity: 1;
transform: translateX(0);
}
}
`;try{document.head.appendChild(r)}catch{}}try{document.body.appendChild(n)}catch{try{document.documentElement.appendChild(n)}catch{return}}t!=="loading"&&setTimeout(()=>{n.parentNode&&(n.style.animation="cucuToastSlide 0.3s ease-out reverse",setTimeout(()=>{try{n.remove()}catch{}},300))},3e3)}
})()
This source diff could not be displayed because it is too large. You can view the blob instead.
import{b as i,d as l}from"./api-client-DVp22mKG.js";async function h(){try{const s=(await chrome.tabs.query({})).filter(e=>{const t=e.url||"";return t.includes("160.191.50.138")||t.includes("localhost:3001")||t.includes("opennotion")||t.includes("cucunote")});for(const e of s)if(e.id)try{const o=(await chrome.scripting.executeScript({target:{tabId:e.id},world:"MAIN",func:async()=>{try{const n=window.Clerk;if(n?.session?.getToken)return await n.session.getToken()}catch{}return null}}))[0]?.result;if(o)return await chrome.storage.local.set({clerkSessionToken:o,clerkTokenSyncedAt:Date.now()}),console.log("[CuCu BG] ✅ Fresh token from tab",e.id,`(${o.length} chars)`),o}catch{}}catch{}return null}chrome.runtime.onMessage.addListener((c,s,e)=>{if(c.type==="SYNC_AUTH"){const{clerkSessionToken:t}=c.data||{};return t&&(chrome.storage.local.set({clerkSessionToken:t,clerkTokenSyncedAt:Date.now()}),console.log("[CuCu BG] ✅ Clerk token auto-synced from content script")),e({success:!0}),!1}if(c.type==="REFRESH_TOKEN")return h().then(t=>{e(t?{success:!0,token:t}:{success:!1,reason:"no_opennotion_tab"})}).catch(t=>{console.log("[CuCu BG] ❌ Token refresh failed:",t?.message),e({success:!1,reason:t?.message})}),!0;if(c.type==="SHOW_NOTE_FORM")chrome.action.openPopup(),chrome.storage.local.set({pendingNote:c.data}),e({success:!0});else if(c.type==="SAVE_NOTE"){const{text:t,url:o,title:n}=c.data,u=[new URL(o).hostname.replace("www.",""),"web-highlight"],a={content:`${t}
---
Source: [${n}](${o})`,tags:u,visibility:"PRIVATE"};return i(a).then(r=>{e({success:!0,memo:r})}).catch(r=>{e({success:!1,error:r.message})}),l(a),!0}return!0});
......@@ -20,7 +20,7 @@
"content_scripts": [
{
"js": [
"assets/content-script.ts-CFoIDPlU.js"
"assets/content-script.ts-Kz-gf73N.js"
],
"matches": [
"<all_urls>"
......@@ -48,7 +48,7 @@
"<all_urls>"
],
"resources": [
"assets/content-script.ts-CFoIDPlU.js"
"assets/content-script.ts-Kz-gf73N.js"
],
"use_dynamic_url": false
}
......
import './assets/service-worker.ts-Co6uxu3b.js';
import './assets/service-worker.ts-DVsIrub1.js';
......@@ -17,8 +17,8 @@
overflow-y: auto;
}
</style>
<script type="module" crossorigin src="/assets/popup-r4Xxxxmr.js"></script>
<link rel="modulepreload" crossorigin href="/assets/api-client-DVp22mKG.js">
<script type="module" crossorigin src="/assets/popup-bQfbpxZ8.js"></script>
<link rel="modulepreload" crossorigin href="/assets/api-client-Y5oDKlCr.js">
<link rel="stylesheet" crossorigin href="/assets/popup-dYtSf2jU.css">
</head>
<body>
......
......@@ -6,7 +6,7 @@
* 3. Handle REFRESH_TOKEN từ popup (refresh Clerk JWT trước khi gọi API)
*/
import { createMemo, syncToSecondaryBackend } from '../shared/api-client';
import { createMemo } from '../shared/api-client';
/**
* Grab fresh Clerk token from ANY OpenNotion tab
......@@ -18,7 +18,7 @@ async function grabFreshToken(): Promise<string | null> {
const candidates = allTabs.filter((t) => {
const url = t.url || '';
return (
url.includes('160.191.50.138') ||
url.includes('172.16.2.210') ||
url.includes('localhost:3001') ||
url.includes('opennotion') ||
url.includes('cucunote')
......@@ -133,9 +133,6 @@ chrome.runtime.onMessage.addListener((message, _sender, sendResponse) => {
sendResponse({ success: false, error: error.message });
});
// Dual write to custom backend (non-blocking)
syncToSecondaryBackend(memoData);
return true; // Keep channel open for async
}
......
......@@ -46,7 +46,7 @@ async function autoSyncClerkToken() {
// Chỉ chạy trên domain OpenNotion (kiểm tra cả localhost dev và production)
const hostname = window.location.hostname;
const isOpenNotionDomain =
hostname === '160.191.50.138' ||
hostname === '172.16.2.210' ||
hostname === 'localhost' ||
hostname.includes('opennotion') ||
hostname.includes('cucunote') ||
......
......@@ -13,7 +13,7 @@ import { createRoot } from 'react-dom/client';
import { NoteForm } from '../components/NoteForm';
import './popup.css';
const WEB_APP_URL = 'http://160.191.50.138:3001';
const WEB_APP_URL = 'http://172.16.2.210:5230';
// ========== Theme Hook ==========
......@@ -52,7 +52,7 @@ async function grabTokenFromTabs(): Promise<string | null> {
const candidates = allTabs.filter((t) => {
const url = t.url || '';
return (
url.includes('160.191.50.138') ||
url.includes('172.16.2.210') ||
url.includes('localhost:3001') ||
url.includes('opennotion') ||
url.includes('cucunote')
......
......@@ -4,7 +4,7 @@
*/
// Default API base URL — empty means user must configure in Settings
const DEFAULT_API_BASE_URL = 'http://160.191.50.138:3001';
const DEFAULT_API_BASE_URL = 'http://172.16.2.210:5230';
// ========== Config ==========
......@@ -195,35 +195,6 @@ export async function createMemoWithToken(token: string, data: CreateMemoRequest
return await response.json();
}
/**
* Sync memo to secondary custom backend
*/
export async function syncToSecondaryBackend(data: CreateMemoRequest): Promise<void> {
try {
const headers = await apiHeaders();
const secondaryApiUrl = 'http://172.16.2.210:5230/api/v1/memos';
// We do a fire-and-forget sync to the secondary backend
const response = await fetch(secondaryApiUrl, {
method: 'POST',
headers,
body: JSON.stringify({
content: data.content,
tags: data.tags || [],
visibility: data.visibility || 'PRIVATE',
}),
});
if (!response.ok) {
console.warn('[CuCu API] Secondary sync returned status:', response.status);
} else {
console.log('[CuCu API] ✅ Successfully synced to secondary backend');
}
} catch (error) {
console.warn('[CuCu API] ⚠️ Failed to sync to secondary backend:', error);
}
}
// ========== Tags ==========
export async function fetchTags(): Promise<string[]> {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment