tangled
alpha
login
or
join now
stream.place
/
streamplace
74
fork
atom
Live video on the AT Protocol
74
fork
atom
overview
issues
1
pulls
pipelines
components: assign a random color on first interaction
Eli Mallon
5 months ago
89f922c8
c95c960b
+77
-8
2 changed files
expand all
collapse all
unified
split
js
components
src
streamplace-provider
index.tsx
streamplace-store
user.tsx
+6
-1
js/components/src/streamplace-provider/index.tsx
···
1
1
import { SessionManager } from "@atproto/api/dist/session-manager";
2
2
import { useEffect, useRef } from "react";
3
3
+
import { useGetChatProfile } from "../streamplace-store";
3
4
import { makeStreamplaceStore } from "../streamplace-store/streamplace-store";
4
5
import { StreamplaceContext } from "./context";
5
6
import Poller from "./poller";
···
13
14
url: string;
14
15
oauthSession?: SessionManager | null;
15
16
}) {
16
16
-
console.log("session in provider is", oauthSession);
17
17
+
const getChatProfile = useGetChatProfile();
17
18
// todo: handle url changes?
18
19
const store = useRef(makeStreamplaceStore({ url })).current;
19
20
···
23
24
24
25
useEffect(() => {
25
26
store.setState({ oauthSession });
27
27
+
// possibly their first login, trigger this so we create a chat profile record for them
28
28
+
if (oauthSession) {
29
29
+
getChatProfile();
30
30
+
}
26
31
}, [oauthSession]);
27
32
28
33
return (
+71
-7
js/components/src/streamplace-store/user.tsx
···
10
10
const did = useDID();
11
11
const pdsAgent = usePDSAgent();
12
12
const store = getStreamplaceStoreFromContext();
13
13
+
const createEmptyChatProfile = useCreateEmptyChatProfile();
13
14
14
15
return async () => {
15
16
if (!did || !pdsAgent) {
16
17
throw new Error("No DID or PDS agent");
17
18
}
18
18
-
const res = await pdsAgent.com.atproto.repo.getRecord({
19
19
-
repo: did,
20
20
-
collection: "place.stream.chat.profile",
21
21
-
rkey: "self",
22
22
-
});
23
23
-
if (!res.success) {
24
24
-
throw new Error("Failed to get chat profile record");
19
19
+
let res;
20
20
+
try {
21
21
+
res = await pdsAgent.com.atproto.repo.getRecord({
22
22
+
repo: did,
23
23
+
collection: "place.stream.chat.profile",
24
24
+
rkey: "self",
25
25
+
});
26
26
+
} catch (e) {
27
27
+
console.error(
28
28
+
"Failed to get chat profile record, attempting creation",
29
29
+
e,
30
30
+
);
31
31
+
}
32
32
+
if (!res || !res.success) {
33
33
+
try {
34
34
+
await createEmptyChatProfile();
35
35
+
res = await pdsAgent.com.atproto.repo.getRecord({
36
36
+
repo: did,
37
37
+
collection: "place.stream.chat.profile",
38
38
+
rkey: "self",
39
39
+
});
40
40
+
} catch (e) {
41
41
+
console.error("Failed to create empty chat profile record", e);
42
42
+
}
25
43
}
26
44
27
45
if (PlaceStreamChatProfile.isRecord(res.data.value)) {
···
32
50
};
33
51
}
34
52
53
53
+
export function useCreateEmptyChatProfile() {
54
54
+
const did = useDID();
55
55
+
const pdsAgent = usePDSAgent();
56
56
+
57
57
+
return async () => {
58
58
+
if (!did || !pdsAgent) {
59
59
+
throw new Error("No DID or PDS agent");
60
60
+
}
61
61
+
const res = await pdsAgent.com.atproto.repo.putRecord({
62
62
+
repo: did,
63
63
+
collection: "place.stream.chat.profile",
64
64
+
rkey: "self",
65
65
+
record: {
66
66
+
$type: "place.stream.chat.profile",
67
67
+
color:
68
68
+
DEFAULT_COLORS[Math.floor(Math.random() * DEFAULT_COLORS.length)],
69
69
+
},
70
70
+
});
71
71
+
if (!res.success) {
72
72
+
throw new Error("Failed to create empty chat profile record");
73
73
+
}
74
74
+
};
75
75
+
}
76
76
+
35
77
export function useGetBskyProfile() {
36
78
const did = useDID();
37
79
const pdsAgent = usePDSAgent();
···
55
97
export function useChatProfile() {
56
98
return useStreamplaceStore((x) => x.chatProfile);
57
99
}
100
100
+
101
101
+
const DEFAULT_COLORS: PlaceStreamChatProfile.Color[] = [
102
102
+
{ red: 244, green: 67, blue: 54 },
103
103
+
{ red: 233, green: 30, blue: 99 },
104
104
+
{ red: 156, green: 39, blue: 176 },
105
105
+
{ red: 103, green: 58, blue: 183 },
106
106
+
{ red: 63, green: 81, blue: 181 },
107
107
+
{ red: 33, green: 150, blue: 243 },
108
108
+
{ red: 3, green: 169, blue: 244 },
109
109
+
{ red: 0, green: 188, blue: 212 },
110
110
+
{ red: 0, green: 150, blue: 136 },
111
111
+
{ red: 76, green: 175, blue: 80 },
112
112
+
{ red: 139, green: 195, blue: 74 },
113
113
+
{ red: 205, green: 220, blue: 57 },
114
114
+
{ red: 255, green: 235, blue: 59 },
115
115
+
{ red: 255, green: 193, blue: 7 },
116
116
+
{ red: 255, green: 152, blue: 0 },
117
117
+
{ red: 255, green: 87, blue: 34 },
118
118
+
{ red: 121, green: 85, blue: 72 },
119
119
+
{ red: 158, green: 158, blue: 158 },
120
120
+
{ red: 96, green: 125, blue: 139 },
121
121
+
];