reboot alterflixhub

This commit is contained in:
Bkk1234 committed 2026-11-01 17:24:44 +07:00
commit 1e84023a44
188 files changed
+31629

No files matched your search

+73
View File
@@ -0,0 +1,73 @@
import {
appBasePath,
appPathFromLocalRoute,
} from "@agent-native/core/client/api-path";
import { useAgentRouteState } from "@agent-native/core/client/navigation";
import { TAB_ID } from "@/lib/tab-id";
export interface NavigationState {
view?: string;
path?: string;
publicPath?: string;
}
export function useNavigationState() {
useAgentRouteState<NavigationState>({
browserTabId: TAB_ID,
requestSource: TAB_ID,
getNavigationState: ({ pathname }) => ({
view: viewForPath(pathname),
path: pathname,
publicPath: appPathFromLocalRoute(pathname),
}),
getCommandPath: commandPathForNavigation,
});
}
export function commandPathForNavigation(command: NavigationState): string {
const fallbackPath = pathForView(command?.view);
const path = typeof command?.path === "string" ? command.path : "";
if (!path) return fallbackPath;
const basePath = appBasePath();
if (!basePath) return path;
const suffixIndex = path.search(/[?#]/);
const pathname = suffixIndex === -1 ? path : path.slice(0, suffixIndex);
const suffix = suffixIndex === -1 ? "" : path.slice(suffixIndex);
const publicPath =
typeof command?.publicPath === "string" ? command.publicPath : "";
const publicPathSuffixIndex = publicPath.search(/[?#]/);
const declaredPublicPath =
publicPathSuffixIndex === -1
? publicPath
: publicPath.slice(0, publicPathSuffixIndex);
const publicViewPath = appPathFromLocalRoute(pathForView(command?.view));
if (
matchesPathPrefix(pathname, basePath) &&
(matchesPathPrefix(pathname, publicViewPath) ||
(declaredPublicPath && declaredPublicPath === pathname))
) {
return `${pathname.slice(basePath.length) || "/"}${suffix}`;
}
return path;
}
function matchesPathPrefix(pathname: string, prefix: string): boolean {
return pathname === prefix || pathname.startsWith(`${prefix}/`);
}
function viewForPath(pathname: string): string {
if (pathname === "/") return "home";
return "home";
}
function pathForView(view?: string): string {
switch (view) {
case "home":
return "/";
default:
return "/";
}
}
+124
View File
@@ -0,0 +1,124 @@
import {
createContext,
useCallback,
useContext,
useMemo,
useState,
type ReactNode,
} from "react";
import { CINEMAS, type Cinema } from "@/lib/cinemas";
import {
fetchRoadDistances,
straightLineDistance,
type Coordinates,
type DistanceResult,
} from "@/lib/geo";
export type NearMeStatus =
| "idle"
| "locating"
| "ready"
| "denied"
| "unsupported"
| "error";
export type NearMeContextValue = {
status: NearMeStatus;
coords: Coordinates | null;
/** ระยะทางต่อ slug — มีค่าเฉพาะหลังขอตำแหน่งสำเร็จ */
distances: Record<string, DistanceResult | undefined>;
requestLocation: () => void;
};
const GEOLOCATION_TIMEOUT_MS = 10000;
const NearMeContext = createContext<NearMeContextValue | null>(null);
/** คำนวณระยะทางเส้นตรงให้ทุกโรง (ใช้เมื่อ OSRM เรียกไม่สำเร็จ) */
function straightLineDistances(cinemas: Cinema[], origin: Coordinates) {
const result: Record<string, DistanceResult | undefined> = {};
for (const cinema of cinemas) {
if (!cinema.coordinates) continue;
result[cinema.slug] = straightLineDistance(origin, cinema.coordinates);
}
return result;
}
/**
* แชร์ตำแหน่งอุปกรณ์และระยะทางถึงแต่ละโรงให้ทุกหน้า
* ขอตำแหน่งเฉพาะเมื่อผู้ใช้กด "ใกล้ฉัน" เท่านั้น (ไม่ขออัตโนมัติ)
* รายการโรงหนังมาจากเนื้อหาที่โหลดใน root loader (แก้จากหลังบ้านได้)
*/
export function NearMeProvider({
cinemas,
children,
}: {
cinemas: Cinema[];
children: ReactNode;
}) {
const [status, setStatus] = useState<NearMeStatus>("idle");
const [coords, setCoords] = useState<Coordinates | null>(null);
const [distances, setDistances] = useState<
Record<string, DistanceResult | undefined>
>({});
const loadDistances = useCallback(
async (origin: Coordinates) => {
const targets = cinemas.filter((cinema) => cinema.coordinates !== null);
try {
const road = await fetchRoadDistances(
origin,
targets.map((cinema) => cinema.coordinates as Coordinates),
);
const next: Record<string, DistanceResult | undefined> = {};
targets.forEach((cinema, index) => {
const destination = cinema.coordinates as Coordinates;
next[cinema.slug] =
road[index] ?? straightLineDistance(origin, destination);
});
setDistances(next);
} catch {
setDistances(straightLineDistances(cinemas, origin));
}
setStatus("ready");
},
[cinemas],
);
const requestLocation = useCallback(() => {
if (typeof navigator === "undefined" || !navigator.geolocation) {
setStatus("unsupported");
return;
}
setStatus("locating");
navigator.geolocation.getCurrentPosition(
(position) => {
const origin: Coordinates = {
latitude: position.coords.latitude,
longitude: position.coords.longitude,
};
setCoords(origin);
void loadDistances(origin);
},
(error) => {
setStatus(error.code === error.PERMISSION_DENIED ? "denied" : "error");
},
{ timeout: GEOLOCATION_TIMEOUT_MS, maximumAge: 60000 },
);
}, [loadDistances]);
const value = useMemo<NearMeContextValue>(
() => ({ status, coords, distances, requestLocation }),
[status, coords, distances, requestLocation],
);
return <NearMeContext.Provider value={value}>{children}</NearMeContext.Provider>;
}
export function useNearMe(): NearMeContextValue {
const value = useContext(NearMeContext);
if (!value) throw new Error("useNearMe ต้องถูกใช้ภายใน <NearMeProvider>");
return value;
}