Skip to content

Commit

Permalink
i18n: Translate logo by rewriting it to a modern functional component
Browse files Browse the repository at this point in the history
  • Loading branch information
danielhjacobs committed Nov 14, 2024
1 parent 1da5a09 commit a174704
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 59 deletions.
100 changes: 41 additions & 59 deletions src/components/logo.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
"use client";

import React from "react";
import React, { useEffect, useRef, useState } from "react";
import Image from "next/image";
import Script from "next/script";
import classes from "../app/index.module.css";
import { t } from "@/app/translate";

declare global {
interface Window {
Expand Down Expand Up @@ -31,40 +32,26 @@ interface LogoState {
player: RufflePlayer | null;
}

export default class InteractiveLogo extends React.Component<
LogoProps,
LogoState
> {
private readonly container: React.RefObject<HTMLDivElement>;
private player: RufflePlayer | null = null;
export default function InteractiveLogo({ className }: LogoProps) {
const container = useRef<HTMLDivElement>(null);
const [player, setPlayer] = useState<RufflePlayer | null>(null);

constructor(props: LogoProps) {
super(props);

this.container = React.createRef();
this.state = {
player: null,
};
}

private removeRufflePlayer() {
this.player?.remove();
this.player = null;
this.setState({ player: null });
const removeRufflePlayer = () => {
player?.remove();
setPlayer(null);
}

private load() {
if (this.player) {
// Already loaded.
const loadPlayer = () => {
if (player) {
return;
}

this.player = (window.RufflePlayer as PublicAPI)?.newest()?.createPlayer();
const rufflePlayer = (window.RufflePlayer as PublicAPI)?.newest()?.createPlayer();

if (this.player) {
this.container.current!.appendChild(this.player);
if (rufflePlayer) {
container.current!.appendChild(rufflePlayer);

this.player
rufflePlayer
.load({
url: "/logo-anim.swf",
autoplay: "on",
Expand All @@ -75,39 +62,34 @@ export default class InteractiveLogo extends React.Component<
preferredRenderer: "canvas",
})
.catch(() => {
this.removeRufflePlayer();
removeRufflePlayer();
});
this.player.style.width = "100%";
this.player.style.height = "100%";
this.setState({ player: this.player });
rufflePlayer.style.width = "100%";
rufflePlayer.style.height = "100%";
setPlayer(rufflePlayer);
}
}

componentDidMount() {
this.load();
}

componentWillUnmount() {
this.removeRufflePlayer();
}

render() {
return (
<>
<Script
src="https://unpkg.com/@ruffle-rs/ruffle"
onReady={() => this.load()}
};

useEffect(() => {
loadPlayer();
return () => removeRufflePlayer();
}, []);

return (
<>
<Script
src="https://unpkg.com/@ruffle-rs/ruffle"
onReady={() => loadPlayer()}
/>
<div ref={this.container} className={this.props.className}>
<Image
src="/logo.svg"
alt="Ruffle Logo"
className={this.state.player ? classes.hidden : classes.staticLogo}
width="340"
height="110"
/>
</div>
</>
);
}
<div ref={container} className={className}>
<Image
src="/logo.svg"
alt={t("logo.alt-tag")}
className={player ? classes.hidden : classes.staticLogo}
width="340"
height="110"
/>
</div>
</>
);
}
3 changes: 3 additions & 0 deletions src/i18n/translations.en.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,8 @@
"mastodon": "Mastodon",
"discord": "Discord",
"tagline": "Putting Flash back on the web"
},
"logo": {
"alt-tag": "Ruffle Logo"
}
}

0 comments on commit a174704

Please sign in to comment.