Skip to content

Commit

Permalink
Merge pull request #3929 from tloncorp/hm/better-lure-url-fetching
Browse files Browse the repository at this point in the history
lure: better url fetching
  • Loading branch information
arthyn authored Sep 17, 2024
2 parents 6058e5a + 0557825 commit 2e7361d
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 57 deletions.
6 changes: 4 additions & 2 deletions apps/tlon-web/src/groups/LureInviteBlock.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export default function LureInviteBlock({
</p>
</div>

{status === 'ready' && (
{status === 'ready' && shareUrl && (
<QRWidget
link={shareUrl}
navigatorTitle={`Join ${group?.meta.title ?? flag}`}
Expand Down Expand Up @@ -83,7 +83,9 @@ export default function LureInviteBlock({
<span
className={cn(
'flex h-5 w-5 shrink-0 items-center justify-center rounded-full',
status === 'disabled' ? 'border border-gray-100' : 'bg-blue-500'
status === 'disabled' || status === 'loading'
? 'border border-gray-100'
: 'bg-blue-500'
)}
>
<CheckIcon className="h-4 w-4 text-white" />
Expand Down
2 changes: 1 addition & 1 deletion apps/tlon-web/src/logic/branch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ export const createDeepLink = async (
$canonical_url: fallbackUrl,
};
if (type === 'lure') {
data.lure = path;
data.lure = token;
} else {
data.wer = path;
}
Expand Down
39 changes: 17 additions & 22 deletions apps/tlon-web/src/state/lure/lure.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,9 @@ interface LureMetadata {

interface Lure {
fetched: boolean;
url: string;
url?: string;
deepLinkUrl?: string;
enabled?: boolean;
enableAcked?: boolean;
metadata?: LureMetadata;
}

Expand Down Expand Up @@ -129,7 +128,7 @@ export const useLureState = create<LureState>(
},
fetchLure: async (flag) => {
const prevLure = get().lures[flag];
const [enabled, url, metadata, outstandingPoke] = await Promise.all([
const [enabled, url, metadata] = await Promise.all([
// enabled
asyncWithDefault(() => {
lureLogger.log(performance.now(), 'fetching enabled', flag);
Expand All @@ -140,7 +139,7 @@ export const useLureState = create<LureState>(
LURE_REQUEST_TIMEOUT
)
.then((en) => {
lureLogger.log(performance.now(), 'enabled fetched', flag);
lureLogger.log(performance.now(), 'enabled fetched', en, flag);

return en;
});
Expand All @@ -149,15 +148,21 @@ export const useLureState = create<LureState>(
asyncWithDefault(() => {
lureLogger.log(performance.now(), 'fetching url', flag);
return api
.scry<string>({
app: 'reel',
path: `/v1/id-url/${flag}`,
})
.subscribeOnce<string>('reel', `/v1/id-link/${flag}`, 4500)
.then((u) => {
lureLogger.log(performance.now(), 'url fetched', flag);
lureLogger.log(performance.now(), 'url fetched', u, flag);
return u;
})
.catch((e) => {
lureLogger.error(
performance.now(),
'url fetch timeout',
e,
flag
);
return undefined;
});
}, prevLure?.url),
}, prevLure?.url) as Promise<string | undefined>,
// metadata
asyncWithDefault(
() =>
Expand All @@ -167,15 +172,6 @@ export const useLureState = create<LureState>(
}),
prevLure?.metadata
),
// outstandingPoke
asyncWithDefault(
() =>
api.scry<boolean>({
app: 'reel',
path: `/outstanding-poke/${flag}`,
}),
false
),
]);

let deepLinkUrl: string | undefined;
Expand All @@ -188,7 +184,6 @@ export const useLureState = create<LureState>(
draft.lures[flag] = {
fetched: true,
enabled,
enableAcked: !outstandingPoke,
url,
deepLinkUrl,
metadata,
Expand Down Expand Up @@ -254,9 +249,9 @@ export function useLure(flag: string, disableLoading = false) {
};
}

export function useLureLinkChecked(url: string, enabled: boolean) {
export function useLureLinkChecked(url: string | undefined, enabled: boolean) {
const prevData = useRef<boolean | undefined>(false);
const pathEncodedUrl = stringToTa(url);
const pathEncodedUrl = stringToTa(url || '');
const { data, ...query } = useQuery(
['lure-check', url],
() =>
Expand Down
49 changes: 18 additions & 31 deletions packages/shared/src/store/lure.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,9 @@ const LURE_REQUEST_TIMEOUT = 10 * 1000;

interface Lure {
fetched: boolean;
url: string;
url?: string;
deepLinkUrl?: string;
enabled?: boolean;
enableAcked?: boolean;
metadata?: LureMetadata;
}

Expand All @@ -40,8 +39,7 @@ interface LureState {
fetchLure: (
flag: string,
branchDomain: string,
branchKey: string,
fetchIfData?: boolean
branchKey: string
) => Promise<void>;
describe: (
flag: string,
Expand Down Expand Up @@ -132,7 +130,7 @@ export const useLureState = create<LureState>((set, get) => ({
const { name } = getFlagParts(flag);
const prevLure = get().lures[flag];
lureLogger.log('fetching', flag, 'prevLure', prevLure);
const [enabled, url, metadata, outstandingPoke] = await Promise.all([
const [enabled, url, metadata] = await Promise.all([
// enabled
asyncWithDefault(async () => {
lureLogger.log(performance.now(), 'fetching enabled', flag);
Expand All @@ -149,12 +147,12 @@ export const useLureState = create<LureState>((set, get) => ({
});
}, prevLure?.enabled),
// url
asyncWithDefault(async () => {
asyncWithDefault<string | undefined>(async () => {
lureLogger.log(performance.now(), 'fetching url', flag);
return scry<string>({
app: 'reel',
path: `/v1/id-url/${flag}`,
}).then((u) => {
return subscribeOnce<string>(
{ app: 'reel', path: `/v1/id-link/${flag}` },
4500
).then((u) => {
lureLogger.log(performance.now(), 'url fetched', u, flag);
return u;
});
Expand All @@ -168,18 +166,9 @@ export const useLureState = create<LureState>((set, get) => ({
}),
prevLure?.metadata
),
// outstandingPoke
asyncWithDefault(
async () =>
scry<boolean>({
app: 'reel',
path: `/outstanding-poke/${flag}`,
}),
false
),
]);

lureLogger.log('fetched', flag, enabled, url, metadata, outstandingPoke);
lureLogger.log('fetched', flag, enabled, url, metadata);

let deepLinkUrl: string | undefined;
lureLogger.log('enabled', enabled);
Expand All @@ -199,7 +188,6 @@ export const useLureState = create<LureState>((set, get) => ({
draft.lures[flag] = {
fetched: true,
enabled,
enableAcked: !outstandingPoke,
url,
deepLinkUrl,
metadata,
Expand Down Expand Up @@ -269,17 +257,17 @@ export function useLure({
};
}

export function useLureLinkChecked(url: string, enabled: boolean) {
export function useLureLinkChecked(url: string | undefined, enabled: boolean) {
const prevData = useRef<boolean | undefined>(false);
const pathEncodedUrl = stringToTa(url);
const pathEncodedUrl = stringToTa(url || '');
const { data, ...query } = useQuery({
queryKey: ['lure-check', url],
queryFn: async () =>
subscribeOnce<boolean>(
{ app: 'grouper', path: `/check-link/${pathEncodedUrl}` },
{ app: 'grouper', path: `/v1/check-link/${pathEncodedUrl}` },
4500
),
enabled,
enabled: enabled && !!url,
refetchInterval: 5000,
});

Expand All @@ -303,12 +291,11 @@ export function useLureLinkStatus({
branchDomain: string;
branchKey: string;
}) {
const { supported, fetched, enabled, enableAcked, url, deepLinkUrl, toggle } =
useLure({
flag,
branchDomain,
branchKey,
});
const { supported, fetched, enabled, url, deepLinkUrl, toggle } = useLure({
flag,
branchDomain,
branchKey,
});
const { good, checked } = useLureLinkChecked(url, !!enabled);

lureLogger.log('useLureLinkStatus', {
Expand Down
2 changes: 1 addition & 1 deletion packages/ui/src/components/InviteUsersWidget.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const InviteUsersWidgetComponent = ({
branchDomain: branchDomain,
branchKey: branchKey,
});
const { doCopy } = useCopy(shareUrl);
const { doCopy } = useCopy(shareUrl || '');
const currentUserIsAdmin = useMemo(
() =>
group?.members?.some(
Expand Down

0 comments on commit 2e7361d

Please sign in to comment.