Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions FORK.md

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions apps/mobile/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { RegistryContext } from "@effect/atom-react";
import { J5_BRANDING } from "../../../scripts/lib/j5-branding.ts";
import { ProjectChipIconRasterizer } from "./native/ProjectChipIconImages";
import { ThreadArrangementHost } from "./features/threads/ThreadArrangementSheet";
import { NameQuestionHost } from "./j5/sharing/NameQuestion";
import * as Dialog from "./components/ui/Dialog";
import { ArchiveWarningHost } from "./j5/archive/ArchiveWarningSheet";
import { CloudAuthProvider } from "./features/cloud/CloudAuthProvider";
Expand Down Expand Up @@ -172,6 +173,7 @@ function AppContent() {
<Dialog.Host />
<ArchiveWarningHost />
<ThreadArrangementHost />
<NameQuestionHost />
<ProjectChipIconRasterizer />
</NativeLayoutMetricsProvider>
</GlobalVoiceInputControl>
Expand Down
3 changes: 3 additions & 0 deletions apps/mobile/src/connection/runtime.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Connection } from "@t3tools/client-runtime/connection";
import * as J5SessionRenewal from "@t3tools/client-runtime/j5/session-renewal";
import { ShellSnapshotLoader } from "@t3tools/client-runtime/state/shell";
import { PullRequestDiffLoader } from "@t3tools/client-runtime/state/pull-requests";
import {
Expand Down Expand Up @@ -48,6 +49,8 @@ const layerProvidedClientConnection = layerSnapshotLoader.pipe(
);

const layerConnection = BackgroundActivity.layerReporter.pipe(
// J5: a saved server's session ended for a change of level renews (FORK.md case 70).
Layer.provideMerge(J5SessionRenewal.layer),
Layer.provideMerge(layerProvidedClientConnection),
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { appAtomRegistry } from "../../state/atom-registry";
import { projectEnvironmentPresentation } from "../../state/environments";
import { environmentPresentations } from "../../state/presentation";
import { environmentSession } from "../../state/session";
import { permissionNoticeEnvironmentIdsAtom } from "../../j5/sharing/upstreamNotices";

const storageKey = "t3code.permission-update.v1";

Expand All @@ -17,7 +18,9 @@ const legacyPermissionEnvironmentsAtom = Atom.make((get) =>
const session = get(environmentSession.sessionStateAtom(environmentId));
return session._tag === "Success" &&
!session.waiting &&
sessionHasLegacyPermissions(session.value)
sessionHasLegacyPermissions(session.value) &&
// J5 (case 73): not for a connection J5 places as a person, which renews its permissions.
get(permissionNoticeEnvironmentIdsAtom).has(environmentId)
? [projectEnvironmentPresentation(environmentId, presentation)]
: [];
},
Expand Down
195 changes: 195 additions & 0 deletions apps/mobile/src/j5/sharing/NameQuestion.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,195 @@
import { useAtomValue } from "@effect/atom-react";
import {
NAME_NOT_USABLE,
levelLabel,
nameIsInUse,
offeredLevel,
} from "@t3tools/client-runtime/j5/sharing";
import { squashAtomCommandFailure } from "@t3tools/client-runtime/state/runtime";
import type { EnvironmentId } from "@t3tools/contracts";
import { isPersonName } from "@t3tools/contracts/j5";
import { useState } from "react";
import { Modal, ScrollView, View } from "react-native";
import { KeyboardAvoidingView } from "react-native-keyboard-controller";
import { SafeAreaView } from "react-native-safe-area-context";

import { AppText as Text, AppTextInput as TextInput } from "../../components/ui/AppText";
import { Button } from "../../components/ui/Button";
import { ListChoiceRow } from "../../components/ui/ListChoiceRow";
import { ListSection } from "../../components/ui/ListSection";
import { appAtomRegistry } from "../../state/atom-registry";
import { environmentPresentations } from "../../state/presentation";
import { useEnvironmentQuery } from "../../state/query";
import { useAtomCommand } from "../../state/use-atom-command";
import { j5Sharing } from "./guestAccess";
import { identityCommands, nameToAskAtom } from "./identity";

const SOMEONE_NEW = "new";

/**
* Asks the name question of the first saved server that needs it. Mounted once, at the root, so
* it covers every screen until it is answered.
*/
export function NameQuestionHost() {
const asking = useAtomValue(nameToAskAtom);
if (asking === null) return null;
return <NameQuestion key={asking.id} environmentId={asking.id} kind={asking.kind} />;
}

/**
* "What should people and agents on <server> call you?", asked before a device uses the server
* (People and access AC5, AC6), as web's `NameDialog` asks it. There is no way to put it off:
* until it is answered the device reaches nothing there.
*/
function NameQuestion({
environmentId,
kind,
}: {
readonly environmentId: EnvironmentId;
readonly kind: "claim" | "first-owner";
}) {
const presentation = useAtomValue(environmentPresentations.presentationsAtom).get(environmentId);
const serverName = presentation?.entry.target.label ?? "this server";
const claimable = useEnvironmentQuery(
kind === "claim" ? identityCommands.claimablePeople({ environmentId, input: {} }) : null,
);
const claimDevice = useAtomCommand(identityCommands.claimDevice, { reportFailure: false });
const setFirstOwnerName = useAtomCommand(identityCommands.setFirstOwnerName, {
reportFailure: false,
});
const people = kind === "claim" ? (claimable.data?.people ?? []) : [];
const [choice, setChoice] = useState<string>(SOMEONE_NEW);
const [name, setName] = useState("");
const [busy, setBusy] = useState(false);
const [error, setError] = useState<string | null>(null);
const naming = kind === "first-owner" || choice === SOMEONE_NEW;

const save = async () => {
const typed = name.trim();
if (naming && !isPersonName(typed)) {
setError(NAME_NOT_USABLE);
return;
}
setBusy(true);
setError(null);
const result =
kind === "first-owner"
? await setFirstOwnerName({ environmentId, input: { name: typed } })
: await claimDevice({
environmentId,
input:
choice === SOMEONE_NEW
? { kind: "new", name: typed }
: { kind: "person", personId: choice },
});
if (result._tag === "Failure") {
setBusy(false);
const cause = squashAtomCommandFailure(result);
setError(
nameIsInUse(cause)
? `Someone on ${serverName} already has that name.`
: cause instanceof Error && cause.message.trim() !== ""
? cause.message
: "The server did not say why.",
);
return;
}
// A claimed device's session is ended for renewal, which reconnects it and reads its access
// again, closing this; it stays "Saving…" until then, and the people list's failed re-read
// during the renewal is not shown. The first owner's access is read again here.
j5Sharing.refreshConnectionAccess(appAtomRegistry, environmentId);
};

const nameInput = (label: string) => (
<TextInput
accessibilityLabel={label}
placeholder={label}
autoFocus={kind === "first-owner" || people.length === 0}
autoCapitalize="words"
autoCorrect={false}
maxLength={80}
value={name}
editable={!busy}
returnKeyType="done"
onSubmitEditing={() => void save()}
onChangeText={(value) => {
setName(value);
setChoice(SOMEONE_NEW);
setError(null);
}}
/>
);

return (
// Full screen, so it can't be swiped away: the question can't be put off.
<Modal visible animationType="slide" presentationStyle="fullScreen" onRequestClose={() => {}}>
{/* Inline flex: on Android a `flex-1` class left this Modal's content zero-height. */}
<SafeAreaView className="bg-screen" style={{ flex: 1 }}>
<KeyboardAvoidingView automaticOffset behavior="padding" style={{ flex: 1 }}>
<ScrollView keyboardShouldPersistTaps="handled" contentContainerClassName="gap-5 p-5">
<View className="gap-2">
<Text accessibilityRole="header" className="text-2xl font-t3-semibold">
What should people and agents on {serverName} call you?
</Text>
{kind === "claim" ? (
<Text className="text-foreground-muted">
This device is paired but the server does not know who it belongs to.
</Text>
) : null}
</View>
{people.length > 0 ? (
<ListSection>
{people.map((person, index) => {
const level = offeredLevel(person.levels);
return (
<ListChoiceRow
key={person.personId}
label={person.name}
description={level === null ? undefined : levelLabel(level)}
selected={choice === person.personId}
separated={index > 0}
disabled={busy}
onPress={() => {
setChoice(person.personId);
setError(null);
}}
/>
);
})}
<ListChoiceRow
label="Someone new"
selected={choice === SOMEONE_NEW}
separated
disabled={busy}
onPress={() => {
setChoice(SOMEONE_NEW);
setError(null);
}}
/>
</ListSection>
) : null}
{naming ? nameInput(people.length > 0 ? "Someone new" : "Your name") : null}
{claimable.error === null || busy ? null : (
<Text className="text-sm text-foreground-muted">
The people on this server could not be listed: {claimable.error}
</Text>
)}
{error === null ? null : (
<Text accessibilityRole="alert" className="text-sm text-danger-foreground">
{error}
</Text>
)}
<Button
label={busy ? "Saving…" : "Continue"}
tone="primary"
fullWidth
loading={busy}
disabled={naming && name.trim() === ""}
onPress={() => void save()}
/>
</ScrollView>
</KeyboardAvoidingView>
</SafeAreaView>
</Modal>
);
}
6 changes: 5 additions & 1 deletion apps/mobile/src/j5/sharing/guestAccess.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,12 @@ import { Atom } from "effect/reactivity";
import { environmentCatalog } from "../../connection/catalog";
import { connectionAtomRuntime } from "../../connection/runtime";
import { useEnvironmentScope } from "../../state/session";
import { j5Environment } from "../state";

export const j5Sharing = createSharingEnvironmentAtoms(connectionAtomRuntime);
/** What this device's connection is on each server; read again when people or levels change. */
export const j5Sharing = createSharingEnvironmentAtoms(connectionAtomRuntime, {
rereadOn: j5Environment.rereadOn("sharing"),
});

const NOTHING_WITHHELD = Atom.make(true);

Expand Down
40 changes: 40 additions & 0 deletions apps/mobile/src/j5/sharing/identity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { serverToAskNameFor } from "@t3tools/client-runtime/j5/sharing";
import {
createEnvironmentRpcCommand,
createEnvironmentRpcQueryAtomFamily,
} from "@t3tools/client-runtime/state/runtime";
import { J5_ACCESS_WS_METHODS } from "@t3tools/contracts/j5";
import { Atom } from "effect/reactivity";

import { environmentCatalog } from "../../connection/catalog";
import { connectionAtomRuntime } from "../../connection/runtime";
import { j5Sharing } from "./guestAccess";

/** What a device does to say whose it is on a server (People and access AC5, AC6). */
export const identityCommands = {
/** For a device the server cannot place: everyone it could be. */
claimablePeople: createEnvironmentRpcQueryAtomFamily(connectionAtomRuntime, {
label: "mobile-j5-access:claimable",
tag: J5_ACCESS_WS_METHODS.listClaimablePeople,
staleTimeMs: 0,
}),
claimDevice: createEnvironmentRpcCommand(connectionAtomRuntime, {
label: `mobile-j5:${J5_ACCESS_WS_METHODS.claimDevice}`,
tag: J5_ACCESS_WS_METHODS.claimDevice,
}),
setFirstOwnerName: createEnvironmentRpcCommand(connectionAtomRuntime, {
label: `mobile-j5:${J5_ACCESS_WS_METHODS.setFirstOwnerName}`,
tag: J5_ACCESS_WS_METHODS.setFirstOwnerName,
}),
};

// Every saved server's answer, which each connection asks for when it connects, so this costs no
// request and holds nothing up.
export const nameToAskAtom = Atom.make((get) =>
serverToAskNameFor(
[...get(environmentCatalog.catalogValueAtom).entries.keys()].map((id) => ({
id,
access: get(j5Sharing.connectionAccess(id)),
})),
),
).pipe(Atom.withLabel("mobile-j5:name-to-ask"));
22 changes: 22 additions & 0 deletions apps/mobile/src/j5/sharing/upstreamNotices.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { permissionNoticeApplies } from "@t3tools/client-runtime/j5/sharing";
import type { EnvironmentId } from "@t3tools/contracts";
import { Atom } from "effect/reactivity";

import { environmentCatalog } from "../../connection/catalog";
import { j5Sharing } from "./guestAccess";

/**
* The saved servers upstream's "Permissions have changed" notice is for: those whose connection
* is no person's, such as a server from before people. A person's session renews instead.
*/
export const permissionNoticeEnvironmentIdsAtom = Atom.make((get) => {
const ids = new Set<EnvironmentId>();
for (const environmentId of get(environmentCatalog.catalogValueAtom).entries.keys()) {
if (
permissionNoticeApplies(get(j5Sharing.connectionAccessQuery({ environmentId, input: {} })))
) {
ids.add(environmentId);
}
}
return ids as ReadonlySet<EnvironmentId>;
}).pipe(Atom.withLabel("mobile-j5:access:permission-notice-environments"));
18 changes: 18 additions & 0 deletions apps/mobile/src/j5/upstreamSeams.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,24 @@ const SEAMS: ReadonlyArray<Seam> = [
"J5_BRANDING.mobile.preview.scheme",
],
},
{
file: "App.tsx",
record: "case 70: the name question",
from: "./j5/sharing/NameQuestion",
reaches: ["<NameQuestionHost />"],
},
{
file: "features/connection/permissionUpdateNotice.ts",
record: "case 73: no pair-again notice for a person",
from: "../../j5/sharing/upstreamNotices",
reaches: ["get(permissionNoticeEnvironmentIdsAtom).has(environmentId)"],
},
{
file: "connection/runtime.ts",
record: "case 70: session renewal",
from: "@t3tools/client-runtime/j5/session-renewal",
reaches: ["Layer.provideMerge(J5SessionRenewal.layer)"],
},
{
file: "features/connection/pairing.ts",
record: "BRANDING.md: pairing QR handling",
Expand Down
11 changes: 7 additions & 4 deletions apps/web/src/j5/sharing/NameDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,13 @@ import { useEnvironment } from "../../state/environments";
import { useEnvironmentQuery } from "../../state/query";
import { useAtomCommand } from "../../state/use-atom-command";
import { PersonAvatar } from "./PersonAvatar";
import { offeredLevel } from "./nameDialog.logic";
import { levelLabel } from "./people.logic";
import { NAME_NOT_USABLE } from "./shareLink.logic";
import { nameIsInUse, refreshSharing, refusalMessage, sharingManage } from "./sharingManage";
import { refreshSharing, refusalMessage, sharingManage } from "./sharingManage";
import {
NAME_NOT_USABLE,
levelLabel,
nameIsInUse,
offeredLevel,
} from "@t3tools/client-runtime/j5/sharing";
import { squashAtomCommandFailure } from "@t3tools/client-runtime/state/runtime";

const SOMEONE_NEW = "new";
Expand Down
2 changes: 1 addition & 1 deletion apps/web/src/j5/sharing/SharingDialogHost.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { useAtomValue } from "@effect/atom-react";
import { serverToAskNameFor } from "@t3tools/client-runtime/j5/sharing";
import { Atom } from "effect/reactivity";
import { useSyncExternalStore } from "react";

Expand All @@ -7,7 +8,6 @@ import { j5Sharing } from "./access";
import { LeftOnNothingRedirect } from "./LeftOnNothingRedirect";
import { LeaveDialog } from "./LeaveDialog";
import { NameDialog } from "./NameDialog";
import { serverToAskNameFor } from "./nameDialog.logic";
import { RenameDialog } from "./RenameDialog";
import { RetiredLinksNotice } from "./RetiredLinksNotice";
import { SessionRenewalWatcher } from "./SessionRenewalWatcher";
Expand Down
Loading
Loading